Condition with CEL expression: branching on session_state

Uses session_state.retry_count to implement retry logic.

Use session_state.retry_count to branch on retry attempts. The separate runner below supplies storage so repeated calls reach the maximum-retries branch.

cel_session_state.py
"""Condition with CEL expression: branching on session_state.
==========================================================

Uses session_state.retry_count to implement retry logic.
Runs the workflow multiple times to show the counter incrementing
and eventually hitting the max retries branch.

Requirements:
    pip install cel-python
"""

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.run import RunContext
from agno.workflow import (
    CEL_AVAILABLE,
    Condition,
    Step,
    StepInput,
    StepOutput,
    Workflow,
)

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
if not CEL_AVAILABLE:
    print("CEL is not available. Install with: pip install cel-python")
    exit(1)


# ---------------------------------------------------------------------------
# Define Helpers
# ---------------------------------------------------------------------------
def increment_retry_count(step_input: StepInput, run_context: RunContext) -> StepOutput:
    """Increment retry count in session state."""
    current_count = run_context.session_state.get("retry_count", 0)
    run_context.session_state["retry_count"] = current_count + 1
    return StepOutput(
        content=f"Retry count incremented to {run_context.session_state['retry_count']}",
        success=True,
    )


def reset_retry_count(step_input: StepInput, run_context: RunContext) -> StepOutput:
    """Reset retry count in session state."""
    run_context.session_state["retry_count"] = 0
    return StepOutput(content="Retry count reset to 0", success=True)


# ---------------------------------------------------------------------------
# Create Agents
# ---------------------------------------------------------------------------
retry_agent = Agent(
    name="Retry Handler",
    model=OpenAIChat(id="gpt-5.6-luna"),
    instructions="You are handling a retry attempt. Acknowledge this is a retry and try a different approach.",
    markdown=True,
)

max_retries_agent = Agent(
    name="Max Retries Handler",
    model=OpenAIChat(id="gpt-5.6-luna"),
    instructions="Maximum retries reached. Provide a helpful fallback response and suggest alternatives.",
    markdown=True,
)

# ---------------------------------------------------------------------------
# Create Workflow
# ---------------------------------------------------------------------------
workflow = Workflow(
    name="CEL Retry Logic",
    steps=[
        Step(name="Increment Retry", executor=increment_retry_count),
        Condition(
            name="Retry Check",
            evaluator="session_state.retry_count <= 3",
            steps=[
                Step(name="Attempt Retry", agent=retry_agent),
            ],
            else_steps=[
                Step(name="Max Retries Reached", agent=max_retries_agent),
                Step(name="Reset Counter", executor=reset_retry_count),
            ],
        ),
    ],
    session_state={"retry_count": 0},
)

# ---------------------------------------------------------------------------
# Run Workflow
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    for attempt in range(1, 6):
        print(f"--- Attempt {attempt} ---")
        workflow.print_response(
            input=f"Process request (attempt {attempt})",
            stream=True,
        )
        print()

Retain the retry counter

The source needs storage to carry its counter across calls. Use this separate runner with one database and a stable session ID:

run_cel_session_state.py
from agno.db.in_memory import InMemoryDb
from cel_session_state import workflow

workflow.db = InMemoryDb()
for attempt in range(1, 6):
    workflow.print_response(
        input=f"Process request (attempt {attempt})",
        session_id="cel-retry-demo",
        stream=True,
    )

The incremented counts are 1, 2, 3, 4, then 1 after the fallback resets the counter. Without a database, each call starts again from the configured default. Use persistent storage to retain the counter across process restarts.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno cel-python fastapi openai

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run the example

Save the source as cel_session_state.py and the separate runner above, then run:

python run_cel_session_state.py

Full source: cookbook/04_workflows/07_cel_expressions/condition/cel_session_state.py