← All news

News / Learn / Working with model APIs

How do you estimate and control LLM API costs?

You'll be able to estimate what an LLM feature costs per request and set limits that keep spend under control.

Most LLM APIs bill by token. A token is a small piece of text, roughly a word or part of a word. You pay for the tokens you send in (input) and the tokens the model writes back (output). Output tokens usually cost more than input tokens.

Cost scales with usage, so a feature that feels cheap in testing can become expensive at scale. If you know the token count per request, you can multiply by expected traffic and see the bill before launch. That lets you decide on pricing, limits, and which model to use.

Start with the token count of a typical request. Add the system prompt, the user message, any retrieved documents, and the conversation history. Most providers return token counts in each response, so log them. Those numbers are your real data.

Next, estimate output length. Output is often the bigger cost driver, so a prompt that asks for a long essay costs far more than one that asks for a short label. Constrain output with clear instructions and a maximum output token setting.

Then multiply. Cost per request is input tokens times the input rate plus output tokens times the output rate. Multiply by requests per day to get a daily figure. Check the provider's current pricing page for the rates, because they change.

Finally, control spend. Trim unnecessary context, cap output length, avoid resending the same long prompt when caching is available, and set per-user or per-day budgets in your own code. Monitor cost per feature, not just the total bill.

Log token usage from each response and estimate cost. Replace the rates with the current values from your provider's pricing page.

def estimate_cost(input_tokens, output_tokens, input_rate_per_m, output_rate_per_m):
    # Rates are price per one million tokens, taken from the provider's pricing page.
    return (input_tokens / 1_000_000) * input_rate_per_m + (output_tokens / 1_000_000) * output_rate_per_m

# Read token counts from the response's usage field, then log them.
usage = {"input_tokens": 1200, "output_tokens": 300}  # example values
cost = estimate_cost(usage["input_tokens"], usage["output_tokens"], 1.00, 4.00)
print(f"Estimated cost per request: {cost:.6f}")

A short sketch to show the idea. Check the official docs of the tool you use.

  • Estimating from word count Tokens are not words. Use the token counts the API returns, and log them, rather than guessing from text length.
  • Ignoring output length Output tokens often cost more. Set a maximum output length and ask for concise answers in the prompt.
  • Resending long history every turn Conversation history grows each turn. Trim or summarize old messages so each request does not repeat the full past.
  • No per-user limits One heavy user or a loop bug can run up a large bill. Set per-user and daily budgets in your own code.

Learn prompt caching, batch requests, and model routing. Routing sends easy tasks to cheaper models and hard tasks to stronger ones, which often cuts cost without hurting quality.

What is a token?

A token is a unit of text the model reads and writes. It is often a word or part of a word. Punctuation and spaces also count toward the token total.

Why do output tokens cost more?

Generating each output token takes more computation than reading an input token. Providers price output higher to reflect that work.

Where do I get exact token counts?

Most APIs return usage data with each response that lists input and output tokens. Record it so you can measure real costs.

Does a bigger context window cost more?

Yes. You pay for the tokens you send, so filling a large context on every request raises cost even if the model can handle it.

How do I stop runaway costs?

Cap output length, set rate and budget limits in your code, and alert when daily spend passes a threshold.

Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.