Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.4 — Tokens, Context Windows, Cost and Rate Limits
Python · Example 1 of 3

Toy tokens and the cost formula

The rates are examples for the lesson only. Real tokenizers split text differently.

Models read and write tokens, not characters or words. A context window caps how many tokens fit in one call, cost is a simple formula of input and output tokens times rates, and rate limits mean you must trim history and retry with backoff.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write estimate_cost(input_tokens, output_tokens, input_rate, output_rate) for rates quoted per million tokens, rounded to 6 decimals.

02

Write backoff_delays(retries, base, cap) returning the list of waits (no jitter) for each retry.

03

Write first_that_works(models, prompt) that tries (name, function) pairs in order and returns the first success with its name, or raises a clear error if all fail.

Explain it without notes

01

What is a token, and why is counting characters a poor way to budget?

02

Explain the cost of a model call, and why a long chat gets expensive.

03

How should a client react to HTTP 429, and what should it not retry?

04

You have a 10-turn chat and the provider quotes per-million-token rates. How would you estimate its total cost, and name two ways to cut it?

Toy tokens and the cost formula
Sign in to run this example in your browser.

Expected output

['Toke', 'niza', 'tion', 'spli', 'ts', 'text', ',', 'not', 'word', 's', '.']
characters: 36 toy tokens: 11
example cost: 0.0304