This example demonstrates advanced workflow orchestration using a graph-based architecture with nodes, edges, conditional routing, and ReAct (Reasoning and Acting) agent integration - inspired by modern AI agent frameworks like LangGraph.
- Graph-Based Workflows: Multi-node workflows with conditional edges
- ReAct Agent Pattern: Reasoning and Acting with tool integration
- State Management: Data flow and state tracking across nodes
- Dynamic Routing: Conditional workflow paths based on analysis
- Tool Integration: Advanced tool usage within workflow context
- Result Aggregation: Combining outputs from parallel execution paths
Input → Analysis → ReAct → Decision
├─→ Math Task ──┐
├─→ Research Task ─→ Aggregation → Output
└─→ Analysis Task ─┘
- Input Node: Entry point, initializes workflow state
- Analysis Node: Classifies task type and complexity
- ReAct Node: Implements reasoning and planning with tools
- Decision Node: Routes to appropriate task execution path
- Task Execution Nodes: Specialized processing (math/research/analysis)
- Aggregation Node: Combines and synthesizes results
- Output Node: Formats final response
- Sequential Edges: Direct node-to-node connections
- Conditional Edges: Route based on state conditions
- Parallel Edges: Multiple execution paths from decision points
The ReAct agent implements the Reasoning and Acting pattern:
- Analyzes the task and context
- Creates step-by-step plans
- Identifies required tools and capabilities
- Executes planned actions
- Uses available tools
- Adapts based on intermediate results
- Calculator: Mathematical operations and computations
- Web Search: Information retrieval (simulated)
- Data Analysis: Statistical analysis and insights
- Planner: Task planning and strategy creation
- Ollama Installation: Download from ollama.com
- Start Ollama:
ollama serve - Pull Model:
ollama pull gemma3:1b
cd examples/09-workflow-graph
go run main.goTry these tasks to see different workflow paths:
Mathematical Tasks (routes to math execution):
Calculate the compound interest on $1000 at 5% for 3 years
Solve the quadratic equation x² + 5x + 6 = 0
What is the derivative of x³ + 2x² - 5x + 1?
Research Tasks (routes to research execution):
Research the latest developments in quantum computing
What are the current trends in artificial intelligence?
Explain the benefits of renewable energy sources
Analysis Tasks (routes to analysis execution):
Analyze the pros and cons of remote work
Compare different machine learning algorithms
Evaluate the impact of social media on society
- Persistent State: Data flows through all nodes
- Context Tracking: Maintains execution context
- History Recording: Tracks all node executions
- Metadata: Additional workflow information
- Task Classification: Automatic task type detection
- Conditional Edges: Route based on analysis results
- Parallel Execution: Multiple specialized processing paths
- Result Synthesis: Intelligent aggregation of outputs
- Execution Tracking: Real-time workflow progress
- Performance Metrics: Node execution times
- Error Handling: Graceful failure management
- State Inspection: View workflow state at any point
/graph- Show detailed workflow graph structure/state- Display current workflow state/history- View execution history/reset- Reset workflow state/help- Show comprehensive help
{
From: "decision",
To: "task_math",
Label: "mathematical_task",
Condition: func(state *WorkflowState) bool {
taskType, exists := state.Context["task_type"].(string)
return exists && strings.Contains(strings.ToLower(taskType), "math")
},
}type WorkflowState struct {
ID string `json:"id"`
Input string `json:"input"`
CurrentNode string `json:"current_node"`
Context map[string]interface{} `json:"context"`
History []NodeExecution `json:"history"`
Result string `json:"result"`
Metadata map[string]string `json:"metadata"`
}func (agent *ReActAgent) Execute(state *WorkflowState) (*WorkflowState, error) {
// 1. Analyze the task
// 2. Create reasoning plan
// 3. Identify required tools
// 4. Execute action sequence
// 5. Update state with results
}- Average Execution Time: 5-15 seconds (depends on task complexity)
- Memory Usage: ~200-400MB (includes full state tracking)
- Scalability: Supports complex multi-step workflows
- Reliability: Built-in error handling and recovery
After running this example, you'll understand:
- Graph-Based Architecture: How to design workflows as directed graphs
- ReAct Pattern: Implementing reasoning and acting in AI agents
- State Management: Managing data flow in complex workflows
- Conditional Routing: Dynamic workflow paths based on conditions
- Tool Integration: Using tools within workflow contexts
- Result Synthesis: Combining outputs from multiple execution paths
- Input Processing: Task received and initial state created
- Analysis Phase: Task classification and complexity assessment
- ReAct Planning: Reasoning about approach and tool requirements
- Decision Routing: Conditional routing to specialized execution paths
- Task Execution: Specialized processing based on task type
- Result Aggregation: Synthesis of results from execution paths
- Output Formatting: Final response preparation and delivery
- Nodes: Processing units with specific capabilities
- Edges: Connections defining possible transitions
- State: Data that flows through the graph
- Routing: Dynamic path selection based on conditions
- Observation: Understanding current state and context
- Thought: Reasoning about next actions
- Action: Executing planned steps with tools
- Reflection: Evaluating results and planning next steps
- Conditional Branching: Different paths based on analysis
- Parallel Processing: Multiple simultaneous execution paths
- State Aggregation: Combining results from parallel paths
- Error Recovery: Handling failures gracefully
This example provides a foundation for building sophisticated AI agent workflows. You can extend it by:
- Adding More Node Types: Create specialized processing nodes
- Enhanced Tool Integration: Implement real tool connections
- Complex Routing Logic: More sophisticated conditional edges
- Persistence Layer: Save and restore workflow states
- Distributed Execution: Scale across multiple instances
- Visual Workflow Designer: GUI for workflow creation
This represents the cutting edge of AI agent architecture, combining the power of graph-based workflows with intelligent reasoning and tool usage!