Technology & AI

Build a Data Analyst and Visualization Agent using Swarm Architecture

The Swarm Architecture brings together specialized AI agents that collaborate to solve complex data problems. Inspired by cloud computing, it pairs a Data Analysis agent for processing with a Visualization agent for charting, integrated to deliver clearer and more efficient insights.

This collaborative design reflects teamwork, where each agent focuses on its strengths to improve outcomes. In this article, we explore the basics of crowdsourcing and go through designing and building an active statistics agent program step by step.

What are Swarm Agents?

Swarm agents act as autonomous AI entities that perform dedicated tasks while working together according to defined processes instead of using a centralized command line. The system uses this technique to reproduce the network intelligence that exists in natural environments such as ants and flocks of birds.

Swarm agents use their imperfect knowledge base to implement their system, which requires them to interact with others to produce better results. The design process creates an efficient system that handles content and system errors while delivering high-quality results in data analysis and visualization tasks.

Basic Principles of Swarm Agents

Swarm systems rely on some basic principles that allow communication without a centralized intelligence. Understanding these principles helps you design structures for solid agents.

  • Shared Decision Making
    Agents operate independently without a single controlling authority. They share information and coordinate through communication, allowing flexible work distribution and quick decision making.
  • Special Agents
    Each agent focuses on a specific task, such as data analysis or visualization. A clear division of roles improves efficiency and ensures high quality results.
  • Communication and Communication Patterns
    Agents communicate through structured communication patterns such as sequential or parallel workflows. Shared content or messages keep activities relevant.
  • Fault Tolerance and Resilience
    Workloads are distributed across agents, allowing the system to scale easily. If one agent fails, the others continue to work without interruption.

Designing a Data Analyst & Data Visualization Swarm

Before coding, we design the system at a high level. Crowd will include at least two roles: Data Analysis Agent, and Data Visualization Agent. The consultant directs the questions to the experts and collects their results. Below is an overview of the architecture and data flow.

High-Level System Architecture

We implement our system with an orchestrator-worker framework. The user’s query comes first to the Lead Agent. The agent divides the work into parts that give special agents.

Design is similar to team building because the consultant acts as a team leader who delegates tasks to experts. Each agent has access to shared context (eg query, previous results, etc.) enabling them to maintain a complete understanding of the situation while taking their time to solve the problem. The system architecture has the following appearance:

  • Data analyst agent: Retrieves and analyzes the raw data according to the query.
  • Data Visualization Agent: Receives analysis results and generates charts.

This modular setup can be extended with more agents if needed:

Agent Roles and Responsibilities

Data analyst agent

The Data Analysis Agent manages end-to-end data processing, including cleaning datasets, pulling data from sources such as CSV files or databases, and running statistical analyses. It uses Python libraries and database tools to calculate metrics and return clear numerical data.

Its agile program directs it to work as a data analysis expert, answering questions with structured calculations. Using tools such as statistical and regression functions, it extracts relevant patterns and summarizes the results of downstream agents.

Data Visualization Agent

Data Visualization Agent converts analysis results into clear visual charts such as bar, line, or pie graphs. Selects appropriate types of charts to highlight patterns and contrasts in data.

Guided by independent information as a visual expert, the agent uses planning tools to create charts on incoming results. It outputs visuals such as embedded charts or image links that directly support the user’s query.

Orchestrator / Coordinating Agent

The Orchestrator agent acts as the first point of access for users. The system processes user queries to select which specific agents will help with the task. Then, it uses its handoff function to distribute its work tasks. It first analyzes the user’s query by parsing it before deciding what data analysis and visualization tasks need to be performed by the Data Analysis Agent.

Data Flow Between Agents

  • Link User Question: The user submits a query (eg.What is the average sales for each region and show us“). The broker agent takes this as input.
  • Data Analysis Coordinator: The coordinator uses the handoff tool to call the Data Analysis Agent, passing the query and any necessary context (such as a dataset reference).
  • Data Analysis Data: The Data Analysis Agent loads or queries the relevant data, performs calculations (eg grouping by region, computing averages) and returns the results (eg a table of averages).
  • Coordinator for Vision Agent: The consultant now requests the Data Visualization Agent, which it provides with the results of the analysis.

