← Building with AI

What tokens are

Models read and write in small pieces of text.

Start this lesson

Models don't read whole words; they read tokens, small chunks of text. A common word may be one token, while a long or rare word may be split into several. For English, a handy rule of thumb is about 4 characters per token, or about 3 words for every 4 tokens. Tokens matter because limits and prices are counted in tokens.

# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic
client = anthropic.Anthropic()

count = client.messages.count_tokens(
    model="claude-sonnet-5-5",
    messages=[{"role": "user", "content": "How many tokens is this sentence?"}],
)
print(count.input_tokens)

For exact numbers, ask the provider (many have a count-tokens endpoint) or read usage on the reply. For quick planning, an estimate is fine.

Other languages, code and numbers often use more tokens per character than plain English. Treat rules of thumb as rough.

The pretend model in this unit counts tokens as characters divided by 4, rounded up, for each message.