Overview
LangGraphAgent implements a directed graph of agents, allowing you to build sophisticated workflows with:
- Conditional branching based on state or events
- Loops and cyclic execution patterns
- Multiple entry and exit points
- Dynamic routing between agents
- Max steps protection against infinite loops
Basic Usage
Creating a LangGraphAgent
Using AgentBuilder
Direct Construction
Configuration
Node Configuration
Node Properties
Conditional Branching
Basic Branching
Multi-Way Branching
Loops and Cycles
Simple Loop
Conditions on the
check node’s targets (pointing back to process) control whether the loop continues. The condition on the complete node determines when to exit.Retry Pattern
Max Steps Protection
Prevent infinite loops withmaxSteps:
Error Handling
State Management
State is shared across all nodes in the graph:Accessing Graph Information
Execution Results
Complex Example: Order Processing
Graph Validation
LangGraph validates the graph structure on construction:- All target nodes exist
- Root node exists in nodes array
- No duplicate node names
Best Practices
// Good
condition: async (lastEvent, context) => {
const value = context.state.get('status');
if (!value || typeof value !== 'string') {
return false;
}
return value === 'success';
}
// Less robust
condition: async (lastEvent, context) => {
return context.state.get('status') === 'success';
}
// Good
const nodes = [
{ name: 'validate_input', agent: validator },
{ name: 'process_payment', agent: processor },
{ name: 'handle_payment_error', agent: errorHandler }
];
// Less clear
const nodes = [
{ name: 'step1', agent: validator },
{ name: 'step2', agent: processor },
{ name: 'error', agent: errorHandler }
];
// Good - prevents infinite loops
const workflow = new LangGraphAgent({
name: 'workflow',
description: 'Safe workflow',
nodes,
rootNode: 'start',
maxSteps: 50
});
const step1 = new LlmAgent({
name: 'step1',
description: 'Step 1',
outputKey: 'step1_result', // Save to state
model: 'gemini-2.5-flash'
});
const step2 = new LlmAgent({
name: 'step2',
description: 'Step 2',
instruction: 'Use data: {step1_result}', // Read from state
model: 'gemini-2.5-flash'
});
{
name: 'retry',
agent: retryAgent,
condition: async (lastEvent, context) => {
// Retry if:
// 1. Not successful
// 2. Fewer than 3 attempts
// 3. Error is retryable
const success = context.state.get('success', false);
const attempts = context.state.get('attempts', 0);
const errorType = context.state.get('errorType', '');
return (
!success &&
attempts < 3 &&
['timeout', 'network'].includes(errorType)
);
}
}
Comparison with Other Workflows
| Feature | LangGraph | Sequential | Parallel | Loop |
|---|---|---|---|---|
| Branching | Yes | No | No | No |
| Loops | Yes | No | No | Yes |
| Conditions | Yes | No | No | Via escalate |
| Complexity | High | Low | Low | Medium |
| Use Case | Complex workflows | Pipelines | Concurrent tasks | Iteration |
Next Steps
Workflow Agents
Overview of all workflow patterns
State Management
Managing state in workflows
Examples
LangGraph examples
LoopAgent
Simpler iterative patterns