Skip to content
The AI Agent Factory

Chapter 27. Agent Architecture: Workflows, Agents and Subagents

Approved by
Panaversity ·
34 min read

Start here if you skipped Chapter 26

If you read Chapter 26, skip this. These are the words from it, and from the chapters before it, that this chapter builds on.

WordWhat it meansWhere this chapter uses it
ConnectorThe AP MCP server: the program through which the worker reads Brightline's vendors, invoices, purchase orders and policy. AP is accounts payable, the team that checks and pays a company's billsEverywhere
The four toolsThe connector's read tools: list_open_invoices, find_vendor, get_purchase_order and search_policy. None of them changes anythingEverywhere
Tool call, tool resultThe model's request to run one tool, and what running it returnedEverywhere
HarnessThe program around a model that runs the loop for you, offers tools and keeps the conversation. The Claude Agent SDK is one (Chapter 22)27.5, 27.7
The worker's instructionsThe operator's rules for every run, which the worker's code sends as the system prompt of every request (25.2)The point, 27.3, the lab
TraceA record of one run, made of spans. A span is one line, for one model call or one tool call (21.10)The point, 27.1, 27.6
Turn limit, budgetTwo stops that code adds to a loop: at most so many calls to the model, and at most so much money. A turn is one call to the model and its reply. A review's budget is $1.00 (21.8)Everywhere
BoundaryThe tools a run has. A run with no tool that pays cannot pay, whatever the model writes (21.8)27.1 to 27.3, 27.7
Check by codePlain code, with no model call, that tests an answer against the run's own tool results (23.16). Lab 26's check fails a review if an open invoice has no entry, if a problem invoice has no policy line, or if a policy line was never returned by search_policyEverywhere
Outer loopYour own code around a run. It starts the run, checks what came back, sends a failure back, and stops (23.16)The point, 27.5
Portal, morning triageThe portal is Brightline's own product, in which the worker runs on a hosted runtime. The morning triage is its scheduled run at 7:10 each weekday, on a budget of $0.50 (23.16)27.1, 27.5
Context windowEverything one request to the model holds, and also the most it can hold (Chapter 26)27.3, 27.6
Subagent, parentA subagent is a second agent with a context window of its own, started for one part of a job. The agent that starts it is the parent (26.5)27.3, 27.4, 27.6
vendor-lookupLab 26's subagent. Its only tool is find_vendor, and it answers with a vendor's id and termsThe point, 27.3, 27.4, the lab
Memory storeNotes a worker keeps and reads in later runs. One store belongs to one person at one company (26.6)27.6
needs_reviewThe status of a result that code would not accept. It waits for a person and is never posted as done (Lab 24)27.2, the lab
Progress fileA file outside the window where the worker writes each decision as it makes it (26.6)What you will build
SummaryText the model writes to replace the older part of a long conversation. It can drop a rule that was only in the conversation (26.4)27.6
A failure's name, and where it beganChapter 24's two questions about a wrong answer: which of six names fits it (24.7), and whether it began in the code around the model or in the model's output (24.8)27.6

The point

Wednesday, May 5, 2027, 9:15 in the morning. You are the builder. On Monday, Brightline's month-end review passed its check by code. The review answers one request from Dave Kowalski, Brightline's controller: "Tell me which open invoices are a problem, and why, with the policy line for each." It runs on a budget of $1.00.

This morning Dave forwards a question from Brightline's outside auditors. It is about one invoice that the review called "a problem". The auditors ask two things. Which steps led to that answer? And would the same invoice go through the same steps next month?

You answer the first half from Monday's trace. The worker listed the open invoices. For this invoice it asked the vendor-lookup subagent for the vendor's terms, read the purchase order, searched the policy, and wrote its decision. You send Dave the five steps, in order, with the values each one returned.

The second half has no answer. In the trace, every one of those steps is a tool call that the model chose. Chapter 26 put Dave's rule, "with the policy line for each", in the worker's instructions, so the model reads it in every request. But during the run, nothing except the model made it happen. Lab 26's check does find a skipped invoice, and it finds it only after the run. So you can show what happened on Monday. You cannot promise what will happen in June.

