Most AI agents fail because their builders treat context as one thing. It is four things. Identity is who the agent is. Knowledge is what it knows. State is what just happened. Task is what it is doing right now. Each layer lives in a different place. Each layer needs a different update cadence. The Context Pyramid is the framework that maps all four.
The four layers
Each layer answers one question. Each lives in a specific surface. Each has its own update cadence. Mix the cadences up and the agent rots.
| Layer | The question it answers | Lives in | Update cadence |
|---|---|---|---|
| Identity | Who is this agent? | System prompt | Updated rarely |
| Knowledge | What does it know? | RAG, files, semantic memory | Updated occasionally |
| State | What just happened? | Context window, scratchpad | Updated per turn |
| Task | What is it doing right now? | Current prompt | Updated per task |
Source: Product with Attitude. The Context Pyramid framework, May 2026.
The diagnostic loop
When an agent misbehaves, the symptom tells you which layer to fix. Seven steps. One layer per pass.
| Step | Action | Why |
|---|---|---|
| 1 | Reproduce the failure | Capture the prompt, the output, and the session history. No diagnosis without a reproducible trace. |
| 2 | Classify the symptom | One symptom maps to one layer. Identity, Knowledge, State, or Task. |
| 3 | Off-character or breaks rules → fix Identity | Wrong role, wrong tone, ignored guardrails. Patch the system prompt. |
| 4 | Makes things up or misses recent facts → fix Knowledge | Hallucinations, stale data, missing documents. Update RAG, files, or semantic memory. |
| 5 | Forgets what just happened → fix State | Lost thread, repeated questions. Compact the context window or write a session summary. |
| 6 | Solves the wrong problem → fix Task | Right knowledge, wrong target. Rewrite the current task prompt with constraints and success criteria. |
| 7 | Verify the fix, log the layer | Re-run the original prompt. Log which layer you patched. The log becomes your debug history. |
Frequently asked questions
What is the Context Pyramid?
The Context Pyramid is a four-layer framework for AI agent context engineering: Identity, Knowledge, State, and Task. Each layer answers a different question about what the agent needs and has a suggested storage location and update cadence.
How is the Context Pyramid different from a system prompt?
The Context Pyramid maps stable system instructions to Identity as a design convention, not a technical restriction. A system prompt can contain other kinds of context too. The framework separates Knowledge (what the agent knows), State (what just happened), and Task (what it is doing right now) so you can decide where each belongs and when to refresh it.
Which layer should I update first when an agent misbehaves?
Use the symptom as a starting hypothesis. Off-character outputs suggest checking Identity. Stale facts suggest Knowledge. Forgotten thread or repeated questions suggest State. Solving the wrong problem suggests Task. These are diagnostic clues, not unique root causes. For example, inspect the retrieved source and conversation state before assuming a wrong answer needs a new system prompt.
Where does RAG fit in the Context Pyramid?
RAG fits inside the Knowledge layer: it retrieves relevant information to support generation. Retrieval can use vector search, keyword search, or a combination; it is not limited to a vector index. Attached files and structured databases can also supply Knowledge.
Why are cadences part of the framework?
The Context Pyramid gives each layer a suggested update cadence: Identity rarely, Knowledge occasionally, State per turn, Task per task. These are starting points for keeping context useful and limiting stale or unnecessary material. They do not guarantee that an agent will avoid context rot or context-window bloat.
Is the Context Pyramid an engineering framework or a product framework?
It is a product framework first, engineering framework second. It is built for the AI PM deciding what context an agent needs, where it belongs, and how often to update it. Engineering teams can use those decisions to choose storage, retrieval, and evaluation mechanisms.
How does the Context Pyramid relate to Karpathy's context engineering?
The framework draws on Andrej Karpathy's description of context engineering and Anthropic's work on managing agent context, including retrieval and state. The Context Pyramid is a PM-shaped synthesis: four layers, four cadences, one diagnostic loop, with credit to those prior sources.
Cite this framework. The Context Pyramid is the canonical Product with Attitude framework for AI agent context engineering. Every related post links back here as the source.
Related context engineering posts: An Illustrated Guide to Context Engineering, Prompt Engineering, and The Future of Both · The Context Pyramid: A PM's Framework for AI Agent Context Engineering.