MyRelay

← Back to the notebook

Count Tokens Before You Send a Request

Before sending a long assignment or debugging conversation, pause for a token check. The aim is not to predict every fraction of a cent. It is to catch oversized requests, decide how much answer you need, and keep an ordinary experiment within your budget. Walk through the following process with one real request before applying it to a batch.

Start with the complete request you intend to send

Gather your instructions, current question, conversation history, and supporting material. Include tool descriptions and response schemas if your application sends them. Counting only the question you typed leaves out other input. A complete request can also include structural tokens that are not visible in the plain text. ([developers.openai.com](https://developers.openai.com/api/docs/guides/token-counting))

For a debugging task, assemble the relevant function, error message, and a short explanation of the expected behavior. Start with that focused package rather than your entire project. Keep the original material nearby so you can restore anything necessary.

Choose a counter that matches your model

Tokens are not words. A token can contain a word fragment, punctuation, or other characters, and the same text can produce different counts across models. For ordinary English prose, dividing the character count by four gives a rough starting estimate, not a reliable billing figure. ([help.openai.com](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens))

For a text-only check, use a tokenizer that explicitly supports your selected model or its encoding. Paste the assembled text into it and record the result. If support is unclear, label the number as approximate rather than treating another model's tokenizer as interchangeable. ([help.openai.com](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens))

Where your provider and connection support it, prefer a request-level counting endpoint. OpenAI documents counting for complete Responses inputs, while Anthropic documents message counting with supported system prompts, tools, images, and documents. Check supported input formats before relying on either approach. Do not assume that an OpenAI-compatible relay also exposes every provider-specific counting feature. ([developers.openai.com](https://developers.openai.com/api/docs/guides/token-counting))

Remove unnecessary context and count again

Save your first count, then edit for relevance. Remove duplicated instructions, unrelated conversation turns, and repeated examples. For a study task, try the passage under discussion rather than an entire chapter. For debugging, preserve the surrounding definitions needed to understand the error.

Run the revised request through the same counter. Compare the two versions and ask whether the smaller one still contains enough evidence to answer correctly. Your target is the smallest useful request, not the shortest possible prompt. An aggressive cut is not worthwhile if it forces you to repeat the task.

Reserve room for the answer before setting a limit

An input count does not predict output length. Check the model's context and output limits separately, then choose an output allowance appropriate to the task. Where applicable, leave room for reasoning tokens as well as the visible answer. ([help.openai.com](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens))

Ask for the result you actually need, such as a brief explanation and one corrected example. Use the output limit supported by your model and endpoint as a separate guardrail. Some limits include non-visible generated tokens, so an allowance is not a promise of an equally long visible response. ([developers.openai.com](https://developers.openai.com/api/docs/guides/token-counting))

Turn your count into a spending estimate

Look up the applicable input and output rates for your chosen service. Multiply each token allowance by its corresponding per-million-token rate, divide by one million, and add the amounts. Account for cached-input pricing or separate charges where relevant. ([help.openai.com](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens))

As a hypothetical example, 3,000 input tokens at $1 per million cost $0.003. An allowance of 500 output tokens at $4 per million adds $0.002, making the planned token cost $0.005. These are illustrative rates, not MyRelay prices. Multiply by your planned request count before starting a batch.

Compare one real request with your estimate

Send one representative request before committing to a larger run. MyRelay lets you inspect each request's tokens and cost, so use that record to compare your plan with actual spending. Create a MyRelay account if you want one personal key and one budget for this workflow.

Record the estimated input, actual usage, and final cost together. Investigate a large difference before continuing, and recount whenever you change the prompt, model, or attached material. The useful habit is simple: assemble, count, trim, budget, and verify.

More to read