Handling step failures, fallbacks, and timeouts

Catch tool/step errors gracefully with deterministic fallback branches

In production, external APIs time out and models occasionally return malformed output. Wrap flaky lookups with explicit error handling and route failures cleanly to a safe fallback step instead of crashing the whole workflow.

Example

from praisonai import Agent, AgentFlow, route

def check_inventory_service(ctx):
    return 'fallback' if 'OUTAGE' in ctx.input else 'ok'

live = Agent(name='LiveInventory', instructions='Confirm the item is in stock in one short sentence.', output='silent')
fallback = Agent(name='FallbackResponder', instructions='Say live inventory is temporarily unavailable and offer email follow-up in one sentence.', output='silent')
flow = AgentFlow(steps=[check_inventory_service, route({'ok': [live], 'default': [fallback]})])
print('Healthy :', flow.start('Check SKU-100')['output'])
print('Fallback:', flow.start('Check SKU-OUTAGE')['output'])

Try it Yourself

Example explained

  • route({'ok': [live_agent], 'default': [fallback_agent]}) — Guarantees users receive a graceful fallback response when an upstream service fails.
  • Healthy vs Fallback — Verifies both the normal and degraded execution paths in one run.

Pair workflow fallbacks with agent-level retries

Use ExecutionConfig(max_retry_limit=2, max_execution_time=30) on individual agents for transient 429s, and route() fallbacks at the workflow level for sustained outages.

Common pitfall: swallowing exceptions silently

When routing to a fallback step, always log the underlying error reason (e.g. via ctx.variables['error']) so on-call engineers see why fallback rate spiked.

Reference: documentation