New Tool Radar

AI Concepts

How to Count Tokens Before an API Call with tiktoken

Count the tokens in your prompt with tiktoken before you send it, so the bill and the context window hold no surprises.

Schematic illustration with three step boxes: pick the right encoding, count the messages, and compare with the limit.

Token counts depend on the tokenizer: the same text is a different token count for different models. tiktoken is OpenAI’s tokenizer library, and counting with the encoding that matches your model gives you the number the API will actually bill. Counting before you send turns a surprise bill into a planned one.

Before you start

Python with tiktoken installed, and the model name you will call.

Pick the encoding for your model

Use the encoding that matches your model: cl100k_base for recent GPT models, o200k_base for the newer ones. The wrong encoding gives a count that is close but not the one you will be billed for.

Check the tiktoken documentation for the mapping from model name to encoding, and pin the version you use so the count does not change under you.

Count every message, not just the prompt

Count the system message, every user and assistant message, and the reply you expect. In a multi-turn conversation every prior message re-enters the token count with each new turn.

Add the overhead per message: the API adds a few tokens per message for formatting. The tiktoken cookbook shows the exact per-message overhead for chat models, so use its recipe rather than counting raw text.

Compare the total with the limit and the budget

Compare the total with the model’s context window and with your budget for the call. If the prompt is near the window, trim the history or the retrieved chunks before you send it.

Log the counted tokens with the billed tokens from the response for a few calls. If they differ, the difference is usually the reply length or a message you forgot to count.

Practice check: fictional Maple Corner support bot, one request

The table below is fictional practice material, worked out by hand from the stated counts. It is the expected result for the practice request, not a run of any tokenizer.

Part Tokens Running total Verdict
System message 320 320 Counted
History, 4 messages 1,480 1,800 Counted
User question 120 1,920 Counted
Message overhead 28 1,948 Counted
Expected reply 400 2,348 Reserved

The request totals 1,948 input tokens plus a 400-token reply reserve, or 2,348 tokens in all. It fits the window with room to spare, and the count is known before the call is sent.

Expected result. UNTESTED: the token counts in the practice table are worked out by hand, not measured by running tiktoken. Count your own messages with the encoding for your model, add the per-message overhead, and compare the total with the window before you send the call.

Limits

  • tiktoken counts OpenAI tokens: other providers and local models use different tokenizers and give different counts.
  • The reply length is an estimate until the model answers: reserve the maximum you will allow, not the average.

Sources

See all