Conditions

Add conditional logic to agent workflows

Conditions let you control workflow branching based on runtime decisions.

Diagram

Conditional Flow

Yes

No

Start

Condition?

Path A

Path B

End

Quick Start

  1. Simple Condition
    rust
    use praisonai::{Agent, when};
    
    let route = when(|input| input.contains("urgent"))
        .then(priority_agent)
        .otherwise(standard_agent);
    
    route.run("urgent: fix bug").await?;
    // Routes to priority_agent
  2. Multiple Conditions
    rust
    use praisonai::when;
    
    let router = when(|i| i.contains("code"))
        .then(coder)
        .when(|i| i.contains("write"))
        .then(writer)
        .otherwise(assistant);

How It Works

Diagram
AgentBAgentARouterUserAgentBAgentARouterUseralt[Contains “urgent”][Default]“urgent task”Check conditionsRoute to PriorityResultRoute to StandardResult

Common Patterns

Intent-Based Routing

rust
use praisonai::when;

let router = when(|input| classify(input) == "technical")
    .then(tech_support)
    .when(|input| classify(input) == "billing")
    .then(billing_support)
    .otherwise(general_support);

Priority Handling

rust
use praisonai::when;

let router = when(|input| is_vip_customer(input))
    .then(vip_agent)
    .otherwise(standard_agent);

Best Practices

Keep conditions simple

Complex conditions are hard to debug. Break into smaller checks.

Always have an otherwise

Provide a fallback for unmatched conditions.


Related