Example: A Data Analyst completes his work by delivering results that are then added to the shared context. The Vision Agent uses this completed task to decide what data to display. The system uses this handoff pattern because it enables agents to work on their specific tasks in an orderly manner. The shared content object is coded as a common mode that agents use to pass information during their service calls.

Using the Swarm agent system

The team needs to perform their initialization task using LangGraph Swarm based on their existing information in the provided notebook.

The system works with two agents including the Text-to-SQL Data Analyst Agent and the EDA Visualization Agent which analyzes the real banking database. A swarm allows agents to work together using structured delivery methods that replace the need for pre-built applications.

Localization and Dependencies

We will start the process by installing all the dependencies needed for our project. This project requires LangChain and LangGraph Swarm and OpenAI models and standard data science libraries.

pip install langchain==1.2.4  
           langgraph==1.0.6  
           langgraph-swarm  
           langchain-openai==1.1.4  
           langchain-community==0.4.1  
           langchain-experimental==0.4.0

We also install SQLite as the program queries local bank databases.

apt-get install sqlite3 -y 

Once installed, we import the necessary modules for agent music, SQL querying, and visualization.

from langchain_openai import ChatOpenAI 
from langgraph_swarm import create_swarm, create_handoff_tool, SwarmState 
from langgraph.checkpoint.memory import MemorySaver 
from langchain_community.utilities import SQLDatabase 
from langchain_community.agent_toolkits import SQLDatabaseToolkit 
from langchain_experimental.utilities import PythonREPL

In this section, we also implement LLM and database connection.

llm = ChatOpenAI(model="gpt-4.1-mini", temperature=0) 
db = SQLDatabase.from_uri("sqlite:///banking_insights.db") 
sql_toolkit = SQLDatabaseToolkit(db=db, llm=llm) 
sql_tools = sql_toolkit.get_tools()

This gives our agents structured access to the database without manually writing raw SQL.

Defining Agent System Information

The LangGraph Swarm system uses information to determine agent actions throughout its framework. Each agent has a clear responsibility.

Information for the Data Analyst

The Data Analyst agent converts spoken queries into SQL queries that it uses to generate summary results.

DATA_ANALYST_PROMPT = """ 

You are a Data Analyst specialized in SQL queries for retail banking analytics. 

Your primary tasks: 
- Convert user questions into correct SQL queries 
- Retrieve accurate data from the database 
- Provide concise, factual summaries 
- Hand off results to the EDA Visualizer when visualization is needed 
"""

This agent never makes charts. Its function is only analytical.

EDA Visualizer Agent Prompt

The EDA Visualizer agent converts query results into charts using Python.

EDA_VISUALIZER_PROMPT = """ 

You are an EDA Visualizer — an expert in data analysis and visualization. 

Your responsibilities: 
- Create clear and business-ready charts 
- Use Python for plotting 
- Return visual insights that support decision-making 
"""

This separation ensures that each agent remains focused and predictable.

Creating Handoff Tools Between Agents

Swarm agents communicate through handoff tools instead of direct calls. This is one of the key strengths of LangGraph Swarm.

handoff_to_eda = create_handoff_tool(
    agent_name="eda_visualizer",
    description="Transfer to the EDA Visualizer for charts and visual analysis",
)

handoff_to_analyst = create_handoff_tool(
    agent_name="data_analyst",
    description="Transfer back to the Data Analyst for additional SQL analysis",
)

These tools allow agents to decide when another agent must take over.

Creating agents

Now we build real agents using it create_agent.

data_analyst_agent = create_agent( 
   llm, 
   tools=sql_tools + [handoff_to_eda], 
   system_prompt=DATA_ANALYST_PROMPT, 
   name="data_analyst" 
)

A Data Analyst agent gets:

  • SQL Tools
  • Rendering tool to the viewer
eda_visualizer_agent = create_agent( 
   llm, 
   tools=[python_repl_tool, handoff_to_analyst], 
   system_prompt=EDA_VISUALIZER_PROMPT, 
   name="eda_visualizer" 
)

