AI ENGINEERING: DEEP DIVE · MODULE 06 — AGENTS & ORCHESTRATION INTERNALS · 17 A hands-on series · math + code

Module 6.2

What's cheap to keep, what's expensive to forget

Ten turns into a Nimbus-bot conversation, the user circles back to something mentioned six turns ago. Three memory architectures handle that very differently — see exactly what each one hands the model right now.

TURN 10: "BACK TO THE EXPORT — HOW MUCH SPACE DO I NEED TO FREE UP?"
WHAT ACTUALLY GETS SENT TO THE MODEL, RIGHT NOW

The math

Three different answers to "what's in context"

Sliding window
context = turns[−N:]
simplest, cheapest — but a hard cliff
turn 4 is gone the moment it's >N turns old
Summarization
context = summarize(turns[:−N]) + turns[−N:]
an extra model call compresses the old part
detail survives, but only whatever the summary kept
Vector-store retrieval
context = top_k(cos_sim(embed(new_turn), turns))
same nearest-neighbor math as Module 1.2 / 5.1
relevance, not recency, decides what's included

Notice the third one isn't a new mechanism at all — it's Module 1.2's cosine ranking, run over conversation turns instead of documents.

The code

Three memory managers

agent_memory.py
def sliding_window(turns, n=4):
    return turns[-n:]

def with_summary(turns, n=2):
    old, recent = turns[:-n], turns[-n:]
    summary = running_summary or summarize(old)   # one extra LLM call, done periodically
    return [summary] + recent

def vector_memory(turns, new_turn, k=2):
    scores = [cosine_sim(embed(new_turn), embed(t)) for t in turns]
    top_k = argsort(scores)[-k:]                       # Module 1.2, again
    return [turns[i] for i in sorted(top_k)] + [turns[-1]]

Production agents often run more than one of these at once — a sliding window for immediate coherence, plus vector retrieval for "did we ever discuss this before."

The Nimbus thread

Only one architecture actually answers turn 10 well

The sliding window has already dropped the one turn (#4) that contains the actual quota number — Nimbus-bot would have to guess or ask again. Summarization keeps the gist ("quota was exceeded") but may have compressed away the exact 50GB figure. Vector retrieval re-surfaces turn #4 specifically because the new question is semantically about the same thing, regardless of how many turns ago it was — the same mechanism Module 5 used for documents, now pointed at the conversation itself.

So what

Agent memory isn't one design — it's a real tradeoff between what's cheap to keep and what's expensive to forget.