How do agents remember things?
You'll be able to explain how agents keep memory across steps and sessions, and pick the right memory type for your product.
In plain English
A language model does not remember anything on its own. Each call starts fresh with only the text you send it. An agent's memory is whatever you put back into that text, or store outside the model and look up later.
Why it matters
Most agent bugs that feel like forgetfulness come from memory design, not model quality. If you know where memory lives, you can control cost, privacy, and accuracy. You also decide what the agent should forget.
How it works
Short-term memory is the conversation so far. It lives in the context window, the fixed amount of text the model can read in one call. Every turn, your code appends the new messages and sends the history again. When the history grows too long, you trim it or summarize older parts. Trimming is cheap but loses detail. Summaries keep the gist but can drop facts.
Working memory is the scratch space of a single task. The agent writes notes, plan steps, or tool results into the prompt, or keeps them in a structured state object that your code passes along. This memory is usually cleared when the task ends.
Long-term memory lasts across sessions. Your code writes facts, preferences, or past outcomes to a database or file, then retrieves the relevant ones before each call. Retrieval often uses embeddings and a vector database, with a similarity search picking the most related entries. Some systems also use keyword or hybrid search, which is often more reliable for names and IDs.
The model never writes to long-term memory directly unless your code allows it. You decide the write rules, the retrieval rules, and how old or wrong entries get updated or deleted.
A concrete example
A minimal pattern: keep recent turns in a list, and store durable facts in a dictionary that gets injected into each call.
MAX_TURNS = 20
history = [] # short-term: recent messages
facts = {} # long-term: saved facts (use a database in production)
def build_messages(user_input):
memory_text = "\n".join(f"{k}: {v}" for k, v in facts.items())
system = "You are a helpful assistant.\nKnown facts:\n" + memory_text
recent = history[-MAX_TURNS:]
return [{"role": "system", "content": system}] + recent + [
{"role": "user", "content": user_input}
]
def remember(key, value):
facts[key] = value
def record_turn(user_input, reply):
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": reply})A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Assuming the model remembers past sessions Store what matters outside the model and reload it at the start of each session.
- Letting history grow without limit Set a budget in tokens. Trim or summarize older turns before you reach the context window limit.
- Saving everything to long-term memory Write only durable, useful facts. Stale or wrong entries will keep misleading the agent, so add update and delete paths.
- Trusting similarity search alone Test retrieval with real queries. Combine semantic and keyword search when exact names or IDs matter.
Going deeper
Study context window management, retrieval-augmented generation (RAG), and how to build evals that check whether the agent recalls the right facts at the right time.
FAQ
Does a bigger context window solve memory?
It helps, but it does not solve it. Long inputs cost more, respond more slowly, and can bury important details. Retrieval of the relevant facts is usually more reliable than stuffing everything in.
Is long-term memory the same as fine-tuning?
No. Fine-tuning changes model weights and is slow and hard to undo. Long-term memory is stored data that your code reads and writes, so you can edit or delete it easily.
Where should I store agent memory?
Use a regular database for structured facts and a vector or hybrid search index for free-text recall. A plain file can work for prototypes.
How do I stop an agent from storing private data?
Filter what gets written, define retention periods, and let users view and delete their stored memories. Treat memory as personal data under your privacy obligations.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.