Conditional workflows

Agent workflows rarely follow a single straight path. As an agent runs, it needs to make choices based on what it knows and what just happened. Conditional branching is how a workflow adapts, choosing one path over another while staying deterministic and understandable.

Making decisions between alternative workflow paths

A workflow often reaches a point where more than one next step is possible. The choice depends on current state or recent results. This is where decision logic turns into control flow.

Instead of a fixed sequence, the workflow evaluates a condition and selects one of several possible paths. The result is still predictable, but no longer linear.

if state["has_data"]:
    next_step = "generate_page"
else:
    next_step = "fetch_data"

Branching based on agent state or tool results

Branches are usually driven by two things: agent state and tool outcomes. State reflects what the agent already knows, while tool results reflect what just happened.

By checking these values explicitly, the workflow can move in a direction that makes sense for the current situation.

result = fetch_planet_data()

if result["status"] == "ok":
    state["planets"] = result["data"]
else:
    state["error"] = result["error"]

Implementing conditional steps in a workflow

In code, conditional steps are implemented using ordinary control structures. The workflow itself becomes a small decision tree expressed with if and else.

Each branch represents a valid continuation of the workflow, not an exception or special case.

def run_step(state):
    if "planets" in state:
        build_html_pages(state)
    else:
        load_planet_data(state)

Skipping or selecting steps dynamically

Not every workflow step needs to run every time. Conditional branching allows steps to be skipped entirely when they are not needed.

This keeps workflows efficient and focused, without adding special flags or duplicated logic.

if state.get("needs_index"):
    generate_index_page(state)

Keeping workflows readable and predictable

As branching increases, readability becomes critical. Clear conditions, small steps, and explicit names help keep the workflow understandable.

The goal is that someone reading the code can see all possible paths without mentally simulating complex logic.

if state["ready"]:
    finalize_site(state)
else:
    prepare_site(state)

Conclusion

Conditional branching allows agent workflows to adapt without becoming chaotic. By making decisions explicit and paths easy to follow, we gain flexibility while keeping behavior predictable. At this point, we are oriented to how workflows choose their direction and remain under clear program control.