Written with Claude and checked against the code and probes it cites.
Every LLM API returns a usage block with each response. It looks like the easy part: read the token counts, multiply by the price list, done. In practice it is where cost tracking quietly drifts from the invoice, and the drift rarely shows up as an error. It shows up as a number that is a bit off, every day.
These are the differences that matter, from building the metering in Garmr, an open-source gateway that has to price every request exactly once.
1. "Input tokens" means different things
The biggest trap is that two major APIs count cached input in opposite ways.
-
Anthropic's Messages API reports cache reads and cache writes in their own fields, beside
input_tokens. Input is the uncached part only. Cache writes are split again into five-minute and one-hour writes, which are priced differently. -
OpenAI's Chat Completions API reports
prompt_tokensincluding cached tokens, with the cached part broken out underprompt_tokens_details.
A tracker that treats both fields as "input" either charges OpenAI's cached tokens twice (once at the full rate, once at the cached rate) or misses Anthropic's cache entirely. On a workload with long system prompts and heavy caching, that is not a rounding error.
Garmr normalises both into four counts: uncached input, cache reads, cache writes and output. For OpenAI, uncached input is prompt_tokens minus cached minus written, and it never goes below zero.
2. Reasoning tokens: inside or outside?
Reasoning models spend tokens you never see. Where they are counted varies.
- OpenAI's
completion_tokensalready includes reasoning tokens;reasoning_tokensin the details is a breakdown. Adding them again doubles the cost of thinking. - Gemini, through Vertex AI's OpenAI-compatible endpoint, put reasoning outside
completion_tokenswhen probed on 15 November 2026. One plain response had nocompletion_tokensat all: prompt 6, reasoning 12, total 18. A streamed one reported completion 1, reasoning 11, total 18. A second identical call again leftcompletion_tokensout.
The only safe rule is to check the arithmetic. If total_tokens is more than prompt plus completion, something was not reported where you expected. Garmr meters the excess as output and flags the record, rather than trusting either field alone.
3. Streams don't always tell you
Streaming is where usage goes missing.
- OpenAI sends usage on one final chunk, only if the request asked for it with
stream_options.include_usage. A client that did not ask gets a stream with no usage at all. Garmr adds the option to every streamed request it forwards, so the count is always there. - A Vertex AI stream sent a
usageobject on every chunk, empty of counts until the last one. A parser that takes the first usage it sees records nothing. - A stream can end without its final usage chunk: the client disconnected, the connection dropped, the provider errored halfway.
When usage is missing, the tempting answer is zero. Zero is wrong: the provider may well have billed for the tokens. Garmr marks the record uncertain and charges the larger of its pre-request estimate and whatever it saw.
4. The tokens you didn't send
Chat templates add tokens. In one probe, an open-weight model (gpt-oss, hosted on Vertex AI) counted 73 input tokens for a six-word prompt. The template's role markers and system scaffolding are billed like anything else.
This matters for estimates, not for settlement: if you predict cost from the text you send, you will be low. Settle from what the provider reports, and estimate generously.
5. Price depends on things the request doesn't say
The same tokens can cost different amounts:
-
Service tiers. OpenAI prices default, flex and priority differently, and the response says which tier served the request, which may not be the one you asked for. Price from the response's
service_tier, never the request. If it is missing or unfamiliar, Garmr uses the dearest tier the request could have reached and flags it, rather than guessing low. - Long context. Above a threshold, some models price the whole request at higher rates, not just the tokens past the line.
- Throughput type. Vertex AI marks each response as on-demand or provisioned throughput, which are billed differently.
A checklist
If you build or run your own cost tracking:
- Normalise every provider into the same four counts, and document how each field maps.
- Never add a detail field to its own total.
- Check that the parts add up to the total, and flag it when they don't.
- Force usage on streams, and treat a missing final chunk as unknown, not zero.
- Price from the response (model, tier), not the request.
- When in doubt, charge high and flag it. Undercounting is the failure that costs money.
Reproduce it
Garmr's repository has the scripts behind these probes. They make a few real calls and cost well under a cent:
VERTEX_PROJECT=... GOOGLE_APPLICATION_CREDENTIALS=... python3 -I scripts/live-vertex.py
scripts/live-openai-compatible.sh # against a local Ollama, free
Providers change these details without much notice, so the answers may differ by the time you run them. That is rather the point.
Garmr is an open-source LLM gateway written in Rust that enforces dollar and token budgets per member and per team. It is built with Claude Code, with tests written first. The code, and the list of what it does not do yet, are at github.com/garmrhound/garmr.
Top comments (0)