RAG, Context & Media AI

Codebase, JSON & Document Context Token Estimator

Convert words, characters, lines of code, or PDF pages into accurate LLM token estimates and check fit across 32K–1M windows.

Input Parameters

units
$ / 1M tok
Calculated Output
Primary Result
—
Adjust inputs to recalculate immediately.

BPE Tokenizer Density Rules of Thumb (vs. English Prose)

Punctuation, camelCase identifiers, brackets, and whitespace make code and JSON consume far more tokens per character than prose.

Content FormatTokens / WordTokens / CharTokens / 1,000 UnitsContext Window Notes
English Prose~1.33 tok/word~0.25 tok/char1,330 tok / 1k wordsOptimal BPE compression
Source Code (TS/Python)~1.75 tok/word~0.35 tok/char14,000 tok / 1k LOCPunctuation + indentation heavy
Dense JSON / API Payloads~2.10 tok/word~0.40 tok/char400 tok / 1k charsKeys, quotes & UUIDs inflate tokens
Technical PDF Pages~1.35 tok/word~0.27 tok/char450,000 tok / 1k pagesTables/OCR add extra noise

Formula & Engineering Methodology

Applies empirically calibrated BPE tokenizer density multipliers matching modern 100k–200k-vocab tokenizers (cl100k/o200k/Llama-3/DeepSeek) across characters, words, lines, and document pages.

est_tokens = quantity * ratio(input_mode, content_type) pct_128k = (est_tokens / 128000) * 100 pct_200k = (est_tokens / 200000) * 100 single_turn_cost = (est_tokens / 1e6) * model_input_rate_per_m

Variable Definitions

Practical Note: Even when a prompt fits inside a 200K–1M window, retrieval precision degrades near the middle ('lost-in-the-middle') and Time-To-First-Token rises linearly/quadratically.

Frequently Asked Questions

Why do JSON and source code consume ~35–60% more tokens per character than English prose?
JSON braces, quotes, colons, UUID hashes, and code indentation fragment into single-character tokens instead of whole multi-letter English word merges.
How does long context affect Time-To-First-Token (TTFT)?
Every prompt turn must prefill all uncached context tokens through every transformer layer before outputting token #1; 100K uncached tokens can add 5–25 seconds of latency.
Should I dump an entire repo into 1M context or use RAG?
For one-off refactors under 100K tokens, full context with prompt caching is fast and simple; for repeated queries across large codebases, AST chunking + RAG is cheaper and higher accuracy.

Related Engineering Calculators

ComputeSizer runs 100% locally in your browser with open formulas and zero account gates. If these VRAM and GPU cost tools saved your team money, support us via Fourthwall or sponsor an ad-free developer placement.