0%

An LLM Call Is an HTTP Request · practice

Count What a Call Cost

Every call to a real model costs money, and the response tells you how much work it was. The last part of the document you have been reading is usage:

"usage": {
  "prompt_tokens": 6,
  "completion_tokens": 42,
  "total_tokens": 48
}

prompt_tokens counts what the model read: every message you sent. completion_tokens counts what it wrote. total_tokens is the two added together.

Where these numbers come from

A real model counts with its own tokenizer, the splitting you saw in Lesson 2. The scripted model only estimates: it counts the characters in your messages, divides by four, and rounds up. What is a Python list? is 22 characters, and 22 divided by 4 is 5.5, which rounds up to 6. The 166-character answer gives 42. A real model would report different numbers for the same text, so treat the scripted counts as a stand-in for the real thing, not a measurement.

Prices are per million tokens

Providers publish two prices per model: one for input tokens and one for output tokens, each in dollars per million tokens. Output almost always costs more. llm_basics.py has two price tables, dated in a comment above them:

LIVE_PRICES = {"input_per_million": 0.03, "output_per_million": 0.13}
LARGER_MODEL_PRICES = {"input_per_million": 2.00, "output_per_million": 10.00}

LIVE_PRICES is what the model behind pl-live cost on OpenRouter when this lesson was written. LARGER_MODEL_PRICES is anthropic/claude-sonnet-5.5, a large, well-known model from the same list, for comparison. Prices change, so these are a snapshot.

To price a call, price each side on its own and add them:

prompt_tokens = 6
completion_tokens = 42

input_cost = prompt_tokens * 0.03 / 1_000_000
output_cost = completion_tokens * 0.13 / 1_000_000
cost = input_cost + output_cost
print(f"${cost:.8f}")

That prints $0.00000564. Six input tokens at three cents per million cost $0.00000018, and 42 output tokens at thirteen cents per million cost $0.00000546. The underscores in 1_000_000 are only there to make the number readable; Python ignores them.

The :.8f in the f-string shows the number with eight decimal places. Without it, Python prints very small numbers in scientific notation, such as 5.64e-06, which means 5.64 millionths.

Use prompt_tokens and completion_tokens, never total_tokens. The total mixes input and output together, and they have different prices, so no single price can turn it into the right cost.

Build call_cost

call_cost(usage, prices) takes a usage dictionary like the one above and a price table with the keys input_per_million and output_per_million, and returns the cost in dollars as a float. Take the numbers from the dictionaries rather than writing them into the function, so the same function prices any call on any model.

Run python llm_basics.py when it works. The demonstration prices the Lesson 4 call at both price tables, once and ten thousand times:

At pl-live prices: $0.00000564 per call, $0.0564 for 10,000 calls
At the larger model's prices: $0.00043200 per call, $4.3200 for 10,000 calls

The same question costs about 77 times more on the larger model. For one call, both are tiny. For a program that answers ten thousand questions a day, the choice of model is a real decision, and it is why later chapters keep a budget.

A response reports prompt_tokens 1,000, completion_tokens 200 and total_tokens 1,200. Why not multiply 1,200 by one price?

Task

In llm_basics.py, replace the unfinished body of call_cost(usage, prices).

Return the cost of one call in dollars as a float: usage["prompt_tokens"] times prices["input_per_million"], plus usage["completion_tokens"] times prices["output_per_million"], with each product divided by one million. Do not use total_tokens.

Run python llm_basics.py to compare the two price tables, then check your work.