DEV Community

Frank Guan for Google AI

Posted on Edited on

Why Your AI Agent Keeps Forgetting (and Getting Slower): 4 Memory Options and 3 Context Fixes

Comments highlight the New York to SF test

Most AI agents have two memory problems. They forget what a user told them last week, and every turn costs more than the one before.

Both come from the same place: how much of the conversation you send to the model, and what you keep between sessions.

In this episode of AI Agent Clinic, Google Cloud engineer Laxmi Harikumar and developer Miles Libbey fix both in Scribant, a voice to action agent Miles built in TypeScript with Agent Development Kit (ADK).

Which memory option should you use?

The episode compares 4 ways to give an agent memory, using one test. A user says "I'm in New York", later says "I moved to San Francisco", then asks "Where do I live now?"

Option What the model gets Tradeoff
Flat prompting The full chat history Easy to build, but tokens grow every turn
Vector RAG Every chunk that matches, old and new Saves tokens, but retrieval alone doesn't say which fact is current
Knowledge graph The current fact Tracks changes, but you build the extraction, schemas and update logic
Memory Bank in Gemini platform The current fact Extracts, deduplicates and updates facts for you in the background

RAG is still the right tool for searching a knowledge base. Memory is a different job: keeping facts about a user current as they change.

How do you keep context small in a streaming agent?

Scribant gets new transcript text every 2 seconds, so resending everything adds up fast. The episode adds 3 fixes, each for a different time span:

  1. Rolling window (this turn): the intent classifier reads only the last 40 words.
  2. Rolling summary (this session): a running summary stands in for everything said so far.
  3. Memory Bank (across sessions): only the extracted facts are stored and recalled later.

What did the demo measure?

On one short test conversation in the episode (about 200 spoken words):

  • The 40 word window used about 86% fewer classifier tokens than sending the whole speech stream.
  • The rolling summary came to 92 tokens.
  • Memory Bank stored about 24 tokens of facts, where ingesting the whole conversation would have taken about 1,400.

These are results from one demo, not benchmarks. Treat 40 words as a starting point and tune it to how your users talk. The token tracking code is in the repo (token-telemetry.ts) if you want to measure your own agent.

Jump to a section

  • 0:00 Intro
  • 1:51 Live demo: spoken thoughts become calendar events and tasks
  • 2:44 Architecture: the 3 stage ADK pipeline and the execution gate
  • 4:21 Memory options: flat prompting, RAG and knowledge graphs
  • 5:56 Wiring up Memory Bank in ADK
  • 9:01 The 40 word rolling window
  • 12:07 Dashboard results
  • 14:41 Takeaways

Code and docs

How do you handle memory in your own agents: full history, RAG, or something else? Tell us in the comments.

Top comments (19)

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

The memory-versus-retrieval split is the load-bearing distinction — RAG finds facts, but only a memory layer can say which fact is current, and the New York-to-San Francisco test makes that concrete. The 24 tokens of stored facts versus 1,400 for the whole conversation is the number that sells it. What happens when Memory Bank extracts a wrong fact — is there a correction path, or does the stale fact just keep winning until someone notices?

Collapse
 
izgorodin profile image
Edward Izgorodin •

The New York to San Francisco test checks one thing: that the newest value wins. Two more lines in the same script catch the stores that pass it for the wrong reason. After the move, ask "Where did I live before San Francisco?": a store that overwrote the fact has nothing to say, while one that kept both facts, with the date the first one stopped being true, answers New York. Then say "I am moving to Seattle in March" and ask "Where do I live now?": an updater that treats the latest statement as the current fact answers Seattle months early. The first line tests history, the second tests the difference between when a fact was said and when it becomes true.

Collapse
 
ayushdhanani369 profile image
Ayush Dhanani •

I use something else, and your RAG point is why. I build Aladdin, a job agent. A fresh worker reads the actual form for each application, instead of carrying a long chat history into it. It only uses facts from the user's own profile that are clearly known. If a question is reworded or the answer is not clear, it asks the user instead of reusing an old answer. That keeps a corrected fact from coming back as a stale one.

Collapse
 
ba796 profile image
brent alexis •

Interesting to see the rolling summary in there, because I live with it from the user side. I run my side project with an AI assistant, and when a long session runs out of room it gets summarized and carries on. That keeps the work going, but small details can drop out - an exact number, or a rule I gave it hours earlier.

What helped me was keeping my standing rules in a separate notes file that gets read at the start of every session. Things like "I do all the commits myself" or "check the live page after every push". They never live in the summary, so they can't get squashed. Pretty much your Memory Bank idea, just done by hand.

Did the demo check what the rolling summary leaves out after a long session, not just how small it gets?

This reply was drafted with AI help, edited by me.

Collapse
 
nexusfoundry profile image
Nexus Foundry •

