- 01Seven layers: model, context, tools, control loop, memory, governance, evidence.
- 02Frameworks and tutorials cover layers one to five. Deployments fail at six and seven.
- 03Build the tool layer first — it determines what governance and evidence you need.
- 04Each layer should be replaceable without touching the others. If swapping the model means rewriting your tools, the boundary is in the wrong place.
- 05Governance and evidence are architecture, not process. You cannot add them to a finished agent as a review step.
The seven layers
Most published stack diagrams stop at four. That is not an oversight — the first four layers are genuinely all you need to produce something that works, and they are what every framework competes on. The last three, and particularly the last two, are what you discover you need at the exact moment an agent touches something real.
| # | Layer | Responsibility | Failure if missing |
|---|---|---|---|
| 01 | Model | Provider abstraction, routing, fallback | Locked to one vendor's pricing, limits and outages |
| 02 | Context | What enters the window, and what it costs | Cost explosion; the agent forgets what matters |
| 03 | Tools | The action surface — schemas, execution, idempotency | Duplicate side effects; unbounded capability |
| 04 | Control loop | Step budget, stop conditions, retry, escalation | Runaway loops; errors that compound silently |
| 05 | Memory | Session state, durable memory, reusable procedures | Re-derives the same knowledge every single run |
| 06 | Governance | Authority boundaries, approval gates, policy in code | Nothing stops a redirected or mistaken agent |
| 07 | Evidence | Tracing, evals, audit trail | You cannot prove, reconstruct, or improve what happened |
The organising principle is replaceability. Each layer should be swappable without touching the others, and the test is concrete: if changing your model provider means rewriting tool definitions, or adding an approval gate means editing the control loop, the boundaries are in the wrong place.
1. Model
The mistake here is not choosing the wrong model; it is choosing anything at all in a way that cannot be revisited. Model releases currently outpace deployment cycles, so a system that names a model in forty places has already lost a decision it will need to make again within months.
Per-task, not per-system
A single model everywhere is almost always wrong in one direction or the other. Planning, ambiguity resolution and anything with a wide solution space want capability. Mechanical extraction, classification and routine tool selection do not. The practical shape is a capable model on the main thread and cheaper models for delegated work that is well-specified — see the Agent SDK guide for how per-subagent model selection works in a real harness.
One constraint worth knowing before you design around model switching: prompt caches are model-scoped. Changing model mid-conversation invalidates the cache, so the cheap pattern is a stable main thread with variety pushed into subagents rather than a router that swaps models turn by turn.
2. Context
Four things compete for the window, and they have very different value density. Instructions are small and load-bearing. Tool schemas are moderate and mostly static. History grows without bound. Retrieved content is the largest and the most variable in usefulness.
Caching is a prefix match, and that dictates layout
Prompt caching keys on the exact bytes of the prompt up to a breakpoint, which means one changing byte early invalidates everything after it. That single fact determines the whole layer’s design: stable content first, volatile content last, and nothing dynamic interpolated into the system prompt.
The classic self-inflicted wound is a timestamp or a session ID in the system prompt. It sits at position zero, changes every request, and quietly makes the entire conversation uncacheable. If your cache-hit rate is zero across identical-looking requests, look for something like that before looking anywhere else.
3. Tools
Two design decisions in this layer matter more than the rest of the stack combined.
Granularity: bash versus dedicated tools
A shell tool gives an agent enormous reach and gives your harness nothing: every action arrives as an opaque command string with the same shape. Promoting an action to a dedicated tool with a typed schema gives you something you can gate, render, log, rate-limit and parallelise.
Start with breadth and promote what you need to control. The signal that an action should be promoted is that you want to do anything to it other than run it.
Guarantees: exactly-once is not free
Every tool that changes state needs an explicit answer to “what happens if this runs twice?”. Agents retry on their own initiative and often cannot tell whether a previous attempt succeeded, so this is not a theoretical concern — it is the most common source of real damage in agent deployments. The mechanism differs from the HTTP pattern because an agent’s retry is not a byte-identical request; see idempotency for agent actions for how to key on intent instead.
- The tool-layer rule
- A tool description should state when to call it, not only what it does — that phrasing measurably changes how often the model reaches for it. And every mutating tool should ship with a matching read, so the agent can check rather than guess.
4. Control loop
This is the layer people write themselves and shouldn’t. The loop is not where differentiation lives, it is well-solved by several harnesses, and hand-rolling it means owning a class of bug — malformed tool-result ordering, dropped parallel calls, mishandled pause states — that has nothing to do with your product.
// Bounds — every one of these should be configurable per run.
maxTurns: 40, // agentic iterations before a hard stop
maxBudgetUsd: 5, // spend ceiling, enforced
taskBudget: { total: 64_000 }, // budget the model is AWARE of, so it paces itself
timeoutMs: 15 * 60_000,
// Stop conditions — distinct outcomes, not one boolean.
// completed → objective met
// exhausted → hit a budget; partial work may be salvageable
// escalated → hit an authority boundary; waiting on a human
// failed → unrecoverableThe distinction in that caption is worth internalising. A hard output ceiling truncates the model mid-thought with no warning. A budget the model can see lets it prioritise and finish gracefully. Production loops want both: the budget for behaviour, the ceiling as a backstop.
Loops compound errors
A single wrong tool call is bounded. A loop is not: the agent acts on a mistaken belief, observes a world that now reflects its mistake, concludes something further, and acts again. By step six the original error is buried under five steps that each followed logically from it, and the transcript reads as reasonable the whole way down. That is why the stop conditions above need to be four distinct outcomes rather than success or failure — escalated in particular is not a failure, and treating it as one is how agents get retried into damage.
5. Memory
Three distinct things get called memory, and conflating them is why the layer is usually built badly.
| Kind | Lifetime | What it holds |
|---|---|---|
| Session state | One run | History, intermediate results, current plan |
| Durable memory | Across runs | Facts, preferences, decisions worth persisting |
| Procedure library | Across runs and agents | Reusable workflows the agent worked out once |
The third is the highest-leverage and the least commonly built. An agent that solves a multi-step problem, writes down the procedure, and reuses it next time is strictly cheaper and more consistent than one that re-reasons from scratch. It is also the design decision behind the fastest-growing open agent runtimes, which is a reasonable signal that it works.
Memory is an injection surface
Anything the agent writes and later reads as instruction is a persistence channel for a prompt injection. Content absorbed from an attacker-controlled source in one session and written to memory affects every later session that loads it — a compromise that outlives the run that caused it. Treat memory writes as untrusted on read, and never store credentials there: memories are replayed verbatim into future contexts.
6. Governance
“If a rule matters, it must be enforced in code that runs regardless of what the model decided. Anything expressed only as instruction is subject to negotiation.”
That is the whole layer in one sentence. A constraint written into the system prompt is a preference; the attacker, or simply an unusual situation, gets to make the closing argument. A constraint enforced at the tool-call boundary is a rule.
The four controls
Two of those have a failure mode worth naming. Least privilege is usually skipped because permissions are inherited rather than designed — the agent gets wired to an integration user that already had broad access and nobody narrowed it, because the demo runs either way. And a gate that fires constantly gets clicked through, which converts a control into a formality that produces a record implying review that did not happen.
Deciding where the boundary sits is its own problem, and it is more a question about the organisation than about the software — when should an agent ask covers the three signals worth gating on. For the adversarial view of the same layer, see the agent threat model.
7. Evidence
This layer is usually confused with logging, and the difference is the audience. Logs are for you, during debugging, and can be lossy. Evidence is for someone who was not there — a reviewer, an auditor, a customer asking why — and it has to be complete enough to answer a question nobody anticipated.
What a trace needs to contain
Enough to answer “why did it do that?” without re-running anything: the objective, each action with its arguments, the result returned, the reasoning that selected it, which authority rule allowed it, and who approved anything gated. The last two are what makes it an audit trail rather than a transcript.
Evaluation is a trajectory problem
Reviewing outputs is sufficient for a system that only produces text. For an agent it is not: the same final answer can be reached through an acceptable path or an alarming one, and only one of those should pass. You are evaluating the sequence of decisions, not the answer — which means your eval harness needs traces, which means this layer is a prerequisite for improving anything above it.
That dependency is the strongest practical argument for building the evidence layer early rather than last. Without traces you cannot build evals; without evals every change to the layers above is a guess.
What to build first
| Order | Build | Because it unblocks |
|---|---|---|
| 1 | Tool surface (layer 3) | Everything — governance, evidence and context all follow from what the agent can do |
| 2 | Bounds on the loop (layer 4) | Safe iteration; you can now run it repeatedly without an open-ended bill |
| 3 | Evidence (layer 7) | Evals, and any ability to tell whether a change helped |
| 4 | Governance (layer 6) | Production. This is the gate between a pilot and a customer |
| 5 | Context and memory (layers 2, 5) | Cost and consistency, once the shape is stable enough to optimise |
Layer one barely appears in that list, which is deliberate. Model choice feels like the first decision and is nearly the last one that matters — as long as you have kept it swappable, which costs almost nothing at the start and a rewrite later.
The through-line: the first five layers make an agent that works, and they are the ones with the most tooling, the most tutorials and the most vendor attention. The last two make an agent you can put in front of someone, and you will mostly have to build them yourself. That asymmetry is not a gap in the ecosystem — it is where the actual work is.
If you are deciding what to build on rather than what to build, the Agent SDK guide covers one harness in production detail, and the definitions piece is worth reading first if it is not yet settled whether you need an agent at all.
Frequently asked questions
What is in an AI agent tech stack?
Seven layers: a model layer that abstracts the provider, a context layer that decides what enters the window, a tool layer that defines the action surface, a control loop that bounds iteration, a memory layer for state that outlives a run, a governance layer that decides what may happen without a human, and an evidence layer that records what did happen.
How do I build an AI agent?
Pick a harness rather than writing the loop, define the narrowest tool surface the task needs, bound the loop with a step and cost ceiling, then add governance and evidence before it touches anything real. Building the first four layers gives you a working agent; the last two are what make it deployable.
Which layer should I build first?
The tool layer, because it determines everything downstream. The set of actions an agent can take decides what governance you need, what evidence you must capture, and how much context it takes to use them correctly. Teams that start with the model or the framework usually rebuild once the tool surface settles.
Do I need a framework to build an agent?
No, but you need a harness. The choice is whether to write the agent loop yourself or take one — the loop itself is not the hard part and not where the differentiation is. Frameworks help most in layers one through five and offer almost nothing for governance and evidence, which is where deployments actually fail.
What is the difference between the control loop and the governance layer?
The control loop decides when the agent stops — step budgets, cost ceilings, stop conditions. The governance layer decides what the agent is permitted to do at all, and which actions require a human first. One bounds how long it runs; the other bounds what it can change.
Why do agent projects fail at the last two layers?
Because layers one through five are what frameworks, tutorials and demos cover, and they are enough to produce something that works. Governance and evidence only become necessary when an action has consequences outside the model, which is exactly the point at which a pilot meets a real customer and gets paused.
Sources
- 01Building effective agents — Anthropic, 2024
- 02Agent SDK overview — Anthropic, 2026
- 03Model Context Protocol specification — Anthropic, 2026
- 04OWASP Top 10 for Large Language Model Applications — OWASP, 2025