C.W.K.
Stream
Lesson 05 of 12 · published

Context Window Implications

~10 min · context-window, long-context

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Context window is the maximum number of tokens the model can process in one request. Its implications cascade through application design, cost, latency, and architecture.

ContextEquivalent textUseful for
4K~3,000 words / 6 pagesShort conversations, simple tools
32K~24,000 words / 48 pagesLonger documents, focused code review
128K~96,000 words / 200 pagesBooks, large codebases
1M~750,000 words / 1,500 pagesWhole repositories, long-form research
10M~7.5M wordsEntire document corpora; LLaMA 4 Scout

The trade-offs

  • Cost. Per-token API pricing means longer prompts cost proportionally more. A 1M-token prompt at $1.25/1M is $1.25 just to read the input.
  • Latency. Prefill (processing the prompt) scales linearly with context length and is compute-bound. A 128K prefill on a 70B model takes seconds before any output appears.
  • KV-cache memory. Linear in context length. At 128K, can be 30+ GB.
  • Quality at depth. Models have a "lost in the middle" effect — information at depth 50K may not be retrieved as reliably as information at depth 1K, even when both are within the advertised context window.

Don't pay for context you don't use. RAG (retrieval-augmented generation) often beats stuffing every document into a long-context model — both on cost and on quality at depth.

Code

Cost estimator: long context vs RAG·python
# Hypothesis: 'just send the whole 200-page doc' vs 'retrieve top 5 chunks of 1000 tokens.'
def cost(input_tokens, output_tokens, in_per_1m, out_per_1m):
    return input_tokens / 1e6 * in_per_1m + output_tokens / 1e6 * out_per_1m

# 200-page doc ≈ 100K tokens
long_ctx  = cost(100_000, 500, 1.25, 10.00)    # Gemini 2.5 Pro
rag_5x1k  = cost(5_000, 500, 1.25, 10.00)
print(f"Long context: ${long_ctx:.4f},  RAG: ${rag_5x1k:.4f}")
# RAG is ~20× cheaper per query. Often the better default.

External links

Exercise

Take a 50-page document. Implement two pipelines: (a) send the whole document plus the question to a 1M-context model; (b) chunk the document, embed it, retrieve the top-5 chunks, and send those plus the question. Measure quality (hand-graded), cost, and latency. Which wins on which dimension?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.