Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 15 min · revision 2

Where LLM Money Goes

Break one model request into what you pay for: uncached input, cache writes and reads, output and hidden reasoning, plus the system prompt, tools, context and history that never appear in the chat. See how calls per action, retries and heavy users multiply the bill, what drives latency, and which fields to log on every call.

Contentsand lesson path

What you will learn

  • Attribute token usage, cost and latency per request, user and feature from logged usage data.

01. Why it matters

The provider's invoice tells you what Lumen spent per model and per day. It cannot tell you that the new triage agent costs three times as much per click as the answer feature, that one customer's script makes a quarter of all calls, or that the biggest item in an answer's price is reasoning that nobody reads. You cannot cut what you cannot see, so cost control starts with attribution: every model call priced, tagged with the request, user and feature it served, and timed, because latency is the other bill your users pay.

The Tokens and Context skill priced a single request from its usage block. This skill scales that up to a running product. This lesson maps where the money goes. Lesson 2 turns usage logs into a cost report, and lessons 3 to 5 apply the levers: caching, choosing and routing models, batch jobs, budgets and per-user limits.

02. What one answer is billed for

Providers bill tokens in a few categories, each with its own price per million: input processed at the full rate, input written to a prompt cache (usually a little dearer), input read from the cache (much cheaper), and output. A chat window shows only the user's question and the visible answer, but the request carried much more, and you pay for all of it:

  • System prompt and tool definitions: sent with every call. Some providers also add their own instructions when tools are present and bill them as input.
  • Retrieved passages and conversation history: often the largest part of the input in a RAG product, and history grows with every turn.
  • Reasoning tokens: a reasoning model thinks before it answers, and those tokens are billed as output even when the API returns only a summary of the thinking, or none at all.

Here is one answer from Lumen's help-centre assistant, priced part by part. The part sizes come from Lumen's own prompt assembly; the billing categories come from the usage block of the response. The system prompt and tool definitions were served from the prompt cache.

Python · request_bill.py
"""Where the money for one Lumen answer goes, part by part."""

# US dollars per million tokens: example figures, not a quote. Pass in your own.
PRICES = {"input": 2.00, "cache_read": 0.20, "output": 10.00}

# (part, tokens, how it was billed). Token counts per part come from your own
# prompt assembly; the billing categories come from the usage block.
PARTS = [
    ("system prompt", 1_150, "cache_read"),
    ("tool definitions", 620, "cache_read"),
    ("retrieved passages", 2_400, "input"),
    ("conversation history", 900, "input"),
    ("user question", 40, "input"),
    ("reasoning (hidden)", 640, "output"),
    ("visible answer", 180, "output"),
]

costs = [(name, tokens, tokens * PRICES[rate] / 1e6) for name, tokens, rate in PARTS]
total = sum(cost for _, _, cost in costs)
for name, tokens, cost in costs:
    print(f"{name:<21} {tokens:>6,} tokens  ${cost:.6f}  {cost / total:6.1%}")
print(f"{'total':<21} {sum(t for _, t, _ in costs):>6,} tokens  ${total:.6f}")
Terminal
python request_bill.py
Expected output
system prompt          1,150 tokens  $0.000230    1.5%
tool definitions         620 tokens  $0.000124    0.8%
retrieved passages     2,400 tokens  $0.004800   31.5%
conversation history     900 tokens  $0.001800   11.8%
user question             40 tokens  $0.000080    0.5%
reasoning (hidden)       640 tokens  $0.006400   42.0%
visible answer           180 tokens  $0.001800   11.8%
total                  5,930 tokens  $0.015234

The question the user typed is half a percent of the cost. Hidden reasoning is the largest single item, and the retrieved passages come second. The cached system prompt and tools cost little because they were cached: at the full input rate those 1,770 tokens would be about a fifth of the bill.

Usage fields differ between providers, so normalise them before you add anything up. In Anthropic's Messages API, input_tokens counts only the uncached remainder, and the prompt size is input_tokens + cache_creation_input_tokens + cache_read_input_tokens. In OpenAI's Responses API, input_tokens already includes the cached tokens reported in input_tokens_details.cached_tokens (and, on GPT-5.6 and later, the cache-write tokens in input_tokens_details.cache_write_tokens, which cost more than plain input), and output_tokens includes output_tokens_details.reasoning_tokens. Pick one convention for your logs and convert at write time.

03. Multipliers: calls per action, retries and the long tail

A single request is rarely the unit that matters. Four things multiply it:

  1. Calls per user action. An agent calls the model once per step and resends the growing conversation each time, so one click can be five or more calls, each larger than the last. Give every call of one action a shared action_id (or trace id) so that you can price the click, not just the call.
  2. Retries and re-asks. A call that fails validation and is asked again costs twice. A call your client abandoned after a timeout may still have been processed and billed, and you may have no usage block to log for it.
  3. Output limits. max_tokens caps the worst case of one call. An answer cut off at the limit is still billed, and if your code then retries, a cap set too low costs more than it saves.
  4. The long tail of users. Cost per user is heavily skewed: a few power users or one integration script can outspend thousands of ordinary users. Look at the top users and at percentiles; a mean hides both.
