AI Agents Forget Between Runs: Causes & Fixes
In this article
AI agents forget between runs because their memory is not persistent by default—each run starts with a fresh context window, and any information not explicitly saved externally is lost. The fix is to implement a memory layer that stores key facts, conversation summaries, and state in an external database or vector store, then reloads it into the prompt at the start of each new run. This article explains why the amnesia happens, the different memory types, and practical ways to make an agent remember.
Why AI Agents Lose Context
Large language models (LLMs) are stateless. When you send a prompt, the model sees only the text in that prompt—it has no built-in recollection of previous conversations or tasks. The context window (e.g., 8k, 32k, or 128k tokens) is a temporary buffer: once the run ends, that buffer is discarded. So if an agent needs to remember a user's name, a project preference, or a previous decision, that information must be stored somewhere outside the model and injected back into the prompt on the next run.
This is not a bug; it's the architecture of transformer-based models. The illusion of memory you get in a single chat session comes from the application appending the entire conversation history to each new request. Between separate runs (e.g., a scheduled job, a new user session, or a multi-step workflow), that history is gone unless the developer explicitly persists it.
Types of Memory in AI Agents
Not all memory is the same. To fix forgetting, you need to know which kind of memory you're dealing with:
- Short-term memory: The current context window. It holds the conversation and any tool outputs during a single run. It's fast but volatile.
- Long-term memory: Persistent storage (database, vector store, file) that survives between runs. This is what you add to fix forgetting.
- Episodic memory: A record of past actions and outcomes—"last time we did X, the result was Y." Useful for learning from experience.
- Semantic memory: Facts about the world or the user—"the user prefers metric units." Usually stored as key-value pairs or embeddings.
- Procedural memory: The agent's own instructions and skills—"how to run a SQL query." Often hardcoded in the system prompt.
Most "forgetting" issues involve missing long-term or episodic memory. The agent doesn't remember because no storage mechanism was implemented.
How to Make an Agent Remember
There are several established patterns to give agents persistent memory. The right one depends on your use case and budget.
- Store conversation summaries: At the end of each run, ask the LLM to generate a short summary of what happened and store it in a database. On the next run, load the summary into the system prompt. This is cheap and works for most chat-style agents.
- Use a vector database for semantic recall: Embed important facts, user messages, or documents, and store the vectors. When a new run starts, query the vector store for relevant chunks and inject them into the prompt. This is good for agents that need to retrieve specific past details.
- Maintain a key-value memory store: For simple facts (user name, preferences, task status), use a JSON file or a simple database. The agent can write to it via a tool call (e.g.,
save_memory(key, value)) and read from it at startup. - Use a dedicated memory framework: Tools like LangChain's memory modules or MemGPT (now Letta) provide ready-made solutions. They handle summarization, retrieval, and injection automatically.
- Persist the entire conversation history: For short-lived tasks, you can store every message in a database and reload the full history. This is simple but quickly hits token limits and costs more.
Whichever you choose, the key is to make memory an explicit part of the agent's workflow: a write step at the end of each run, and a read step at the beginning.
Common Memory Mistakes
Even with a memory system, agents can still forget or misremember. Here are frequent pitfalls:
- Not updating memory: The agent writes a fact once but never updates it when the user changes their mind. Always include an update mechanism.
- Storing too much: Loading a huge history into the prompt dilutes attention and increases cost. Summarize or retrieve only what's relevant.
- Ignoring context window limits: Even a good memory system can overflow the context. Keep summaries short and use retrieval to pull only needed chunks.
- No memory invalidation: Facts become stale. Add timestamps and a way to forget or overwrite old data.
- Mixing up users: If you have multiple users, make sure memory is namespaced by user ID. Otherwise, one user's data leaks into another's context.
A common misconception is that the model itself can be trained to remember. You cannot fine-tune a model to have persistent memory—fine-tuning changes weights, but the model still has no state between calls. Memory must always be external.
Cost and Performance Trade-offs
Adding memory increases latency and cost. Every time you inject a summary or retrieved chunks, you're using more input tokens. For high-volume applications, this adds up. On the other hand, forgetting can make an agent useless, so the trade-off is often worth it.
To keep costs down:
- Use short summaries instead of full transcripts.
- Retrieve only the top 3–5 relevant chunks from a vector store.
- Cache the system prompt and static memory to avoid re-sending it.
- For simple facts, use a key-value store that costs nothing to read.
Performance also matters. A vector search adds a few milliseconds, which is usually fine. But if your agent runs in real-time (e.g., a chatbot), you need to balance memory depth with response speed. Start with a simple summary and expand only if the agent's performance requires it.
Alternatives to Full Memory
Sometimes you don't need persistent memory at all. Consider these alternatives:
- Stateless agents: If the task is one-shot (e.g., "classify this email"), no memory is needed. The prompt contains all necessary context.
- External state files: Instead of the agent remembering, store state in a file or database that another system manages. The agent reads the file at start and writes at the end.
- Human-in-the-loop: For critical tasks, ask the user to confirm or re-enter information. This avoids the complexity of memory but adds friction.
- Re-running with new context: For batch jobs, you can re-fetch data from a source system (e.g., a CRM) rather than relying on the agent's memory.
These approaches are simpler and more reliable when the agent doesn't need to learn from past interactions. Use memory only when the agent's value depends on continuity.
Frequently Asked Questions
Can I make an AI agent remember without a database?
Not really. The model itself is stateless. You could store memory in a file or even in the agent's own code (e.g., a JSON file), but that's still external storage. The only way to have memory is to persist data somewhere outside the model and reload it.
How long does an agent's memory last after a run?
It lasts exactly as long as you store it. If you save a summary to a database, it can last indefinitely. If you don't save anything, it's gone the moment the run ends. There's no built-in retention.
Does increasing the context window fix forgetting?
No. A larger context window only lets you fit more in a single run. Between runs, the context is still cleared. You'd have to re-send the entire history each time, which is inefficient and still limited by the window size.
What's the cheapest way to add memory?
Storing a short text summary at the end of each run and injecting it into the system prompt at the start of the next run. This uses minimal tokens and requires only a simple key-value store or even a text file.
How do I prevent memory from becoming outdated?
Add a timestamp to each memory entry and have the agent overwrite or delete entries when new information contradicts them. You can also implement a decay mechanism that removes memories older than a certain age.
Is memory the same as fine-tuning?
No. Fine-tuning changes the model's weights permanently, but it doesn't give the model access to new facts or conversation history. Memory is external data that is injected into the prompt at runtime. They serve different purposes.