Token Calculator by Programming Language (2026)

Hub Updated 2026-11 · 8 languages

Source code is billed like prose, but not at prose rates. Here is how many tokens 100 characters of Python, JavaScript, Rust and five other languages actually cost you — and what that adds up to on a real 1M-context request.

Tokens per 100 characters of source code

Measured on the o200k_base tokenizer that GPT-4o, the GPT-5.x family and GPT-6 all bill on. Lower is cheaper. The full methodology, per-language code samples and the tiktoken snippet to reproduce the numbers are in the full language breakdown.

Rank Language Tokens / 100 chars Tier
1 Python 28–32 Cheapest
2 JavaScript 32–36 Cheap
3 TypeScript 35–40 Mid-low
4 Go 38–42 Mid
5 Java 38–44 Mid-high
6 Rust 42–48 Expensive
7 C++ 45–52 Very expensive
8 C 48–55 Most expensive

The spread from Python to C is about 70% on identical logic. That is not a rounding error — it is the difference between a cheap agent loop and an expensive one.

What each language costs per 1M-character request

Same file size, same model, different bill. The figures below price a 1M-character source file at Claude Sonnet 5.5 rates ($2/MTok input, $10/MTok output) with a 50% output split on a 1M context window.

Language Tokens for 1M chars Input cost Output cost Total per session
Python (28–32) ~300K $3.00 $12.00 $15.00
JavaScript (32–36) ~340K $3.40 $13.60 $17.00
TypeScript (35–40) ~375K $3.75 $15.00 $18.75
Go (38–42) ~400K $4.00 $16.00 $20.00
Java (38–44) ~410K $4.10 $16.40 $20.50
Rust (42–48) ~450K $4.50 $18.00 $22.50
C++ (45–52) ~485K $4.85 $19.40 $24.25
C (48–55) ~515K $5.15 $20.60 $25.75

Python token count

Python sits at the floor, 28–32 tokens per 100 characters. def foo(x): return x + 1 compresses to about 9 tokens; the same logic in Java runs closer to 18. Three reasons stack up: indentation-as-syntax collapses four spaces into one token per level instead of a brace pair, dynamic typing removes type annotations from the hot path, and the ~30 keywords that appear in every Python file (def, class, return, if, for, import) all hit single-token IDs in the BPE table.

Best fit: AI agent loops, prototypes, throwaway scripts — anything a model reads dozens of times.

JavaScript token count

JavaScript lands at 32–36, about 15% above Python. Curly braces and semicolons each cost a token, but the dynamic-typing culture keeps type annotations out of the picture. Modern ES syntax (?., ??, destructuring) is denser than the verbose jQuery-era code still sitting in legacy repositories — which is often where the real bill goes.

TypeScript token count

TypeScript runs 35–40 tokens per 100 characters, roughly 10% above JavaScript, because type annotations add tokens to every signature. A typed function like function process(input: ReadonlyArray<User>): Promise<Result<User, Error>> is longer than its JS equivalent on every line. Worth it when the IDE and refactor guarantees matter more than the token bill.

Go token count

Go comes in at 38–42 — surprisingly mid-pack for a statically typed language. The syntax itself is lean: short keywords, no header files, := type inference keeps simple functions tight. The overhead comes from verbose package management (package main, import (...)) and explicit error returns (if err != nil { return err }) on every call.

Java token count

Java is the classic verbose-by-design language at 38–44 tokens per 100 characters. public static final void main(String[] args) is 9 tokens for what Python writes as def main():. Spring Boot code runs worse than this range — annotations such as @Autowired and @Transactional cost 2–3 tokens per occurrence. The ecosystem value is real; the token cost is the trade-off.

Rust token count

Rust reaches 42–48 tokens per 100 characters, past Java despite its modern syntax. Macros and lifetime annotations are dense in meaning but expensive in tokens — fn process<'a, T: Deserialize<'a>>(input: &'a str) -> Result<T, Error> is a mouthful. The borrow checker saves you bugs the model never would, so the token bill is the cost of safety.

C++ token count

C++ sits at 45–52. Templates, operator overloading and STL headers stack up: a mid-size C++ file pulls in 10–20 #include lines, each one a token cluster. The performance ceiling is unmatched, but if you are not shipping a kernel or a game engine, the token cost is hard to justify in 2026.

C token count

C is the worst in the study at 48–55 tokens per 100 characters. Headers (#include <stdio.h>), explicit types, manual memory management (malloc, free, sizeof) and the absence of standard-library compression — printf("%d", x) against Python's print(x) — all stack tokens.

Why language choice changes your bill

Three factors separate the cheap languages from the expensive ones, and they compound. Syntax density decides how much meaning fits per character. Keyword frequency in the tokenizer's vocabulary decides whether your keywords are single tokens or two-to-three token sequences — run enc.encode("public static final") and see. Identifier naming style matters more than most people expect: identifiers cost roughly 0.3 tokens per character, so userAuthenticationService and ctx are not interchangeable at scale.

Reproduce any of these numbers

Three lines with tiktoken is the whole check. If your own file comes back in the 50+ range, you have a token-efficiency problem worth fixing before you ship it to a model.

import tiktoken
enc = tiktoken.get_encoding("o200k_base")
src = open('file.c').read()
print(f"{len(enc.encode(src)) / len(src) * 100:.1f}")

Count your own code before you ship it

Paste any source file into the calculator and see the real token count and cost across every provider.

Open the Token Calculator →