Skip to content
PHP2Shell: The PHP Bug
Akshay Thakare

Agent Memory: What an AI Agent Should Remember, and Where

Short-term context, long-term memory and task state are different problems. How to store each, keep context windows small, and avoid remembering the wrong things.

Agentic AIIntermediate3 min readUpdated 11 Oct 2026

Models are stateless

An LLM remembers nothing between calls. Everything an agent "knows" during a step is what you put into that step's context window. Memory is therefore an application design problem.

KindWhat it holdsLifetimeTypical storage
Working memoryCurrent conversation, recent tool resultsOne task or sessionThe prompt itself
Task statePlan, completed steps, intermediate resultsUntil the task finishesA database row or graph state
Long-term memoryUser preferences, facts learned, past outcomesAcross sessionsDatabase or vector store

Working memory: keep it small

Context windows are large but not free: long prompts cost more, run slower and can bury the important parts. Common techniques:

  • Keep the last few turns verbatim and summarize older turns.
  • Store large tool outputs outside the prompt and include only the relevant excerpt.
  • Drop tool results once the step that needed them is complete.

Task state: make it explicit

Don't rely on the model to remember where it is in a multi-step job. Persist state in code:

Python.py
state = {
    "goal": "Reconcile yesterday's payments",
    "steps_done": ["fetched_payments", "fetched_ledger"],
    "mismatches": [{"payment_id": "P102", "reason": "amount differs"}],
}

Frameworks such as LangGraph model this as a state object passed between nodes, with checkpoints so a failed run can resume instead of starting over.

Agent memory layersSwipe to explore →

Rendering agent memory layers

Each step builds its prompt from working memory, persisted task state and relevant long-term memories retrieved from storage.

Long-term memory: be selective

Write only durable, useful facts ("prefers SQL examples in Snowflake syntax"), not every message. Retrieve by relevance — often with embeddings — and include only a handful per step.

A simple rule

Put in the prompt only what the next step needs, persist everything required to resume, and remember long-term only what you would be comfortable showing the user.

Finished this note?

Share