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.
- 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:1bandqwen2.5:3b. Pull them withollama pull llama3.2:1bandollama 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.
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.
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
tokenisationmight be two tokens andtheone. The model turns your text into tokens, works on tokens, and writes tokens back.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.
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.
So the whole bill for one call is this:
the only formula in this missioncost = input_tokens x input_price + output_tokens x output_priceEverything 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:
Terminaluv init --bare --python 3.13 cost cd cost uv add tiktoken ollamatiktokenis the tokenizer OpenAI-style models use, so you can count tokens on your own PC with no account and no network.ollamais 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 thiscostfolder: 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.Confirm tiktoken works. This prints the token numbers that
Hello, world!becomes for gpt-4o:Terminaluv run python -c "import tiktoken; print(tiktoken.encoding_for_model('gpt-4o').encode('Hello, world!'))"Output[13225, 11, 2375, 0]Four numbers, so four tokens: one for
Hello, one for the comma, one forworldand 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.
uv --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
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'
cost folder, and use uv run python ... (not a bare python), so it uses the project's environment where uv add put the packages.