One click on the triage agent, call by call (illustrative)
step  prompt tokens  of which cached  output tokens
   1          2,950            2,300            480
   2          3,600            2,950            510
   3          4,250            3,600            430
   4          4,900            4,250            560
one click: 15,700 prompt tokens and 1,980 output tokens in four calls

Without caching, all 15,700 prompt tokens would be billed at the full input rate. With the stable prefix and earlier turns cached, each call pays the cheap cache-read rate for everything before the last step, and the full rate (or the slightly higher cache-write rate) only for what that step added. That is why lesson 3 starts with prompt caching, and why an agent feature needs its cost reported per action.

04. Latency is part of the bill

Users pay in seconds, and three numbers describe it:

  • Time to first token (TTFT): queueing plus processing the prompt. Long uncached prompts take longer to process; a cached prefix usually starts faster. For a reasoning model, the first visible token also waits for the thinking.
  • Output speed: tokens generated per second for one request. Total time is roughly TTFT plus output tokens divided by that speed, so long answers and long reasoning are slow as well as expensive.
  • Throughput: requests or tokens per second that a deployment can serve at once. It matters for rate limits and decides the price of self-hosting, which lesson 4 estimates.

Report latency as percentiles per feature (p50 for the typical user, p95 for the unlucky one), and measure an agent per action: the sum of its calls plus the tools between them.

05. One usage record per call

Attribution needs one record per model call, written by your code at the moment the response arrives. This is the record Lumen writes, one JSON object per line of usage.jsonl:

JSON · one record of usage.jsonl, pretty-printed
{
  "request_id": "req-00731",
  "ts": "2026-10-08T14:02:11",
  "action_id": "act-9c41e2d0",
  "tenant": "acme",
  "user_id": "u-0042",
  "feature": "triage_agent",
  "model": "large",
  "attempt": 1,
  "input_tokens": 50,
  "cache_write_tokens": 650,
  "cache_read_tokens": 3600,
  "output_tokens": 431,
  "reasoning_tokens": 270,
  "ttft_ms": 466,
  "latency_ms": 6500
}

A few rules make these records trustworthy:

  • Tokens come from the response, never from estimates made later. Here input_tokens follows the Anthropic convention (uncached remainder only) and reasoning_tokens is a part of output_tokens, not an addition to it. In your logs, model should hold the exact model id that the API returned; the examples in this skill use the placeholders large and small.
  • Every dimension you will ever group by is logged now: feature, user, tenant, action, attempt, and in a real system the prompt version as well. You cannot add a tag to last month's calls.
  • Prices stay out of the record. Keep a price table with the date each price took effect, copied from your provider's pricing page, and compute cost when you analyse. Prices change, and cache, batch and long-context rates differ from the base rate.
  • No prompt or answer text. A usage log is for counting; it should not become a second copy of your users' data.

Each record also tells you which lever to pull. Repeated prefixes point to prompt caching and repeated questions to response caching (lesson 3). Expensive easy requests point to a smaller model or a router (lesson 4). Large outputs point to tighter output limits and shorter formats, and large inputs to trimming context, as the Tokens and Context skill showed. Offline work points to a batch API, and runaway users or features to budgets and limits (lesson 5).

06. Check your understanding

Practice check · 3 questions

Reading usage and attributing cost

Apply the ideas of this lesson to three new situations. Each question has one best answer.

Q1. A 10,000-token prompt was served mostly from cache. Provider A reports input_tokens 2,000 and cache_read_input_tokens 8,000. Provider B reports input_tokens 10,000 with cached_tokens 8,000 in its details. Your log adds input and cached tokens for both. What goes wrong?
Q2. A reasoning model returns a 150-token visible answer, and the usage block reports output_tokens 1,400. The thinking itself was not returned. How many output tokens are you billed for?
Q3. The triage agent makes between three and seven model calls per user click. Which extra field lets you report cost per click instead of cost per call?
Practice only: not graded, not saved, not evidence.

07. Apply it

Start the practice project for this skill on your RAG API:

  1. Wrap your model client so that every call appends one record to usage.jsonl with the fields above: tokens normalised to one convention, feature, user, tenant, action id, attempt, prompt version, model id, time to first token (from the first streamed event) and total latency.
  2. Create prices.csv with one row per model and columns for input, cache write, cache read and output prices, plus the date and the URL of the pricing page you copied them from.
  3. Run your evaluation set through the API once and keep that log. It is the before baseline: lesson 2 turns it into cost per 1,000 queries, and lessons 3 and 4 try to lower it without lowering the pass rate.

08. Review prompt

Without looking back, answer in your own words:

  1. Name the four billing categories of input and output, and three kinds of token that a user never sees but you pay for.
  2. Why can one user action cost far more than one request, and which field makes that visible?
  3. How do TTFT, output speed and throughput differ, and which of them does a long answer affect?
  4. Why do you keep prices out of the usage records, and what do you keep in them instead?

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check