A pattern language for working with agents

Each pattern is named, numbered and quoted from the people who published it or run it.

subtask, resultTaskOrchestrator, Model callOrchestratorWorker, Model callWorkerWorker, Model callWorkerWorker, Model callWorker6Orchestrator and workers
  • Person
  • Model call
  • Agent
  • Tool
  • Code
  • Context
How to read

People and agents

  1. 1Plan before codingHave the agent research and write a plan, and approve it yourself before it edits anything.
  2. 2Human approvalA person approves sensitive or irreversible actions before the agent takes them.
  3. 29Asynchronous coding agentHand a task to an agent that works unattended in the cloud and returns a pull request to review.
  4. 30Parallel agents in separate worktreesRun several agents at once, each in its own checkout, then review and merge each one.
  5. 33Checkpoint and rewindSnapshot the code before each agent turn, and go back to it when the agent goes off track.

Systems of agents

  1. 3Prompt chainingSplit a task into fixed steps; each call works on the output of the one before.
  2. 4RoutingClassify each input, then send it to the prompt, model or agent built for that kind.
  3. 5ParallelizationRun independent subtasks or repeated attempts at once, then combine the results.
  4. 6Orchestrator and workersA lead model splits a task as it goes, hands the parts to workers and combines their results.
  5. 7Evaluator and optimizerOne model writes, another judges against clear criteria, and the loop repeats until it passes.
  6. 8AdvisorA fast, cheap model does the work and asks a stronger model for guidance at hard moments.
  7. 9HandoffOne agent passes the conversation to another that specializes in what comes next.
  8. 13Subagent with its own context windowSend a side task to a subagent with a fresh context window; only its short summary comes back.
  9. 20GuardrailsScreen user input before the agent sees it, and its output and actions before they take effect.
  10. 26Plan-and-executeA planner model writes the whole plan, executors carry out the steps, and the planner replans.
  11. 32LLM cascadeA cheap model answers first; only answers that fail a confidence check go to a stronger model.
  12. 34Multi-agent debateHave agents argue over each other’s answers for several rounds, then let a judge settle the result.
  13. 37Dual LLMKeep untrusted text away from the model that can act; a second model with no tools reads it instead.

One agent

  1. 10Agent loopA model uses tools in a loop, deciding each next step from what the last one returned.
  2. 11ReflectionThe agent critiques its own output or past attempts, then tries again.
  3. 12CompactionWhen the context window fills, summarize the conversation and continue from the summary.
  4. 14Structured note-takingThe agent writes notes to a file outside its context window and reads them back later.
  5. 15Tool result clearingReplace old tool results in the history with a short placeholder once the agent has used them.
  6. 16Just-in-time context retrievalKeep file paths, queries and links in context; load the data with tools only when it is needed.
  7. 21SandboxingRun the agent inside set limits on files and network so it can act without asking each time.
  8. 22Code actionsThe agent writes code that calls its tools, and an interpreter runs it and returns the result.
  9. 27Todo listThe agent keeps a task list in its context and rewrites it as it works, so the goal stays in view.
  10. 28SkillsKeep each skill’s name and description in context; load its instructions only when a task needs it.
  11. 35Tool searchGive the agent a search over its tools, and load only the tool definitions a task needs.

One call

  1. 17LLM as a judgeOne model call grades another model’s output against written criteria.
  2. 18Retrieval-augmented generationOne retrieval step before the call: add passages relevant to the query to the prompt.
  3. 23Contextual retrievalBefore indexing, prepend to each chunk a short model-written note on where it sits in its document.
  4. 24RerankingRetrieve many candidates, then let a reranker keep only the most relevant few for the model.
  5. 25Query rewritingHave a model rewrite the user’s query, with the conversation so far, before searching with it.
  6. 36SpotlightingMark untrusted text inside the prompt so the model can tell it apart from your instructions.

Practices

  1. 19Eval-driven developmentWrite evals first, run them on every change, and turn each reported failure into a test case.
  2. 31Test-first developmentWrite the tests first and confirm they fail; the agent then works until they pass.