Code token estimator
Sending code to an LLM is more expensive than sending prose of the same length — operators, indentation, and short identifiers all fragment into extra tokens. This estimator measures your code with per-language density and tells you the token count, how much of a context window it eats, and the cost, before you spend a single API call discovering the file was too big.
How it works
The tool applies a character-per-token density tuned for each language: dense languages like JSON and minified code pack the fewest characters per token, while comment-heavy prose-like code sits closer to natural text. It estimates tokens from your pasted code, prices the input against your chosen model, and shows the fraction of a typical context window the file occupies. Everything runs in your browser.
Why code tokenises differently from prose
Modern LLMs use byte-pair encoding (BPE), which learns token boundaries from training data. Common English words often become a single token. Code is different — it is dense with punctuation (brackets, semicolons, arrow operators), short variable names that the tokeniser has not seen before, and repeated indentation characters. These patterns all produce more tokens per character than typical English prose, which is why a 500-character Python function might cost more tokens than a 500-character paragraph.
Rough rule of thumb: English prose runs about 4 characters per token; code typically runs about 3 characters per token, though highly punctuated languages like Lisp or Haskell can go lower.
Practical planning guide
| Scenario | What to do |
|---|---|
| File is under 20% of the context window | Paste it directly |
| File is 20–70% of the context window | Consider what surrounding prompt you need alongside it |
| File is over 70% of the context window | Chunk it, summarise non-essential sections, or strip comments |
| Same file sent on every request | Ask whether your provider supports prompt caching — repeated prefixes can become nearly free |
Tips and notes
For large codebases, the practical question is usually “does this fit?” — the context window bar answers it at a glance and tells you whether to chunk or summarize. If you are sending the same files repeatedly (for example a fixed framework header on every request), prompt caching can make the repeated portion nearly free. Stripping comments and dead code cuts tokens but can degrade the model’s reasoning about the code, so trim carefully. Treat the count as a close estimate and confirm with the provider’s tokenizer before sizing a large automated run.