Skip to content
Eldan.ai

Engineering, , 5 min read

Tokens are a budget

Every model call has a bill and a clock. Treat cost and latency as design constraints, the way you treat memory on a phone, and write the budget down before you write the feature.

On this page

Every model call has a bill and a clock. The bill is measured in tokens. The clock is measured in time to the first token and time to the last. Teams that treat these as afterthoughts discover them in their first invoice or their first user complaint about speed. Teams that treat them as design constraints, like memory on a phone, build different systems.

Know what you pay for

Providers charge separately for input tokens and output tokens, and output tokens usually cost several times more each. Output is also slower, because the model produces it one token at a time. A verbose answer therefore costs twice, in money and in waiting. Input tokens are cheaper but they add up, because every request resends the system prompt, the conversation history and any retrieved documents. A chat that runs for twenty turns sends the first turn twenty times.

Measure before you optimise. For every request, log the input and output token counts, the model, the latency and the feature that made the call. Then look at the distribution, not the average. In most products a small share of requests, the long conversations and the big documents, accounts for a large part of the bill. Find the expensive five percent first.

Levers, in order of effort

Shorten the output. Ask for a structured answer with only the fields you need. Set a maximum output length. Do not ask for an explanation nobody reads.

Trim the input. Keep the system prompt tight. Send only the retrieved passages that matter, which is one more reason to get retrieval right. Summarise or truncate old conversation turns, but test that the summary keeps what later turns rely on.

Cache the prefix. Many providers offer prompt caching: if the beginning of your prompt is identical across requests, the repeated part is billed at a lower rate and processed faster. This only works when the stable parts come first and the variable parts come last, and when nothing in the prefix changes between calls. A timestamp or a user’s name at the top of the prompt silently disables the cache. Check the provider’s documentation for the minimum prefix length and the cache lifetime, because both vary.

Route by difficulty. Not every request needs your most capable model. A cheap model can classify the request, answer the easy ones and hand the hard ones to a stronger model. This is worth doing only with an eval, because you need to know how often the cheap model is good enough. Measure quality per route, not just overall.

Batch what is not urgent. If a job does not need an answer in seconds, such as nightly summaries, many providers offer batch interfaces at a lower price in exchange for slower turnaround.

Consider a smaller or self-hosted model. For a narrow, stable task with a good eval, a smaller model can match a large one at a fraction of the cost, and an open-weight model that you run yourself can fix your costs and keep data in your own environment. The trade is operational work, so count the people and the hardware, not just the tokens.

Latency is a design problem too

Users judge speed by when something appears, not by when it finishes. Streaming tokens as they arrive makes a five-second answer feel immediate. For agents, the number that matters is steps multiplied by latency per step, so removing an unnecessary tool call saves more than tuning a prompt. Run independent tool calls in parallel when the model returns several.

Set targets in percentiles. The median says how it usually feels. The 95th percentile says how bad it gets for one user in twenty. Alert on the second.

A worked estimate

Take a support assistant. The system prompt is 800 tokens, the retrieved passages are 2,500, the conversation history is 1,200 and the answer is 300. That is 4,500 input tokens and 300 output tokens per request. At 20,000 requests a month, you send 90 million input tokens and generate 6 million output tokens.

The monthly cost is requests multiplied by the sum of input tokens times the input price and output tokens times the output price. You can plug in your provider’s prices in a spreadsheet. Now try the levers. Sending three passages instead of five cuts about 1,000 input tokens. Caching the 800-token prefix lowers the price of that part. Keeping history to the last four turns saves more. Each lever has a number, and you can test its effect on quality with the eval before you ship it.

Write the budget down

For each feature, write one line: the maximum cost per request and the maximum latency at the 95th percentile. Put those numbers in your eval gate next to quality, so that a change which improves accuracy by one point and triples the cost fails the build and starts a conversation. Without a number, cost only gets noticed when it is too late to be cheap.

A small habit

Every time you add a feature, estimate the tokens before you write the code. Count the system prompt, the typical retrieved context and the expected output, multiply by the expected traffic and look at the monthly figure. Five minutes of arithmetic is a good way to find out that an idea is affordable, or that it needs a different design.