LangChain AI Multi-Agent Template
AgentTemplate for building multi-agent systems with LangChain AI's unified framework.
LangChain AI Multi-Agent Template
Overview
A starter template for building multi-agent systems using LangChain AI's unified framework. This template provides a production-ready structure for creating collaborative agent teams.
Template Structure
my-multi-agent-system/
├── agents/
│ ├── __init__.py
│ ├── researcher.py
│ ├── analyst.py
│ └── writer.py
├── tools/
│ ├── __init__.py
│ ├── web_search.py
│ └── citation_checker.py
├── memory/
│ ├── __init__.py
│ └── conversation.py
├── main.py
├── config.py
└── requirements.txt
Quick Start
1. Setup
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies
pip install langchain-ai langchain-ai[openai]
2. Configure API Key
export OPENAI_API_KEY="sk-..."
# or
export ANTHROPIC_API_KEY="sk-ant-..."
3. Define Your Agents
agents/researcher.py
from langchain_ai import Agent
def create_researcher() -> Agent:
return Agent(
name="Researcher",
instructions="""You are a thorough researcher.
Find relevant, credible sources and extract key information.
Always cite your sources with URLs.""",
tools=["web_search", "academic_search"]
)
agents/analyst.py
from langchain_ai import Agent
def create_analyst() -> Agent:
return Agent(
name="Analyst",
instructions="""You are a critical analyst.
Evaluate source credibility, identify biases,
and synthesize findings into coherent insights.""",
tools=["citation_checker"]
)
agents/writer.py
from langchain_ai import Agent
def create_writer() -> Agent:
return Agent(
name="Writer",
instructions="""You are a clear, engaging writer.
Transform analysis into well-structured,
accessible reports with proper citations.""",
tools=["grammar_check"]
)
4. Create Custom Tools
tools/web_search.py
from langchain_ai import tool
@tool
async def web_search(query: str) -> list[dict]:
"""Search the web for information."""
# Implement your search logic
results = await perform_search(query)
return results
5. Build the System
main.py
from langchain_ai import MultiAgentSystem, Memory
from agents import create_researcher, create_analyst, create_writer
from tools import web_search
# Create agents
researcher = create_researcher()
analyst = create_analyst()
writer = create_writer()
# Add tools
researcher.add_tool(web_search)
# Create system with memory
system = MultiAgentSystem(
agents=[researcher, analyst, writer],
memory=Memory.conversation(store="sqlite"),
router="auto",
collaboration="handoff"
)
# Run
if __name__ == "__main__":
result = system.run(
"Research the impact of AI on healthcare in 2025."
)
print(result)
Configuration
config.py
from pydantic import BaseModel
class Config(BaseModel):
api_key: str
model: str = "gpt-4o"
max_tokens: int = 4000
temperature: float = 0.7
@classmethod
def from_env(cls) -> "Config":
import os
return cls(
api_key=os.environ["OPENAI_API_KEY"],
model=os.environ.get("MODEL", "gpt-4o")
)
config = Config.from_env()
Advanced Patterns
Pattern 1: Hierarchical Team
from langchain_ai import Agent, MultiAgentSystem
# Manager coordinates specialists
manager = Agent(
name="Manager",
instructions="Coordinate the team and ensure quality."
)
specialists = [
Agent(name="Specialist_1", ...),
Agent(name="Specialist_2", ...),
]
system = MultiAgentSystem(
agents=[manager] + specialists,
collaboration="hierarchical"
)
Pattern 2: Parallel Execution
# Multiple agents work in parallel
researchers = [
Agent(name=f"Researcher_{i}", tools=[web_search])
for i in range(3)
]
system = MultiAgentSystem(
agents=researchers,
collaboration="parallel",
aggregator="synthesis"
)
Pattern 3: Human-in-the-Loop
system = MultiAgentSystem(
agents=[researcher, analyst, writer],
human_review_points=["after_research", "after_analysis"],
approval_required=True
)
Best Practices
- Keep agents focused - Each agent should have a single, clear responsibility
- Use appropriate tools - Don't overload agents with unnecessary tools
- Implement memory - Essential for multi-step workflows
- Add observability - Track agent decisions for debugging
- Test iteratively - Start with one agent, add complexity gradually
Troubleshooting
| Issue | Solution |
|---|---|
| Agents not collaborating | Check router and collaboration settings |
| Missing information | Add more relevant tools |
| Poor output quality | Refine agent instructions |
| High latency | Use smaller models for simple tasks |
Resources
Last updated: May 2026
