Skip to content
Back to the blog
Context management Martin Pohl

Token window: how to work with a long context without wasting it

Sliding windows, RAG, summary chains and context pruning. When a 1M-token context window earns its price, and when it is an expensive overkill.

Sdílet:

Current models offer 200k tokens (Claude), 400k (GPT-5) and 1M (Gemini) of context. It is tempting to drop a whole codebase into the prompt and “let it figure it out”. Then the invoice arrives.

A large context window has two hidden taxes: cost (input tokens add up fast) and quality (models forget what sits in the middle, the “lost in the middle” effect).

When a 1M context earns its place

  • A one-off analysis of a document or a repo (research, an audit)
  • Complex reasoning where you need every connection visible at once
  • Multimodal jobs (video plus text)

When a 1M context is expensive overkill

  • A chatbot whose history is a week old. Who remembers a week later what was being discussed?
  • RAG over documentation. Five relevant chunks beat a full dump on accuracy and on price
  • An agent workflow. Every step resends 50k tokens of context it no longer needs

4 strategies for context that stays useful

1. Sliding window

Keep the last N messages (typically 10–20). Drop or summarize the older ones. A good fit for chat.

2. Summary chain

Every M messages, have the model write a short summary of the context so far. Instead of 50k of history you send a 500-token summary plus the last 5 messages. That keeps 80%+ of the context for a fraction of the price.

3. RAG (retrieval-augmented generation)

Embed the documents and retrieve only the relevant chunks. For most Q&A use cases this beats long context on the price/quality ratio by an order of magnitude.

4. Context pruning

Before every request, go through the context: drop tool outputs older than 3 steps, delete duplicate info, compress failed attempts. This is critical for AI agents. Without pruning you can spend $50 an hour on a single run.

Concrete figures

An agent fixing a bug, 30 steps, each step adding a tool output (~2k tokens):

  • Naive: step 30 pays for 60k input tokens. Cumulative input: ~900k tokens.
  • With pruning (only the last 5 tools plus a summary): ~10k tokens per step, cumulative ~300k.

3× cheaper, and the model keeps its attention on the current problem.

Long context is not free, even when it “works”

Gemini 2.5 Pro is $1.25 per 1M input tokens. A 500k-token request is $0.625 on input alone. 1,000 such requests a day is $625 a day, $19k a month. Most teams would get the same result for 10× less with RAG.

In practice: measure input tokens per request

In Tokeny watch avg input tokens per request over time. If it rises, the context is carrying ballast. Estimate the price impact in the LLM cost calculator.

Related: 10 ways to cut AI costs · Prompt optimization

Related guides and price pages

LLM Cost Calculator

Estimate across 11 models

GPT API cost

Per-token prices for OpenAI models

Claude API cost

Haiku, Sonnet, Opus pricing

How to calculate LLM cost

The formula, step by step

10 ways to save

Caching, routing, batch APIs

Prompt optimization

Cut tokens by 30–60%

Token window strategy

RAG, sliding window, pruning

Track real AI spend, not just estimates

Upload a CSV from Cursor, Grok Bot or OpenRouter and see daily spend in USD and CZK. The account is free and does not ask for a card.

Sdílet:

Written after hours. If this helped, support my work with a coffee ☕