agent idle
Re:Invent 2026

Managing context &
memory
for AI agents

Storage, Retrieval and Lifecycle

An agent without memory

MeI’m allergic to gluten.
AgentI’ll keep that in mind.
NEXT PROMPT · memory: 0 items
MeSuggest some dinner ideas.
AgentHere’s a creamy carbonara pasta recipe…
BUT PASTA HAS GLUTEN!

Without
memory,

a goldfish.
Each LLM call starts from scratch.
The LLM doesn’t have any context about you.
A goldfish in a round glass bowl
Fun fact: goldfish actually remember for up to 3 months.

Just send the whole conversation?

Prompt 1
MeI’m allergic to gluten.
7 tok
Prompt 2
MeI’m allergic to gluten.
AgentI’ll keep that in mind.
MeDinner ideas?
21 tok
Prompt 3
MeI’m allergic to gluten.
AgentI’ll keep that in mind.
MeDinner ideas?
AgentGluten-free risotto?
MeAdd it to the cart.
38 tok
LLM
Every prompt resends the entire conversation. And it grows with every turn.

It works fine.

Until it doesn’t.
LLM context is limited

You’ll run out of space

context 7,912 / 8,000
What about past chats?

Most of them aren’t relevant to the current chat

214 past chats · 3 relevant
Things change

Love pasta but now shouldn’t eat it

2 facts in conflict

3 problems we’ll solve today

1
What’s worth remembering?
2
How do we find the right memory at the right time?
3
How do we update and forget?

Scan to access
the slides

QR code to access the slides

Meet the grocery shopping agent

Create a shopping list for a quick dinner tonight.
Two quick options: pasta with basil sauce, or stir-fry rice noodles. Which one should I add?
The pasta. What’s my total?
Added 2 items. Your cart total is €9.20.

What it can do

1Plan a meal (reason)plan_meal()
2Find products by meaningsearch_products() · $vectorSearch
3Fill the cartadd_to_cart()

Before we start: the tools

The agent (code + Bedrock)
Grocery Shopping Agent
Atlas Agent Engine
Runs your agents
Gives them memory
Governed & secure
MongoDB AtlasMongoDB in the cloud
DocumentsFlexible, nested data
MongoDB SearchFinds by keywords
Vector SearchFinds by meaning

Agent Engine memory

Saves each turn

Every message in the conversation is recorded.

record_turn()
→
Decides what to keep

Durable facts become long-term memories.

extraction (async)
→
Helps find them

The right memories reach your agent when it needs them.

build_context()
All memories live in your MongoDB database in the cloud.

How the demo fits together

AWS BedrockThe agent framework
Grocery Shopping Agent: agent loop, tools, session, 3 callbacks
Atlas Agent EngineThe memory service
Called from the callbacks
record_turnSaves each turn
ExtractionDecides what to keep
build_contextFinds what’s relevant
MongoDB AtlasYour own cluster
MemoriesWritten by Agent Engine
ProductsSearched by agent tools
Carts & plansWritten by agent tools
01

What’s worth
remembering?

3 problems we’ll tackle today

1
What’s worth remembering?

Decide what becomes a long-term memory, and what doesn’t.

2
How do we find the right memory at the right time?

Pick the few memories that matter for this request.

3
How do we update and forget?

Keep memories current, and let go of what’s stale.

Quick poll: what should the agent remember?

fact_01“I like quick meals.”
fact_02“I shop for two.”
fact_03“I’m hosting friends on Saturday.”
fact_04“I need lemons tonight.”
fact_05“I just bought a blender.”
fact_06“I’m allergic to gluten.”
You’re the extractor.For each one: ALWAYSYES, USEFULA FEW DAYSNO

You decided how long each one should live

“I’m allergic to gluten.”
“I like quick meals.”
“I shop for two.”
“I’m hosting friends on Saturday.”
“I need lemons tonight.”
“I just bought a blender.”
Alwaysttl ∞
Yes, usefuluntil updated
For a few daysTTL index
Nonot stored
You just did what a memory system has to do:
You decided what kind of memory each one istype, and how long it should livettl.
How do you teach an agent to do the same?
you · 600 humans→extractor · LLM + rules

4 types of long-term memory

Episodictype: "episodic"Things that happened:

“Yesterday I rated matcha with 1 star.”