The Visualizer agent finds:

  • Python REPL for plotting
  • A tool to reverse engineer

This two-way extraction enables iterative thinking.

Creating a Swarm Graph

With the agents ready, we now assemble them into a LangGraph Swarm.

workflow = create_swarm( 
   agents=[data_analyst_agent, eda_visualizer_agent], 
   default_active_agent="data_analyst", 
   state_schema=SwarmState 
)

The Data Analyst agent is set as the default login. This makes sense because every application starts with understanding the data. We also enable memory so that the crowd can retain the context of the conversation.

checkpointer = MemorySaver() 
swarm_graph = workflow.compile(checkpointer=checkpointer)

Action Task

The following function works as public interface whispering.

def run_banking_analysis(query: str, thread_id: str = "default", verbose: bool = True):
    return swarm_graph.invoke(
        {"messages": [("user", query)]},
        config={"configurable": {"thread_id": thread_id}},
    )

Running the Swarm: End-to-End Example

Now, let’s go through a real example to understand how the current behaves.

result4 = run_banking_analysis( 
   "Start with customers grouped by state, then drill down into branches    within that state, and finally into account types under each branch —    showing the number of accounts at each level", 
   thread_id="test4", 
   verbose=True 
)

Answer:

======================================================================
SWARM ANALYSIS: 'Start with customers grouped by state, then drill down into branches within that state, and finally into account types under each branch — showing the number of accounts at each level'
======================================================================

USER: Start with customers grouped by state, then drill down into branches within that state, and finally into account types under each branch — showing the number of accounts at each level

A bar graph
EDA VISUALIZER: I have created a grouped bar chart showing the number of accounts by customer state, branch, and account type. Each group of bars represents a branch, with bars colored and labeled by the combination of state and account type.  

Insights:  
- The Dubai Marina branch has a higher number of checking accounts in the DL state compared to savings accounts.  
- Paris Champs-Élysées shows a balanced distribution of checking and savings accounts across states, with MH state having the highest savings accounts there.  
- Sydney Harbour branch has a notable number of checking accounts in DL and KA states, while savings accounts are more prominent in MH and DL states.  

This visualization helps identify which branches and states have more accounts by type, enabling targeted marketing or resource allocation for account management.  

If you want, I can also prepare a hierarchical treemap or sunburst chart to better visualize the drill-down structure from state to branch to account type. Would you like me to do that? ======================================================================  

EXECUTION COMPLETE (3 steps) ======================================================================

Read more: Build a Revenue Report Agent using Swarm Architecture

The conclusion

The combination of expert agents enables us to create intelligent pipelines using network-based multi-agent systems. This guide shows how to create a swarm system that includes a Data Analysis Agent and a Data Visualization Agent controlled by an orchestrator. Swarm agents provide organizations with two benefits because they allow teams to make decisions without centralized control and allow team members to take on different responsibilities that allow them to complete complex projects efficiently and reliably.

Defined agent roles and communication patterns exist as coded objects that we used to develop a system that takes a user query and produces both analysis and visual output.

Frequently Asked Questions

Q1. What is swarm architecture in AI?

A. It is a system where specialized AI agents interact, each handling tasks such as analysis or visualization, to solve complex data problems efficiently.

Q2. What roles do agents play in this whirlwind system?

A. The Data Analyst processes and analyzes the data, while the Visualization Agent creates the charts, which are compiled by an orchestrator that controls the workflow.

Q3. Why use swarm agents instead of a single AI agent?

A. Swarm agents improve scalability, fault tolerance, and operational expertise, allowing complex workflows to run faster and more reliably.

Vipin Vashisth

Hello! I am Vipin, a data science and machine learning enthusiast with a strong foundation in data analysis, machine learning algorithms, and programming. I have practical experience in building models, handling messy data, and solving real-world problems. My goal is to use data-driven insights to create effective solutions that drive results. I am eager to contribute my skills to the collaborative environment while continuing to learn and grow in the fields of Data Science, Machine Learning, and NLP.

Sign in to continue reading and enjoy content curated by experts.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button