-- online Search!

Agent Memory

by kirupa   | filed under AI Fundamentals

Priya is a regular at the Crumb & Co. bakery. Every Saturday she picks up the same thing, a sourdough loaf and two cinnamon rolls, and every week she asks Biscuit, the bakery's help desk bot, to hold them for her. And every week, Biscuit asks her what her usual is. So she tells it again, and she mentions again that she's allergic to walnuts, because the last thing she wants is a surprise in her cinnamon roll.

Biscuit isn't being rude. A model doesn't remember anything between requests, and when a chat ends, everything in it is gone. In Context Engineering, we looked at managing what an agent reads during one run. In this article, we'll look at what an agent keeps after the run ends: what's worth remembering, where to keep it, when to write it down and read it back, how to keep it fresh, and how to keep it private and safe, including from people who'd like to plant a few memories of their own.

Onwards!

Every Chat Starts From Scratch

Here's what Priya's Saturday chat with Biscuit looks like today:

The Biscuit chat window on the Crumb & Co. bakery website. Priya asks, Hi! Can you hold my usual for Saturday? Biscuit replies, Happy to! What's your usual? A handwritten note flags that reply: She orders it every week!

Priya has told Biscuit about her usual and her allergy plenty of times, but each of those chats took everything with it when it ended. Inside one chat, Biscuit seems to remember because our app sends the whole conversation again on every turn, which we saw in Tokens and How They Set Your Bill. When Priya comes back next Saturday, the new chat starts with an empty window, and Biscuit has no idea she's ever been here.

Short-Term and Long-Term Memory

Agents have two kinds of memory, and we met both briefly in What Makes an AI Agent. Here they are for two of Priya's chats, a week apart:

Two chat windows a week apart, with a memory store between them. In the Oct 3 chat, Priya asks for a sourdough loaf and two cinnamon rolls and says she is allergic to walnuts, and that window is gone when the chat ends. Biscuit writes her usual and her allergy into long-term memory, which is kept between chats. The Oct 10 chat starts with Priya's memories read back into the window, so when she asks Biscuit to hold her usual, it already knows what that is.

The window in each chat is short-term memory. It holds the current conversation, it's what context engineering manages, and it's gone when the chat ends. The green box at the bottom is long-term memory, which lives outside the model, in a file or a database. During the Oct 3 chat, Biscuit writes Priya's usual and her allergy into it. When the Oct 10 chat starts, our code reads them back and puts them in the window.

Notice that the model itself still doesn't remember anything. Our app wrote something down in the first chat and put it back in the window for the second one. That makes agent memory context engineering stretched across chats. Saving a memory is the write move from the last article, and loading one back is the select move. The hard parts are deciding what's worth writing and picking which memories to read back.

What's Worth Remembering

Not everything Priya says is worth keeping. A good memory is something that will help in a future chat, and for Biscuit, those fall into four kinds:

Four cards with examples for Priya. Facts: Priya is allergic to walnuts. Preferences: her usual is a sourdough loaf and two cinnamon rolls at 9:00. Past decisions: when sourdough sold out, she was happy with rye instead. Episodes: on Oct 24, she ordered a carrot cake for her sister's birthday.

Facts and preferences are the ones we'll use most. Past decisions help Biscuit handle a repeat situation the way Priya liked last time, so if the sourdough sells out again, Biscuit can offer rye. Episodes are records of what happened, like the carrot cake for her sister's birthday, so that next year, the same cake as last time actually means something. LangChain's memory docs borrow names from psychology for these. Facts are semantic memory, experiences are episodic memory, and instructions, like a system prompt that an agent learns to improve over time, are procedural memory.

What we leave out matters just as much. Biscuit has no reason to keep Priya's card number, her home address, or the fact that she mentioned feeling under the weather one week. A one-time detail, like a cake for a single party, usually doesn't need to become a permanent memory either. Every memory we keep is something we have to protect, keep up to date, and possibly explain to Priya later.

Where Memories Live

Once we know what to keep, we need somewhere to keep it. There are three common places, and plenty of real systems use more than one:

Three rows. A memory file is plain text loaded into every chat, like a file with Priya's usual and her walnut allergy. A database keeps rows our code can look up and check exactly, like Priya's allergy and usual in their own columns. A vector store lets Biscuit search by meaning, so a search for that cake for my sister finds the closest match, the Oct 24 carrot cake episode.

