Router with CEL expression: route from session_state
Route using session_state.preferred_handler and change the configured default for subsequent runs.
"""Router with CEL expression: route from session_state.
=====================================================
Uses session_state.preferred_handler to persist routing preferences
across workflow runs.
Requirements:
pip install cel-python
"""
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.workflow import CEL_AVAILABLE, Step, Workflow
from agno.workflow.router import Router
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
if not CEL_AVAILABLE:
print("CEL is not available. Install with: pip install cel-python")
exit(1)
# ---------------------------------------------------------------------------
# Create Agents
# ---------------------------------------------------------------------------
detailed_agent = Agent(
name="Detailed Analyst",
model=OpenAIChat(id="gpt-5.6-luna"),
instructions="You provide detailed, in-depth analysis with examples and data.",
markdown=True,
)
brief_agent = Agent(
name="Brief Analyst",
model=OpenAIChat(id="gpt-5.6-luna"),
instructions="You provide brief, executive-summary style analysis. Keep it short.",
markdown=True,
)
# ---------------------------------------------------------------------------
# Create Workflow
# ---------------------------------------------------------------------------
workflow = Workflow(
name="CEL Session State Router",
steps=[
Router(
name="Analysis Style Router",
selector="session_state.preferred_handler",
choices=[
Step(name="Detailed Analyst", agent=detailed_agent),
Step(name="Brief Analyst", agent=brief_agent),
],
),
],
session_state={"preferred_handler": "Brief Analyst"},
)
# ---------------------------------------------------------------------------
# Run Workflow
# ---------------------------------------------------------------------------
if __name__ == "__main__":
print("--- Using session_state preference: Brief Analyst ---")
workflow.print_response(input="Analyze the current state of cloud computing.")
print()
# Change preference
workflow.session_state["preferred_handler"] = "Detailed Analyst"
print("--- Changed preference to: Detailed Analyst ---")
workflow.print_response(input="Analyze the current state of cloud computing.")Understand the state lifetime
This example mutates workflow.session_state, which supplies defaults for subsequent fresh sessions. It demonstrates changing configuration on one workflow object; it does not persist a user’s preference. To remember preferences per session, use a database and a stable session ID, then update that session’s state. Adding a database alone does not turn changes to the defaults dictionary into updates to an existing stored session.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno cel-python fastapi openaiExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as cel_session_state_route.py, then run:
python cel_session_state_route.pyFull source: cookbook/04_workflows/07_cel_expressions/router/cel_session_state_route.py