Sub topic 12
Workflows versus autonomous agents
This chapter assumes you can read Python, have seen the agent loop from the earlier chapters, and know what tool calling is. Here you will decide how much control the model should have over the order of steps.
Two ways to sequence work
A workflow is a program where your code decides the order of steps. The model may do work inside a step, such as classifying text or drafting a reply, but it never picks what happens next.
An autonomous agent is the loop you already built. The model chooses the next action on each turn, and the code only runs tools and enforces limits.
Most real systems fall between the two. You can put a fixed workflow around one open-ended step, or let an agent call a small workflow as a tool.
A workflow in code
This workflow routes a support message. The code fixes the order: classify, then branch. The model only fills in the label.
def classify(call_model, text: str) -> str:
prompt = f"Answer with one word, billing or other.\n\n{text}"
reply = call_model([{"role": "user", "content": prompt}])
return (reply.get("content") or "").strip().lower()
def handle_ticket(call_model, text: str) -> str:
label = classify(call_model, text)
if label == "billing":
return "Routed to the billing team."
return "Routed to general support."Every run follows one of two paths. You can test both paths and predict the cost of each run.
Common workflow patterns
- Prompt chaining: the output of one call becomes the input of the next.
- Routing: a classification step sends the input to a specialized branch, as above.
- Parallelization: independent calls run at the same time and the code merges the results.
- Evaluator loop: one call drafts, another critiques, and the code repeats up to a fixed count.
Choosing between them
| Question | Favors a workflow | Favors an agent |
|---|---|---|
| Are the steps known in advance? | Yes | No, they depend on what is found |
| How costly is a wrong action? | High | Low, or reversible |
| Do you need predictable cost and latency? | Yes | Variable cost is acceptable |
| How varied are the inputs? | Narrow | Wide, with many edge cases |
Start with the simplest option that works: one prompt, then a workflow, then an agent. Each step up adds flexibility but costs predictability, makes failures harder to debug, and increases the tokens spent per task.
Common mistakes
- Using an agent for a task with a fixed recipe. The model may skip a step or repeat one.
- Forcing a workflow onto a task where the needed steps differ for every input. The branches multiply and still miss cases.
- Forgetting that an agent still needs a step limit and the error handling from earlier chapters.
Exercise
A nightly job must fetch a report, summarize it with a model, and email the summary. Should this be a workflow or an agent? Give one reason.
Show answer
A workflow. The steps and their order are known in advance, so code can run them in sequence and call the model only for the summary. This makes cost and behavior predictable, and an agent would add variability without any benefit.
Exercise
A user asks a research assistant open questions, and the sources to check differ for each question. Which design fits, and which control from earlier chapters must you keep?
Show answer
An agent fits, because the next search depends on what the previous one found. Keep a step limit (max_steps) and tool error handling so the loop cannot run forever or crash on a failed tool.