Agentic Design Patterns

Start with the least agent you need

  • An agent is a loop where the model decides the next step. Every step of autonomy you hand over costs latency, tokens and variance
    • Autonomy here means how many times the model, rather than your code, picks what happens next. A single call has none. A 10-step ReAct loop has ten.
    • Variance means runs differs from each other in ReAct compared to workflow
  • So I treat the patterns below as a ladder. Move up one rung only when evals show the lower rung is failing
  • Most production systems end up as a mix: a fixed workflow outside, a small ReAct loop inside each step (see the alert triage case study)

Control-flow patterns

PatternWho decides
the next step
Use whenWatch out for
Single call (+ structured output)CodeClassification, extraction, rewriting, scoringValidate the schema, retry on invalid output
Chain / workflowCode decides the orderSteps are known up front (extract → validate → draft)Brittle when inputs vary a lot
RouterA classifier, then code dispatchesDistinct intents need different prompts, tools or modelsMisroutes; always keep an “other” fallback
ParallelCode fans out and mergesIndependent sub-tasks, or voting for confidenceMerge logic; cost multiplies
ReActModel, one step at a timePath is unknown, tools are cheap and safeSkips checks, loops, drifts; needs a budget
Plan and executeModel writes the plan, executor runs itLong tasks where a visible plan helpsStale plan; re-plan on failure
Orchestrator – workerModel spawns workers with their own contextBreadth: research, many files, many hostsCoordination cost; workers can’t see each other
Evaluator – optimizerGenerator plus a separate evaluatorOutput can be graded against a rubric or testsLenient judge; cap the iterations
  • Single call
    • Use case: classification (intent, harm category), extraction into a JSON schema, scoring a document
    • Not an agent, but the most common LLM call in production. Structured output turns it into a function you can unit test
  • Chain vs router
    • Chain: the order lives in code. Easy to debug, and each step can have its own prompt and model
    • Router: classify first, then dispatch. A cheap model handles easy traffic and hard cases go to a bigger model or a full agent. Same idea as a ranking funnel
  • ReAct vs plan and execute
    • ReAct: think → act → observe, repeat. Flexible, but the model can quietly skip an important check
    • Plan and execute: plan once, then run the steps. In our planner the plan was fixed and the model called next_step() to move forward. Order was deterministic; the model was free inside a step
  • Orchestrator – worker
    • Each worker gets a fresh context and returns a short summary, so the orchestrator’s context stays small
    • Uber[5] defaults subagents to a cheaper model, while the primary model does the decomposition and evaluation
  • Evaluator – optimizer
    • Anthropic’s[3] long-running app harness uses planner → generator → evaluator. They found it easier to tune a separate evaluator to be skeptical than to make the generator critical of its own work
    • The evaluator needs concrete criteria. “Is this good?” is hard to grade; “does this follow these principles?” is gradable

Loading state

  • Agent runs take seconds to minutes. A blank spinner kills trust
  • It needs streaming. Harnesses like LangChain / LangGraph stream events (tokens, tool calls, tool results) as the model works; what to show is up to the app
  • 4 ways to implement it
    • 1) Static mapping from tool calls
      • When you see web_search(query), publish “Searching the web for: query”
      • Pro: free and deterministic
      • Con: generic, it can’t say why
    • 2) Model-written labels in the tool schema
      • Add a field like status_message to every tool’s arguments; the model fills it when it calls the tool
      • Pro: human readable at almost no extra cost
      • Con: the model can describe the action loosely; the label says one thing while the arguments do another
    • 3) Side-channel summary over reasoning
      • A cheap model summarizes the streamed thinking every few seconds, out of the critical path
      • Pro: richer than tool names
      • Con: extra cost, and the summary can misstate what the model is doing
    • 4) Explicit plan or task list
      • What we did with our planner
      • TaskCreate / TaskUpdate like interface, each item with a short summary. The UI renders it as a checklist
      • Pro: progress is real, and the list doubles as the scratchpad (see Memory)
  • Design concerns
    • Throttling
      • Let each status stay for 1-2 seconds
      • Otherwise, if tools are called every few milliseconds, the state flickers
    • Collapse after completion
      • Keep the steps as an expandable list. It acts as the reasoning trace and an audit log
    • Long runs need notifications, not spinners
      • Background agents keep working after you close the app and ping you when they finish or need approval

Memory

LLM is stateless; the harness provides memory at three levels.

LevelWhat it holdsLifetimeWritten by
Working memoryThe conversation: messages, tool calls, tool resultsOne sessionHarness appends every step
ScratchpadPlan, todo list, findings ledgerOne task, survives compactionModel via a tool, or harness code
Long-term memoryFacts about the user, preferences, lessonsAcross sessionsBackground pass after the turn

