๐Ÿ› ๏ธ How-To

How to Count Tokens and Estimate Your AI API Bill

Which tokenizer to use for which model, how Claude and Gemini count tokens, what counts as input and output, and a cost formula you can fill in with your own prices.

To count tokens, run your text through the tokenizer of the model you will call. For OpenAI models that means the o200k_base or cl100k_base encoding in OpenAI's open-source tiktoken library. Anthropic and Google use their own tokenizers and offer count endpoints in their APIs. For a quick estimate, paste the text into the Token Counter.

To estimate the bill, count input and output tokens separately, multiply each by its price per million tokens and add them up. Output usually costs more, and on reasoning models it includes thinking you never see. The sections below show the code, the formula and a worked example with placeholder prices you replace with your provider's.

How many tokens your text is

OpenAI and Google both publish a rule of thumb for English:

โ‰ˆ4

characters per token (OpenAI and Google)

โ‰ˆ75

words per 100 tokens (OpenAI)

60โ€“80

English words per 100 tokens (Google, Gemini models)

Source: OpenAI Help Center; Google Gemini API docs, checked October 2026

Rules of thumb break down outside plain English. Here is a 44-word support message in English and its translations into Spanish, German and Russian, counted with the two OpenAI encodings. In this sample, the newer o200k_base vocabulary needs far fewer tokens for the non-English versions.

Tokens for one support message in four languages
Tokens for one support message in four languagesEnglish: 49 / 49; Spanish: 64 / 77; German: 56 / 74; Russian: 67 / 124o200k_basecl100k_base4949English6477Spanish5674German67124Russian

Source: Measured with the o200k_base and cl100k_base vocabularies, October 2026

Show data
Languageo200k_basecl100k_base
English4949
Spanish6477
German5674
Russian67124

Code behaves differently again: a 241-character Python function came to 58 tokens with both encodings, about 4.2 characters per token. Tokenizers also change between model generations. Anthropic's docs say that its newer tokenizer, used from Claude Opus 4.7 onward, produces roughly 30 percent more tokens for the same text than earlier Claude models, so do not reuse old counts after a model switch (Anthropic).

Count tokens for OpenAI models with tiktoken

OpenAI publishes its tokenizers as tiktoken. Its model table maps model names to encodings. As of October 2026:

Which tiktoken encoding each OpenAI model uses

Encoding Models
o200k_base GPT-4o, GPT-4.1, GPT-4.5, the GPT-5 family, o1, o3, o4-mini
cl100k_base GPT-4, GPT-3.5 Turbo, text-embedding-3-small and -large, text-embedding-ada-002
o200k_harmony gpt-oss open-weight models
python
import tiktoken

enc = tiktoken.get_encoding("o200k_base")
text = "How many tokens is this sentence?"
print(len(enc.encode(text)))   # 7

tiktoken.encoding_for_model("gpt-4o") picks the encoding from a model name, but it raises an error for names its table does not know yet. In that case, choose the encoding yourself.

Two details make the API count a little higher than the text count:

  • Message formatting. Each chat message adds a few tokens for roles and boundaries. OpenAI's cookbook uses 3 tokens per message plus 3 to prime the reply, and calls this "an estimate, not a timeless guarantee" (OpenAI Cookbook).
  • Tools and files. Function definitions, images and files are counted on top of your text.

For an exact number that includes all of this, OpenAI's Responses API has a counting endpoint, POST /v1/responses/input_tokens, which takes the same input as a real request and returns input_tokens (OpenAI).

Count tokens for Claude and Gemini with their APIs

tiktoken does not know other vendors' vocabularies, so its count is only a ballpark for Claude or Gemini. Both vendors offer an endpoint instead.

Claude. POST /v1/messages/count_tokens accepts the same body as a message request, including the system prompt, tools, images and PDFs, and returns input_tokens. Anthropic says it is "free to use" but rate limited by usage tier, and that the count is an estimate that "might differ by a small amount" from the real request (Anthropic).

python
import anthropic

client = anthropic.Anthropic()
count = client.messages.count_tokens(
    model="your-claude-model-id",
    system="You are a support assistant.",
    messages=[{"role": "user", "content": "Where is my invoice?"}],
)
print(count.input_tokens)

Gemini. The countTokens method returns the tokens in the input only (Google). With the Python SDK:

python
from google import genai

client = genai.Client()
result = client.models.count_tokens(
    model="your-gemini-model-id",
    contents="Where is my invoice?",
)
print(result.total_tokens)

Gemini counts media at fixed rates on its default settings: an image with both sides at 384 pixels or less is 258 tokens, larger images are split into 768ร—768 tiles of 258 tokens each, video is 263 tokens per second when processed at the default one frame per second, and audio is 32 tokens per second (Google). As of October 2026 the same page lists lower video rates for low resolution and for its agentic video mode, so check it for your model and settings.

Know what counts as input and what counts as output

Your bill has two meters, and more goes into each than the text you type.

Input tokens are everything you send: the system prompt, the whole chat history, tool and function definitions, retrieved documents, images and files. In a chat, the history is resent with every turn.

Output tokens are everything the model generates: the visible answer plus any reasoning. OpenAI says reasoning tokens "are billed as output tokens" even though the API does not show them (OpenAI). Google labels its output price "including thinking tokens", and Anthropic bills thinking as output too.

Each API reports both meters in the response, so you can log real usage:

Where each API reports token usage

