Flow

Create conditional agent workflows

Agents can follow conditional flows – branch based on conditions and data.

Diagram

Conditional Flow

yes

no

🤖 Start

Condition?

Path A

Path B

🏁 End

Quick Start

  1. Simple Usage
    typescript
    import { Agent, Flow, when } from 'praisonai';
    
    const classifier = new Agent({ instructions: 'Classify requests' });
    const support = new Agent({ instructions: 'Handle support' });
    const sales = new Agent({ instructions: 'Handle sales' });
    
    const flow = new Flow()
      .start(classifier)
      .when('support', support)
      .when('sales', sales);
    
    await flow.run('I need help with billing');
    // Classifier routes to appropriate agent
  2. With Configuration
    typescript
    const flow = new Flow()
      .start(classifier)
      .when('support', support)
      .otherwise(generalAgent);

User Interaction Flow

Diagram
HandlerClassifierFlowUserHandlerClassifierFlowUserRequestClassifyCategoryRoute to handlerResponse

Configuration Levels

typescript
// Level 1: Method chain - Simple flow
const flow = new Flow()
  .start(classifier)
  .when('A', agentA)
  .when('B', agentB);

// Level 2: Array - Multiple conditions
const flow = new Flow({
  routes: [
    { condition: 'support', agent: support },
    { condition: 'sales', agent: sales }
  ]
});

// Level 3: Instance - Full control
const flow = new Flow({
  start: classifier,
  routes: [
    {
      condition: (output) => output.includes('urgent'),
      agent: urgentHandler,
      priority: 1
    }
  ],
  fallback: generalAgent
});

Flow Methods

MethodDescription
.start()Set starting agent
.when()Add conditional branch
.otherwise()Set fallback handler
.run()Execute the flow

API Reference

Workflows Module

Workflow and flow patterns

Conditions Module

Conditional routing


Best Practices

Keep flows simple

Start with 2-3 branches, add complexity as needed.

Always have a fallback

Use .otherwise() to catch unmatched cases.

Test each path

Verify all branches work before production.


Related