CONTENTS
Workflows and agents
"Agent" is the most overloaded word in the field. Some people use it for any LLM app; others reserve it for systems that run for hours on their own. Before you can design one, you need a definition precise enough to make decisions with.
Workflows versus agents
Anthropic's "Building effective agents" (December 2024) draws the line this book uses:
- Workflows are systems where LLMs and tools are orchestrated through predefined code paths. Your code decides the sequence of steps.
- Agents are systems where LLMs dynamically direct their own processes and tool usage. The model decides the sequence of steps.
That's the decision that matters: who controls the flow, your code or the model? Workflows are predictable, cheap and easy to test, and they only handle the paths you foresaw. Agents handle open-ended problems and paths you didn't foresee, at the cost of predictability, tokens and latency.
The building block of both is what the post calls the augmented LLM: a model with retrieval, tools and memory available.
The workflow patterns
Anthropic names five workflow patterns. They're worth memorizing; most production systems are combinations of them.
Prompt chaining. Break a task into fixed sequential steps, each a model call, with optional code checks ("gates") between them. Example: write an outline, check it meets the criteria, then write the document from it. Use it when a task splits cleanly into steps, and you'll trade a little latency for higher accuracy per step.
Routing. Classify the input, then send it to a specialized prompt, tool set or model. Example: send refund requests, technical questions and general questions to different handlers; send easy questions to a small model and hard ones to a large one. Routing lets each path be optimized without the others interfering.
Parallelization. Run model calls at the same time and combine them in code. Two variants: sectioning (split a task into independent parts, like checking an answer for policy violations in one call while generating it in another) and voting (run the same task several times and aggregate, like three independent reviews of a code change).
Orchestrator-workers. A central model breaks the task into subtasks dynamically, hands them to worker calls, and synthesizes the results. Unlike sectioning, the subtasks aren't known in advance. Example: a coding change that touches an unknown number of files.
Evaluator-optimizer. One call generates; another evaluates against criteria and gives feedback; repeat until it passes. Use it when you have clear evaluation criteria and iteration measurably helps, as with translation or writing to a style guide.
chaining in ─► A ─► gate ─► B ─► C ─► out
routing in ─► classify ─┬─► handler 1
└─► handler 2
parallel in ─┬─► A ─┐
└─► B ─┴─► combine ─► out
orch.-workers in ─► orchestrator ─┬─► worker ─┐
├─► worker ─┼─► synthesize ─► out
└─► worker ─┘
eval.-optimizer in ─► generate ⇄ evaluate ─► out
The agent loop
An agent is simpler to write than its reputation suggests. Strip away frameworks and it's a loop:
def run_agent(goal, tools, max_steps=30):
context = [system_prompt(tools), user_message(goal)]
for step in range(max_steps):
reply = model.call(context)
context.append(reply)
if not reply.tool_calls:
return reply.text
for call in reply.tool_calls:
result = execute(call)
context.append(tool_result(call.id, result))
return escalate("step limit reached", context)
This is the ReAct pattern (Yao et al., 2022) in modern form: the model alternates between reasoning about what to do and acting through tools, and each tool result informs the next round of reasoning. In the original paper, interleaving reasoning with actions beat imitation and reinforcement learning baselines by 34 percentage points on the ALFWorld benchmark, and grounding answers in Wikipedia lookups reduced hallucination.
Everything interesting in agent design is in the details that loop glosses over: what execute is allowed to do, what goes into context and how it's trimmed, what happens when a tool fails, and when the loop should stop and ask a human.
When to use an agent
Anthropic's guidance: agents fit open-ended problems where you can't predict the number of steps or hard-code a path, and where you can trust the model's decisions within the environment. Their two showcase domains are customer support (conversation plus actions, with clear success criteria like a resolved ticket) and coding (where automated tests verify the work).
Notice what those have in common: feedback from the environment. A coding agent runs the tests and sees them fail. A support agent sees whether the refund API succeeded. Agents work when each step produces ground truth they can react to. Agents struggle when they can't tell whether they're making progress.
A decision checklist:
| Question | Points toward workflow | Points toward agent |
|---|---|---|
| Can you list the steps in advance? | Yes | No, it depends on what's found |
| Does the environment give feedback per step? | Doesn't matter | Required |
| Cost of a wrong action | High | Low or reversible |
| Latency budget | Tight | Generous |
| Volume | Very high | Moderate |
Many good systems are workflows with one agentic step inside: a fixed pipeline where one stage ("investigate the bug") is a bounded agent loop.
Agents are software: 12 factors
Dex Horthy's "12-Factor Agents", named after the classic Twelve-Factor App methodology, came from his observation that most production agents that work aren't built on heavy frameworks. They're mostly ordinary software with LLM steps placed carefully. The twelve factors:
- Natural language to tool calls.
- Own your prompts.
- Own your context window.
- Tools are just structured outputs.
- Unify execution state and business state.
- Launch/pause/resume with simple APIs.
- Contact humans with tool calls.
- Own your control flow.
- Compact errors into the context window.
- Small, focused agents.
- Trigger from anywhere, meet users where they are.
- Make your agent a stateless reducer.
The common thread is ownership. Frameworks are convenient until something breaks, and then you need to see and change the exact prompt, the exact context and the exact control flow. Anthropic's post says the same: start with the model APIs directly, because many patterns are a few lines of code, and frameworks can hide the prompts and responses you need to debug.
Factor 12 deserves a closer look. A reducer is a function that takes the current state and an event and returns the next state. If your agent is next_state = step(state, event), with the state being the context plus business data, saved after every step, then pausing, resuming, retrying and debugging all become easy. You can replay any run. You can fork from step 14 to test a fix. You can resume after a crash. Chapter 8 builds on this.
Stopping conditions and escalation
Every agent loop needs explicit answers to "when does it stop?":
- Success: the model says it's done, and a check confirms it (tests pass, the record exists).
- Step or budget limit: a maximum number of steps, tokens, dollars or seconds.
- Repetition: the same tool call with the same arguments three times means it's stuck.
- Escalation: the model asks for help, or a policy requires a human (Factor 7 makes "ask a human" just another tool).
Hitting a limit should hand off gracefully, with the context so far, not just fail.
Key takeaways
- Workflows: your code controls the flow. Agents: the model does. Choose deliberately.
- Learn the five workflow patterns: chaining, routing, parallelization, orchestrator-workers, evaluator-optimizer.
- An agent is a loop of model calls and tool executions. The design lives in what tools may do, how context is managed, and when the loop stops.
- Agents work best where the environment gives clear feedback each step.
- Own your prompts, context and control flow. Treat the agent as a stateless reducer over saved state.
Exercises
- Pick a task you'd be tempted to build as an agent. Design it as a workflow instead. What would the workflow fail to handle?
- Implement the agent loop above against any model API with two tools. Add the four stopping conditions.
- For each of the five workflow patterns, name a product feature you've used that probably works that way.