How AI Agent Memory Actually Works
AI agent memory works in three layers. The context window holds short-term memory for the current session and vanishes when it closes. Persisted state saves structured facts between sessions. Retrieval-based long-term memory stores knowledge as searchable chunks the agent fetches on demand, which is the only layer that scales affordably.
The three layers of agent memory
People say an agent has memory as if memory were a single feature. It is not. There are three separate mechanisms, and confusing them is why so many agents feel forgetful or run up surprising bills.
The first layer is the context window: the working memory of the current conversation. The second is persisted state: a database or file where the agent writes facts it wants to survive a restart. The third is retrieval: a searchable store of knowledge the agent pulls from when a query needs it.
Each layer answers a different question. The context window answers what are we talking about right now. Persisted state answers what do I already know about this user or task. Retrieval answers where can I find the fact that would let me answer this. Good agent design assigns each responsibility to the layer built for it instead of forcing one layer to do all three jobs.
Short-term memory: the context window
The context window is everything you send the model in a single call: the system prompt, the conversation so far, tool outputs, and the current question. It is short-term memory because it exists only for that call. Close the session and it is gone.
It has two properties worth internalizing. It is precise, because the model attends directly to every token you place in it. And it is expensive, because you pay for every token on every call, and cost scales with how much history you carry forward. A long chat that replays its full transcript on each turn pays for the entire transcript again and again.
The context window is also finite. When the conversation outgrows the limit, something has to be dropped or summarized, and whatever you drop is forgotten. This is the core limitation people bump into: a bigger window buys you more short-term memory, but it never becomes long-term memory, and it gets linearly more expensive as you fill it.
Persisted state: memory between sessions
Persisted state is what most tutorials mean when they say add memory to your agent. The agent writes structured records to a store, a user profile, a list of preferences, a running summary, and reloads them at the start of the next session.
This is durable and cheap to read. It is the right tool for a bounded set of explicit facts: the user prefers metric units, the project deploys on Fridays, the last invoice number was 4471. You decide what to save, so you control exactly what survives.
That control is also the limitation. Persisted state only remembers what you told it to remember. It does not help when the agent needs open-ended knowledge it was never explicitly instructed to store, like the reasoning buried in a design document or the answer to a question nobody anticipated. For that you need a layer that can hold everything and find the relevant slice on demand.
Long-term memory: retrieval is how you scale
Retrieval-based long-term memory stores knowledge as chunks in a searchable index. When a query arrives, the agent searches the index, pulls the few chunks that matter, and places only those in the context window. The knowledge lives outside the prompt until the moment it is needed.
This is the layer that scales. A retrieval store can hold a million documents, and a query still only loads the handful of chunks relevant to it. Your per-call cost tracks what you retrieve, not what you know. Doubling the size of the knowledge base does not double the price of a question.
Why retrieval and the context window are partners, not rivals
Retrieval does not replace the context window. It feeds it. Retrieval decides which small subset of a huge knowledge base earns a place in short-term memory for this specific call. Think of the context window as a desk and retrieval as the filing cabinet. You do not make the desk bigger to hold more files. You keep the desk small and pull the right folder when you need it.
Why retrieval beats a bigger context window on cost
The tempting shortcut is to stuff everything into a large context window and skip retrieval entirely. It works until you see the bill. Every token in the window is priced on every call, so carrying a large knowledge dump in the prompt means paying to reprocess it each turn, whether or not the current question touches it.
Retrieval flips the economics. You pay to load only the chunks a query actually uses. More importantly, when the retrieved chunk already contains the answer, the model summarizes or quotes it instead of reasoning the answer from scratch. Recall is cheaper than regeneration. On RDK, private vault retrieval answering the bulk of queries this way drops token spend by 80 to 90 percent, because most answers are fetched rather than regenerated.
The mental model: a bigger context window makes short-term memory larger and more expensive. Retrieval makes long-term memory effectively unlimited while keeping per-call cost flat. Only one of those scales.
Where RDK fits: encrypted chunks as the long-term layer
RDK is the retrieval layer. You index files from a local vault, Obsidian notes, docs, code, as encrypted private chunks on the RDK network. Your agent searches those chunks before it queries an LLM, so it recalls what you already know instead of paying to regenerate it.
RDK calls the strategy stacked retrieval. Private vault retrieval answers 40 to 65 percent of queries. A public network of chunks that other builders have published adds another 15 to 20 percent. The LLM handles the remaining 5 to 10 percent as a fallback, for the genuinely novel questions no stored knowledge covers. Each layer catches what the one before it missed, and the LLM only earns its cost on the queries that truly need fresh reasoning.
This is what long-term agent memory looks like in practice: durable, searchable, priced by what you retrieve, and separated cleanly from the short-term context window. Persisted state still holds your explicit facts. The context window still does the reasoning. Retrieval carries the knowledge, which is the part nobody explains and the part that decides whether your agent is affordable at scale.
Frequently asked questions
- Is a larger context window the same as long-term memory?
- No. A larger context window gives an agent more short-term memory for the current session, but it still vanishes when the session ends and you pay for every token on every call. Long-term memory means knowledge that persists outside the prompt and is fetched on demand, which is what retrieval provides.
- What is the difference between persisted state and retrieval memory?
- Persisted state stores a bounded set of explicit facts you chose to save, like preferences or a running summary, and reloads them each session. Retrieval stores open-ended knowledge as searchable chunks and finds the relevant slice on demand. Use persisted state for known facts, retrieval for everything an agent might need to look up.
- Why does retrieval reduce token cost?
- Retrieval loads only the few chunks a query needs into the context window instead of the entire knowledge base, so per-call cost tracks what you retrieve, not what you know. When a retrieved chunk already holds the answer, the model quotes it rather than regenerating it. On RDK this drops token spend by 80 to 90 percent.
- Do I still need the context window if I use retrieval?
- Yes. Retrieval and the context window are partners. Retrieval decides which small subset of your knowledge earns a place in the window for a given call, and the model then reasons over exactly those chunks. The window stays small and cheap while the knowledge base behind it can be effectively unlimited.