Skip to main content
AgentFlow provides advanced workflow patterns for complex agent orchestration.

Quick Start

1

Simple Flow

use praisonai::{Agent, AgentFlow};

let flow = AgentFlow::new()
    .agent(Agent::simple("Process the input")?)
    .agent(Agent::simple("Enhance the output")?);

let result = flow.run("Hello world").await?;
2

With Patterns

use praisonai::{AgentFlow, FlowStep, Route, Parallel};
use std::sync::Arc;

let agent = Arc::new(Agent::simple("Handle this")?);

let flow = AgentFlow::new()
    .step(FlowStep::Route(Route {
        condition: Box::new(|input| input.contains("urgent")),
        if_true: Arc::clone(&agent),
        if_false: None,
    }));

Flow Patterns

Route

Conditional branching based on input:
use praisonai::{AgentFlow, FlowStep, Route};
use std::sync::Arc;

let urgent_handler = Arc::new(Agent::simple("Handle urgent requests")?);
let normal_handler = Arc::new(Agent::simple("Handle normal requests")?);

let flow = AgentFlow::new()
    .step(FlowStep::Route(Route {
        condition: Box::new(|input| input.contains("urgent")),
        if_true: urgent_handler,
        if_false: Some(normal_handler),
    }));

let result = flow.run("This is urgent!").await?;

Parallel

Execute multiple agents simultaneously:
use praisonai::{AgentFlow, FlowStep, Parallel};
use std::sync::Arc;

let analyzer1 = Arc::new(Agent::simple("Analyze sentiment")?);
let analyzer2 = Arc::new(Agent::simple("Extract keywords")?);
let analyzer3 = Arc::new(Agent::simple("Summarize content")?);

let flow = AgentFlow::new()
    .step(FlowStep::Parallel(Parallel {
        agents: vec![analyzer1, analyzer2, analyzer3],
    }));

let result = flow.run("Analyze this text").await?;

Loop

Iterate over a collection:
use praisonai::{AgentFlow, FlowStep, Loop};
use std::sync::Arc;

let processor = Arc::new(Agent::simple("Process this item")?);

let flow = AgentFlow::new()
    .step(FlowStep::Loop(Loop {
        agent: processor,
        items: vec!["Item 1".into(), "Item 2".into(), "Item 3".into()],
    }));

let result = flow.run("Process all items").await?;

Repeat

Execute the same agent multiple times:
use praisonai::{AgentFlow, FlowStep, Repeat};
use std::sync::Arc;

let refiner = Arc::new(Agent::simple("Improve and refine this text")?);

let flow = AgentFlow::new()
    .step(FlowStep::Repeat(Repeat {
        agent: refiner,
        times: 3,  // Refine 3 times
    }));

let result = flow.run("Initial draft").await?;

Combining Patterns

Chain multiple patterns:
let flow = AgentFlow::new()
    .agent(Agent::simple("Preprocess")?)           // Simple step
    .step(FlowStep::Parallel(Parallel { ... }))    // Parallel analysis
    .step(FlowStep::Route(Route { ... }))          // Conditional
    .agent(Agent::simple("Finalize")?);            // Final step

let result = flow.run("Input").await?;

FlowStep Types

TypePurposeWhen to Use
AgentSingle agent stepSimple processing
RouteConditional branchDifferent paths based on input
ParallelConcurrent executionIndependent analyses
LoopIterate over itemsProcess collections
RepeatMultiple passesIterative refinement

Best Practices

Begin with linear flows, add patterns as needed.
Wrap agents in Arc when they’re used in multiple steps.
Verify each pattern works before combining.