Skip to content
advanced

The Anatomy of Agent Context: Instructions, History, Knowledge, Tools, State, Memory, and Schemas

This is the debugging scene every agent builder eventually hits, and it usually ends the same way: you rewrite a sentence in the system prompt, the symptom…

Published 2026-09-11Updated 2026-09-1210 min read
Overhead view of a MacBook laptop on a dark desk, showcasing modern technology and minimalism.
Overhead view of a MacBook laptop on a dark desk, showcasing modern technology and minimalism. Photo by Nao Triponez on Pexels.

The agent did the wrong thing. You read the prompt. The prompt looks fine.

This is the debugging scene every agent builder eventually hits, and it usually ends the same way: you rewrite a sentence in the system prompt, the symptom moves somewhere else, and you have learned nothing. The failure was never in a sentence. It was in the assembled bundle — and you were reading one source while the model was reading seven.

Context is not a string you build. It is a set of sources you arbitrate.

Why "The Prompt" Is the Wrong Unit of Analysis

The document model of context is: I write instructions, the model reads them, the model acts. That model is correct for a single-turn, single-source task with no tools and no persistence. It breaks the moment any of those four constraints fails, and production agents break all four at once.

What replaces it is an assembly step. Every turn, your code selects material from several independent sources, serializes it into one token sequence, and hands it to the model. The model never sees your sources. It sees the concatenation. When behavior is wrong, the bug lives in the selection, the ordering, or the write path — not in the prose.

Two axes make the rest of this tractable:

  • Lifetime — does this source get rebuilt every turn, checkpointed per session, or persisted across sessions?
  • Ownership — who is allowed to write it: you, your runtime, or the model?

Keep one distinction straight before going further. The context window is a capacity owned by the model provider. Agent context is a selection owned by your code. A large window gives you room for context; it does not give you context. After instructions, tool schemas, and forty turns of history, the effective room for evidence is far smaller than the number on the pricing page.

Three Layers, Not Seven Siblings

A flowchart with three inputs—semantic content containing instructions, history, knowledge, tool output, and selected memory; runtime state containing plan, checkpoint, and memory store; and a serialization contract containing roles, tool schema, and output schema—converging on a context assembler and then a single model-visible request sequence.
The model receives a serialized request, not the underlying sources; debugging starts by tracing selection, state, and serialization separately.

The seven labels are useful as a checklist. They are misleading as an architecture, because they mix three different kinds of thing:

  • Semantic content — material selected to inform this specific model call: instructions, conversation history, retrieved knowledge, tool outputs, selected memory.
  • Runtime state — the application-owned data your assembler reads to decide what to include: plan, step counter, session checkpoint, memory store, tool registry.
  • Serialization contract — the schema that turns the selected material into a valid request: message roles, tool-call format, structured-output fields.

Schemas are not a seventh payload competing with the other six for budget. They are the contract that determines how the payload is expressed. Tools are not one source either: the tool schema is authored contract, and the tool output is untrusted observation. Keeping those layers separate is what lets you answer the only question that matters during a failure: which layer produced the tokens the model actually saw?

Knowledge check

Check your understanding

Answer this question before you continue.

An engineer says, “The tool schema is just another piece of retrieved knowledge competing for token budget.” Which correction best matches the article’s model?
Comparison Reasoning

Focus: Distinguish semantic content, runtime state, and the serialization contract when analyzing what an agent receives.

The Seven Sources of Agent Context

SourceLayerLifetimeWritten byTypical failure
InstructionsSemanticStable across turnsDeveloperSilently stale after a model or product change
Conversation historySemanticSessionRuntime + modelCrowds out everything else
KnowledgeSemanticTurnRetrieval systemFreshness tied to index, not conversation
Tool schemaContractStableDeveloperFormat drift breaks parsing silently
Tool outputSemanticTurnExternal systemUntrusted output treated as instruction
StateRuntimeSessionYour codeTwo writers, last write wins
MemoryRuntime + semanticCross-sessionModel or developerNever written, or never selected

Instructions are what you author: system prompt, policies, persona, few-shot examples, tool descriptions. They are stable across turns, which is exactly why they rot. A model upgrade or a product change can invalidate an instruction that still reads perfectly.

Conversation history is user turns, assistant turns, and prior tool results, appended in order. In the naive design it is append-only and unbounded, which makes it the source most likely to consume your budget by accident.

