Memory for AI Agents: Short-Term Context, Long-Term Memory and Compaction

How AI agents remember: the context window as working memory, summarisation and compaction, long-term memory stores, retrieval and what to forget.

Published December 1, 2026

Language models are stateless: every request starts fresh. Everything an agent "remembers" is something the application chose to put back into the prompt. Designing memory means deciding what stays in the context window, what gets summarised, what is stored outside for later retrieval, and what is safely forgotten.

The concept

Short-term memory is the context window itself: system instructions, the current conversation and recent tool results. It is fast and precise but limited and expensive, and quality can degrade when it is stuffed with irrelevant material. Long-running agents use compaction: when context grows past a threshold, older turns are summarised into a compact note and the raw history is dropped.

Long-term memory lives outside the model: files the agent writes (a NOTES.md or a progress log), key-value stores of user preferences, or vector stores of past interactions retrieved by similarity. The hard part is not storage but relevance: retrieving the right memory at the right time and keeping memories accurate as facts change.

Memory tiers

System promptstable instructions, persona, rulesWorking memoryrecent turns + tool resultsCompacted historysummaries of older turnsLong-term storenotes, preferences, past tasksKnowledge basedocs retrieved on demand (RAG)
Memory tiers from most immediate to most persistent.

Compaction

  1. Context reaches ~70-80% of the window
  2. Summarise older turns: decisions, facts, open questions
  3. Keep recent turns verbatim
  4. Replace old history with the summary
  5. Persist key facts to long-term memory if they matter later
A typical compaction cycle.

Long-term memory stores

StoreWhat to keepRetrieval
Notes file in the workspaceTask progress, decisions, TODOsAgent reads it at start
Key-value profileUser preferences, account factsLoaded by user ID
Vector storePast conversations, solutionsSimilarity search
Relational DBStructured facts (orders, tickets)Tool call / SQL

Practical examples

Compaction prompt

Preserve what matters for continuing the task.

Summarise the conversation so far for your future self. Keep:
- the user's goal and constraints
- decisions made and why
- files changed and their current state
- open questions and next steps
Drop: raw tool output, resolved dead ends. Max 300 words.

Agent-maintained progress file

Simple, transparent long-term memory for coding agents.

# PROGRESS.md
- [x] Added pagination to /api/orders (cursor-based)
- [ ] Update OpenAPI spec
Decisions: cursor = base64(created_at,id); page size max 100

Store and recall user preferences

Explicit, editable memory beats opaque embeddings for facts.

memory.set(user_id, "preferred_language", "TypeScript")
memory.set(user_id, "timezone", "Asia/Kolkata")
profile = memory.get_all(user_id)          # injected into the system prompt

Common mistakes

  • Stuffing the entire history into every request until quality and cost degrade.
  • Summaries that drop decisions or constraints, so the agent repeats mistakes.
  • Storing everything in a vector DB and retrieving irrelevant memories.
  • No way for users to see or delete what the agent remembers.

Key takeaways

  • Models are stateless; memory is what you put back into context.
  • Compact long histories into structured summaries.
  • Use explicit stores for facts and similarity search for fuzzy recall.
  • Make memory inspectable and deletable.

FAQ

Is a bigger context window a substitute for memory design?

No. Larger windows help, but cost, latency and attention to relevant details still favour curated context.

Should memory be per user or per task?

Usually both: user-level preferences and task-level progress notes.

How do I handle outdated memories?

Timestamp them, prefer newer facts, and let users or the agent overwrite entries explicitly.

More AI articles