The "NY then SF then where do I live" test is the right minimal case, and it exposes the thing retrieval-based memory can't fix: recency is a property of the fact, not the chunk. Vector search will happily return both "I'm in New York" and "I moved to San Francisco" with nearly identical scores, and something downstream has to decide which wins. RAG answers "what's similar," memory answers "what's current" — your table names that split correctly.

Two additions from running agents that deal with this daily:

  1. Whatever you store, keep valid_from/superseded_by on the fact itself, not just timestamps on the chunk. When the SF fact lands, the NY fact should be marked superseded at write time — otherwise you're relying on the model to notice the contradiction at read time, which is exactly the failure you're trying to fix.

  2. The rolling-summary fix has a subtle failure mode worth naming: summaries are lossy compaction of facts you didn't know were facts. "User prefers window seats" and "user is anxious about flying" compress identically into a summary token budget, but only one is a durable preference. If the summary is the only cross-session artifact, durable-but-quiet facts die first. That's the argument for Memory Bank-style extraction being a separate pipeline rather than a bigger window.

The 86% token reduction from a 40-word classifier window is real but I'd guess it doesn't survive contact with multi-intent utterances — "remind me to call mom and also what's the weather" needs more than the last 40 words of speech, though maybe not of text. Voice makes the window problem worse than it needs to be.

Collapse
 
mudassirworks profile image
Mudassir Khan •

the 40 word window at 86% fewer tokens is believable for an intent classifier, but i would want to know what it did to misclassification on turns where the intent started earlier than the window. in our streaming setups the cheap classifier was never the problem, the missed context was, so we logged every low confidence call next to the full transcript and tuned the window on real misses instead of a guess.

the 92 token rolling summary worries me a little too, since summaries quietly drop corrections like the New York to San Francisco move. did you test whether the summary and Memory Bank ever disagree on the current fact?

Collapse
 
saifuddin profile image
Saifuddin Ahmed •

Great breakdown, especially the line between retrieval and keeping a fact current. One thing I'd add from my work on speech for Sudanese Arabic: in a voice-to-action agent, memory is only as clean as the transcript feeding it. In our Whisper evaluation, text normalisation alone moved word error rate by about 11 to 13 points, mostly spelling variants of the same word. If a city name arrives in two or three spellings across sessions, Memory Bank may store them as separate facts or fail to replace the old one. And a 40-word window is not the same amount of meaning in every language: Arabic attaches pronouns and prepositions to the word itself. Did you try normalising transcripts before extraction, or sizing the window in tokens instead of words? Curious how Scribant handles code-switched speech.

Collapse
 
yonijd profile image
jonatan draganczuk •

I went through this in two stages.

First, with Gemini:

At first I used Gemini thinking its memory was very useful. Then I realized they didn't know how to implement it. It used it as context in almost everything, even when I didn't ask it to check memory. That made it slow for no reason. And when I really needed it, it didn't have indexed or tagged the section I wanted to locate. So, what's the point?

That's why I designed my own memory system for Gemini. A simple one, real and working, because the idea was different: not loading all the memory all the time, but going to fetch only what was tagged when it was needed. I'm going to tell that in detail in an article I'm preparing.

Then, with Waio:

The last straw was when I wanted to use Gemini to organize the publication of Waio, a local orchestrator I'm building for web AI. It was never aware of the project updates, or it didn't relate them. It talked about old versions as if they were current. For all that, I stopped using Gemini.

And that's why I designed a simple one for Waio, which worked 10 times better than Gemini's. But I left it halfway, because it's still under study. It requires more research, and implementing improvements in my program that I still don't know how to do.

The funny thing is that now with Meta AI the opposite happens to me: it only goes to memory when I ask it to, and it seems to work.

For me the key is not remembering everything. It's knowing when NOT to use memory, and when you do use it, finding the right thing fast

Collapse
 
digbijoylabs profile image
digbijoylabs •

One pattern that fixed both the forgetting and the latency creep for us was strictly separating conversational history from execution state.

Once you wire up 3+ tool servers, verbose JSON parameter schemas and raw tool stdout quietly eat half the attention budget before the model even plans the next step. Stripping uncalled tool schemas between turns and keeping only a compact typed state object (rather than replaying raw stdout every turn) cut token growth dramatically and stopped mid-workflow instruction drift.

Collapse
 
shamazkhan profile image
Shamaz Khan •

I've experienced a similar problem with AI coding agents, particularly after context compaction. Architectural decisions and engineering constraints gradually disappear, even though they haven't changed.

I wonder if we should distinguish between conversational memory and engineering policy. Memory can be compressed and updated, but certain constraints should persist independently and be enforced rather than simply remembered.

I've been exploring this through Stet, a small open-source experiment.

Do you see persistent policy enforcement becoming a separate architectural layer alongside memory and context management?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.