Conditional when and repeat
Branch conditionally with when
In addition to route, parallel, and loop, AgentFlow exports when and repeat directly from praisonai. Use when(condition, [step]) to run one or more steps only when a condition matches.
Expected output: AgentFlow runs escalator when the checker outputs URGENT and prints the resulting incident alert.
Example
from praisonai import Agent, AgentFlow, when
checker = Agent(instructions='Reply with the single word URGENT if the note mentions outage, else OK.')
escalator = Agent(instructions='Write a one-line incident alert.')
flow = AgentFlow(
steps=[
checker,
when('URGENT', [escalator]),
]
)
print(flow.start('Database outage on checkout API'))Example explained
- from praisonai import Agent, AgentFlow, when — Imports the top-level AgentFlow conditional helper.
- when('URGENT', [escalator]) — Runs the list of steps only when the previous output matches URGENT.
Refine a draft with repeat
Use repeat(agent, max_iterations=2) when you want an agent to iterate on its own work up to a fixed number of passes — such as drafting and then tightening a paragraph.
Expected output: the editor runs twice in sequence and returns the polished sentence.
Example
from praisonai import Agent, AgentFlow, repeat
editor = Agent(instructions='Shorten and sharpen the text to under 12 words.')
flow = AgentFlow(steps=[repeat(editor, max_iterations=2)])
print(flow.start('PraisonAI makes it very easy and straightforward to build AI agents quickly.'))Example explained
- repeat(editor, max_iterations=2) — Runs the same step up to 2 times, feeding pass 1 into pass 2.
Reference: documentation