when syntax that works in both AgentFlow pipelines and Task teams.
when expressions gate tasks and AgentFlow steps on runtime variables.
Overview
Conditional execution allows you to control workflow branching based on variables, scores, or other runtime values. PraisonAI supports:- String expression conditions - Simple
{{variable}}syntax for comparisons - Dictionary routing - Map decision values to next tasks
- Callable conditions - Custom Python functions
Quick Start
1
Task or AgentFlow
Condition Syntax
String Expression Conditions
Use{{variable}} placeholders with comparison operators:
Examples
Task Condition Parameters
when Parameter
The when parameter accepts a string expression condition:
then_task and else_task
Route to different tasks based on condition result:
routing Parameter (Advanced)
For LLM-driven decisions, use the routing parameter (formerly condition):
The
condition parameter still works for backward compatibility, but routing is preferred for clarity.should_run Callable
For complex logic, use a callable:
AgentFlow Conditions
when() Function
Nested Conditions
Flow Diagram
How It Works
Best Practices
Use simple conditions
Use simple conditions
Keep conditions readable and simple. Complex logic should go in
should_run callables.Provide both then_task and else_task
Provide both then_task and else_task
Always specify both branches to make the flow explicit:
Use routing for LLM decisions
Use routing for LLM decisions
When the LLM needs to make a decision, use
routing with task_type="decision":Migration Guide
From condition to routing
Adding when to existing Tasks
API Reference
Task Parameters
Task Methods
Related
AgentFlow
Learn about deterministic pipelines
AgentTeam
Multi-agent task orchestration

