# Chapter 27. Agent Architecture: Workflows, Agents and Subagents (/building-ai-workers/agent-architecture/overview)

---
type: Document
title: 'Chapter 27. Agent Architecture: Workflows, Agents and Subagents'
description: For each step of Brightline's month-end review, decide who chooses what happens next, your code before the run or the model during it, by the job's business needs, error tolerance and audit needs. Build the review as a workflow and as an orchestrator with subagents, compare them, and read a broken trace for the step nobody decided. Built on Claude first, then ported to OpenAI.
status: stable
order: 327
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T08:16:32Z
chapter: '27'
part: III
expert_status: manager
objectives:
  - id: CCDV-F.D1.S1
    label: primary
  - id: CCDV-F.D1.S3
    label: supporting
word_budget: 3500
prerequisites:
  - '01'
  - '02'
  - '03'
  - '04'
  - '05'
  - '06'
  - '07'
  - 8
  - 9
  - '10'
  - '11'
  - '12'
  - '13'
  - '14'
  - '15'
  - '16'
  - '17'
  - '18'
  - '19'
  - '21'
  - '22'
  - '23'
  - '24'
  - '25'
  - '26'
build_step: Brightline's month-end review built twice, as a workflow your code runs and as an orchestrator with subagents, run on the same invoices, compared against the audit's needs, then three broken traces diagnosed and a port
artifact: brightline-ap with docs/architecture.md (the table of steps), a workflow build, an orchestrator build with subagents, tag ch27
lab_data: https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch27.zip
delta_entries:
  - D-013
  - D-014
  - D-022
concepts:
  - '27.1'
  - '27.2'
  - '27.3'
  - '27.4'
  - '27.5'
  - '27.6'
  - '27.7'
last_verified: '2026-11-30'
sources:
  - id: anthropic-multi-agent-research
    title: How we built our multi-agent research system, Anthropic (verified 2026-11-30)
    resource: https://www.anthropic.com/engineering/multi-agent-research-system
generated:
  at: 2026-11-30T08:16:32Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

<!-- EDITOR: which objective ids does the front matter carry? The spec makes this chapter the main one for exam objectives 1.1 and 1.3, and supporting for 7.2 (spec, raised point 12). This draft labels CCDV-F.D1.S3 "supporting", because Chapter 23's overview still holds it as primary and the plan allows one primary chapter for each objective, and it lists no id for 7.2. The other option: S3 primary here, Chapter 23's changed to supporting in the same pull request, and an id for 7.2 added as supporting. -->

## Start here if you skipped Chapter 26

*If you read [Chapter 26](../context-engineering/overview.md), skip this. These are the words from it, and from the chapters before it, that this chapter builds on.*

| Word | What it means | Where this chapter uses it |
| --- | --- | --- |
| **Connector** | The 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 bills | Everywhere |
| **The four tools** | The connector's read tools: `list_open_invoices`, `find_vendor`, `get_purchase_order` and `search_policy`. None of them changes anything | Everywhere |
| **Tool call, tool result** | The model's request to run one tool, and what running it returned | Everywhere |
| **Harness** | The 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 instructions** | The operator's rules for every run, which the worker's code sends as the system prompt of every request ([25.2](../prompts-and-structured-output/system-or-user.md)) | The point, 27.3, the lab |
| **Trace** | A record of one run, made of **spans**. A span is one line, for one model call or one tool call ([21.10](../agent-loop-by-hand/reading-a-trace.md)) | The point, 27.1, 27.6 |
| **Turn limit, budget** | Two 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](../agent-loop-by-hand/the-stops-you-add-yourself.md)) | Everywhere |
| **Boundary** | The 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 code** | Plain code, with no model call, that tests an answer against the run's own tool results ([23.16](../hosted-agents/loops-check-feedback-and-stop.md)). 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_policy` | Everywhere |
| **Outer loop** | Your 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 triage** | The 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 window** | Everything one request to the model holds, and also the most it can hold (Chapter 26) | 27.3, 27.6 |
| **Subagent, parent** | A 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](../context-engineering/isolation-with-subagents.md)) | 27.3, 27.4, 27.6 |
| **`vendor-lookup`** | Lab 26's subagent. Its only tool is `find_vendor`, and it answers with a vendor's id and terms | The point, 27.3, 27.4, the lab |
| **Memory store** | Notes a worker keeps and reads in later runs. One store belongs to one person at one company ([26.6](../context-engineering/memory-outside-the-window.md)) | 27.6 |
| **`needs_review`** | The status of a result that code would not accept. It waits for a person and is never posted as done ([Lab 24](../evaluating-ai-workers/lab.md)) | 27.2, the lab |
| **Progress file** | A file outside the window where the worker writes each decision as it makes it (26.6) | What you will build |
| **Summary** | Text 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](../context-engineering/compaction.md)) | 27.6 |
| **A failure's name, and where it began** | Chapter 24's two questions about a wrong answer: which of six names fits it ([24.7](../evaluating-ai-workers/naming-the-failure.md)), and whether it began in the code around the model or in the model's output ([24.8](../evaluating-ai-workers/integration-layer-or-model-output.md)) | 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.

