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 2 of 3

Trim history to a token budget

Try budget=30 and budget=100 and see how many messages survive.

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?

Trim history to a token budget
Sign in to run this example in your browser.

Expected output

kept 5 of 13 messages
system | Be brief.
user | question number 5
assistant | answer number 5
user | question number 6
assistant | answer number 6