ThisCalculator

Everyday

Estimate and Control AI Agent Token Costs

Updated · Standard formula, shown below

Estimate and Control AI Agent Token Costs

Estimating and controlling token costs helps you set realistic budgets, spot waste, and keep agent runs predictable. You’ll need the model’s published input and output rates, a way to inspect usage, and a representative task to test; costs vary by model and provider.

What You’ll Need

  • The model and pricing information for your provider, including separate input and output rates and any cached-token rates.
  • Usage records from your agent platform or API, ideally showing tokens by request and model.
  • A small set of representative tasks, such as a short question, a multi-step research task, and a tool-using workflow.
  • A spreadsheet or simple script for recording token counts, costs, and results.

Step-by-Step

  1. Define what you are measuring. Decide whether you need cost per model call, completed task, user session, or time period. For an agent, cost per completed task is often the most useful because one task may involve several model calls, tool results, retries, or a handoff to another model. Write down what counts as a completed task and which calls belong to it.
  2. List every model and rate category. Check the current pricing page for each model your agent can call. Record the price per million input tokens and output tokens, and any distinct rates for cached input or other token categories. Confirm the currency and billing unit. Do not assume that all tokens cost the same or that prices stay unchanged; keep the date and model name with your notes.
  3. Capture usage at the request level. Log the model name, input tokens, output tokens, cached tokens if reported, task identifier, and whether the request succeeded. Use usage data returned by the API or platform rather than estimating from character counts. If the platform reports only aggregate usage, start there, but note that it may conceal expensive individual tasks.
  4. Calculate cost for each call. Convert each per-million rate into a per-token rate by dividing it by 1,000,000. Multiply the input-token count by the input rate and the output-token count by the output rate, then add the results. If cached input has a separate price, calculate that portion using its own rate and avoid counting those tokens again as ordinary input. Add any other billable token category shown by your provider.
  5. Sum calls to get the agent’s task cost. Group all calls belonging to the same task and add their costs. Include calls made for planning, tool selection, summarizing tool output, retries, and final responses. Also track the number of calls and total tokens: these figures help explain why two tasks with similar prompts can have different costs.
  6. Run a representative sample. Test realistic tasks, not just a short prompt. Include cases with long context, large tool results, and occasional retries. Run each case more than once if the workflow is variable. Calculate the mean and a high-percentile cost, such as the 90th percentile, so your budget reflects both ordinary and more expensive runs. Keep task quality or completion rate alongside cost; the cheapest run is not useful if it fails.
  7. Estimate a budget from observed use. Multiply expected completed tasks by a realistic cost per task. Use the sample’s typical cost for a planning estimate and its higher-percentile cost for a conservative limit. For example, if a measured task uses 12,000 input tokens and 2,000 output tokens, apply the relevant per-token rates to those counts; do not apply one blended rate unless you label it as an approximation. Add room for bursts, experiments, and changes in usage.
  8. Set limits and alerts. Configure provider or application spending alerts where available. Add an application-level maximum for tokens, model calls, retries, or elapsed time per task. Decide what happens at the limit: stop, ask for user confirmation, or return a partial result. A spending alert may notify you without stopping requests, so verify whether it is a warning or an actual hard cap.
  9. Reduce waste, then measure again. Remove repeated instructions, trim irrelevant conversation history, and avoid sending full tool output when a concise relevant excerpt will do. Set suitable output limits and stop generation once the required answer is complete. Use a smaller or less costly model for routine steps only when it meets your quality needs. Retest the same tasks after each change and compare both cost and success rate.
  10. Review actual spend regularly. Compare your own logs with provider billing records over the same period. Investigate gaps, unusually expensive tasks, changes in model selection, and token growth. Update rates when pricing changes and rerun your sample when prompts, tools, context length, or agent behavior changes.

Common Mistakes

  • Counting only the final answer: Agent planning, tool calls, and retries can generate many additional model requests.
  • Using character count as a token count: The relationship varies with language, formatting, code, and tokenizer. Treat rough estimates as rough.
  • Mixing units: Check whether rates are per token or per million tokens, and whether usage is input, output, or cached input.
  • Ignoring failures: Failed or repeated calls may still use billable tokens. Include them in operational cost, while reporting successful-task cost separately if useful.
  • Budgeting from an average alone: Long-context tasks and unusual tool results can cost substantially more than a typical run.
  • Optimizing tokens without checking results: Shorter context or smaller models may reduce cost but can also lower accuracy or completion rates.

Tips

  • Keep a cost ledger with date, model, task type, input and output tokens, retries, result, and cost.
  • Track cost per successful task as well as cost per attempt; this reveals when a cheaper configuration causes more failures.
  • Make token and call limits configurable, and test their behavior before relying on them in production.
  • Separate experiments from normal usage so tests do not distort everyday cost reports.
  • Recheck pricing and usage definitions periodically; providers may change rates or reporting fields.

Troubleshooting

  • Your estimate is below the bill: Compare the billing period, model names, and rate categories. Check for unlogged retries, background calls, cached-token treatment, or other billable services. Reconcile requests individually where possible.
  • Token counts are missing: Check whether the API response, platform logs, or usage dashboard exposes them. Add request-level logging if permitted by your system’s data-handling rules. Until then, label estimates as approximate and validate them against aggregate billing.
  • Costs vary sharply between runs: Inspect differences in context length, tool output, number of calls, and retries. Group results by task type and use a higher-percentile budget rather than relying on one sample.
  • A limit does not stop spending: Confirm whether it is a notification threshold or an enforcement control. Add a separate application-level stop condition if needed, and test it with a deliberately low limit.
  • Cost fell but task quality worsened: Compare completion and error rates against the original setup. Restore relevant context, adjust the model or output limit, and make changes one at a time to identify the cause.

Frequently Asked Questions

How do I estimate the cost of one agent task?
Sum the cost of every model call made for that task, using its actual token counts and the applicable rates for each token category.

Are input and output tokens priced the same?
Not necessarily. Check the current rate for the exact model and category; input, output, and cached input may have different prices.

Can I estimate tokens from the length of a prompt?
Only roughly. Tokenization depends on the text and model, so use reported token counts for budgeting whenever available.

What is a sensible per-task spending limit?
Base it on representative runs, including higher-cost cases, then choose a limit that fits your budget and decide how the agent should respond when it reaches it.

Do retries count toward token costs?
They can. Include their usage in the task’s total and track retry frequency so recurring failures do not go unnoticed.

How often should I recalculate my estimate?
Review it when provider rates, models, prompts, tools, or agent workflows change, and compare estimates with actual billing regularly.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.