Get Started

Anthropic's Workflow Patterns: Simple Over Complex

Schluntz and Zhang (Anthropic, Dec 2024) distinguish workflows (predefined paths) from agents (dynamic tool-use). Five workflow patterns cover most cases. Start with direct API calls. Add agents only when steps cannot be predicted.

60 minutesLearn + BuildPython (stdlib)Free lesson

Learning Objectives

  • Name Anthropic's five workflow patterns: prompt chaining, routing, parallelization, orchestrator-workers, evaluator-optimizer.
  • Explain the agent-vs-workflow distinction and the engineering cost of each.
  • Identify when to pick a workflow over an agent (and vice versa).
  • Implement all five patterns in stdlib against a scripted LLM.

The Problem

Teams reach for multi-agent frameworks for problems that want a single function call. The cost is real: frameworks add layers that obscure prompts, hide control flow, and invite premature complexity. Schluntz and Zhang's Dec 2024 post is the most-cited industry pushback: start simple, add complexity only when it earns its cost.

The Concept

Workflows vs agents

  • Workflow. LLMs and tools orchestrated through predefined code paths. Engineers own the graph.
  • Agent. LLMs dynamically direct their own tools and take their own steps. The model owns the graph.

Both have their place. Workflows are cheaper, faster, and easier to debug. Agents unlock open-ended problems but make failure modes harder to reason about.

The augmented LLM

Foundation for all five patterns: one LLM with three capabilities wired in, search (retrieval), tools (actions), memory (persistence). Any API call can use these.

The five patterns

  1. Prompt chaining. Output of call 1 is input to call 2. Use when a task has a clean linear decomposition. Optional programmatic gates between steps.
  2. Routing. A classifier LLM picks which downstream LLM or tool to invoke. Use when categorically different inputs need different handling (tier-1 support vs refund vs bug vs sales).
  3. Parallelization. Run N LLM calls concurrently, aggregate results. Two shapes: sectioning (different chunks) and voting (same prompt, N runs, majority/synthesis).
  4. Orchestrator-workers. An orchestrator LLM dynamically decides which workers (also LLMs) to run and synthesizes their output. Similar to agent loops but the orchestrator does not loop indefinitely.
  5. Evaluator-optimizer. One LLM proposes an answer, another LLM evaluates it. Iterate until the evaluator passes. This is Self-Refine (Lesson 05) generalized.

Where workflows beat agents

  • Predictable tasks. If you can enumerate the steps, you should.
  • Cost-bound tasks. Workflows have bounded step counts; agents can spiral.
  • Compliance-bound tasks. Auditors want to read the graph, not infer it from trajectories.

Where agents beat workflows

  • Open-ended research. When the next step depends on what the last step returned.
  • Variable-length tasks. Minutes to hours of work where step count is unknown.
  • Novel domains. When you don't yet know the right workflow, exploration first, codify later.

The context-engineering companion

"Effective context engineering for AI agents" (Anthropic 2025) formalizes the adjacent discipline: the 200k window is a budget, not a container. What to include, when to compact, when to let context grow. Covered in detail in Phase 14 lesson on context compression (Phase 14 earlier lesson 06 in this curriculum before the renumber).

Build It

code/main.py implements all five workflow patterns against a ScriptedLLM:

  • prompt_chain(input, steps)sequential.
  • route(input, classifier, handlers)classification + dispatch.
  • parallel_vote(prompt, n, aggregator)N runs, aggregate.
  • orchestrator_workers(task, workers)orchestrator picks workers.
  • evaluator_optimizer(task, proposer, evaluator, max_iter)loop until pass.

Run it:

Code
python3 code/main.py

Each pattern prints its trace. Total lines of code per pattern is ~10-15; the cost of a framework is measured in thousands.

Use It

  • Direct API calls for most tasks.
  • Framework only when the pattern genuinely needs durable state (LangGraph), actor-model concurrency (AutoGen v0.4), or role templating (CrewAI).
  • Reach for the Claude Agent SDK when you want the Claude Code harness shape without rebuilding it.

Ship It

outputs/skill-workflow-picker.md picks the right pattern for a given task description, including the decision rationale and the refactor path to an agent if workflows fall short.

Exercises

  1. Implement routing with a confidence threshold. Below threshold -> escalate to human. Where does the threshold land for a tier-1 support use case?
  2. Add a timeout to parallel_vote. What happens when one call hangs? How do you aggregate with missing votes?
  3. Turn evaluator_optimizer into a bandit: keep the top-2 outputs across iterations so a late good result doesn't get overwritten by a late bad one.
  4. Combine prompt chaining with routing: a router picks one of three chains. Measure token cost vs a single big-prompt alternative.
  5. Pick one of your production features. Draw the workflow graph. Count steps. Would an agent actually be better here?

