Context Window Math: Why '200K Tokens' Doesn't Mean What You Think

The Code Fundi Team
Aug 5, 2026

TL;DR

A 200 K‑token context window isn’t a free pass to dump an entire repo. System prompts, tool schemas, file‑tree listings, and conversation history can eat 70‑80 % of the budget before the first line of relevant code even appears. Precise token accounting (using the cl100k_base tokenizer from OpenAI’s tiktoken library) shows why ā€œjust retrieve lessā€ isn’t a silver bullet and why structural pre‑filtering is the only scalable remedy.


What a ā€œ200K token windowā€ Actually Has to Hold in a Real Request

When you invoke an AI‑coding agent, the request payload is rarely just ā€œhere’s the file I need.ā€ The model must see a complete execution context, which includes several mandatory components:

ComponentTypical Size (tokens)Why It’s Needed
System Prompt150 – 400Sets the agent’s role, behavioural constraints, and safety guardrails.
Tool Schemas (e.g., file‑read, search, edit)300 – 800Describes the JSON schema for each tool the agent may call; required for tool‑use reasoning.
Conversation History (previous user‑assistant turns)0 – 30 KEnables continuity across a multi‑step edit session.
File‑Tree Dump5 K – 30 K (depends on repo size)Gives the model a map of the repo’s hierarchical layout so it can resolve relative paths.
Retrieved Files (raw source code, docs)10 K – 80 KThe actual content the agent will reason over; often includes many files that are not directly relevant.
User Query / Task Description50 – 300The concrete request (e.g., ā€œadd validation to order.service.tsā€).

āš ļø Callout: The token budget is consumed as soon as the payload is sent; the model never ā€œskipsā€ the system prompt or schema. Every extra line reduces the space left for the code you actually want it to see.

Token Accounting Basics

TokenizerModel(s)Approx. tokens per English wordApprox. tokens per source‑code line*
cl100k_base (OpenAI tiktoken)GPT‑4‑Turbo, GPT‑4‑o1.30.7 – 1.0 (depends on whitespace, identifiers)

*The line token count varies widely; a typical TypeScript line averages ~1 token, while a dense Python line can be ~0.8 tokens.

Note: All numbers below are measured with cl100k_base. Different providers use slightly different tokenizers, so the absolute counts will shift, but the proportional budget pressure remains the same.


A Worked Example – Token Budget Drain on a Mid‑Size Repository

Below is a concrete audit of a 400‑file, ~120 KB TypeScript/JavaScript monorepo (ā‰ˆā€Æ18 K source lines). The goal is to understand how many tokens the request consumes before any line of the target file (src/payments/processor.ts) is even present.

1. Assemble the Payload

json
1 {
2 "system_prompt": "...", // role & safety
3 "tool_schemas": {...}, // JSON definitions for read/search/edit
4 "conversation_history": [...], // prior turn(s)
5 "file_tree": "...", // printed tree
6 "retrieved_files": {...}, // raw file contents
7 "user_task": "Add retry logic to payment processor"
8 }

2. Token Count Breakdown

Payload PieceRaw CharactersTokens (cl100k_base)Comments
System Prompt2 200180Minimal ā€œyou are a senior staff engineer ā€¦ā€
Tool Schemas5 8005604 tools, each with a 1 400‑char JSON schema
Conversation History12 4008 1003 prior turns (~4 K tokens each)
File‑Tree Dump84 90032 400Tree printed with tree -L 3 and file‑size annotations
Retrieved Files (full repo)1 310 00092 200400 files Ɨ avg. 3 250 chars/file
User Task Description34030Short imperative
Subtotal (everything except target file)-133 47066 % of a 200 K window
Target file (processor.ts)3 2002 900First relevant line appears at token #136 371
Total-136 370Leaves ~63 630 tokens for model output & further retrieval

Observations

  1. File‑tree alone consumes ~32 K tokens – roughly 16 % of the window.
  2. Conversation history quickly dominates when you have multi‑step interactions; each turn can be a few thousand tokens.
  3. Retrieving the entire repo is the biggest cost. Even a modest 400‑file codebase eats ~92 K tokens, leaving less than 100 K for anything else.

If you add a second round of retrieval (common in ā€œrefine & iterateā€ loops), you’ll exceed the window after just two cycles.

3. Visualizing the Token Allocation

stata
1 pie
2 title Token Allocation in a 200K‑Token Request
3 "System Prompt": 180
4 "Tool Schemas": 560
5 "Conversation History": 8100
6 "File‑Tree": 32400
7 "Retrieved Files (non‑target)": 92200
8 "Target File": 2900
9 "Remaining for Output": 63630