Working memory, the conversation itself

  • The harness appends each user message, model output, tool call and tool result
  • For durability, the harness saves a snapshot after each step in a DB against the thread or session ID
    • Lets you resume after a crash, or after a human approval that took an hour
  • Long runs need compaction when we approach the context window (next section)

Scratchpad, notes outside the message list

  • Plans, todo list, intermediate findings
  • Survives compaction, since it is not part of the message history and gets re-injected
  • Who writes the scratchpad?
    • 1) The model via a tool call, like my planner’s next_step(). Flexible, but the model can forget to update it
    • 2) Harness code appends each tool result to a ledger. Most reliable
  • When do we read it?
    • 1) Every turn, injected by the harness
      • Todo list and current plan at the end of the context on every step, right next to the next decision
      • Works for a small scratchpad
    • 2) On demand, when the model calls a read tool
      • read_file()
      • Scales, but the agent can forget to look
      • Fix is to inject a small index (file names with one-line descriptions) every turn
      • Same idea as OpenAI[4] keeping AGENTS.md to about 100 lines as a map into docs/, and Claude Code[11] loading only the first 200 lines of its MEMORY.md index at session start

Long-term memory, across sessions

  • Facts about the user, preferences, lessons learned
  • Options
    • Key-value store, namespaced by user / tenant
    • Vector DB for memory retrieval
    • Plain markdown files: CLAUDE.md, AGENTS.md, MEMORY.md
      • My profile is injected on every turn by the Claude desktop app
      • OpenClaw keeps a curated MEMORY.md plus daily logs (memory/YYYY-MM-DD.md). Today’s and yesterday’s logs load automatically; older ones are reached through a memory_search tool
  • Background pass writes new facts after each turn, rather than the model saving them mid reply
    • In contrast, the scratchpad is on the hot path
    • Off the critical path, the writer can have its own prompt and rules (what not to store, no guesses about the user)
  • The read path matters as much as the write path: always on (small profile), index plus on demand, or semantic retrieval

When the context fills up

Four options, from cheapest to most model-driven:

  1. Observation masking
    • Drop or trim old tool outputs, keep the actions and reasoning
    • Tool observations were about 84% of an average SWE-agent turn, and simple masking stayed competitive with LLM summarization at a lower cost
  2. Compact in place
    • Summarize the history and keep going in the same conversation
    • Uber triggers automatic compaction at 400K tokens, even on 1M-context models
  3. Reset with a handoff note
    • The model writes a structured note; a fresh context starts from the note plus the task
    • Anthropic found compaction alone wasn’t enough for Sonnet 4.5, which got anxious near its context limit, so resets became part of their harness
    • NVIDIA’s “You don’t need long context” (Nov 2026)[6]: Claude Opus 4.5 with one action per context scored 70.2% on SWE-bench Verified vs 70.0% with full history. Serving at 256K context cost 3.6x to 7x more per generated token than at 16K
  4. Model manages its own context
    • Give the model tools to read a range of its input and to call itself on a sub-problem (recursive calls)
    • “Trained Agentic Context Management” fine-tunes a small model on exactly this setup. With 8K tokens of context it matched GPT-5.4 with 1M tokens on OOLONG-synth, for documents over 40K tokens
  • Compaction is lossy in a specific way. On real user sessions it drops instructions about how to work, and scope limits like files not to touch, far more often than the feature requirements
    • So keep rules in files the harness reloads from disk (AGENTS.md, the scratchpad), not in the chat
    • OpenClaw’s bootstrap files survive compaction for exactly this reason
  • Compaction also breaks the prompt-cache prefix. Keep stable content (system prompt, tool definitions) at the front and append after it
    • Uber’s numbers: cache reads cost 0.1x the input price; writes cost 1.25x for a 5-minute TTL and 2x for 1 hour. Interactive sessions use 1 hour because engineers pause; subagents use 5 minutes

References

  1. Anthropic: Building effective agents
  2. Anthropic: Effective context engineering for AI agents
  3. Anthropic: Harness design for long-running application development
  4. OpenAI: Harness engineering, leveraging Codex in an agent-first world
  5. Uber Engineering: Efficient software factory (Aug 2026)
  6. NVIDIA Research: You don’t need long context (arXiv 2611.05306)
  7. The Complexity Trap: observation masking vs LLM summarization (arXiv 2508.21433)
  8. Benchmarks Are Surprisingly Compaction-Friendly (arXiv 2610.31778)
  9. Sandlund: Trained Agentic Context Management (arXiv 2610.02404)
  10. OpenClaw docs: Agent workspace
  11. Claude Code docs: Memory

One thought on “Agentic Design Patterns”

Leave a Reply