Semantictype: "semantic"Facts and preferences:

“I’m allergic to gluten.” (direct)
“Prefers oat milk.” (generalized)

Proceduraltype: "procedural"How to do a task:

“Deliver on Saturday mornings, never on weekdays.”

Taxonomictype: "taxonomic"What terms mean:

“‘The usual order’ means bread, eggs and bananas.”

Short-term memory is the current conversation. “Lemons tonight”, “I bought a blender” remain there.

Select what to memorize

1
Durable?

Will it still be true next week?

2
Relevant?

Does this agent need it?

3
Confident?

How sure are we?

4
Stated or guessed?

Did the user say it?

“I’m allergic to gluten.”
STATED
durable ✓relevant ✓conf 0.98stated
“She probably doesn’t like matcha.”
GUESSED
durable ?relevant ✓conf 0.41inferred

Agent Engine implementation

In the background, by Agent Engine
record_turn
→
Snapshot
→
Extract
→
Store

You record each turn as short-term memoryrole · content · session_id

After N messages, or when the chat goes idleN=6 · idle 30s

Categorize with an LLMtype · stated · conf · ttl

Long-term memories in MongoDB, with vector embeddings1,024 dims · $vectorSearch

Asynchronous: the user never waits for memory to be persisted.
SECTION TAKEAWAY
Remember by type and lifetime, not by default.

Classify each piece of information by type, keep what’s durable and relevant, and know what was stated and what was guessed.

02

The right memory
at the right time

3 problems we’ll tackle today

1
What’s worth remembering?

Decide what becomes a long-term memory, and what doesn’t.

2
How do we find the right memory at the right time?

Pick the few memories that matter for this request.

3
How do we update and forget?

Keep memories current, and let go of what’s stale.

Every conversation adds memories

Week 18
Returned the yogurtOrdered lemonsBought bananasHosting friends Saturday Asked for couponsTried kombuchaLikes spicy foodAsked about wine
Month 140
Month 3150
Month 6400+

Send them all?

ME“Create a shopping list for a quick dinner tonight.”
More tokens, more cost
More tokens, slower answers
More noise, worse answers
Only 4 of these matter for dinner.
The allergy didn’t even fit.
Context limit

Keyword search vs. semantic search

Me: “Add something to drink for the weekend.”
IN MEMORY
Delivery on Saturday mornings

Keyword search

“weekend”: no match

Nothing found

Semantic search

Delivery on Saturday mornings → weekend

Match!

Vector embeddings capture meaning

A long array of numbers that captures what the text means.
“weekend”
→
[0.12, -0.48, 0.91, 0.07, -0.33, 0.64, …]
↑ each number is one dimension of meaning1,024 in total →

Vector embeddings capture meaning

Similar terms have vectors that are positioned close together in the vector space.
● “weekend” [0.12, -0.48, 0.91, …] ● “Saturday” [0.14, -0.45, 0.88, …] ● “Sunday” [0.10, -0.51, 0.86, …] ● “end of the week” [0.17, -0.42, 0.84, …]
MEANING SPACE
close = similar meaning “weekend” Saturday Sunday end of the week matcha wet grass dirt

Semantic search finds the dinner memories

Me: “Create a shopping list for a quick dinner tonight.”
“I like quick meals.” quick dinner ≈ quick meals · 0.86
The 1-star matcha review 0.31
“I loved the 20-minute stir-fry.” a fast dinner · 0.81
Olive oil from 3 months ago 0.44
“I shop for two.” dinner portions · 0.77
Deliver on Saturday mornings 0.29
Gluten allergy 0.12
Found limit: 3
Left out below threshold

Where’s the allergy?

MEMORY“I’m allergic to gluten.”
≠cosine 0.12
REQUEST“Quick dinner tonight.”
No shared words. No similar meaning.
To a search engine, these two have nothing in common.
Relevance is
not importance.

Pin the constraints

Fixed constraints search“Allergies, intolerances and dietary restrictions” find({type: "constraint"}) · runs on every request
→
THE PROMPT
📌 PINNED12 tokConstraints (always apply)
Allergic to gluten
96 tokRelevant memories from search
14 tokThe request

Assemble the context deliberately

30memories retrieved
30that matter
More tokens = more cost
More tokens = slower answers
More noise = worse answers
Rankby score
Remove duplicates30 → 19
Fit the token budget19 → 2

