AI Engineering Tutorial · LLM Basics · Lesson 2 of 25

Tokens and Context Windows

How models read text as tokens, what the context window limits, and why tokens drive cost and latency.

Create a free account to save this page, take notes and track your progress. Sign in

Tokens

Models don't read characters or words; they read tokens, pieces of text from a fixed vocabulary. In English, one token is roughly 4 characters or three quarters of a word; code, other languages and unusual words use more tokens.

  • Input tokens: everything you send (system prompt, conversation, documents, tool definitions).
  • Output tokens: what the model generates.
  • Pricing is per million input and output tokens; output tokens usually cost more.

The context window

The context window is the maximum number of tokens (input plus output) the model can handle in one request. Everything the model "knows" about your request must fit in it; the model has no memory between requests unless you send the history again.

Estimate tokens and cost for one requestTry it yourself
def estimate_tokens(text):
    return max(1, len(text) // 4)          # rough rule for English text

system = "You are a helpful support assistant for an online bookshop."
history = ["Where is my order 10042?", "It shipped yesterday and arrives Friday."] * 20
question = "Can I change the delivery address?"
total = estimate_tokens(system) + sum(estimate_tokens(m) for m in history) + estimate_tokens(question)
print("estimated input tokens:", total)
price_in, price_out = 3.00, 15.00          # illustrative $ per million tokens
print(f"cost of one call with 300 output tokens: ${(total * price_in + 300 * price_out) / 1e6:.5f}")

Output

estimated input tokens: 342
cost of one call with 300 output tokens: $0.00553

For exact counts, model APIs provide token-counting endpoints; the 4-characters rule is only an estimate. See Context windows and Token counting.

Test yourself

Why does a long chat get more expensive with every message?

  1. The model charges a monthly fee per chat
  2. The whole history is sent again as input tokens on every request
  3. Output tokens are free only for the first message
  4. Tokens expire after an hour
Show answer

Answer: The whole history is sent again as input tokens on every request

Model APIs are stateless; each request includes the conversation so far, so input tokens grow as the chat grows.

Was this page helpful?