To estimate AI API cost per request, count the input and output tokens, divide each by 1,000,000, then multiply by that model's price per 1M tokens. Add the two results together. That's the whole formula. The hard part is guessing token counts before you ship, and knowing which hidden multipliers (long context, retries, agent loops) inflate the bill. This guide walks through both.
How to estimate AI API cost per request
Almost every model API bills two meters separately:
- Input tokens: everything you send, including the system prompt, chat history, tool definitions and attached files.
- Output tokens: what the model writes back. Output is usually priced higher than input.
The formula:
cost = (input_tokens / 1,000,000 × input_price) + (output_tokens / 1,000,000 × output_price)
Worked example with made-up rates, just to show the math: say a model costs $1.00 per 1M input tokens and $4.00 per 1M output tokens. A request with 3,000 input tokens and 800 output tokens costs 0.003 × 1.00 + 0.0008 × 4.00 = $0.0062. Run that 10,000 times a month and you spend about $62. Swap in the real rates for your model and the same steps apply.
Step 1: Estimate your token counts
You don't need exact numbers up front. A rough rule for English text is that one token is about four characters, or roughly three-quarters of a word. Code and non-English text usually take more tokens per word, so leave a margin.
- Measure a real prompt. Send one typical request and read the
usagefield in the response. It reports prompt and completion tokens exactly. - Separate fixed from variable parts. The system prompt and tool schemas repeat on every call. User input and retrieved documents change.
- Cap the output. Set
max_tokensso a runaway answer can't cost ten times what you planned. - Take a P90, not an average. A few long requests dominate the bill. Budget for the 90th percentile and you'll rarely be surprised.
Step 2: Read the price per 1M tokens correctly
Prices are quoted per 1M tokens, which is awkward for a single call. Convert once and keep a small table. Check three things on any price list:
- Input and output are listed separately.
- Some models charge more for very long prompts or discount cached input.
- The currency and billing method. BangunWeb shows USD prices for English visitors and bills in IDR from prepaid tokens.
Live rates for every model are on the AI model price table per 1M tokens. Package size also affects the per-token rate, so check the token package pricing before you size a budget. Prices change often, which is why this article doesn't print them.
Step 3: Let the API tell you the real cost
Once you're making calls, stop guessing. This Python snippet uses the OpenAI SDK against an OpenAI-compatible endpoint, reads usage and computes the cost. Fill in the two price constants from the model page.
from openai import OpenAI
client = OpenAI(
base_url="https://router.bangunweb.com/v1",
api_key="bwr_live_...",
)
# USD per 1M tokens: copy these from the live model page
INPUT_PRICE = 0.0 # fill in
OUTPUT_PRICE = 0.0 # fill in
resp = client.chat.completions.create(
model="deepseek-v4-flash",
max_tokens=500,
messages=[
{"role": "system", "content": "You are a concise code reviewer."},
{"role": "user", "content": "Review this function for bugs: ..."},
],
)
u = resp.usage
cost = (u.prompt_tokens / 1_000_000) * INPUT_PRICE \
+ (u.completion_tokens / 1_000_000) * OUTPUT_PRICE
print(u.prompt_tokens, u.completion_tokens, f"${cost:.6f}")
Log those three numbers per request and you'll have a real cost distribution within a day. Endpoints, auth headers and examples are in the AI Router API docs.
Where estimates go wrong
Chat history grows every turn
In a chatbot, each reply resends the whole conversation. Turn 10 pays for turns 1 to 9 again. Summarize or trim old messages once the history gets long.
Coding agents loop
Tools like Claude Code, Cline or OpenCode make many calls per task: read files, plan, edit, run tests, fix. A single "simple" task can be dozens of requests, each carrying a large context. Estimate per task, not per request, and measure a few real tasks. If you're setting one up, see the OpenCode setup with BangunWeb.
Reasoning and thinking tokens
Models with extended reasoning may produce hidden thinking tokens that count as output. Check whether completion_tokens includes them and cap them if the API lets you.
Retries and errors
Failed or repeated calls can still consume tokens. Add a retry limit and log failures.
Quick ways to lower the cost per request
- Match the model to the job. Use a cheaper, faster model for classification, extraction and drafts. Save premium models for hard reasoning. Browse the DeepSeek API pricing page for low-cost options.
- Shorten the prompt. Remove boilerplate, deduplicate retrieved chunks, send only the relevant file section.
- Set
max_tokens. Ask for concise answers or structured JSON. - Cache static prefixes where the provider supports it.
- Watch what others use. The Top Model board shows the most used models over the last 7 days, a handy sanity check when choosing.
A simple monthly budget template
| Input | Example value | Your value |
|---|---|---|
| Avg input tokens per request | 3,000 | |
| Avg output tokens per request | 800 | |
| Requests per day | 500 | |
| Days per month | 30 | |
| Safety margin | +30% |
Multiply per-request cost by requests per month, then add the margin. Compare two or three models using the same row values and pick the cheapest one that passes your quality test on real prompts.
Prepaid tokens make budgeting easier
With a prepaid balance, your worst case is the amount you topped up. There's no surprise invoice at month end. BangunWeb AI Router uses prepaid tokens with no subscription and no credit card, and you can pay with QRIS, Indonesian bank transfer or USDT. If you mainly need Claude, the walkthrough in Claude API without a credit card covers the setup, and the pay-as-you-go page explains the billing model.
Start measuring your own cost
Pick one model, run twenty real requests and log the token usage. That gives you a better estimate than any rule of thumb. When you're ready, grab one key for many models from the BangunWeb AI Router, point your OpenAI-compatible client at it and compare costs across models side by side.