К содержанию
Статья ещё не опубликована на языке Russian. Показана версия на English.

Context Window Management: Sliding Windows, Rolling Summaries, and Long Conversations

2 мин на чтение

Even when a model advertises a 200K+ token context window, feeding an entire 6-month customer chat history into every turn is a bad idea: latency climbs, costs multiply, and outdated facts from three months ago ("I want the blue one") pollute today's order ("ship the green one"). Good context engineering keeps memory compact, fresh, and structured.

Comparing Context Compression Patterns

PatternHow It WorksStrengthsPitfalls
Sliding Window (Last N Turns)Keep only the most recent 10-20 messagesSimple, exact wording preservedForgets customer constraints stated earlier
Rolling Summary + Recent TurnsPeriodically compress older turns into structured stateConstant token footprint across long threadsPoor summary prompts drop exact IDs or prices
Structured Working MemoryMaintain explicit JSON facts (city, selectedSku, budget)High precision, auditable, easy to overwriteRequires careful update rules when user changes mind

Keeping Rolling Summaries Lossless on Key Facts

When summarizing older turns, instruct the summarizer to output structured bullet facts rather than vague narrative prose, and explicitly handle state overrides (latest user statement wins):

ts
export interface ConversationMemory {
  rollingSummary: string;
  verifiedFacts: {
    preferredLanguage?: string;
    activeOrderId?: string;
    deliveryCity?: string;
    openQuestions: string[];
  };
  summarizedThroughMessageId: string;
}

Token Budget Allocation Recipe

Reserve explicit slices of your per-turn prompt budget so no single section crowds out instructions or tool results:

  1. System + Policy + Tools: fixed budget (~1,000 to 2,000 tokens, prefix-cached).
  2. Rolling Summary + Verified Facts: capped at ~400 to 600 tokens.
  3. Recent Verbatim Turns: last 8 to 12 messages (newest first, truncating very long individual messages safely).
  4. Retrieved RAG Evidence: capped at ~2,000 to 3,500 tokens.

Next: ship prompt and model improvements without breaking live customers in Safe AI Rollouts.

Было ли это полезно?