Rolling Window

Context Management

Definition

A context management technique that retains only the N most recent conversation turns, evicting older turns as new ones arrive. Simple to implement with zero summarization overhead. Best deployed as a rolling-window+summary-header hybrid to prevent silent loss of early constraints or decisions. One specific technique under the broader umbrella of [context compression](/glossary/context-compression).

Example Usage

Keep the last 20 turns in context; when turn 21 arrives, evict turn 1 and merge its key fact into a pinned summary header at the top of the system prompt.

Why It Matters for the CCA-F Exam

The exam tests rolling windows as a Domain 5 strategy, typically in contrast with summarization. Know that a pure rolling window is safe only when each turn is self-contained, that the production pattern combines eviction with a pinned summary header, and that token-based windows are more accurate than turn-count windows.

In Depth

A rolling window retains only the N most recent conversation turns in the active context, automatically evicting older turns as new ones arrive. It is the simplest approach to context budget management: keep recent history, drop old history — no classification logic required.

1. Token-based vs turn-based windows

Turn-count windows are easy to implement but imprecise. One turn can be 10 tokens or 10,000 tokens, so a window of "20 turns" has unpredictable token consumption. Token-based windows track actual token counts and evict when the running total exceeds a threshold. For production systems, target 60–70% of the model's context limit as your maximum window size. This headroom absorbs system prompts, tool schemas, and the current turn's tool results without surprise overflows.

2. Evicting oldest turns

A rolling window is mechanically a fixed-capacity deque: append new turns to the tail, pop from the head when capacity is exceeded. The eviction decision is deterministic and requires no per-turn semantic judgment. This makes it the lowest-latency compression option — no extra model calls, no classification overhead.

3. The silent-loss risk

The danger is silent context degradation. If a user specified a constraint, selected a configuration, or provided a critical value five turns ago, a pure rolling window discards it without any error signal. The model continues responding — coherently, apparently — but without context it previously held. This failure mode is worse than an explicit error because it is invisible.

4. The rolling-window + summary-header hybrid

The production-ready form pairs eviction with a persistent summary block pinned at the start of the system prompt. When a turn is evicted from the window, its key facts are merged into the header before removal. The window provides recency; the header provides persistence. This is what the CCA-F exam treats as the correct pattern for stateful conversations.

5. When a pure rolling window is appropriate

For stateless or near-stateless applications — a single-turn Q&A chatbot, a customer support bot where each exchange is self-contained — a pure rolling window without a summary header is safe and adds no latency. The risk grows proportionally with how much earlier turns constrain later behavior.

For tasks where early context carries forward-looking constraints or decisions, see summarization strategy, which converts evicted content into a smaller representation rather than dropping it entirely.

Example

Rolling window using a fixed-size deque; a pinned summary_header carries forward facts evicted from the window.

python
import anthropic
from collections import deque

client = anthropic.Anthropic()
MAX_TURNS = 10
history = deque(maxlen=MAX_TURNS * 2)  # each turn = 1 user + 1 assistant message

def chat(user_input: str, summary_header: str = "") -> str:
    history.append({"role": "user", "content": user_input})
    messages = list(history)

    system = "You are a helpful assistant."
    if summary_header:
        system += f"\n\n<prior_context>\n{summary_header}\n</prior_context>"

    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        system=system,
        messages=messages,
    )
    reply = response.content[0].text
    history.append({"role": "assistant", "content": reply})
    return reply

How It's Tested & Common Confusions

Tested as:

  • Scenario: choosing a compression approach — rolling window appears as the "low complexity, high loss" option; know when it's appropriate vs. when summarization is safer.
  • Scenario: silent context loss — questions ask what happens to a constraint stated early in a conversation when only a plain rolling window is in use. Answer: it is silently discarded.
  • Token vs turn count — the exam expects you to know that token-based eviction is more precise.
  • Hybrid pattern — the rolling-window+summary-header is the tested production form; pure eviction without a header is flagged as risky for stateful tasks.

Frequently Asked Questions

What is the main risk of a rolling window without a summary header?

Silent context loss. The model stops seeing early turns without any indication that information was dropped. Constraints, user preferences, or key values stated early in the conversation disappear silently, leading to inconsistent or incorrect responses.

Should the window be measured in turns or tokens?

Tokens are more accurate. Turn count is a useful proxy but a single long tool result can consume as many tokens as twenty short turns. For production systems, track approximate token counts and evict when you approach 60–70% of the model's context limit.

When is a pure rolling window (no summary header) acceptable?

When each conversation turn is largely self-contained — for example, a single-turn Q&A bot or a customer support flow where users don't build on earlier exchanges. If early turns set constraints that later turns must respect, add a summary header.