Log in·Sign up
BestRelay

Back to the Journal

A Token Budget Starts With the Whole Request

A prompt that looks short in an editor can become a surprisingly large API request. Instructions, conversation history, tool definitions, and attachments all contribute to the material a model processes. Counting only the latest message creates a false sense of precision. The useful number is the size of the complete request, measured with a method appropriate to its destination.

Step one captures everything the model will receive

Begin by assembling the same request your application will actually send. Include system instructions, developer instructions where supported, previous messages, retrieved passages, tool definitions, and tool results. Count after template expansion and document selection, not before. Otherwise, your estimate describes a draft rather than the submitted payload.

Check whether your client adds material automatically. Coding assistants may attach repository context or retain earlier tool output. This matters when using Claude Code, Codex, or Gemini CLI: the visible question may represent only a fraction of the input.

Step two chooses a counter for the destination

Use the provider’s documented token-counting facility when available for the chosen model and request type. Confirm which fields and modalities it supports. A counter that accepts plain text does not necessarily account for images, audio, message framing, or tool schemas.

If an official counting facility is unavailable, use a tokenizer documented as compatible with the target model. Treat character ratios and word counts as rough planning aids, not admission checks. Source code, punctuation, and non-English text can produce very different token densities. Even a compatible tokenizer can miss provider-specific request overhead.

Step three reserves room for the response

Input size is only half of a context budget. Check the selected model’s documented context limit, maximum output allowance, and any rules governing reasoning tokens. Then reserve space for the response before deciding whether the input fits. An output setting is generally a ceiling, not a promise that the model will generate that many tokens.

For example, an estimated 24,000-token input and a 4,000-token response allowance suggest a 28,000-token planning budget. Add headroom when counting is approximate. For agent workflows, also budget for later tool results and follow-up turns rather than assuming the first request represents the whole task.

Step four separates context capacity from billing estimates

Fitting inside the context window does not establish the final price. Estimate charges using the applicable input and output rates, then account for cache reads and writes separately where supported. Cache eligibility, minimum lengths, retention rules, and hit behavior vary. Do not assume repeated text will receive cached pricing merely because your application has sent it before.

BestRelay bills pay-as-you-go from provider-reported token counts, with cache reads and writes priced separately. That makes the distinction practical: a local estimate helps control requests, while the provider’s usage report determines the metered token quantities. Keep those two records separate rather than overwriting the estimate with the final number.

Step five validates the route without changing models

Token counting must follow the actual model destination. BestRelay is a quality-first API relay that forwards each request to the official model named in it, without substitution. Its coverage includes Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, and Grok. Compatibility with any OpenAI SDK does not make those providers share one tokenizer or one accounting convention.

Create a BestRelay account to test this workflow with your own requests. Members also earn a referral commission when people they invite top up; keep that program separate from calculations of request usage.

Step six compares estimates with actual usage

After a request completes, record the provider-reported usage alongside your estimate, model identifier, and counting method. Preserve available input, output, and cache categories rather than flattening everything into one total. Investigate recurring differences before adjusting your safety margin.

Finally, repeat the count whenever the payload changes. Trimming history, adding a tool, or replacing an attachment can invalidate an earlier estimate. A reliable workflow counts the assembled request, reserves generation space, and checks the result against actual usage. That discipline turns token counting from guesswork into an engineering control.

Want to try the models discussed here? Sign up