Conversational Memory

The running message history the agent reads each turn.

Recent conversation turns are retained and replayed so the model has context for the next turn — as a full transcript, a fixed-size window of the last N turns, or a running summary once the raw history outgrows the budget. This is the short-term tier of agent memory.

In practiceA travel-planning chatbot replays the last 20 turns at the start of each response so the model remembers the user said 'window seat only' three messages ago.

Level
04 · Production
Subdomain
Memory Architecture
Also known as
Chat History, Conversation Buffer, Dialogue Memory
Trade-off
Improved conversational context is achieved against higher token costs, privacy risks, and context window exhaustion.
Native in
LangGraph, OpenAI Agents SDK, Microsoft Agent Framework, AutoGen / AG2, CrewAI
The mechanism

How it works.

Every turn runs the same loop: load the history of this thread, put it in front of the new message, call the model, store what was said. A second step keeps the history inside the token budget.

1 · Every turn

  1. New messageThe thread ID selects the conversation
  2. Load the historyEverything stored for this thread
  3. Call the modelHistory first, then the new message
  4. Store the new itemsQuestion, tool calls and answer

2 · When the history grows too long

  1. MeasureTokens or message count against a limit
  2. ShortenTrim, delete or summarise
  3. Keep the recent turnsThe last messages stay word for word
Trim
Drop the oldest messages before the model call. The stored history stays complete.
Delete
Remove messages from the stored state for good. What remains must still be a valid message sequence.
Summarise
Replace the earlier messages with one summary and keep the latest ones unchanged.
Walk it through

A real run, step by step.

TaskA travel assistant, 40 turns into a conversation. In turn 3 the user said “window seat only”. Turn 41 is “book the flight”. Three ways to build the context:
Illustrative token counts.
StrategyIn contextTokensSeat wish
Full historyTurns 1 to 4012,400Yes
Last 20 turnsTurns 21 to 406,100No
Summary and last 20 turnsA summary of turns 1 to 20, then turns 21 to 406,500Only if the summary kept it
What this shows: the window is the cheapest option and it forgets without a signal. Nothing tells the model, or the user, that turn 3 fell out. A summary can keep the wish, but only if the summariser judged it worth keeping.
In code

The pattern in code.

Plain Pythonpython
def reply(thread_id, user_message):
    history = store.get_items(thread_id)  # before the run
    if count_tokens(history) > 4000:
        old, recent = history[:-20], history[-20:]
        # earlier turns become one summary
        history = [summarize(old)] + recent
    answer = model(history + [user_message])
    store.add_items(thread_id, [user_message, answer])  # after the run
    return answer
Fit

Use it, or not.

When to reach for it

  • User context must be maintained across multiple turns.
  • References to prior statements are expected.
  • Conversational flow is critical.

When it backfires

  • Privacy regulations forbid storage.
  • History consumes unnecessary tokens.
  • Tasks are strictly stateless.
Pitfalls

Two ways this pattern will hurt you.

Unbounded history exhausts the context window

Every turn appends to the buffer; eventually the prompt is mostly stale dialogue and the model truncates the most recent — and most relevant — turns.

Countermeasure

Cap the buffer by token budget. Summarize older turns into a single block, or fall back to retrieval against an episodic store.

Summarization silently drops the detail you needed

To stay under budget, older turns are compressed into a summary. The one concrete fact a later turn depends on — a booking code, an exact figure — is exactly what the summary paraphrases away.

Countermeasure

Extract and pin durable facts (IDs, preferences, commitments) to a structured slot before summarizing the prose, so compaction never touches them.

Threat exposure

What this pattern exposes

Adopting this pattern opens these attack surfaces. Each links to its entry in the threat model.

Full threat model →

EU law

Regulation

Duties from EU law that this pattern helps meet, or that land on it. A reading aid, not legal advice.

  • GDPR Art. 15A person can obtain confirmation that their data is processed, access to it, and the recipients it was disclosed to.Controllers · since 25 May 2018
  • GDPR Art. 17Personal data is erased without undue delay when a ground for erasure applies.Controllers · since 25 May 2018
  • GDPR Art. 35Before processing that is likely to result in a high risk, in particular with new technologies, the controller assesses its impact on the protection of personal data.Controllers · since 25 May 2018Explained on the DPIA page

An engineer's reading aid, not legal advice. The legal texts and their interpretation are authoritative.

Framework support

Where Conversational Memory is native.

LangGraphmessage state + trimmingNative
OpenAI Agents SDKsession historyNative
Microsoft Agent Frameworkthread historyNative
AutoGen / AG2conversable-agent historyNative
CrewAIshort-term memoryNative

Search

Search patterns, frameworks, and pages.