Beginner project

Token and cost explorer

Builds on 3 lessonsabout 1 h 25 min of reading first

In this project

What you need first

Skills you will practise

  • LLM fundamentals
  • Tokenization
  • Embeddings
  • Sampling and decoding
  • Model selection
Skip to the tasks

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.

Criterion0123
TokenizerWord or character count usedOne family’s tokenizer used for both modelsRight tokenizer for each modelRight tokenizer for each model, checked against real usage numbers
ArithmeticWrong or uncheckedCorrect for one prompt by handHand-checked on three promptsHand-checked, and the units (per token, per million) are explicit
KV-cache estimateMissingFormula applied with guessed numbersFormula applied with published config values, “not published” where missingAs level 2, with sequence length = input plus output tokens stated and checked by hand
AssumptionsHiddenSome listedAll listed in the report, with source and date for every price and config rowAs level 2, and the output-token assumption is justified
FindingsDescribe the numbers onlyState a conclusionConclusion tied to the tableFive 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

Diagram: Prompts go through each model’s own tokenizer to get token counts.

Prompts file

Tokenizer per model

Cost model

Price table

Model-config table

Report table and findings

Diagram summary: Prompts go through each model’s own tokenizer to get token counts. The cost model combines the counts with the price table and the model-config table, which you fill in, and writes the report.
DecisionAlternative rejectedWhy
Count with each model’s own tokenizerOne tokenizer for bothCounts from another family are only estimates, so the comparison would be unfair.
Prices and config in tables the user editsNumbers in the codePrices 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.