CSE Roadmap

Core tier · about 6 hours · 8 parts

Part 5 of 8 · 3 min read

What tokens cost

Counting tokens with a real tokenizer, then turning counts into money, delay and window space.

Counting with a real tokenizer

import tiktoken                       # pip install tiktoken

enc = tiktoken.get_encoding("cl100k_base")      # pick the encoding that matches your model
ids = enc.encode("Tokens are not words.")
print(len(ids), ids)
print([enc.decode([i]) for i in ids])           # see the pieces

Other options: the Hugging Face AutoTokenizer.from_pretrained("model-name") for open models, and the token-counting feature or the usage field in your model provider's API. For billing, trust the provider's own count: each model family has its own tokenizer, and counts differ between them.

I could not run this snippet

tiktoken downloads its vocabulary file the first time it runs, and my workspace cannot reach the site that serves it (the error was a proxy error). So I could not show real output. Exercise Hard 1 asks you to run it and report what you get.

From tokens to money

Rule of thumb for English: one token is roughly three quarters of a word, or about four characters, so 100 tokens is roughly 75 words. It is a rough guide only. Code, numbers, rare names and many non-English languages need more tokens per word. (cost.py implements it as characters / 4.)

Cost. Providers price input tokens and output tokens separately, per million tokens, and output is usually several times dearer. For one request:

cost = (input_tokens x input_price + output_tokens x output_price) / 1,000,000

A worked example with invented prices (3 per million input tokens, 15 per million output tokens) for 10,000 requests of 2,000 input and 500 output tokens each:

Part Calculation Cost
Input 10,000 x 2,000 = 20,000,000 tokens x 3 / 1,000,000 60.00
Output 10,000 x 500 = 5,000,000 tokens x 15 / 1,000,000 75.00
Total 135.00

cost.py computes this and a test confirms 135.00. Notice that output is a fifth of the tokens (5 million of 25 million) but over half of the cost (75 of 135). Also check how your provider bills the hidden "thinking" tokens of reasoning models. Most count them as output tokens, which can multiply the output cost.

Latency. The time until the last word grows with the number of output tokens, because the model produces them one at a time. A long input adds delay before the first token appears.

The context window is shared. The window limit counts input plus output. A request with 7,500 input tokens and a 1,000-token output cap does not fit in an 8,000-token window, and fits_context tests this.

Agents multiply all of this. Each step of an agent's loop usually re-sends the whole conversation so far. If every step adds 500 tokens, the input of step 10 is about 5,000 tokens, and the total over ten steps is 500 x (1 + 2 + ... + 10) = 27,500 input tokens, not 5,000. This is the main reason long agent runs get expensive, and why Level 7 teaches ways to keep the context short.

Check yourself

  • Using the rule of thumb, about how many tokens is a 2,000-character email?
  • Why is a 1,000-token reply more expensive than a 1,000-token prompt?
  • Why do ten steps of 500 tokens cost 27,500 input tokens and not 5,000?