Get Started

Cut your AI bill without cutting quality

An LLM feature bills you per token, and the bill grows quietly with every user. This mission measures what actually drives the cost, then applies one lever at a time and shows the numbers move: trimming a prompt cut its input tokens by 82%, caching served four of five repeated calls for free in about a millisecond each, batching ten jobs into one call dropped input tokens by 73%, and a small model answered an easy job as well as a big one in a fraction of the time. Measured with a real tokenizer and local models on a no-GPU laptop, so nothing here costs you a penny to try.

80 minIntermediateTools: Free. Everything runs locally with tiktoken and Ollama. The prices in the calculator are clearly labelled example figures, not a quote.Step 1 free
Tested 12 Oct 2026 · tiktoken 0.14.0, ollama 0.6.3 (Python client), Ollama running llama3.2:1b and qwen2.5:3b, Python 3.13.5, uv 0.11.9, Windows 11, no GPU
A token counter that tells you what any prompt costs before you send it, using a real tokenizer (tiktoken) and the model's own count
A response cache (SQLite, keyed on a hash of the request) that serves repeated calls for free, plus measured before-and-after numbers on trimming, capping output, batching and routing to a small model
A tiny cost calculator that turns your real token counts into pounds at prices you set yourself, so you can price your own workload
Before you start
  • Level 0 done: Install anything from GitHub, safely and The terminal survival kit.
  • uv, the Python tool from Level 0. Check with uv --version. It downloads Python 3.13 and two small packages for you.
  • Ollama with a couple of small models, from Run AI on your own PC. This mission uses llama3.2:1b and qwen2.5:3b. Pull them with ollama pull llama3.2:1b and ollama pull qwen2.5:3b (about 1.3GB and 1.9GB). No GPU needed.
  • A little Python: functions, lists and dicts. You do not need to know tiktoken or Ollama's API. Every file is complete and explained.
01/07What you actually pay for0 done
STEP 01 of 07~10 min

What you actually pay for

Before you can cut a bill, you have to know what is on it. An LLM charges per token, and the two halves of every call (what you send, what it writes) cost different amounts.

  1. Every paid LLM charges by the token, not by the request. A token is a chunk of text, very roughly 4 characters or about three quarters of a word in English, so tokenisation might be two tokens and the one. The model turns your text into tokens, works on tokens, and writes tokens back.

  2. One call has two sides, billed at two different rates:

    • Input tokens are everything you send: the system prompt, the instructions, any examples, the user's message and any documents you paste in. You usually pay less per input token.
    • Output tokens are everything the model writes back. You usually pay more per output token, often several times more, and the model makes them one at a time, so they also drive most of the waiting.
  3. There is a ceiling on the input side called the context window: the most tokens a model can take in one call (the prompt plus the space left for its answer). Go over it and the call is rejected or the oldest text is dropped. A bigger window lets you send more, but you still pay for every token you put in it, so a large window is not a reason to send more than you need.

  4. So the whole bill for one call is this:

    the only formula in this mission
    cost = input_tokens x input_price + output_tokens x output_price
  5. Everything that follows is one of three moves: send fewer input tokens, make fewer output tokens, or make fewer and cheaper calls. To measure any of it you need a project. Make one with uv:

    Terminal
    uv init --bare --python 3.13 cost
    cd cost
    uv add tiktoken ollama
  6. tiktoken is the tokenizer OpenAI-style models use, so you can count tokens on your own PC with no account and no network. ollama is the Python client for the local models you installed, which you will use to measure real timings and the models' own token counts. Drop the kit files into this cost folder: count_tokens.py, cache.py, cost_calc.py, and the demo scripts trim_demo.py, cap_demo.py, cache_demo.py, batch_demo.py, route_demo.py, stream_demo.py. Or type each one as you reach it.

  7. Confirm tiktoken works. This prints the token numbers that Hello, world! becomes for gpt-4o:

    Terminal
    uv run python -c "import tiktoken; print(tiktoken.encoding_for_model('gpt-4o').encode('Hello, world!'))"
    Output
    [13225, 11, 2375, 0]
  8. Four numbers, so four tokens: one for Hello, one for the comma, one for world and one for !. Each number is that token's id in the model's vocabulary. That is all a tokenizer does: text in, a list of token ids out, and the length of that list is what you pay for.

    TipAll the timings in this mission come from single runs on a no-GPU Windows laptop, so your seconds will differ and will swing from run to run. The token counts do not: a tokenizer is deterministic, so the same text is the same number of tokens every time, on every machine. Trust the token numbers; treat the seconds as a guide.
✓
Checkpointuv --version works, the cost folder has your kit files in it, and the tiktoken command prints [13225, 11, 2375, 0].
If it breaks'uv' is not recognised + 1 more
'uv' is not recognised
Install it as in Install anything from GitHub (winget install --id=astral-sh.uv -e on Windows, brew install uv on a Mac), then close the terminal and open a new one.
'ModuleNotFoundError: No module named tiktoken'
Run the commands from inside the cost folder, and use uv run python ... (not a bare python), so it uses the project's environment where uv add put the packages.
Still stuck? Ask the community
Next mission · AI engineeringBuild a coding agent that fixes its own mistakes
→