Skip to content
The AI Agent Factory

27.2 Workflow patterns

Approved by
Panaversity ·
20 min read

In everyday life. You visit a small clinic. First the desk, then the nurse, then the doctor: the order is printed on the wall. At the desk, a clerk reads your age and sends you to the children's room or the adults' room. Later, one room takes your blood while another takes an X-ray, because neither needs the other. The doctor reads both results and may send you back for one more test, and after two tries sends you to a specialist. The nurse, the clerk and the doctor each use their own judgment. None of them decides where you go next. The clinic's rules do. And a patient whom no sign covers goes back to the desk, to a person. In this section, the staff are model calls, the clinic's rules are your code, and the patient is one invoice.

What you will learn. You practice this in Lab 27, Task 1, where you build the workflow.

  • What does it mean that code holds the plan?
  • How does a chain hold the order?
  • How does a route hold the branch?
  • Which steps can run side by side?
  • Who holds the stop when a check sends work back?
  • What happens to a case that no branch covers?

Why it matters. "Code decides" is not one design. Each of the four patterns answers a different part of the auditors' question, and each fails on a different case.

At work. It is still Wednesday morning. The workflow's column of the table is filled (27.1). Before your coding agent writes src/workflows/month-end.ts, you write its plan in five plain steps:

  1. Code calls list_open_invoices.
  2. For each invoice, code calls find_vendor and get_purchase_order with the ids on the invoice.
  3. Code sends the model one request for that invoice, with what the lookups returned. The model says whether the invoice is a problem and gives the policy line. Its only tool is search_policy, and it has a turn limit.
  4. Code records the decision.
  5. After the last invoice, code runs Lab 26's check.

Every "then" and every "for each" in that plan is a line of your code.

What does it mean that code holds the plan?

The order, the branch, what runs side by side, and the stop are all written before the run starts. Anthropic's essay "Building effective agents" lists these as patterns for workflows.1

Figure 27.2. Four workflow patterns, each named by what code holds. A model's answer can choose between two arrows that code drew. It cannot draw a new one.

How does a chain hold the order?

Each step runs after the one before it, and takes its output. The essay calls this prompt chaining, which "decomposes a task into a sequence of steps, where each LLM call processes the output of the previous one."1 In this book a chain can mix model calls with plain code. Brightline's chain is: look up, then judge, then check. The first two run for each invoice. The check reads the whole review, so it runs once, after the last invoice.

A chain is what answers the auditors. The same invoice goes through the same steps in the same order, because the order is not the model's to change.

Its price is time, and a fixed shape. The essay says a chain will "trade off latency for higher accuracy".1 Latency is the time you wait for an answer. The essay adds that a chain fits a task that "can be easily and cleanly decomposed into fixed subtasks."1 An invoice that needs a step the chain does not have gets no such step.

How does a route hold the branch?

Code sends each input down one of a fixed set of paths. The essay's routing "classifies an input and directs it to a specialized followup task."1 To route is to sort first, and then use the prompt written for that kind.

At Brightline, an invoice with no purchase order is its own kind. The connector says so. It answers with a tool error that names the missing purchase order and quotes the policy line, po-matching.md:9 (17.3). So code sends that invoice to a second prompt, written for it.

The sorting can also be a model call. The essay allows classification "either by an LLM or a more traditional classification model/algorithm."1 Then the model picks among branches, and code fixed which branches exist.

Its price: a route knows only the kinds you wrote a branch for.

Which steps can run side by side?

Steps that do not depend on each other. Code can start several such steps at the same time, which is to run in parallel. The essay's name is parallelization, and its first kind is "Breaking a task into independent subtasks run in parallel."1 Looking up two different invoices is the simple case: neither lookup needs the other's result.

Code decides what runs side by side, and how many. That is the difference from an orchestrator, where the model decides it (27.3).

Its price: a step that needs another step's result cannot run beside it. In a review, a late invoice can depend on an earlier decision (26.1). Those decisions must stay in order.

In this book's lab, the workflow judges each invoice alone, in a request of its own. So it cannot see an earlier decision. An invoice whose answer depends on one is a case that no branch covers (below).

The essay names a second kind, voting: "Running the same task multiple times to get diverse outputs."1

Who holds the stop when a check sends work back?

Code does, as a round limit. In the essay's evaluator-optimizer pattern, "one LLM call generates a response while another provides evaluation and feedback in a loop."1 The plain name is check and retry: one call writes, a judge reads, and a failure goes back with what was wrong.

You have built this loop already, with code as the judge. In 23.16, the check by code reads the report, sends the specific failure back, and stops after two corrections. In 23.17, the outcome is the same loop with a grader. A grader is a model that judges against a rubric, a written list of what a good answer has. In both, the judge may ask for another round. It never decides how many rounds there are. Your code holds that number, and what happens when it is reached.

The essay says when the pattern fits: "when we have clear evaluation criteria, and when iterative refinement provides measurable value."1 Its price is one more round of model calls for each failure. And a judge catches only what it tests.

What happens to a case that no branch covers?

It stops, and it is marked for a person. This is the price of every pattern above: a workflow handles only what its builder planned for. When an invoice fits no branch, the workflow does not guess. It marks the invoice needs_review, and the invoice waits for Ana. needs_review is Lab 24's status for a result that code would not accept.

The other fair choice is a step the model decides. An orchestrator can take that invoice by a path of its own. It does so at a higher cost, and with no promise of the same path (27.3). In Lab 27, Task 2, you give both builds such an invoice and record what each does.

The operating rule: in a workflow, a model's answer may choose among the arrows your code drew. If you need the model to draw a new arrow, that step is an agent's, and it needs a stop, a boundary and a check.

Do this now. Ask your coding agent to find the plan in code you already have. Copy this line:

Read the code that runs this project's worker. List each place where the code fixes the order of two steps, picks a branch, starts steps at the same time, or stops a retry loop, with the file and line for each. Then list each place where a model's answer picks what happens next. Say which of those have no limit in code. Change no file.

Check yourself

Question 1 / 8

0 answered

In a workflow, a model's answer may choose between two arrows that code drew. What can it never do?

  1. Building effective agents, Anthropic.↑↑2↑3↑4↑5↑6↑7↑8↑9↑10
Owner
Panaversity