Module 057 min

Context and memory

The model has no memory. Everything it "knows" about your task was pasted in front of it a moment ago. Context engineering is deciding what gets pasted — and what gets thrown away.

After this module
  • You can budget a context window deliberately instead of stuffing it and hoping.
  • You can explain retrieval (RAG) and memory as two different mechanisms that are often confused.

Every call to a model is amnesiac. It does not remember the last call, your last message, or the document it read an hour ago. If something needs to be known, it must be in the context window — the fixed token budget sent with that request.

That budget has to hold everything: the instructions, the goal, the conversation so far, the tool definitions, retrieved documents, tool results, and enough room left for the answer. It is a budget in the real sense — spending it in one place means not spending it in another.

5.1Budget it like money

A sane starting allocation for a working agent looks roughly like this. Drag the window size and watch the tokens and the per-call input cost move.

Try it · context budget$3 / million input tokens
128k
System 5% Tools 10% Memory 25% History 20% Retrieved 30% Answer 10%
Context sent128k
Input cost / call$0.0000

Fixed percentages are a starting point, not a law. Long conversations should shrink history and grow summary; research tasks should grow retrieval and shrink everything else.

Two effects matter more than the exact split. First, cost scales with every iteration: an eight-step task re-sends the context eight times, so a fat context is not one bill, it's eight. Second, attention dilutes: the more irrelevant text surrounds the useful text, the more often the model latches onto the wrong thing. A million-token window is a capability, not a strategy.

The trap · stuffing

"Just put the whole knowledge base in the prompt" feels safe and is usually the most expensive way to be wrong. You pay for tokens you don't need, you slow every call down, and you bury the two paragraphs that mattered. Retrieve narrowly, then verify.

5.2Retrieval: fetching the right paragraphs

RAG (retrieval-augmented generation) is the standard answer to "the answer is in our documents, not in the model". The pipeline has two halves — one that runs when documents change, and one that runs on every question.

RETRIEVAL-AUGMENTED GENERATION INDEXING · OFFLINE Documents Chunk300–800 tokens Embedvector per chunk Vector index QUERYING · EVERY REQUEST Question Embed Top-k match Context+ sources k≈5 Model answers from those passages, with citations
Retrieval is a search problem wearing a language-model hat. Chunking and ranking decide quality; the model only writes up what it was handed. Bad retrieval cannot be fixed by a better model.

Where beginners get stuck:

5.3Three kinds of memory

Working memory
The current context window. Fast, complete, and gone the moment the task ends. Think: what's on the desk.
Episodic memory
A log of what happened: past conversations, past attempts, outcomes. Retrieved later by similarity or time. Think: the notebook.
Semantic memory
Facts and preferences distilled from many episodes — "this customer hates phone calls", "our fiscal year ends in March". Stored as structured records or a knowledge graph. Think: the file cabinet.
Procedural memory
How to do things: tool usage patterns, successful plans, playbooks. Often stored as prompts, examples or code rather than as text to retrieve. Think: the muscle.
# The three moves every memory system makes
write(memory, event)            # capture: cheap, append-only, timestamped
results = search(memory, query) # recall: similarity + recency + importance
context = select(results, budget) # inject: only what fits, with sources

Notice what memory is not: it is not fine-tuning, and it is not the model remembering. It is a database with a retrieval policy, plus a prompt that says "here is what you knew before". That framing keeps you honest about failure — if memory returns the wrong record, the model will confidently act on it.

5.4When the conversation outgrows the window

Long-running agents must compress. The reliable pattern is hierarchical: keep the last few turns verbatim, keep a rolling summary of everything older, and keep a structured task list that never gets summarized away because it's the actual state.

TechniqueWhat it doesWatch out for
Summarise old turnsReplace history with a compact recapDetails vanish — decisions and numbers get lost first
Extract, don't summarizePull decisions, IDs, constraints into a structured blockNeeds a schema; more work up front, far more reliable
Keep the task list outsideState lives in code/file, not in proseIf it drifts out of sync, the agent contradicts itself
Retrieve on demandOld context becomes searchable instead of residentAdds a step and latency; the agent must know to ask
Do this now · 3 minutes

Take the repetitive task you wrote down in module 01 and answer: what would the agent need to know at step one to do it well? Then mark each item S (stable instructions), T (this task's data) or M (memory from before). Anything you can't classify is probably something you'd be dumping in "just in case".

Check yourselfQuestion 1 of 1

Your RAG assistant gives vague, half-right answers. Which change is most likely to help?

60-second recap

  • No memory, only context. Whatever isn't sent in this request does not exist to the model.
  • Budget deliberately: system, tools, memory, history, retrieval, answer room. Cost and attention both scale with what you stuff in.
  • RAG is a search problem. Chunking, hybrid search, reranking and citations do the work; the model writes it up.
  • Memory is a database with a policy — write, recall, inject. Not fine-tuning, not magic.
  • Compress hierarchically when history grows: recent verbatim, older summarized, state kept structurally.