Build Your First Multi-Agent AI System with Python and LangGraph
Building Multi-Agent AI Systems: Plain Python vs. LangGraph As developers, we often tackle complex tasks by breaking them down into smaller, manageable pieces. This principle applies equally to AI systems, especially

Building Multi-Agent AI Systems: Plain Python vs. LangGraph
As developers, we often tackle complex tasks by breaking them down into smaller, manageable pieces. This principle applies equally to AI systems, especially when working with Large Language Models (LLMs). While a single, sophisticated prompt can sometimes achieve remarkable results, it can quickly become unwieldy, difficult to maintain, and challenging for smaller, locally run models to execute reliably. This is where multi-agent AI systems shine.
In this article, we'll explore how to construct a multi-agent system in Python, first using a minimalist, framework-free approach, and then by leveraging the LangGraph library. Our goal is to illustrate the core concepts and highlight the advantages an orchestration framework like LangGraph brings for more intricate workflows, all while running our agents locally with Ollama and Qwen to avoid API costs.
What is a Multi-Agent System?
At its core, a multi-agent AI system is a collection of specialized AI agents that collaborate to accomplish a larger objective. Instead of burdening a single LLM with a broad, complex prompt, the workload is distributed among several agents. Each agent is designed with:
- A focused, specific responsibility.
- Its own tailored prompt and set of instructions.
- A defined position within the overall workflow.
This division of labor allows each agent to have a simpler, more precise objective, making its prompt easier for the underlying LLM to follow consistently. Multi-agent systems are particularly effective when a task naturally segregates into distinct roles or sequential steps, such as planning, drafting, reviewing, or applying different specialized prompts to various parts of a process. If a single agent can handle a task efficiently and reliably with a clear prompt, adding more agents might introduce unnecessary complexity or latency.
Our Use Case: A Study Guide Generator
To demonstrate these concepts, we'll build a simple AI-powered study guide generator. Given a topic, our system will produce a structured study guide complete with an outline, detailed notes, and review questions. A single-agent approach might use a prompt like this:
plaintext Create a beginner-friendly study guide for this topic: {topic} The output should have exactly these sections:
- Outline
- Break the topic into 3 short study sections
- Notes
- Write short, clear study notes for each section
- Keep the explanations concise and easy to understand
- Review Questions
- Write 3 short review questions based on the notes Return the result in clean Markdown.
This monolithic prompt places a significant cognitive load on a smaller local model. Our multi-agent solution, however, will split this into three specialized agents:
- Planner: Breaks the given topic into logical study sections.
- Teacher: Crafts concise study notes for each section.
- Quiz Writer: Generates relevant review questions based on the notes.
We'll implement this workflow in two ways: first using standard Python for explicit coordination, and then with LangGraph, which models the workflow as a directed graph with nodes, edges, and shared state.
Getting Started: Installation
To follow along, you'll need Ollama installed and a Qwen model pulled. Ollama enables running LLMs locally, eliminating API costs.
First, pull the model:
bash ollama pull qwen3.5:4b
Next, set up your Python environment and install the necessary libraries:
bash pthon3 -m venv venv source venv/bin/activate pip install langchain-ollama langgraph
Simple Python Implementation
Our first approach uses plain Python functions to coordinate the LLM calls. Each agent is simply a dedicated function with a focused system prompt. The build_study_guide function orchestrates these calls sequentially, passing the output of one agent as input to the next.
python import time from langchain_ollama import ChatOllama
Local Ollama model used by all three agents.
MODEL = ChatOllama(model="qwen3.5:4b", temperature=0)
def ask(system: str, user: str) -> str: """Run one LLM call with a system prompt and user input.""" response = MODEL.invoke([ {"role": "system", "content": system}, {"role": "user", "content": user}, ]) return response.content
def run_agent(name: str, system: str, user: str) -> str: """Helper that logs how long each agent takes.""" print(f"Calling agent {name}...") start = time.time() result = ask(system, user) print(f"Finished {name} in {time.time() - start:.1f}s") return result
Agent 1: create a short outline
def planner_agent(topic: str) -> str: return run_agent( "planner_agent", "Break this topic into 3 short study sections.", topic, )
Agent 2: turn the outline into notes
def teacher_agent(topic: str, outline: str) -> str: return run_agent( "teacher_agent", "Write short beginner-friendly notes using the outline. Keep it concise.", f"Topic: {topic} Outline: {outline}", )
Agent 3: write review questions from the notes
def quiz_agent(topic: str, notes: str) -> str: return run_agent( "quiz_agent", "Write 3 short review questions based on the notes.", f"Topic: {topic} Notes: {notes}", )
def build_study_guide(topic: str) -> str: """Run all three agents in sequence and combine their output.""" outline = planner_agent(topic) notes = teacher_agent(topic, outline) quiz = quiz_agent(topic, notes) return ( f"# Study Guide: {topic} " f"## Outline {outline} " f"## Notes {notes} " f"## Review Questions {quiz} " )
if name == "main": print("Warming up model...") MODEL.invoke("Say ready.") print("Model ready. ") topic = input("Enter a study topic: ").strip() print(" " + build_study_guide(topic))
To run this, save it as study_guide_v1.py and execute:
bash python study_guide_v1.py
This version demonstrates that a multi-agent system can be remarkably simple, requiring no external frameworks for basic sequential orchestration.
LangGraph Version: Nodes and Edges
For more complex workflows involving shared state, conditional branching, or loops, an orchestration framework becomes invaluable. LangGraph allows us to define our multi-agent system as a graph, where:
- Each specialized agent becomes a node.
- Data shared between agents is managed in a graph state.
- The execution order is explicitly defined by edges.
python from typing import TypedDict import time from langchain_ollama import ChatOllama from langgraph.graph import StateGraph, START, END
Local Ollama model used by all nodes.
MODEL = ChatOllama(model="qwen3.5:4b", temperature=0)
Shared state passed between nodes.
class StudyState(TypedDict): topic: str outline: str notes: str quiz: str
def ask(system: str, user: str) -> str: response = MODEL.invoke([ {"role": "system", "content": system}, {"role": "user", "content": user}, ]) return response.content
def run_node(name: str, system: str, user: str) -> str: print(f"Calling node {name}...") start = time.time() result = ask(system, user) print(f"Finished {name} in {time.time() - start:.1f}s") return result
Node 1: create the outline
def planner(state: StudyState) -> dict: return { "outline": run_node( "planner", "Break this topic into 3 short study sections.", state["topic"], ) }
Node 2: write notes from the outline
def teacher(state: StudyState) -> dict: return { "notes": run_node( "teacher", "Write short beginner-friendly notes using the outline. Keep it concise.", f"Topic: {state['topic']} Outline: {state['outline']}", ) }
Node 3: write review questions from the notes
def quiz_writer(state: StudyState) -> dict: return { "quiz": run_node( "quiz_writer", "Write 3 short review questions based on the notes.", f"Topic: {state['topic']} Notes: {state['notes']}", ) }
def build_graph(): graph = StateGraph(StudyState) # Add the nodes graph.add_node("planner", planner) graph.add_node("teacher", teacher) graph.add_node("quiz_writer", quiz_writer) # Define the order of execution graph.add_edge(START, "planner") graph.add_edge("planner", "teacher") graph.add_edge("teacher", "quiz_writer") graph.add_edge("quiz_writer", END) return graph.compile()
if name == "main": print("Warming up model...") MODEL.invoke("Say ready.") print("Model ready. ") app = build_graph() topic = input("Enter a study topic: ").strip() result = app.invoke({ "topic": topic, "outline": "", "notes": "", "quiz": "", }) print( f"
Study Guide: {topic}
" f"## Outline {result['outline']} " f"## Notes {result['notes']} " f"## Review Questions {result['quiz']} " )
Save this as study_guide_v2.py and run:
bash python study_guide_v2.py
The LangGraph version achieves the same outcome but provides a structured, visualizable representation of the workflow. Each node updates the shared StudyState dictionary, and the graph defines how this state flows from one node to the next.
Comparison and Tradeoffs
Both implementations generate a study guide using a multi-agent approach. The simple Python version is ideal for straightforward, linear workflows where explicit function calls are sufficient. It offers minimal overhead and maximum control, often being the most practical starting point.
LangGraph, however, excels when the workflow complexity increases. Its explicit graph structure, shared state management, and ability to define conditional edges and loops make it a superior choice for dynamic and sophisticated agent coordination. While introducing a framework adds a layer of abstraction, it significantly enhances maintainability and scalability for evolving multi-agent systems.
Common Multi-Agent Patterns
Beyond the sequential pipeline demonstrated here, several other multi-agent patterns are common:
- Parallel Specialists: Multiple agents independently process the same input, and their outputs are subsequently merged. Ideal for non-dependent subtasks.
- Orchestrator–Subagent: A high-level agent decomposes a task, delegates parts to specialized subagents, and then integrates their results. Useful for coordinating many specialized agents.
- Supervisor / Router: An agent determines which specialist should handle an incoming request, based on input type or context.
- Human-in-the-loop: An agent drafts content or performs an action, but human review or approval is required before the workflow proceeds. Crucial for sensitive or user-facing applications.
- Review / Refinement loop: One agent produces an output, and another agent (or the same one recursively) checks or improves it, enhancing quality at the cost of potential latency.
Conclusion
Building multi-agent AI systems, even simple ones, significantly improves the manageability and output quality of LLM applications, particularly with smaller, locally hosted models. By breaking down complex prompts into focused agent responsibilities, we create more robust and adaptable systems.
Whether you opt for the simplicity of plain Python or the structured power of LangGraph depends on your workflow's complexity. For a fixed, linear sequence, Python is often best. For dynamic flows, shared state, and advanced coordination, LangGraph provides the necessary tools. Experiment by extending this example—perhaps add a 'rewriter' node for simpler language, a 'reviewer' to validate quiz questions, or conditional logic for different topic complexities. Happy tinkering!
FAQ
Q: When should I choose plain Python over LangGraph for a multi-agent system?
A: Plain Python is ideal for lightweight, fixed-sequence, linear workflows where each agent's output directly feeds into the next, and explicit function calls offer sufficient coordination. It provides simplicity and avoids framework overhead.
Q: What are the key components of a multi-agent system built with LangGraph?
A: In LangGraph, the key components are Nodes (representing individual agents or steps), a StateGraph (which defines the shared state passed between nodes), and Edges (which define the flow and order of execution between nodes).
Q: How do agents communicate or share information between steps in the provided examples?
A: In the simple Python version, agents communicate by directly passing return values from one function (agent) as arguments to the next function. In the LangGraph version, agents communicate by reading from and writing to a shared StudyState dictionary, which is passed between nodes.
Related articles
JPMorgan Chase Taps Seattle for Critical AI Control Layer Development
Global financial giant JPMorgan Chase is making a significant strategic investment in Seattle, establishing a new AI software infrastructure team. This pivotal group will build an "AI control layer" to manage the bank's AI operations, aiming to control costs, protect intellectual property, and prevent vendor lock-in.
The Motorola Edge 70 Max is all about power: Android — Key Details
Motorola has launched its new flagship, the Edge 70 Max, designed for power users with a massive 7100mAh silicon-carbon battery and 25W Qi2 wireless charging. It’s the first Android phone since the Pixel 10 Pro XL to support full 25W Qi2, surpassing other Qi2-enabled Androids capped at 15W. The device also offers 90W wired charging and a Snapdragon 8 Gen 5 chip.
Best Verizon Plans 2026: Navigating Your Wireless Future
Verizon has been shaking things up, introducing price adjustments and a new 'Simplicity' plan in late 2025 and early 2026. Their approach remains distinct: optional perks allow for customization, but this flexibility
Is Your Smart Fridge a Scraper? New Data Uncovers Hidden Botnets
New data from Anubis' honeypot reveals a pervasive scraping problem, with nearly 90% of observed scraper IPs not on traditional threat lists. This global phenomenon is likely driven by compromised smart appliances, highlighting a hidden botnet threat. The findings underscore the need for advanced WAFs and user vigilance in securing IoT devices.
Master Excel PivotTables: Summarize Data with Ease
Learn to create, customize, and analyze data with Excel PivotTables in simple, step-by-step instructions. Discover how to prepare your data, use the PivotTable Fields pane, and apply interactive filters like slicers for instant insights. Gain control over large datasets and generate clear reports effortlessly.
DeepMind CEO calls for independent body to regulate frontier AI
DeepMind CEO Demis Hassabis has proposed an independent standards body, modeled after FINRA, to regulate frontier AI models. The body would test advanced AI systems and develop best practices for their release, initially on a voluntary basis before potentially becoming mandatory. This initiative aims to provide technically focused, adaptable oversight to the rapidly evolving field of AI.