So you write down, for each step, who should decide it. The file is docs/architecture.md, and it holds one table with one row for each step.

  • Known before the run. Some steps never change with what the worker finds. They are listing the open invoices, taking them one at a time, and looking up a vendor and a purchase order by id. Your code takes these steps. Taking every invoice in turn becomes a loop in code.
  • Depends on what is found. Whether an invoice is a problem, and which policy line says so, cannot be known before the run. The model keeps that step. It gets search_policy as its only tool, a turn limit, and Lab 26's check on what it returns.

Monday's instructions gave all of this as one job, "is this invoice a problem?". One part of it never changes and must take the same path: the lookups. The other part changes with what is found: the judgment. The table splits them. What stays with the model is small. Inside it, the model's searches may still differ from run to run. Around it, your code holds every other step.

That design is a workflow: code you wrote holds the plan, and it calls the model for the steps that need judgment. In Lab 27 you build it. Then you build a second design. In it the model holds the plan, and it also decides when to hand part of the work to a subagent. That one is an orchestrator. You run each twice on the same invoices. Then you add one invoice that no branch of the workflow covers. A branch is one of the paths its code can take. You record every result in the same table.

Last, you read three traces of runs that went wrong. In the first, the worker makes the same search_policy call again and again, and no limit stops it. The table has a row for when the whole run stops. In that run, the row has no owner.

You have met every piece of this before. In 9.1 a manager chose "the simplest approach that works". An agent was worth its cost only when the steps depend on what it finds. In 12.6 you were the coordinator who split work across sessions. In 21.8 you added the stops yourself. In 23.16 your code started the morning triage, checked it and sent failures back. That outer loop is already a workflow around an agent. In 23.17 every pattern came with one warning: "Each one spends from the session's budget." In 26.5 you built a subagent. The one new thing is the table. Chapter 26 gave Dave's rule a home that the model keeps reading. This chapter asks whether the model should be the one that takes every invoice in turn.

So before you build anything, answer one question. For each step of Dave's review, does my code decide what happens next, or does the model? And when a run goes wrong, how do I find the step nobody decided? Start with its first step: who decided each step on Monday?

For managers

When an AI Worker does a job of several steps, something decides what happens after each step. This chapter shows a builder, the person who makes the worker, how to choose who decides, and how to find a step where nobody chose.

A step has one of three owners. Code that the builder wrote before the run does the same thing every time. The model, the AI itself, decides during the run, and may take a different path next time. The third owner is a person.

Dave Kowalski is the controller of Brightline, the invented company in this book. He asked for a month-end review of its open invoices. The review passed its check, plain code that tests the result. Then the outside auditors asked two things about one invoice. Which steps led to the answer? Would it go through the same steps next month? The record of the run answered the first. Nothing answered the second, because the model had chosen every step.

So the builder writes an architecture: a table with one row for each step of the job. Ask your builder three questions about each row, the three tests in Anthropic's exam guide.

  1. Does this step change with what the worker finds? In the exam guide, the name of this test is business requirements. Listing the open invoices does not. Deciding whether an invoice is a problem does.
  2. What does a wrong choice cost, and what would catch it? In the exam guide, the name of this test is error tolerance. Dave accepts a review that flags a fine invoice. He does not accept one that skips an invoice.
  3. Must the step take the same path every time, or is a record of the path enough? In the exam guide, the name of this test is audit constraints. Every run leaves a record. Only code can promise the same path next month.

A step known before the run, or one that must take the same path, goes to code. A step that depends on what is found goes to the model, if a wrong choice is cheap or a check catches it. If neither is true, it goes to a person.

For every step the model keeps, code still holds three things, so ask for each. The first is a stop, which is a limit on its calls to the model and on money. The second is a boundary, which is the only tools that step has. The third is a check on the result.

Under the steps, the table has four more rows, for the whole run. When does the run stop? What does a helper see, when the worker starts a second agent for part of the job? Whose saved notes may the run read? What stays in the model's context window? The window is everything one request to the model holds. Each needs an owner too. Where nobody decided one, the model or a product's default setting did. So when a worker misbehaves, ask which of the four nobody decided.

