Skip to content

Tokens Are a Budget, Not a Word Count

By Maksym Kuzmitskyi
6 min read
Tokens Are a Budget, Not a Word Count

Tokens Are a Budget, Not a Word Count

The question is six words long. The request includes the previous conversation, application instructions, tool descriptions and a policy document.

Charging the question with responsibility for the whole bill seems a little unfair. It is also a good description of how misleading the chat interface can be.

For our documentation assistant, the useful unit is the complete model request. Tokens describe how the selected model represents that input and its output. They are not a count of the words someone typed into the last text box.

A tokenizer decides where the pieces are

A tokenizer converts content into a sequence of token identifiers. A token may represent a whole common word, a word fragment, punctuation, whitespace or another encoded unit. Different tokenizers can split the same string differently.

That matters for source code, identifiers, non-English text and structured data. Counting spaces does not reveal the encoding. Neither does dividing the number of characters by a familiar constant.

I might use a rough estimate for an early capacity sketch. I would label it as an estimate and leave room for error. I would not use it to assert that a near-limit production request fits.

Anthropic's token-counting documentation explicitly ties counting to the selected model and describes the returned count as an estimate that can differ from actual usage. That is a useful limitation, not a reason to replace it with a less informed guess.

There is no tokenizer installed for these examples, and I have not called a counting API. The code below operates on declared usage numbers. It demonstrates accounting, not an actual encoding of the policy document.

Capacity and billing are different ledgers

The context window is the model's bounded working space for a request. Depending on the model/API, the relevant accounting includes supplied input, generated output and reasoning-related tokens. The maximum output allowance can have its own limit too.

An output reserve is capacity we leave for generation. It is not necessarily the number of tokens eventually generated or charged. Filling the available input capacity and then requesting a substantial answer is not a good way to discover the provider's overflow behavior.

Billing is another ledger. Cached input may be priced differently from newly processed input. Reasoning tokens may be reported within an output total or through related fields. The names alone do not tell us which numbers overlap.

For example, Anthropic's cache usage breakdown uses separate input fields for uncached input, cache reads and cache writes. Its plain input field is not the complete input total when caching is involved.

An application should normalize that provider-specific response before doing arithmetic. Otherwise a well-formatted dashboard can be consistently wrong.

Make the normalization contract explicit

This example uses a deliberately fictional price schedule and a normalized usage format. Here, total input includes the cached subset. There are no cache writes or other billable categories in this small example:

type Usage = { totalInput: number; cachedInput: number; output: number; }; type Rates = { freshInput: number; cachedInput: number; output: number; }; function quoteUsage(usage: Usage, ratesPerMillion: Rates): number { if (Object.values(usage).some((value) => !Number.isSafeInteger(value) || value < 0)) { throw new Error('Usage must contain non-negative integer counts'); } if (usage.cachedInput > usage.totalInput) { throw new Error('Cached input cannot exceed total input'); } if (Object.values(ratesPerMillion).some((value) => !Number.isFinite(value) || value < 0)) { throw new Error('Rates must be finite and non-negative'); } const freshInput = usage.totalInput - usage.cachedInput; return ( freshInput * ratesPerMillion.freshInput + usage.cachedInput * ratesPerMillion.cachedInput + usage.output * ratesPerMillion.output ) / 1_000_000; }

The subtraction is the interesting line. Charging total input at the fresh rate and then adding cached input again would double-count the cached portion under this contract.

We can check the arithmetic without buying a model call:

const fictionalRates: Rates = { freshInput: 2, cachedInput: 0.2, output: 8 }; const quote = quoteUsage( { totalInput: 1_200, cachedInput: 800, output: 200 }, fictionalRates, );

The result is 0.00256 currency units. That is a fixture result, not a current provider quote. The normalization step from a real response remains provider-specific. A real bill may also include cache writes, tools, images, regional modifiers or categories this function intentionally does not model.

I would reject missing usage rather than quietly report zero. Zero is a measurement. Unknown means the accounting path needs attention.

Shorter can still be less useful

It is tempting to optimize the number before understanding the request. Remove all the examples. Drop the source titles. Shorten every instruction. The prompt gets smaller, and the assistant loses the details that distinguished the right policy from the wrong one.

The useful question is what each piece contributes. Repeated boilerplate and an irrelevant tool catalog are different from the one policy exception needed to answer correctly.

I would compare correctness, latency and charged usage on representative tasks. Latency is elapsed time; it does not map to token count by a universal constant. Network delays, processing, generated output and tool work can all contribute.

Prompt caching can reduce repeated processing of eligible prefixes. It does not remove those prefixes from the logical context window. A cheaper repeated document is still a document the application chose to supply.

The allowance needs an owner

A request can have a limit while the full task remains unbounded. An agent may make another request, retry a failed operation or retrieve another large result. Per-call accounting needs to feed into a task-level budget rather than reset the spending decision every turn.

That enforcement belongs in the application. Written instructions to be economical can influence behavior; they are not a substitute for tracking usage and stopping work when the approved limit is reached.

One terminology collision is worth removing now: an OAuth access token is an authorization credential, not an LLM's encoded unit of content. Reducing the number of model tokens does not reduce credential permissions. Keeping credentials out of model context is a separate responsibility.

For the documentation assistant, I want the request size to be understandable before it is clever. Which documents did we include? What did the selected counter estimate? What usage did the response actually report? Which categories did the billing calculation interpret?

A very small answer can still be wrong. Next I want a predictable shape for that answer, while keeping the word predictable away from claims it cannot support.

Which number does your cost dashboard call input tokens: all supplied input, only uncached input, or something normalized in between? That label deserves a precise answer.

A note from the author

I’m Maksym Kuzmitskyi, a senior software developer working mainly with React and TypeScript. I build frontend applications and design workflows for working with AI in software delivery. If this article connects with something you’re working on, I’d love to hear from you.

Telegram

More than a blog post

I share frontend news and the reasoning behind it throughout the day. Pick the language that feels natural to you.

Need to discuss your project? Get in touch.