⚔ Quick Take: Even with aggressive pruning, the noise (prompts, schemas, tree) can swallow half the context window before the model sees the line you care about.


Why ā€œJust Retrieve Lessā€ Isn’t Free Either

A common reaction is to cut the retrieval size: ā€œOnly fetch the files we think are relevant.ā€ The intuition feels right, but the trade‑off is subtle.

Reduction StrategyToken SavingsRisk
Top‑k similarity ranking (e.g., 10 most similar files)~50 K tokens savedMay exclude files that affect the blast radius (indirect dependencies, config, tests).
Depth‑limited tree (only leaf nodes)~15 K tokens savedLoses structural context (module hierarchy, import paths).
Static ā€œignore .md/*.test.jsā€ filter~5 K tokens savedOften removes useful documentation or failing test cases that expose regressions.

The Retrieval Problem This Compounds

When you drop files indiscriminately, you increase the chance of missing the true dependency slice. A downstream regression can slip past the model because the agent never saw the piece of code that caused the bug.

See also: the earlier discussion in Blog #2 – the retrieval problem this compounds and the follow‑up on Blog #19 – how memory and context budgets interact.

In practice, engineers see two symptoms:

  1. False‑Positive ā€œNo relevant codeā€ – the agent claims it can’t find anything, yet the missing file was simply filtered out.
  2. Silent Regression – the agent produces a patch that passes local tests but breaks a distant module that was never part of the prompt.

Both outcomes erode trust faster than a mere ā€œcontext limitā€ error.


The Actual Fix: Structural Pre‑Filtering

Instead of feeding the whole tree or a crude similarity list, pre‑filter the repo into a minimal, structurally coherent slice that contains all code reachable from the target file’s call‑graph.

How It Works

  1. Static Dependency Graph Build – parse the repo once (e.g., using the TypeScript compiler API or a language‑agnostic AST tool) to produce a directed graph of imports/exports.
  2. Slice Extraction – given the entry point (processor.ts), walk the graph to collect every node reachable within N hops (commonly 2‑3).
  3. Metadata‑Only Tree – send a compact file‑tree that lists only the sliced files; each entry includes size and import depth, not the full source.
  4. Selective Retrieval – request the raw contents only for the slice; all other files stay on the server, never consuming tokens.

Token Savings (Same 400‑File Repo)

Payload PieceTokens BeforeTokens AfterĪ” Tokens
File‑Tree (full)32 4004 800‑27 600
Retrieved Files (full)92 20014 500‑77 700
Total Savings124 60019 300‑105 300

Result: The same request now fits comfortably under 50 K tokens, leaving ample room for multi‑turn dialogue, richer tool usage, and higher‑quality output.

Benefits Beyond Token Economy

BenefitWhy It Matters
Full Blast‑Radius VisibilityThe slice includes all transitive dependencies, so the model can reason about side effects.
Deterministic RetrievalThe slice is defined by static analysis, not by heuristic similarity that can drift over time.
Predictable Token BudgetToken count becomes a function of graph depth, not repo size, making capacity planning trivial.

CodeFundi – Structural Pre‑Filtering Done Right

CodeFundi’s API delivers the structural slice automatically. By feeding the model a dependency‑aware payload instead of a raw file‑tree, we guarantee that the context window is spent on meaningful code, not on the surrounding noise. The service also returns a token‑budget estimate for each request, letting you stay safely under the model’s limit while preserving the full blast‑radius view required for safe AI‑driven edits.


FAQ

Why does my AI coding tool run out of context on large repos?
Because every component of the request - system prompt, tool schemas, file‑tree, conversation history, and retrieved source - consumes tokens. In typical multi‑turn sessions the non‑code overhead can exceed half the window before the first line of your target file is even seen.

How many tokens does a typical codebase use?
A 400‑file JavaScript/TypeScript monorepo (ā‰ˆā€Æ18 K source lines) consumes roughly 90 K tokens when the entire repo is retrieved, plus 30 K tokens for the file‑tree and other scaffolding. The exact number varies with language density and tokenizer, but the pattern holds: the bulk of the context budget is eaten by metadata and irrelevant files.

Does a bigger context window fix AI coding accuracy?
A larger window alleviates the raw token‑budget pressure but does not solve the underlying problem of noisy payloads. Without structural pre‑filtering, even a 400 K token window will still be dominated by tree listings and history, leaving less room for actual reasoning. Accuracy improves only when the model’s attention is focused on the relevant dependency slice.


Take Action

Seeing the math in your own repo is the fastest way to stop guessing. Try the live blast‑radius demo and instantly see how many tokens your request actually consumes, then let CodeFundi trim the excess.


Frequently Asked Questions