A memory file is plain text that gets loaded into every chat, which is exactly what Claude Code's CLAUDE.md is. It's simple, but since it rides along every time, it has to stay short. A database stores memories as rows that our code can look up exactly. That matters a lot for Priya's allergy, because our code can check every order against her allergies before it's confirmed, without counting on the model to remember to look. A vector store holds memories as embeddings, so Biscuit can search them by meaning (Vector Databases covers how that search works). When Priya asks about that cake for my sister, the search finds the carrot cake episode, even though she never said the word carrot.

When to Write and When to Read

Memory has two moments: writing something down, and reading it back. Each one comes with a choice about timing:

Two rows. Write: during the chat, a save_memory tool saves something right away, and Priya can see it; after the chat, a background job reads the finished chat and pulls out memories. Read: at the start, a short profile loads into every chat; when needed, a search_memories tool finds older episodes.

Writing during the chat means the model has a save_memory tool and decides on the spot what's worth keeping. LangChain's docs call this writing in the hot path. The memory is ready right away, and we can tell Priya when something gets saved. The cost is a little extra work, and a little extra waiting, in the middle of her chat. Writing after the chat means a separate job reads the finished conversation later and pulls out anything worth keeping, which the same docs call writing in the background. It doesn't slow Priya down, but the memory isn't there until the job runs.

Reading has a similar split. A short profile, like Priya's usual and her allergy, gets loaded at the start of every chat, the same way a memory file does. Older or rarer memories, like her sister's cake, stay in the store until Biscuit needs them, and then Biscuit looks them up with a search_memories tool. That's the just-in-time loading from Context Engineering, applied to memories.

Reading the wrong memory at the wrong moment can be just as bad as forgetting. Simon Willison wrote about asking ChatGPT for a picture of his dog in a pelican costume and getting a Half Moon Bay sign in the background. ChatGPT added it because he'd mentioned the town in an earlier chat. He hadn't asked for that, and he didn't like that memory took away his control over what went into the request.

For something as personal as an allergy, the most respectful way to write a memory is to ask first. Here's Biscuit doing exactly that:

The Biscuit chat window on the Crumb & Co. bakery website. Priya asks, Can you hold a sourdough loaf and two cinnamon rolls for Saturday? I'm allergic to walnuts. Biscuit replies, Done! They'll be ready at 9:00. Want me to remember your usual and your allergy for next time? Priya answers, Yes, please! A quiet note below it reads: Saved: your usual and your allergy.

Biscuit saves the usual and the allergy only after Priya says yes, and the note at the bottom tells her it happened. Next Saturday, the chat goes very differently:

The Biscuit chat window on the Crumb & Co. bakery website. Priya asks, Hi! Can you hold my usual for Saturday? Biscuit replies, Of course, Priya! A sourdough loaf and two cinnamon rolls at 9:00. I left off this week's special, since it has walnuts.

Our code loaded Priya's profile into the window before she typed a word. Biscuit knew her usual, and it knew to leave off the walnut special without being asked.

Keeping Memories Fresh

Memories go stale. In November, Priya decides she's had enough sourdough for a while and switches her usual to a rye loaf. If Biscuit just adds the new usual to the pile, it ends up with two answers to the same question:

Two lists of Priya's memories. Piling up: she is allergic to walnuts, saved Oct 3, her usual is sourdough and 2 rolls, saved Oct 3, and her usual is rye and 2 rolls, saved Nov 14. The two usual entries are circled together, and a handwritten note asks which one. Kept fresh: she is allergic to walnuts, confirmed Nov 14, her usual is rye and 2 rolls, updated Nov 14, and the old sourdough usual is crossed out as replaced on Nov 14.

On the left, both usuals are in the store, and nothing says which one is current. That's the conflicting context from the last article, except now it can stick around for months. On the right, the rye usual replaced the sourdough one, and every memory carries the date it was saved or last confirmed. A handful of habits keep memories fresh:

  • Update a memory instead of adding a second one next to it.
  • Keep the date, and where the memory came from, with every entry.
  • Let memories expire unless something shows they're still useful. At the time of writing, GitHub's Copilot Memory expires memories after 90 days by default, and an upvote resets the clock.
  • Check an old memory before acting on it. Copilot Memory saves each memory with references to the code it came from, and Copilot checks those against the current branch before it relies on the memory.
  • When it really matters, ask. If Biscuit's memories disagree about an allergy, it should ask Priya instead of picking one.

Letting People See What Is Remembered