Knowledge is retrieved per turn: documents, snippets, tickets, code. Its freshness depends on the index, not on the conversation. A retrieval result can be perfectly relevant and six months out of date.

Tool schema is authored context you control. Tool output is untrusted context produced by an external system. Conflating them is a security bug, not a style issue.

State is the runtime scratchpad: plan, step counter, intermediate artifacts. Your code writes it. The model sees only what you choose to expose.

Memory is durable, cross-session material — user preferences, past decisions, procedural rules. It has its own write policy and its own retrieval problem, and it is the source most likely to be missing entirely.

Ownership: Who Is Allowed to Write Each Source

Three write classes, and the distinction matters more than the inventory:

  • Developer-authored: instructions, schemas. Reviewed by definition.
  • System-authored: state, tool results, retrieved knowledge. Written by code you control, but sourced from systems you do not.
  • Model-authored: memory writes, plan updates, self-summaries.

The dangerous class is not "model-authored" in isolation. It is any durable write whose inputs include model output, user text, tool output, or retrieved content. A code-mediated write that persists a model-suggested fact is just as exposed as a direct model write — the model still chose the content, and the content still came from somewhere untrusted.

The trust boundary is not the model. It is the write path. Any durable write influenced by model output or external content needs provenance, validation, and a version history before it becomes state.

I would rather ship an agent with no memory writes than one with unreviewed memory writes. The first is forgetful. The second is confidently wrong in a way that compounds.

One more distinction worth holding: a framework's memory object or state graph is an abstraction. The mechanism underneath is always a store, a selection function, and a write policy. When the abstraction misbehaves, you debug the three parts.

Knowledge check

Check your understanding

Answer this question before you continue.

A runtime validates the JSON shape of a model-suggested user preference and then persists it as memory. According to the article, what is still required before treating that memory as trusted state?
Scenario Interpretation

Focus: Evaluate the trust implications of durable writes influenced by model output or external content.

Freshness: Turn-Scoped, Session-Scoped, and Durable Context

Lifetime determines what you rebuild, what you checkpoint, and what you persist.

Turn-scoped sources — retrieved knowledge and tool outputs — are rebuilt or re-selected every turn. This is your main lever on token cost, and the place where caching tempts you most.

Session-scoped sources — conversation history and runtime state — get checkpointed so the agent can resume mid-trajectory without replaying the transcript. Exact checkpoint semantics depend on your runtime, so treat this as a mechanism to implement rather than a guarantee to assume.

Cross-session sources — memory and durable artifacts — are where most production agents quietly fail. Not because the store is broken, but because nothing writes to it, or nothing selects from it. An empty memory layer and a missing one produce identical behavior.

The freshness tradeoff is blunt: re-deriving context each turn is expensive but correct; caching it is cheap but silently wrong when the underlying store changes. A cached retrieval result that outlives the document it came from produces an agent that cites a deleted policy with total confidence.

Knowledge check

Check your understanding

Answer this question before you continue.

An agent continues citing a policy after that policy was deleted from the knowledge store. The retrieval result was cached across turns. What is the most direct diagnosis?
Debugging

Focus: Diagnose a freshness failure by relating a cached source to its underlying store and lifetime.

Precedence: Resolving Conflicts Without a Total Order

Most agent misbehavior is a precedence bug wearing a reasoning costume. The common collisions:

  • A retrieved document contradicts an instruction.
  • A tool error contradicts the plan.
  • A stale memory contradicts the current user turn.

There is no universal ranking that resolves these correctly. Authority is a property of your application's trust model, not of the token position. What you can do is make the resolution explicit:

  1. Classify each item by trust level in code: policy, developer instruction, user turn, validated observation, unvalidated observation, model-derived.
  2. Validate or normalize observations before they enter the bundle. A tool error is evidence, not an instruction.
  3. Label the item's role in the serialized message so the model can distinguish "here is a rule" from "here is what a document said."
  4. Serialize according to the runtime's message contract.

Recency and position are behavioral hazards to test, not precedence rules. Material near the end of the window often dominates generation, so a low-authority source placed last can outrank a high-authority source placed first. Concatenation order is a precedence decision whether or not you intended it as one.

If you cannot point to the line of code that decides a conflict, you do not have a precedence rule. You have an ordering accident.

When precedence is genuinely ambiguous, the better move is often to surface the conflict as a labeled conflict — "the retrieved policy says X, your instruction says Y" — rather than silently picking a winner. The model is frequently better at adjudicating a stated conflict than at recovering from a hidden one.