Key Terms

TermWhat people sayWhat it actually means
Workflow"Predefined flow"Engineer-owned graph of LLM and tool calls
Agent"Autonomous AI"Model-owned graph; dynamic tool direction
Augmented LLM"LLM with tools"LLM + search + tools + memory; the atomic unit
Prompt chaining"Sequential calls"Output of call N is input to call N+1
Routing"Classifier dispatch"Pick which chain/model handles the input
Parallelization"Fan out"N concurrent calls; aggregate by sectioning or voting
Orchestrator-workers"Dispatcher agent"Orchestrator LLM picks specialist LLMs dynamically
Evaluator-optimizer"Proposer + judge"Iterate until evaluator passes; Self-Refine generalized

Further Reading

Check what you learned

7 questions. Answer each one to see why.

Question 1 of 70 correct

How does Anthropic distinguish a workflow from an agent?

The full code

Every file for this lesson. Copy it, run it, then change it.

main.py
"""All five Anthropic workflow patterns in stdlib.

prompt chaining, routing, parallelization (voting), orchestrator-workers,
evaluator-optimizer. Each pattern is 10-15 lines; the point is to show how
small they are compared to a framework.
"""

from __future__ import annotations

from collections import Counter
from dataclasses import dataclass
from typing import Any, Callable


class ScriptedLLM:
    def __init__(self, script: dict[str, str | list[str]]) -> None:
        self.script = script
        self.index: dict[str, int] = {}
        self.calls: list[str] = []

    def __call__(self, prompt: str) -> str:
        self.calls.append(prompt)
        value = self.script.get(prompt)
        if isinstance(value, list):
            i = self.index.get(prompt, 0)
            self.index[prompt] = min(i + 1, len(value) - 1)
            return value[i]
        if isinstance(value, str):
            return value
        return f"[unhandled: {prompt}]"


def prompt_chain(input_text: str, llm: Callable[[str], str],
                 steps: list[tuple[str, str]]) -> list[tuple[str, str]]:
    current = input_text
    trace: list[tuple[str, str]] = []
    for label, template in steps:
        prompt = template.format(text=current)
        output = llm(prompt)
        trace.append((label, output))
        current = output
    return trace


def route(input_text: str, classifier: Callable[[str], str],
          handlers: dict[str, Callable[[str], str]]) -> tuple[str, str]:
    label = classifier(input_text)
    handler = handlers.get(label) or handlers.get("default")
    if handler is None:
        return label, f"no handler for {label}"
    return label, handler(input_text)


def parallel_vote(prompt: str, llm: Callable[[str], str], n: int = 5) -> tuple[str, Counter]:
    votes = [llm(prompt) for _ in range(n)]
    counts = Counter(votes)
    winner, _ = counts.most_common(1)[0]
    return winner, counts


@dataclass
class Worker:
    name: str
    handles: Callable[[str], bool]
    fn: Callable[[str], str]


def orchestrator_workers(task: str, workers: list[Worker],
                         synth: Callable[[list[tuple[str, str]]], str]) -> tuple[str, list[tuple[str, str]]]:
    outputs: list[tuple[str, str]] = []
    for worker in workers:
        if worker.handles(task):
            outputs.append((worker.name, worker.fn(task)))
    return synth(outputs), outputs


def evaluator_optimizer(task: str, proposer: Callable[[str, str | None], str],
                        evaluator: Callable[[str, str], tuple[bool, str]],
                        max_iter: int = 5) -> tuple[str, list[tuple[str, str, str]]]:
    trace: list[tuple[str, str, str]] = []
    feedback: str | None = None
    for i in range(max_iter):
        candidate = proposer(task, feedback)
        ok, judge = evaluator(task, candidate)
        trace.append((candidate, "PASS" if ok else "FAIL", judge))
        if ok:
            return candidate, trace
        feedback = judge
    return candidate, trace


def demo_chain(llm: ScriptedLLM) -> None:
    print("-" * 70)
    print("1. PROMPT CHAINING — summarize then title")
    print("-" * 70)
    trace = prompt_chain(
        input_text="Agents are ReAct loops with tools, memory, and guardrails.",
        llm=llm,
        steps=[
            ("summarize", "summarize: {text}"),
            ("title", "give a 6-word title: {text}"),
        ],
    )
    for label, output in trace:
        print(f"  [{label}] {output}")


