Implement dynamic routing and control flow in your multi-agent workflows based on agent outputs and state
Conditional edges enable dynamic routing in your agent workflows. Instead of following a fixed path, agents can route to different next steps based on their output, state, or structured data.
edges: - from: "agent_a" to: "agent_b" # Always goes to agent_b
With conditional edges, the next step is determined at runtime:
Copy
edges: - from: "classifier" condition: "Route to the appropriate team based on {{classifier.output.category}}" condition_type: "literal" possible_outputs: ["tech_support", "billing_support", "general_support"]
name: "Content Quality Gate"description: "Review and revise content until it meets quality standards"architecture: "workflow"agents: - name: "writer" agent_type: "llm_agent" system_prompt: "Write a blog post on the given topic." - name: "quality_checker" agent_type: "llm_agent" system_prompt: | Review the blog post for quality. Check for: - Clear and engaging writing - Proper grammar and spelling - Logical structure - Appropriate length Determine if it passes quality standards. structured_output: enabled: true schema: passes_quality: type: "bool" description: "Whether the content meets quality standards" issues: type: "list[str]" description: "List of quality issues found" suggestions: type: "list[str]" description: "Suggestions for improvement" - name: "reviser" agent_type: "llm_agent" system_prompt: | Revise the blog post based on the quality feedback provided. Address all issues and implement suggestions. - name: "publisher" agent_type: "llm_agent" system_prompt: "Format and publish the approved blog post."edges: - from: "__start__" to: "writer" - from: "writer" to: "quality_checker" # Conditional edge based on quality check - from: "quality_checker" condition: "Does the content pass quality standards? Check {{quality_checker.output.passes_quality}}" condition_type: "boolean" routing: yes: "publisher" # If passes_quality = true no: "reviser" # If passes_quality = false - from: "reviser" to: "quality_checker" # Re-check after revision - from: "publisher" to: "__end__"entry_point: "writer"
Use Structured Output: Always use structured output with str fields for conditional routing. Specify allowed values clearly in descriptions and system prompts to ensure reliable, predictable routing decisions.
Clear Conditions: Make routing conditions explicit and mutually exclusive. Avoid ambiguous categories.
Limit Iterations: Set maximum iteration counts for loops to prevent infinite cycles.
Handle All Cases: Ensure every possible output value has a corresponding route. Missing routes will cause errors.
Test Edge Cases: Test your conditional logic with edge cases and unexpected inputs.
Quality Gates: Check if work meets standards → approve or send for revisionClassification: Categorize inputs → route to specialized handlersEscalation: Try automated solution → escalate to human if neededIterative Refinement: Generate → evaluate → improve until acceptable
Conditional edges only work with workflow architecture, not with single agents
For literal edges, every value in possible_outputs must be a valid agent name