Knowledge check

Check your understanding

Answer this question before you continue.

A low-authority retrieved note is serialized last and the agent follows it instead of a developer instruction serialized first. Which conclusion is most accurate?
Misconception Check

Focus: Apply the article’s distinction between trust-based authority and accidental precedence caused by serialization order.

Instrumenting the Assembly Step

The mental model is only useful if it is observable. Log the assembled context per turn as structured records tagged by source, never as one flattened string. A flat string cannot attribute a failure.

Minimum useful fields per record:

{
  "turn": 14,
  "role": "user",
  "order": 3,
  "source": "knowledge",
  "source_id": "policy-2026-04",
  "version": "v7",
  "owner": "retrieval",
  "tokens": 1840,
  "written_at": "2026-05-27T09:14:02Z",
  "transformation": "retrieved",
  "inclusion_reason": "matched query: refund policy",
  "truncated": false,
  "dropped": false
}

role and order reconstruct the model-visible sequence. source_id and version let you diff against the store. transformation records whether the item was retrieved, summarized, or truncated. inclusion_reason is the only field that tells you why the assembler chose this item over the one you expected.

Token accounting per source exposes the real problem behind the marketing number. Instructions plus tool schemas plus history can consume most of the window before retrieval gets a turn, and you will not see it until you count.

A trace diff is the debugging surface. Same task, two consecutive turns: which sources changed, which stayed byte-identical. If history grew by 4,000 tokens and knowledge did not change, you know where the budget went.

One trap: compression and summarization are context transformations, not context sources. Record when history was summarized and what was dropped. Otherwise a later failure gets misattributed to the model when the summary ate the evidence.

Failure Modes and When This Decomposition Is Overkill

Amnesia. Durable context is never written, so every session starts from zero and the agent re-asks what it already knew.

Silent overwrite. Two writers touch the same state or memory key, the later write wins, and there is no version history to explain the change.

Context starvation. High-volume tool output or history crowds out the retrieved evidence the decision actually needed.

Cross-agent leakage. When multiple agents share a context layer, one agent's untrusted input becomes another agent's trusted instruction. Shared context creates a new trust boundary, and it is usually undocumented.

Now the honest boundary. The decomposition earns its complexity when sources have independent lifecycles, independent trust levels, or independent write paths — or when you need to diagnose a failure you cannot attribute. A single-turn, single-source, no-tool task does not need it. Neither does a many-source system where every source is immutable and one deterministic assembler governs the order. Source count is not the signal. Independent failure surfaces are.

The Next Move

Instrument one turn of your own agent. Dump the assembled context tagged by source, with role, order, token counts, and timestamps. Then answer three questions:

  1. Which source is largest?
  2. Which source was written by the model or influenced by untrusted input?
  3. Which source would win a conflict — and which line of code decides that?

The answers usually name the next fix. If the largest source is history, you have a compression problem. If a model-influenced source has no review step, you have a trust problem. If you cannot answer the third question, you have a precedence problem — and that one is worth fixing before you touch another sentence of the prompt.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

Two consecutive traces show the same task and unchanged knowledge, but conversation history grew by 4,000 tokens while the agent stopped using relevant evidence. What should the engineer investigate first?
Question 1 of 2Scenario Interpretation

Focus: Use structured assembly traces and diffs to attribute a context failure to a changing source.

Which system most clearly justifies the article’s full context decomposition?
Question 2 of 2Comparison Reasoning

Focus: Decide when decomposing context into independent sources is justified by independent failure surfaces.

References

  1. Context Engineeringwww.langchain.com
  2. Context is Key for Agent Securityarxiv.org
  3. Shared Agent Context: How We Are Tackling Partner Agent Collaboration | Microsoft Community Hubtechcommunity.microsoft.com
  4. What Is Agent Context? A 2026 Definition, Examples, and Pillar Guide | puppyonewww.puppyone.ai
8sources checked
8source domains
10searches run

Research updated Sep 11, 2026

Related sites

Build the foundations behind advanced AI systems

Use LearnLLMFast for practical LLM application foundations and LearnPyFast for the Python mechanisms that support implementation work.

LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast
Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast

Keep exploring

Related AI engineering tutorials

Continue with adjacent system layers, implementation patterns, and current AI engineering ideas.