API Input Output Cache and reasoning
OpenAI Responses input_tokens output_tokens input_tokens_details.cached_tokens, output_tokens_details.reasoning_tokens
OpenAI Chat Completions prompt_tokens completion_tokens prompt_tokens_details.cached_tokens, completion_tokens_details.reasoning_tokens
Anthropic Messages input_tokens output_tokens cache_creation_input_tokens, cache_read_input_tokens
Gemini generateContent prompt_token_count candidates_token_count cached_content_token_count, thoughts_token_count

With Anthropic's prompt caching, input_tokens covers only the uncached part, so add the two cache fields to get the full input.

Estimate the bill with one formula

Prices are quoted per million tokens, with separate rates for input and output. The formula:

cost = (input_tokens ร— input_price + output_tokens ร— output_price) รท 1,000,000

Here is a worked example for a support bot. The prices are placeholders, not any vendor's real prices: replace them with the current rates from your provider's pricing page.

  1. Measure one typical request: 800 tokens of system prompt plus 1,200 tokens of history and question make 2,000 input tokens. The answer is 400 output tokens.
  2. Multiply by volume: 10,000 requests a day gives 20,000,000 input tokens and 4,000,000 output tokens.
  3. Apply the placeholder prices of $1.00 per million input tokens and $5.00 per million output tokens: $20 for input plus $20 for output, or $40 a day.
  4. Multiply by 30 for a month: $1,200.

2,000 + 400

input and output tokens per request

$40

per day at 10,000 requests

$1,200

per month

Source: Worked example with placeholder prices of $1 and $5 per million tokens

Now change one assumption. If a reasoning model spends 800 hidden tokens thinking before each 400-token answer, output triples to 1,200 tokens per request. At the same placeholder prices, output costs $60 a day instead of $20, and the bill doubles to $80 a day.

Chat history grows the input side the same way. With an 800-token system prompt, 150-token questions and 300-token answers, turn n sends 800 + 150n + 300(n โˆ’ 1) input tokens:

Input tokens per turn in one conversation (example)

Turn Input tokens sent Running total
1 950 950
2 1,400 2,350
5 2,750 9,250
10 5,000 29,750

A ten-turn chat sends about 30,000 input tokens to produce 3,000 tokens of answers. This is also why long chats hit the context window; see tokens and context windows explained.

Reduce the tokens you pay for

Start with whatever is sent on every request, because savings there multiply by your volume.

  • Trim the system prompt and tool definitions. Cut examples the model no longer needs and tools it never calls in that flow.
  • Cap the output. Set max_output_tokens (OpenAI Responses), max_tokens (Anthropic) or maxOutputTokens (Gemini), and ask for the length you need.
  • Summarize or drop old history. Keep the last few turns verbatim and a short summary of the rest.
  • Send excerpts, not whole documents. Retrieve the relevant passages; see what RAG is.
  • Use prompt caching. Put the stable part of the prompt first. As of October 2026, OpenAI, Anthropic and Google all bill cached prefixes below the normal input rate; Anthropic, for example, charges cache reads at 0.1ร— the base input price on most models and cache writes at 1.25ร— or 2ร—, depending on how long the cache lives (Anthropic pricing). Multipliers vary by model, so check your provider's page.
  • Batch work that can wait. OpenAI, Anthropic and Google each offer a batch API at 50% of the standard price, with results within a day (OpenAI, Google).
  • Match the model to the step. Use a smaller model, or a lower reasoning effort or thinking budget, for classification, extraction and routing.

To count tokens, compare prices per model or track what each request costs, start with these:

Prompt Token Counter

Monitor token usage for effective AI communication.

OpenAI Cookbook

Resource for learning to use AI through practical examples.

Free + from $4.00/m
liteLLM

Access over 100 language models with ease and reliability.

Openrouter.ai

Access multiple large language models for diverse AI tasks.

Free + from $0.50/m
Token Counter Count tokens, words and characters and see how much of a context window you use.

FAQ

How many tokens is 1,000 words?

For typical English text, about 1,300 to 1,700 tokens. OpenAI's rule of 75 words per 100 tokens gives about 1,330; Google's 60 to 80 words per 100 tokens gives 1,250 to 1,670. Code and most other languages need more.

Can I use tiktoken to count tokens for Claude or Gemini?

Only as a rough estimate. Each vendor uses its own tokenizer, so use Anthropic's count_tokens or Gemini's countTokens when the number matters.

Do the count endpoints cost anything?

Anthropic's token counting is free and rate limited by usage tier. OpenAI's and Google's docs describe their counting endpoints without listing a separate charge; check their pricing pages before you call them at volume.

Why is my bill higher than my estimate?

The usual causes are reasoning tokens, chat history resent on every turn, tool definitions, retries and message formatting tokens. Compare your estimate with the usage fields in real responses to find the gap.

Are images and files counted as tokens?

Yes. They are converted to input tokens, and the rate depends on size and the vendor. Count a sample request with the vendor's endpoint to see the real number.

Share

Roman Shalabanov

Founder, Ask AI For It

Roman is a web developer and indie maker with nearly two decades of experience building web projects. With a background in computer science education, he writes about AI tools, software, product launches, distribution, SEO, and growing online businesses, drawing on his own experience building and promoting web products.

Explore in the directory

Portkey

Centralized management for AI applications and workflows.

Free + from $49/m
Openlit

Real-time monitoring and management for AI application performance.

PromptLayer

Manage and optimize prompts for language model applications.