Examples
This directory contains example implementations of LangGraphGo Swarm.
Basic Example
Location: basic/main.go
A simple demonstration of a two-agent swarm:
- Alice: An addition expert who can do math
- Bob: A pirate-speaking agent
Shows basic agent creation, handoff tools, and multi-turn conversations.
cd basic
export OPENAI_API_KEY=your_key_here
go run main.go
Customer Support Example
Location: customer_support/main.go
A realistic customer support system demonstrating:
- Multiple specialized agents (Flight Assistant, Hotel Assistant)
- Agent handoff between different service domains
- Maintaining user context and reservations across handoffs
- Mock database for flights and hotels
- Tool integration (search and booking functions)
cd customer_support
export OPENAI_API_KEY=your_key_here
go run main.go
Features Demonstrated
- Dynamic Agent Routing: Automatically routes to the appropriate specialist
- Context Preservation: User reservations persist across agent transitions
- Domain-Specific Tools: Each agent has specialized tools for their domain
- Natural Handoffs: Agents can seamlessly transfer control
Running Examples
Prerequisites
-
Install Go 1.21 or later
-
Set your OpenAI API key:
export OPENAI_API_KEY=your_api_key_here
-
Install dependencies:
go mod download
Run an Example
cd examples/<example_name>
go run main.go
Creating Your Own Example
Here's a template for creating a new agent swarm:
package main
import (
"context"
"github.com/smallnest/langgraphgo/graph"
"github.com/tmc/langchaingo/llms/openai"
"github.com/yourusername/langgraphgo_swarm/swarm"
)
func main() {
ctx := context.Background()
model, _ := openai.New()
// 1. Create your agents
agent1 := createYourAgent(model)
agent2 := createAnotherAgent(model)
// 2. Create the swarm
workflow, _ := swarm.CreateSwarm(swarm.SwarmConfig{
Agents: []swarm.Agent{
{Name: "Agent1", Runnable: agent1, Destinations: []string{"Agent2"}},
{Name: "Agent2", Runnable: agent2, Destinations: []string{"Agent1"}},
},
DefaultActiveAgent: "Agent1",
})
// 3. Compile and run
app, _ := workflow.Compile()
result, _ := app.Invoke(ctx, initialState)
}
Example Patterns
Pattern 1: Sequential Specialists
Agents work in sequence, each handling a specific part of the task:
User → Analyst → Researcher → Writer → Reviewer
Pattern 2: Hub and Spoke
A coordinator agent routes to specialists:
┌─→ Flight Agent
Coordinator ├─→ Hotel Agent
└─→ Car Rental Agent
Pattern 3: Peer Collaboration
Agents can call each other as needed:
Alice ⟷ Bob ⟷ Charlie
Tips
- Clear Responsibilities: Give each agent a clear, specific role
- Good Descriptions: Write clear handoff tool descriptions so agents know when to transfer
- Context Management: Include relevant context in your state schema
- Error Handling: Always handle errors from agent invocations
- Testing: Test individual agents before combining into a swarm
Additional Resources