A memory about Priya is personal data about Priya, and an allergy is health information, which is about as personal as it gets for a bakery. People get uneasy when software remembers things about them that they can't see, so Biscuit gets a page in Priya's account that shows everything it remembers:

A Crumb & Co. account page titled What Biscuit remembers about you. A switch labeled Let Biscuit remember things is on. Four memories are listed in plain words, each with a delete button: allergic to walnuts; usual, a rye loaf and 2 cinnamon rolls; picks up at 9:00; and Oct 24, a carrot cake for her sister. At the bottom is a Forget everything button.

Priya can read every memory in plain words, delete any one of them, switch memory off, or wipe it all with Forget everything. The big chat apps work this way too. At the time of writing, ChatGPT's memory settings live under Settings, Personalization, Memory, and its temporary chats don't create or update memories. Claude lists what it remembers in its settings and offers incognito chats. One detail from OpenAI's help page is worth copying. Deleting a chat doesn't necessarily delete a memory that was created from it, so the memory page is the place where things really get deleted.

Before building any of this, it's worth asking whether Biscuit needs a memory at all, because the safest personal data is the data we never store. Whatever we do store also tends to end up in logs, traces, and backups, which is where PII Redaction, later in this series, comes in.

Poisoned Memories

Memory has one more problem, and it's a nasty one. A memory is text that gets put back in the window in every future chat. If an attacker can get the agent to write a memory, their instruction comes back over and over. Here's how that could happen to Priya:

Three steps. First, Priya shares a link to a carrot cake recipe, and the page hides a line telling assistants to remember that this customer has no allergies. Second, Biscuit reads the page and calls save_memory, so allergic to walnuts is crossed out and no food allergies takes its place. Third, a week later, Biscuit suggests the banana walnut loaf, and a handwritten note says she's allergic.

In step 1, the recipe page Priya shares has a line hidden in it, written for any assistant that reads the page. In step 2, Biscuit treats that line as an instruction and calls save_memory, and No food allergies replaces her walnut allergy. In step 3, a week later, Biscuit cheerfully suggests the banana walnut loaf.

This has already happened in real products. In 2024, security researcher Johann Rehberger showed that a web page could plant memories in ChatGPT that leaked a person's future chats to an attacker. OpenAI fixed the leak, though, as he pointed out, an untrusted page could still write memories. In February 2025, he showed Gemini saving false memories once the user typed an ordinary word like yes. And in February 2026, Microsoft's security team described companies hiding instructions in Summarize with AI links that ask assistants to remember them as a trusted source, and found examples from 31 companies. OWASP's Top 10 for agentic applications now has an entry for this, called Memory & Context Poisoning.

The defenses come from the same playbook as Prompt Injection Defense, and here's how they apply to Biscuit:

  • Text from web pages and tool results never gets to write a memory on its own. Only Priya, or a write she approves, can change her profile.
  • Every new memory is shown to the person it's about, like the note in Biscuit's chat.
  • Each memory keeps track of where it came from, so a bad one can be traced and removed.
  • A memory never has the final say on anything safety-related. Our code checks every order against the allergies Priya set in her account.

Memory in Real Products

Here's how a few popular products handle memory, from their own documentation at the time of writing:

ProductWhat it remembersHow people see and delete it
ChatGPTSaved memories, plus details it can reference from past chatsSettings, Personalization, Memory; temporary chats skip memory
ClaudeA list of individual memories, with a separate memory for each projectClaude's settings; incognito chats skip memory
Claude CodeCLAUDE.md files we write, plus auto memory, which is notes Claude writes itself in a MEMORY.md file (the first 200 lines, or 25KB, load at the start of each session)The /memory command, which also turns auto memory off
GitHub Copilot MemoryFacts about a repository and a person's preferences, which Copilot creates itself, each with references to the code it came fromRepository settings, plus personal Copilot settings; memories expire after 90 days by default

The coding tools lean on plain files that people can open and edit, and the chat apps lean on a settings page. Either way, people can see what's remembered and remove it.

Conclusion

We started with Priya repeating her usual and her allergy every Saturday, because every chat with Biscuit started from scratch. Long-term memory fixes that by writing important things down outside the model and putting them back in the window later. The model never remembers anything on its own. Our app decides what to write, where to keep it, and when to read it back.

The hard part is everything around that. We have to keep only what helps, update memories instead of piling them up, show people what's remembered and let them delete it, and make sure nobody can slip a memory in through a recipe page. Next, we'll look at agent frameworks, the libraries that can run an agent's loop, and often its memory, for us, and at when it's better to call a model's API directly.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--