Lesson 04.02 · Phase 04 · Memory & Context Engineering

Conversation buffers & truncation

Cut history in whole units, from the oldest end, and pin what must never be cut. A slice in the wrong place either forgets the user or breaks the request.

Learning objectives

  • Implement four buffer strategies: keep everything, last N messages (naive and safe), and a token window.
  • Explain why a tool call and its results must be kept or dropped together, and detect histories a provider would reject.
  • Pin the messages that must survive truncation, and mark the place where history was cut.
  • Measure what each strategy costs in tokens and what it forgets.

The simplest chat memory is a list: append every message, send the whole list. It works for ten turns. At a hundred turns it costs real money on every call (Phase 3 · Lesson 11), and eventually it no longer fits the context window at all.

Loading the full lesson…