<!-- EDITOR: the day and the clock, Wednesday, May 5, 2027, 9:15 a.m., are proposed in the chapter's spec (raised point 3a) and are not yet in specs/labs/part-2-facts.md. The other option is any other day after Monday, May 3, 2027, with the lab changed to match. -->

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?

<!-- OPEN: which invoice the auditors ask about. The spec's worked run leaves it blank until the runs are recorded, so this page names none. -->

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.

<!-- EDITOR: what the words "for each" mean on this page. In Chapter 26 they are the end of Dave's rule, "with the policy line for each". The spec's worked run also uses them for taking every invoice in turn ("for each" becomes a loop). This draft keeps the words for Dave's rule only, and writes "taking every invoice in turn" and "every open invoice" for the loop, here, at the end of "You have met every piece of this before" and in Task 1's "What good looks like". The other option: use the spec's words in both senses, and say where the page first uses them that they also mean "every invoice, one at a time". -->

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.

<!-- OPEN: recorded runs for the worked run in "The point". Whether the workflow passes the check twice by the same steps in the same order, what each build does with the extra invoice, and what each run costs are not fixed until the runs are recorded (spec, "The worked run"). Which vendor the extra invoice comes from, and what is unusual about it, is the spec's raised point 3d. This page states none of them. -->

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](../../managing-ai-workers/work-that-runs-without-you/fit-the-workflow.md) 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](../agentic-coding/many-sessions.md) 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](../hosted-agents/patterns-and-when-to-host-it-yourself.md) 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?

<!-- EDITOR: the chapter's question, printed in bold above, is the agent's recommendation and is not yet confirmed by the editor (spec, raised point 1). The other four candidates are in `.work/chapter-spec/agent-architecture/questions.md`. If he chooses another, each section's job changes with it. -->

## 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."[^anthropic-multi-agent-research] 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: "One review, drawn twice: who decided each step." Two tables side by side with the same eight rows, top to bottom: (1) list the open invoices, (2) take the next invoice, (3) look up the vendor, (4) look up the purchase order, (5) find the policy line, (6) decide, (7) write the decision, (8) check. LEFT table, headed "Monday's run": rows 1 to 7 are each marked "the model decided". Row 8, "check", is marked "code decided". RIGHT table, headed "the workflow": rows 1, 2, 3, 4 and 7 are marked "code decided". Rows 5 and 6, "find the policy line" and "decide", are marked "the model decided". Row 8, "check", is marked "code decided". So the check is code in both tables. The two marks use two colors and two shapes, so the figure reads without color. A note under the LEFT table: "the same path next month? Nothing promises it." No arrows. The four rows for the whole run are not drawn here. The alt text must give every row's owner in both tables, say that the check is code in both, and give the note under the left table. -->

*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](lab.md), 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.

<!-- EDITOR: which runtime runs each build? This draft follows the spec's proposal (raised point 8): the workflow is the reader's own code calling the Messages API once for each invoice, the orchestrator runs on the Claude Agent SDK in `src/sdk/`, and the hosted worker gets one reading step and no build. 27.5 ("What can you not take back into code on a hosted runtime?") and Task 1 say so. If he decides otherwise, both change. Also open there: with whose access the workflow's code calls the connector, and what holds its $1.00 budget. This page says neither. -->

<!-- OPEN: the lab task numbers that the concepts name ("Lab 27, Task 1" and so on) follow the spec's lab plan and are proposed until the lab is built. "Lab 27" links to this page's own lab heading, and that link changes when the chapter is split into pages. -->

## In this chapter

| Section | The question it answers |
| --- | --- |
| [27.1](workflow-or-agent.md) Workflow or agent | For this step, does code decide, or the model, or a person? |
| [27.2](workflow-patterns.md) Workflow patterns | In which four ways can code hold the plan, and what can each not plan for? |
| [27.3](an-orchestrator-and-its-subagents.md) An orchestrator and its subagents | What does an orchestrator hand to the model, and which limits stay in code? |
| [27.4](what-a-subagent-gets.md) What a subagent gets, and what it hands back | What can code check going in, and coming back? |
| [27.5](where-it-runs.md) Where it runs | In each place a loop can run, which decisions can your code hold? |
| [27.6](diagnosing-a-misbehaving-agent.md) Diagnosing a misbehaving agent | Which row did nobody decide, or decide wrongly? |
| [27.7](how-todays-tools-realize-it.md) How today's tools realize it | On each product, who decides the next step, and what are its default limits? |
| [Lab 27](lab.md) | The table first, then a workflow and an orchestrator, a comparison, three diagnoses and a port, from tag `ch26` to tag `ch27` |
| Check yourself | One open question for each concept, with an outline of its answer |

[^anthropic-multi-agent-research]: How we built our multi-agent research system, Anthropic.
