Case File: The Token Bill
Language models read and bill in tokens, not words, and they can only read so many at once. Count tokens in a day of real prompts, find the ones that would be cut off, and work out the monthly bill on a large and a small model. Real Python on prompts generated for you alone.
Sabi Logistics · Assistant 1
The Token Bill
The customer assistant is a hit, and nobody knows what a month of it will cost or how much of each message the model really reads
Chidi Founder · Phone call
Chidi. The customer assistant has been live for a week and people love it. Then Zainab from finance asked a question I could not answer: “What does this cost per month, and what happens if it doubles?”
I know the vendor charges “per token”, and I know the model “can only read so much”, but I have no feel for either. Emeka has pulled a day’s sample of what customers send, one prompt per line.
Tell me how many tokens we send in total, how many prompts are so long that the model would cut them off (so it never sees the end), which one is the longest, what a month costs on the large model, and how much we would save if the small model could do the job. I want numbers I can put in front of Zainab.
The brief
What you need to know first.
- A language model (LLM) does not read letters or words. It reads tokens: pieces of text, roughly a short word or part of a word. Real tokenizers are learned from data and differ between models, so a real count must come from the vendor’s own tokenizer. For this lab a token is simply a run of letters and digits or a single punctuation mark, which
re.findall(r"\w+|[^\w\s]", text)finds:"Hello, Lagos."is 4 tokens (Hello,,,Lagos,.). - Two limits and one bill follow from tokens:
- The context window is the most tokens the model can read at once, your prompt and its reply together. Longer input is cut off or rejected, so the model may never see the part that mattered (often the end, where the question is).
- Cost is charged per million tokens, with different prices for input (what you send) and output (what it writes, usually several times dearer).
- Latency grows with the tokens too.
- The arithmetic:
cost_per_request = input_tokens × price_in / 1,000,000 + output_tokens × price_out / 1,000,000. For a month, multiply by requests per day and by 30. Use the average input tokens per prompt (df["tokens"].mean()), because a month is many prompts. - Count once, store it:
df["tokens"] = df["text"].apply(count_tokens), thendf["tokens"].sum(),(df["tokens"] > LIMIT).sum()anddf["tokens"].idxmax()answer the questions.df.loc[idx, "prompt_id"]reads a column from that row. - Sending less is the cheapest saving: shorter instructions, only the relevant documents, a smaller model for easy questions. Prices change often, so these numbers are your estimate; always check the vendor’s current price list.
The lab gives you prompts.csv and five blanks. When all five are right it shows a report reference starting with GT{. Submit it at the bottom of the page.
Preparing your data…
Zainab: “Usage doubles in December. Without recalculating anything, what happens to the bill, and what are two things we could do to stop it doubling?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Chidi: “So the bill is not a mystery, it is arithmetic, and a handful of very long prompts are doing the damage.”
What a senior engineer would do next. Log tokens in and out for every request from day one, so the bill and the long-prompt problem are visible without guessing. Put a budget on each request (a maximum prompt length that trims or summarises old messages instead of failing), use the small model first and escalate only when needed, and cache repeated prompts. Always test the saving: a cheaper model is only cheaper if the answers are still good enough, which is what the evaluation case later in this course measures.
The traps. Counting words instead of tokens; forgetting that the reply is billed too, at a higher price; judging a month from one quiet day; and letting a long conversation grow without trimming until it is cut off mid-question.
Write the cost note for finance
Write the short note for Zainab: the total tokens in the day's sample and the longest prompt, how many prompts exceed the context limit and what that means for the answers, the monthly bill on the large model and what the small model would save (and what must be tested before switching), and two ways to keep the bill under control as usage grows.
🧪 Want to practise this on your own computer? See how to set up your lab →