Shared state with WorkflowContext and variables
Read input, previous_result, and shared variables across steps
As workflows grow past 2 steps, later agents need both the original user request and intermediate facts discovered earlier. Every function step in AgentFlow receives ctx (WorkflowContext) exposing ctx.input, ctx.previous_result, and ctx.variables.
Example
from praisonai import Agent, AgentFlow
def enrich_ticket(ctx):
ctx.variables['account_tier'] = 'Enterprise'
return f"{ctx.input} [tier={ctx.variables['account_tier']}]"
def attach_footer(ctx):
return f"{ctx.previous_result} (SLA={ctx.variables.get('account_tier', 'Standard')})"
responder = Agent(name='Responder', instructions='Acknowledge the ticket in one short sentence.', output='silent')
flow = AgentFlow(steps=[enrich_ticket, responder, attach_footer])
print(flow.start('Checkout API returns 504')['output'])Example explained
- ctx.variables['account_tier'] — Stores typed state in Step 1 that any downstream step can read reliably.
- ctx.previous_result — Reads the exact output produced by the immediately preceding step.
Inspect every step's output in result['steps']
flow.start(…) returns a dictionary containing both result['output'] (final reply) and result['steps'] (every intermediate step name and output) — essential for debugging and telemetry.
Common pitfall: mutating global dicts instead of ctx.variables
Global variables leak between concurrent users in web servers. Always store per-run state on ctx.variables so every workflow run stays isolated.
Reference: documentation