Skip to content

10. Layer 5: Memory & State ​

Problem Solved ​

Context is finite, but tasks may require information across steps, sessions, or runs. Layer 5 separates what the model sees now from what the system remembers for later.

Service Provided to the Layer Above ​

Memory and state provide relevant information to the control loop without forcing every fact into the active context window.

Memory Tiers ​

TierLifetimeStorageExample
Short-term memoryCurrent runActive contextSystem prompt, recent steps, latest observation
Working stateCurrent taskHarness state storeCounters, current plan, file references, intermediate results
Long-term memoryAcross runsExternal database, vector index, key-value store (20)(21)User preferences, past task summaries, domain facts
Artifact storeCurrent or long-livedObject storage, filesystem, database blobsLarge documents, images, generated reports, datasets

The key distinction:

Context is what the model sees. Memory is what the system can choose to show the model.

The context window has a hidden cost. Transformer models compare every token in the context to every other token, and that work grows quadratically with length: the per-layer attention cost is O(n²·d), so doubling the context roughly quadruples the attention work (22). A long context is therefore not just expensive per token, each extra token also makes every other token costlier to process. Keeping the active context small and retrieving only what is needed is a direct latency and cost optimization, not just a budgeting choice.

State Keys ​

Large objects should not be inlined into context.

text
Bad:
  Paste a 50-page document into every model call.

Good:
  Store the document externally.
  Keep a reference key in state.
  Retrieve only the relevant section when needed.

State keys let tools and model decisions refer to objects without bloating the context window.

State itemContext representation
Large fileFile ID or path reference
Query resultResult ID plus short summary
Image or audioArtifact ID plus metadata
PlanCurrent step plus goal summary
Tool outputStructured summary plus reference to full output

Memory Operations ​

OperationPurpose
WriteStore a fact, artifact, or state update
RetrieveFetch relevant information for the current decision
SummarizeCompress long history into decisions and constraints (11)
SupersedeMark old state as replaced by newer state
ExpireRemove information that is no longer valid
VerifyCheck source, timestamp, or permission before use

Design Rules ​

  1. Do not treat the context window as a database.
  2. Retrieve only information relevant to the next decision.
  3. Keep timestamps and sources for stored facts.
  4. Separate raw artifacts from summaries.
  5. Make state updates deterministic and auditable.
  6. Mark superseded information instead of silently mixing old and new state.
  7. Protect sensitive memory with access controls.

Failure Modes ​

FailureSymptomFix
Stale memoryAgent acts on outdated factsTimestamps, supersession, refresh policies
Irrelevant retrievalContext fills with unrelated factsBetter retrieval scope, ranking, filters
Contradictory memoryOld and new facts conflictSupersede rules, explicit conflict resolution
Artifact bloatLarge objects consume contextState keys and on-demand retrieval
Memory leakState grows without expirationTTL, compression, archival
Trusting retrieved textRetrieved content is treated as instruction (23)Separate data from instructions, sanitize retrieved content