def demo_route(llm: ScriptedLLM) -> None:
    print("\n" + "-" * 70)
    print("2. ROUTING — classify then dispatch")
    print("-" * 70)

    def classifier(text: str) -> str:
        return llm(f"classify: {text}")

    handlers = {
        "refund": lambda t: llm(f"handle refund: {t}"),
        "bug": lambda t: llm(f"handle bug: {t}"),
        "sales": lambda t: llm(f"handle sales: {t}"),
        "default": lambda t: "escalate to human",
    }

    for inp in ("I want my money back",
                "the CLI crashes on ctrl-c",
                "do you offer volume pricing"):
        label, out = route(inp, classifier, handlers)
        print(f"  [{label}] {out}")


def demo_parallel(llm: ScriptedLLM) -> None:
    print("\n" + "-" * 70)
    print("3. PARALLELIZATION — N voters on a boolean")
    print("-" * 70)
    winner, counts = parallel_vote("is this code safe to ship?", llm, n=5)
    print(f"  winner: {winner}")
    print(f"  counts: {dict(counts)}")


def demo_orchestrator(llm: ScriptedLLM) -> None:
    print("\n" + "-" * 70)
    print("4. ORCHESTRATOR-WORKERS — specialist pool")
    print("-" * 70)

    workers = [
        Worker("python_reviewer",
               handles=lambda t: "python" in t.lower(),
               fn=lambda t: llm(f"review python: {t}")),
        Worker("security_reviewer",
               handles=lambda t: True,
               fn=lambda t: llm(f"review security: {t}")),
        Worker("style_reviewer",
               handles=lambda t: "style" in t.lower(),
               fn=lambda t: llm(f"review style: {t}")),
    ]

    def synth(outputs: list[tuple[str, str]]) -> str:
        return " | ".join(f"{name}: {out}" for name, out in outputs)

    task = "review this python change for style and security"
    final, outputs = orchestrator_workers(task, workers, synth)
    for name, out in outputs:
        print(f"  [{name}] {out}")
    print(f"  synth: {final}")


def demo_evaluator_optimizer(llm: ScriptedLLM) -> None:
    print("\n" + "-" * 70)
    print("5. EVALUATOR-OPTIMIZER — propose, judge, refine")
    print("-" * 70)

    def proposer(task: str, feedback: str | None) -> str:
        prompt = f"propose: {task}"
        if feedback:
            prompt += f" (fix: {feedback})"
        return llm(prompt)

    def evaluator(task: str, candidate: str) -> tuple[bool, str]:
        verdict = llm(f"evaluate: {candidate}")
        ok = verdict.startswith("PASS")
        return ok, verdict

    final, trace = evaluator_optimizer(
        "write a one-line summary of ReAct", proposer, evaluator
    )
    for i, (cand, verdict, reason) in enumerate(trace, 1):
        print(f"  iter {i}  [{verdict}] {cand}  // {reason}")
    print(f"  final: {final}")


def main() -> None:
    print("=" * 70)
    print("ANTHROPIC WORKFLOW PATTERNS — Phase 14, Lesson 12")
    print("=" * 70)

    llm = ScriptedLLM({
        "summarize: Agents are ReAct loops with tools, memory, and guardrails.":
            "Agents: ReAct + tools + memory + guardrails.",
        "give a 6-word title: Agents: ReAct + tools + memory + guardrails.":
            "Agents as ReAct with Guardrails Built In",

        "classify: I want my money back": "refund",
        "classify: the CLI crashes on ctrl-c": "bug",
        "classify: do you offer volume pricing": "sales",
        "handle refund: I want my money back": "refund filed",
        "handle bug: the CLI crashes on ctrl-c": "bug logged",
        "handle sales: do you offer volume pricing": "quote sent",

        "is this code safe to ship?": ["yes", "yes", "no", "yes", "no"],

        "review python: review this python change for style and security":
            "python ok",
        "review security: review this python change for style and security":
            "security ok",
        "review style: review this python change for style and security":
            "style ok",

        "propose: write a one-line summary of ReAct":
            "ReAct loops thoughts and tool calls.",
        "evaluate: ReAct loops thoughts and tool calls.":
            "FAIL: missing observations",
        "propose: write a one-line summary of ReAct (fix: FAIL: missing observations)":
            "ReAct interleaves thought, action, and observation until done.",
        "evaluate: ReAct interleaves thought, action, and observation until done.":
            "PASS",
    })

    demo_chain(llm)
    demo_route(llm)
    demo_parallel(llm)
    demo_orchestrator(llm)
    demo_evaluator_optimizer(llm)

    print(f"\ntotal llm calls across all five patterns: {len(llm.calls)}")
    print("direct API + small helpers. no framework needed.")


if __name__ == "__main__":
    main()