Full conversation history is real memory, but it's also real, growing cost — an agent needs a way to compress the past without forgetting it entirely.
1Short-Term History vs Long-Term Summary
Short-term memory — the exact recent messages, verbatim — is what an agent needs for immediate context, like referring back to something said two turns ago. Long-term memory doesn't need that precision; a compressed summary of what happened earlier is usually enough to keep the agent coherent about the broader conversation without paying to resend every raw message forever.
2Capture Before You Clear
A classic and easy-to-miss bug: reading from a data structure you're about to empty, in the wrong order. recent = history[-keep_recent:] has to run while history still holds those messages — clearing first destroys the exact data the next line assumed was still there.
3Step-by-Step Breakdown
Memory Doesn't Grow Forever. TriageAgent's conversation history is real short-term memory — every message so far, sent back to the model on every step so it has full context. Left unmanaged, that history grows without bound: more tokens, more cost, more latency, and eventually more than the model's context window can even hold.
Trigger a Real Memory Summarization. When history grows past max_messages, the oldest entries should collapse into a single summary message so long-term context survives without keeping every raw message. Capture the recent messages you need to keep before the list gets cleared out from under you.
Why must recent be captured from history before history.clear() runs, rather than after?
- →Clearing the list first would erase the very messages you still needed to preserve, before there was any chance to read and save them.
- →The order between these two lines makes no difference to the final result.
Two Kinds of Memory, Both Working. TriageAgent now has bounded short-term memory (recent messages) backed by a long-term summary, plus real chunked docs from the last lesson. Next: actually wiring retrieval into the search_docs tool so it searches those real chunks instead of returning a placeholder.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Mark Summarized History Distinctly in Any Conversation UI
A collapsed summary message should be visually distinguishable from a real verbatim message, so a user reviewing the transcript understands some detail was compressed away.
<span class="summary-badge">Summarized</span> earlier conversationSEO Implications
- 1
Target 'LLM agent memory management' and 'conversation history summarization' separately
Developers managing context length search for the agent-specific framing and the general summarization technique independently.
Best Practices
Summarize With a Real Model Call in Production, Not a Static Placeholder
This exercise uses a fixed summary string to keep the logic isolated and testable — a real agent would call the model itself to generate an actual summary of the messages being compressed.
Frequent Bugs
Clearing or reassigning the history list before reading the messages meant to survive the trim.
Always capture any slice of data you need to keep before performing a destructive operation like clear() on the same structure.
Real-World Examples
Long-Running Support Conversations
A support conversation spanning 40 back-and-forth messages would, without summarization, resend all 40 on every single agent step — a real summarization trigger keeps token usage bounded regardless of how long the conversation runs.
if len(history) > 40: summarize_and_trim(history)