Overview
Conditional edges allow you to:- Route based on classification: Send content to different agents based on categories
- Implement quality gates: Retry or revise work that doesn’t meet standards
- Create decision trees: Branch workflows based on agent decisions
- Build adaptive systems: Change behavior based on runtime conditions
Conditional edges enable dynamic routing in workflows, allowing agents to make runtime decisions about which path to take next.
How Conditional Edges Work
In a standard workflow, edges are static:Conditional Edge Types
There are two types of conditional edges:1. Boolean Edges
For binary yes/no decisions (quality gates, approvals):condition: Template string with variable substitutioncondition_type: Must be"literal"possible_outputs: List of valid agent names to route to
llm_model: Specific model to use for this routing decision. Must be compatible with BAML.
Basic Conditional Routing
Boolean Routing (Quality Gates)
Route based on true/false conditions:Multi-Way Routing (Classification)
Route to different agents based on categories:Using with Structured Output
For reliable routing, use structured output to control conditional edges:Loop-Back Edges
Conditional edges can route back to a previous agent, creating loops for iterative refinement. Userecursion_limit at the system level to prevent infinite cycles:
Conditional Routing from START
You can use a conditional edge from__start__ to dynamically select the first agent based on user input. Set entry_point to "START" to enable this:
When
entry_point is "START", no automatic edge from __start__ to an agent is created. You must define your own edge from __start__ (either static or conditional).How Evaluation Works
Under the hood, conditional edges use a multi-step evaluation pipeline:- Variable substitution: Template variables like
{{agent.output.field}}are replaced with actual values from the workflow state - LLM evaluation: The substituted condition is sent to an LLM via BAML structured output for reliable parsing. This uses the default model unless overridden by
llm_model. - Validation: The LLM’s response is validated (against
possible_outputsfor literal, oryes/nofor boolean) - Retry logic: If the response is invalid, the evaluation is retried up to 3 times with a 1-second delay
- Fallback: If all retries fail, the edge routes to
__end__to terminate the workflow gracefully
The BAML evaluation returns both a routing decision and a reasoning explanation. The reasoning is included in observability events for debugging.
Template Variables in Conditions
Condition templates support the same{{ }} syntax as agent prompts:
Special Node Names
Both__start__/__end__ and START/END are valid node names in edges. They are normalized internally:
Best Practices
Common Use Cases
Quality Gates: Check if work meets standards → approve or send for revision Classification: Categorize inputs → route to specialized handlers Escalation: Try automated solution → escalate to human if needed Iterative Refinement: Generate → evaluate → improve until acceptableNext Steps
Structured Output
Learn how to define schemas for reliable conditional routing
Sequential Patterns
Build linear pipelines with static edges
Supervisor Pattern
Use dynamic routing without explicit edges
YAML Configuration
Complete guide to workflow and edge configuration