Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 53 ยท Tokens and Embeddings

Counting Tokens and Estimating Cost

Learn rules of thumb for tokens per word, why input and output tokens are priced differently, and how to budget prompts and estimate LLM API costs.

Every LLM bill, every context limit and every latency number is measured in tokens. If you have read Tokens and Tokenization and Byte-Pair Encoding from Scratch, you know what a token is. This lesson is about the practical side: how many tokens a text will use, what that costs, and how to keep a prompt inside a budget.

You will not need an API key. We use a rough estimator and fake prices so the arithmetic is the point. Real prices change often, so always read your provider's current price page and plug the numbers into the same formulas.

Rules of thumb

Real tokenizers differ, so the only exact way to count is to run the tokenizer your model uses. Many providers offer a token-counting function or return the usage in every response. For quick planning, these averages for common English text are good enough:

  • 1 token is about 4 characters.
  • 1 token is about three quarters of a word, so 100 words is about 130 tokens.
  • A page of text (500 words) is roughly 650 tokens.

Estimating tokens fast

Characters Divide the character count by about 4.
Words Multiply the word count by about 1.3.
Pages A page of 500 words is about 650 tokens.

These are English averages. Code, numbers and other languages differ.

1 / 2

The last point matters for both cost and quality. Tokenizers are trained mostly on English text, so the same sentence in another language can need two to four times as many tokens. That means a higher price and less room in the context window.

Input, output and the price list

Providers usually quote prices per million tokens, and they charge input (everything you send: system prompt, history, retrieved documents, your question) and output (what the model writes) at different rates. Output is typically several times more expensive, because each output token needs its own forward pass, while input tokens are processed in parallel.

The formula is short:

TEXT
cost = input_tokens / 1,000,000 * input_price
     + output_tokens / 1,000,000 * output_price

Below is a small estimator. The prices belong to three invented models. The token estimate is a heuristic, not a real tokenizer.

Python A token and cost estimator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the last column. A fraction of a cent per call sounds free, but multiplied by 100,000 calls the choice between a small and a large model is a very different bill.

What counts as input

Beginners often count only the user's question. In a chat, the whole conversation is sent again on every turn, so input grows with each message. Add a system prompt, a few examples and some retrieved documents, and the question may be 2% of what you pay for.

Python Chat history makes input grow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Input per turn rises steadily, so total input grows roughly with the square of the number of turns. This is why long chats get expensive, and why later lessons cover trimming, summaries and prompt caching.

Budgeting a prompt

Every model has a context window (see The Model Landscape). Input plus the output you allow must fit inside it. A simple budget:

  1. Start with the window size, for example 8,000 tokens.
  2. Subtract the maximum output you will request (the max_tokens setting).
  3. Subtract the fixed parts: the system prompt and the format instructions.
  4. What remains is the space for history and retrieved documents.

When the variable part is too big, you drop or shorten something. The helper below keeps the most recent messages that fit.

Python Fit messages into a token budget

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Always leave headroom. A heuristic can be off by 20% or more, and a request that exceeds the window fails instead of being truncated gracefully. For anything that matters, use the provider's real tokenizer to count.

Comparing languages

Because tokenizers favor common scripts, the "4 characters per token" rule fails for text outside English. The toy model below uses made-up characters-per-token values to show the effect on a monthly budget.

Python The same message in different languages

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Do not trust invented numbers for planning. Run a sample of your own real texts through your model's tokenizer, then compute the average tokens per message.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Roughly how many tokens are 100 English words?

    A token is about three quarters of an English word, so 100 words is roughly 130 tokens. The exact count depends on the tokenizer.
  2. 2. Why is output usually priced higher than input?

    Generation is sequential: one pass per new token. Reading the prompt can be done for all input tokens at once, so it is cheaper.
  3. 3. In a chat application, why does the input size grow with every turn?

    The API is stateless. To give the model memory you send the history again, so you pay for it again.
  4. 4. What is the safest way to count tokens before sending a request?

    Heuristics are fine for planning, but only the real tokenizer gives the exact number the provider will bill and limit.

Summary

  • A token is about 4 characters or three quarters of an English word; this varies by tokenizer, content and language.
  • Providers price input and output tokens separately per million tokens, with output costing more.
  • Cost is input_tokens / 1e6 * input_price + output_tokens / 1e6 * output_price, multiplied by your traffic.
  • Chat history, system prompts, examples and retrieved documents all count as input on every request.
  • Budget the window as: window minus max output minus fixed prompt parts, and leave headroom.
  • Non-English text and code often cost more tokens per idea.

Next: turn tokens into vectors of numbers in Embeddings: Meaning as Numbers.