A workflow, where code holds the order of the steps, handles only what its builder planned for. A case it does not cover goes to a person, or to a step the model decides. An agent, where the model holds the order, can handle that case. It costs more, and it cannot promise the same path.

Why it matters

A worker that passes its check can still be a worker you cannot explain. A check by code reads the result. It says nothing about the path, or about next month's path. Anthropic's engineers write of their own research system: "Agents make dynamic decisions and are non-deterministic between runs, even with identical prompts."1 Non-deterministic means that the same input can give a different run.

The same gap appears when a run goes wrong. A loop that never ends, a subagent that saw too much and a note from the wrong company look like three different faults. Each one is a decision about the run that the builder never made. So choosing a design and diagnosing a fault use one table, and this chapter teaches them together.

Figure 27.1 shows Dave's review twice, with the same steps and different owners.

Figure 27.1. Monday's run and the workflow. The steps are the same. On Monday the model decided every step but the check. In the workflow it decides two.

How this chapter works

The chapter has seven concepts and one lab. Read the concepts in order.

Some of the product features for several agents are in beta, a test stage that can still change. Where a concept names one, it says so. 27.7 lists those, each with its date.

Three words are used in a narrow sense. "Workflow" has one meaning here, which 27.1 gives. "Orchestrator" is the exam guide's word, and the chapter uses no other for it. "Nobody decided" means that the builder never chose a step's owner.

Four subjects belong to other chapters. What a subagent receives is 26.5's, and is not taught again. Guardrails are Chapter 28's. Measuring what each design costs is Chapter 29's. A workflow that must survive a crash or wait for days is Chapter 32's.

After this chapter you can:

  • Write the table of steps for a job, and choose each step's owner by the guide's three tests.
  • Build the steps that code holds as a chain, a route, steps side by side, or a check with a stop.
  • Say what an orchestrator hands to the model, and which limits stay in code.
  • Check both ends of a subagent's work: the brief going in and the answer coming back.
  • Choose where the loop runs by which decisions your code must hold.
  • Read a trace of a run that went wrong, find the row nobody decided, and choose the pattern that corrects it.

What you will build

Who's who. You are the builder. Dave Kowalski is Brightline's controller. He owns AP policy, and he asks for the month-end review. Ana Morales is Brightline's AP analyst. Brightline Wholesale Supply is the invented wholesale distributor whose AP worker you have built since Chapter 16.

Where you are. At the end of Lab 26 the review passes its check by code, in src/hosted/check-triage.ts. The worker writes each decision to a progress file. It asks the vendor-lookup subagent for a vendor's terms. In all of it, the model picks every next step, and no file says who should.

What you build instead, in Lab 27, in three sittings, from tag ch26 to tag ch27:

  1. The table, then both designs. You fill docs/architecture.md, then build the workflow and the orchestrator.
  2. A comparison you can defend. You run each build twice on the same invoices, and write down your choice and its price.
  3. Three diagnoses. You name the fault in each of three broken traces, and its row, before you read any code.
  4. The port. You rebuild the orchestrator on the OpenAI Agents SDK.

As in Chapters 17 to 26, coding agents write every file and run every command. You specify, check and approve.

In this chapter

SectionThe question it answers
27.1 Workflow or agentFor this step, does code decide, or the model, or a person?
27.2 Workflow patternsIn which four ways can code hold the plan, and what can each not plan for?
27.3 An orchestrator and its subagentsWhat does an orchestrator hand to the model, and which limits stay in code?
27.4 What a subagent gets, and what it hands backWhat can code check going in, and coming back?
27.5 Where it runsIn each place a loop can run, which decisions can your code hold?
27.6 Diagnosing a misbehaving agentWhich row did nobody decide, or decide wrongly?
27.7 How today's tools realize itOn each product, who decides the next step, and what are its default limits?
Lab 27The table first, then a workflow and an orchestrator, a comparison, three diagnoses and a port, from tag ch26 to tag ch27
Check yourselfOne open question for each concept, with an outline of its answer
  1. How we built our multi-agent research system, Anthropic.↑
Owner
Panaversity