In Agent Engine: build_context does all three.

A tight budget can cut the allergy. Pinned constraints shouldn’t compete for space.
SECTION TAKEAWAY
Search finds what’s relevant, not always what’s important.

Retrieve semantically and pin the constraints that must apply to every request.

03

Update
and forget

3 problems we’ll tackle today

1
What’s worth remembering?

Decide what becomes a long-term memory, and what doesn’t.

2
How do we find the right memory at the right time?

Pick the few memories that matter for this request.

3
How do we update and forget?

Keep memories current, and let go of what’s stale.

What’s the truth?

Before
“I love pasta! Carbonara, lasagne, all of it.”
Now
“I can’t eat gluten anymore.”

Agent memory

Loves pasta.semantic · v1 · is_latest: true
Can’t eat gluten.semantic · v1 · is_latest: true
Should it suggest carbonara for dinner?
Life changes
A year ago: “I like quick meals.”
Now: “I’ve got time while waiting for Claude - I can cook a slow recipe.”
An agent that never updates isn’t forgetful. It’s confidently wrong.

Memory is a lifecycle

ADD
It’s new.
“I started baking gluten-free bread.”
+ “Bakes gluten-free bread” · v1
A new memory.
UPDATE
It replaces something old.
“I’m looking for slow recipes now.”
“Quick meals” · v1 · is_latest: true
“Slow recipes” · v2 · is_latest: true
Replaces “I like quick meals”.
REINFORCE
It confirms what we know.
My gluten allergy, mentioned for the 10th time.
#1 allergic to gluten#2 allergic to gluten #3 allergic to gluten#4 allergic to gluten #5 allergic to gluten#6 allergic to gluten #7 allergic to gluten#8 allergic to gluten #9 allergic to gluten#10 allergic to gluten
“Allergic to gluten” · 1 doc
reinforcement_count: 1
confidence: 0.90
One memory, more confident. Not 10 copies.

Agent Engine consolidates & keeps the history

memories · a year ago{ "content": "I like quick meals.", "type": "semantic", "version": 1, "is_latest": true, "reinforcement_count": 4 }
→
memories · now{ "content": "Looking for slow recipes now.", "type": "semantic", "version": 2, "is_latest": true, "previous_version_id": "v1", "reinforcement_count": 1 }
Me · today“I’ve got time while waiting for Claude. I can cook a slow recipe.”
Me · next week“Another slow-cooked one, please.”

Forgetting is part of memory

Short-lived: TTL index
“I’m hosting friends on Saturday.”
Useful for a few days. Delete it after.
{ "type": "episodic", "content": "Hosting friends Saturday", "expires_at": ISODate("2026-12-06") }
createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 })
time to live 5d 00h 00m
On request: delete it
“Please forget my old address.”
Delete it, explicitly.
{ "type": "semantic", "content": "Lives at 12 Elm Street", "expires_at": null }
Me“Please forget my old address.”
deleteMany({ user_id, topic: "address" }) · all versions
SECTION TAKEAWAY
Memory is a lifecycle, not an insert.

New information adds, updates or reinforces what the agent knows. What’s short-lived, or what the user asks to forget, gets deleted.

Back to our 3 questions

1
What’s worth remembering?

✓Not everything. Keep durable facts, drop short-lived details. Agent Engine extracts by type, in the background.

2
The right memory at the right time?

✓Search by meaning with vector search (or hybrid search) and pin the constraints.

3
How do we update and forget?

✓Memory is a lifecycle. Keep version history. Expire short-lived data and explicitly delete everything the user asks to forget.

Good agent memory
isn’t about remembering
everything.

It’s about remembering what matters,
finding it at the right moment,
and knowing when to forget.
QR code for mdb.link/tryatlasTry MongoDB Atlasmdb.link/tryatlas

One last memory

memories · just now{ "type": "episodic", "content": "Spoke at Re:Invent 2026. The audience was great.", "stated": true, "is_latest": true, "expires_at": null }
QR code for mdb.link/tryatlasTry MongoDB Atlasmdb.link/tryatlas
AGENT TRACE · Atlas Agent Engine
→ / PageDown / clicker: next · ← back · B blackout · T trace · F fullscreen · S speaker notes