Beginner project
Token and cost explorer
Builds on 3 lessonsabout 1 h 25 min of reading first
In this project
What you need first
- Completed:How LLMs workLLM foundations
- Completed:Tokenization and embeddingsLLM foundations
- Completed:Sampling parameters and the model landscapeLLM foundations
Skills you will practise
- LLM fundamentals
- Tokenization
- Embeddings
- Sampling and decoding
- Model selection
Problem
Build a small tool that tells you what a prompt will cost before you send it. For each prompt it counts tokens with the right tokenizer for two models, then estimates the price and the KV-cache memory of one request. You use it to make one real decision, such as which model to use for a feature.
Requirements
- A script or notebook in any language. Input: a JSONL file with one prompt per line (or one text file per prompt).
- Two models you might really use. Count tokens for each with the tokenizer or token-counting tool that the provider documents for that model, not with a stand-in from another family.
- A price table and a model-config table that you fill in yourself from the providers’ current pages, with the date you read them. The program reads these tables; it does not contain prices or config numbers in its code.
- A cost estimate per request: input tokens times the input price, plus an assumed number of output tokens times the output price. State the output-token assumption.
- A KV-cache estimate per request, using the formula from How LLMs work. Sequence length is the input tokens plus your assumed output tokens. Reuse the model-config numbers you found for the Homework of that lesson where you can. If a number is not published for a model, write “not published” and skip that estimate. Do not guess it.
- Every assumption is printed in the report, labelled as an assumption.
- Constraint: use at least 10 prompts, including one short, one long, one code, one in a language other than English, and one with many numbers.
- Acceptance: a reviewer can change one price in the table and see the cost change, without editing code.
Prerequisites
Tasks
- To do: Write 10 prompts of different kinds (short, long, code, non-English, numbers) and save them in a file.
- To do: Create the price table and the model-config table, with the source and date for every row.
- To do: Count tokens for each prompt with each model’s own tokenizer, and compare with the usage numbers a real response returns for at least one prompt.
- To do: Compute cost per request, and cost per 1,000 requests, for both models.
- To do: Compute KV-cache bytes per request for the models where the config is published, reusing your How LLMs work homework numbers.
- To do: Produce the 10-prompt results table (prompt, tokens per model, cost per model, KV-cache per model).
- To do: Draw the data-flow diagram in the Architecture part and write the two-line “why this, not that”.
- To do: Write five sentences of findings that end in one real decision (which model, or which prompt to shorten, and why).
- To do: Stretch: report the token ratio of code to prose of similar length for each model.
- To do: Stretch: switch one model for a newer one, recount the 10 prompts, and report how the counts and costs change.
Evaluation
Record real measurements and never invent numbers. Mark every estimate as an estimate and give the source and date of every price and config value.
| Criterion | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Tokenizer | Word or character count used | One family’s tokenizer used for both models | Right tokenizer for each model | Right tokenizer for each model, checked against real usage numbers |
| Arithmetic | Wrong or unchecked | Correct for one prompt by hand | Hand-checked on three prompts | Hand-checked, and the units (per token, per million) are explicit |
| KV-cache estimate | Missing | Formula applied with guessed numbers | Formula applied with published config values, “not published” where missing | As level 2, with sequence length = input plus output tokens stated and checked by hand |
| Assumptions | Hidden | Some listed | All listed in the report, with source and date for every price and config row | As level 2, and the output-token assumption is justified |
| Findings | Describe the numbers only | State a conclusion | Conclusion tied to the table | Five sentences tied to one real decision, with what would change it |
Pass mark: at least 2 on every criterion, so 10 out of 15 with no criterion below 2.
Architecture
| Decision | Alternative rejected | Why |
|---|---|---|
| Count with each model’s own tokenizer | One tokenizer for both | Counts from another family are only estimates, so the comparison would be unfair. |
| Prices and config in tables the user edits | Numbers in the code | Prices change often. A table can be updated and dated without touching code. |
Interview questions
Provenance: Generated practice. Not reported from any company.
- Why can you not compare token counts across model families? Practise with F5 in the Fundamentals bank.
- Your API cost tripled after users started sending longer prompts. Where does the extra cost come from? Practise with S4 in the Scenario-based bank.
- Explain what the KV-cache is and why it limits concurrency. Practise with F4 in the Fundamentals bank.
Portfolio checklist
- To do: The README says what the tool answers and shows the results table.
- To do: Prices and config values are in a table with sources and dates.
- To do: The findings name one decision and what would change it.
- To do: Nothing in the repository presents an invented number as real.