Sub topic 12
Workflows versus autonomous agents
This chapter assumes you have read the earlier chapters on the agent loop, tool calling, and error handling. You should be able to read the run_agent loop and explain why it has a step limit.
A model-driven system does not have to be an agent. Many useful systems fix the sequence of steps in code and use the model only inside each step. This chapter covers where the line falls, which workflow patterns are worth knowing, and how to choose between a workflow and an agent.
The core difference: who decides the next step
In a workflow, your code decides the path. The model fills in steps, such as classifying text or drafting a reply. The control flow is written by you and is the same on every run.
In an agent, the model decides the path. At each turn it chooses which tool to call, whether to call another, or whether to stop. The control flow differs from run to run.
| Property | Workflow | Autonomous agent |
|---|---|---|
| Next step chosen by | Your code | The model |
| Number of model calls | Known in advance | Varies, bounded by a step limit |
| Cost and latency | Predictable | Variable, with a long tail |
| Testing | Test each step separately | Test whole runs and trajectories |
| Failure mode | A step gives a bad output | The model picks a wrong path or loops |
| Best for | Well-understood tasks | Open-ended tasks with unknown steps |
The two are ends of a spectrum. A workflow that lets the model pick one of three branches already hands over some control. An agent with a tight tool list and a low max_steps has little freedom.
Common workflow patterns
These patterns cover most cases where a fixed structure is enough.
- Prompt chaining. Step A's output becomes step B's input. For example, extract facts from a document, then write a summary from those facts. You can validate the output between steps and stop early if a check fails.
- Routing. A first model call classifies the input, and your code sends it to a specialized handler. Each handler gets its own prompt and tools.
- Parallelization. Independent subtasks run at the same time, and your code merges the results. One variant runs the same prompt several times and takes a vote.
- Orchestrator and workers. A model splits a task into subtasks at run time, and workers handle each one. This is the most agent-like workflow, because the model decides how many subtasks there are.
- Evaluator and optimizer. One call produces a draft and another critiques it. Your code repeats the cycle until the critique passes or a round limit is reached.
A routing workflow in code
The classify function below stands in for a model call that returns one label. The routing logic around it is ordinary code.
def classify(ticket: str) -> str:
# Stand-in for a model call that must return exactly one label.
text = ticket.lower()
if "refund" in text or "invoice" in text:
return "billing"
if "error" in text or "crash" in text:
return "technical"
return "other"
HANDLERS = {
"billing": lambda t: f"Billing reply drafted for: {t}",
"technical": lambda t: f"Technical reply drafted for: {t}",
"other": lambda t: f"General reply drafted for: {t}",
}
def run_workflow(ticket: str) -> str:
label = classify(ticket)
# An unexpected label falls back to the general handler instead of crashing.
handler = HANDLERS.get(label, HANDLERS["other"])
return handler(ticket)
print(run_workflow("The app shows an error on startup"))
print(run_workflow("I need a refund for last month"))Notice what the workflow gives you. The number of steps is fixed at two. Each handler can be tested alone. If routing is wrong, the bug is in classify, and you can measure it with a labeled set of tickets. The HANDLERS.get fallback shows the main defensive habit: treat the model's output as untrusted input, even when it only selects a branch.
What the agent adds, and what it costs
An agent earns its extra cost when you cannot list the steps in advance. Examples are debugging a failing test suite, researching a question across unknown sources, or operating a user interface. The number and order of actions depend on what each tool returns.
The costs are concrete:
- Variable cost. Every turn resends the growing history, so a run of ten steps costs far more than ten times a single call.
- Compounding errors. If each step succeeds 95% of the time, a ten-step run succeeds about 60% of the time (0.95 to the tenth power).
- Harder debugging. The same input can follow different paths, so you need traces to see what happened.
- Larger risk surface. The model can reach every tool you give it, in any order, so permissions matter more.
Decision criteria
Ask these questions in order:
- Can a single well-prompted call do it? If yes, stop there. Add retrieval or examples before adding structure.
- Can you draw the steps in advance? If yes, use a workflow. Choose chaining for sequences, routing for distinct input types, and parallelization for independent subtasks.
- Do the steps depend on intermediate results you cannot predict? If yes, an agent is justified.
- Is the cost of a wrong action tolerable? If an action cannot be undone, such as sending money or deleting data, add guardrails and human approval no matter which design you pick.
- Can you measure success? Without test tasks, you cannot tell whether extra autonomy helped.
Hybrids are the usual answer
Most production systems mix the two. A workflow handles the predictable outer structure, and one step runs a small agent with a narrow tool list and a low step limit. For example, a routing workflow can send "technical" tickets to an agent that may call a log search tool up to five times, while "billing" tickets use a fixed chain.
This gives you autonomy only where it pays off. It also limits the damage of a bad run, because the agent's step limit and tool list apply to one branch and not to the whole system.
Common mistakes
- Using an agent for a fixed pipeline. If every run calls the same three tools in the same order, you have a workflow with extra cost and extra failure modes.
- Unbounded orchestrators. A model that creates subtasks at run time needs a cap on the number of subtasks and on the depth of nesting.
- No fallback on routing. A classifier can return a label you did not expect. Always handle that case.
- Evaluating only the final answer. For agents, also check the trajectory: which tools were called, how many steps were taken, and whether any step was wasted.
Exercise
A support system receives emails that fall into five known categories, each with its own response template and lookup tool. A teammate proposes a fully autonomous agent with all tools available. Which design would you choose, and what is the main reason?
Show answer
Use a routing workflow. The categories and the steps for each are known in advance, so code can fix the control flow. A classifier call picks the category, and a category-specific handler runs with only its own tool. This gives predictable cost, lets you test the classifier and each handler separately, and limits the tools the model can reach. If one category later turns out to need open-ended investigation, replace that handler with a bounded agent and keep the rest as a workflow.
Exercise
A ten-step agent run succeeds at each step 90% of the time, and steps fail independently. Roughly what fraction of runs complete with no failed step, and what does this suggest about design?
Show answer
About 35% (0.9 to the tenth power is roughly 0.349). Errors compound over long runs, so keep runs short, narrow the tool list, validate between steps, and use workflow structure for any part of the task whose steps you already know.