LLM API Cost Calculator for Chat Apps (Formulas)
An LLM API cost calculator for a chat app needs only four numbers: input tokens, output tokens, the two prices and how history grows. Here are the formulas, worked examples and a small Python function, using $0.15 per 1M input tokens and $0.50 per 1M output tokens.
Updated

The basic formula
Every request has two token counts. Input tokens are everything you send: system prompt, history and the new message. Output tokens are what the model writes. The cost of one request is:
cost = (input_tokens x 0.15 + output_tokens x 0.50) / 1,000,000
Example: a request with 3,000 input tokens and 200 output tokens costs (3,000 x 0.15 + 200 x 0.50) / 1,000,000 = (450 + 100) / 1,000,000 = $0.00055. Output tokens cost 3.33 times as much as input tokens, but chat apps usually send far more input than they receive, so input often dominates the bill.
As a rough conversion for English text, 100 tokens is about 75 words. That varies by language and content, so measure with real traffic. Every response includes a usage block, and streamed responses report usage at the end. The docs describe the fields.
Why chat history changes everything
The API has no memory. To continue a conversation you resend earlier messages, so input tokens for turn n are:
input(n) = S + U + (n - 1) x (U + R)
where S is the system prompt, U the average user message and R the average reply. Take S = 500, U = 50, R = 200, so each turn adds 250 tokens to history.
| Turn | Input tokens | Cost of that turn |
|---|---|---|
| 1 | 550 | $0.000183 |
| 10 | 2,800 | $0.00052 |
| 50 | 12,800 | $0.00202 |
| 100 | 25,300 | $0.003895 |
Each row is input x $0.15 / 1M plus 200 x $0.50 / 1M, which is $0.0001. Turn 100 costs more than twenty times as much as turn 1, even though the user typed the same amount.
Total cost of a whole conversation
Summing the series gives the total input for N turns:
total_input = N x (S + U) + (U + R) x N x (N - 1) / 2
For 50 turns: 50 x 550 + 250 x 1,225 = 27,500 + 306,250 = 333,750 tokens, which is $0.05006. Output is 50 x 200 = 10,000 tokens, or $0.005. The chat costs $0.055.
For 100 turns: 100 x 550 + 250 x 4,950 = 55,000 + 1,237,500 = 1,292,500 tokens, which is $0.19388, plus 20,000 output tokens at $0.01. The chat costs about $0.204.
Doubling the length multiplied the cost by 3.7. That square term is why long chats surprise people. The 100,000-token context window is the ceiling: when history plus your next message approach it, you must trim or summarise anyway.
Trimming with a sliding window
Keep only the last K messages and the cost per turn stops growing. With K = 20 messages (10 turns), a request carries roughly 500 + 50 + 9 x 250 = 2,800 tokens at the steady state, close to 3,000 with some slack. Over 100 turns that is about 300,000 input tokens, or $0.045, plus $0.01 for output: $0.055 in total. That is the same price as the full-history 50-turn chat, for a chat twice as long.
The cost is memory. Details outside the window vanish, so pair the window with a short summary and a notes block, as described in our Python example. A 300-token summary adds only 300 x $0.15 / 1M = $0.000045 per request.
A calculator in Python
This function simulates a chat with and without a window. Change the four defaults to match your app.
# Prices for https://api.nsfwapihub.com/v1, model "uncensored"
def estimate(turns, system=500, user=50, reply=200, window=None):
"""Return dollars for a chat of `turns` turns. window = max past messages kept."""
total_in = 0
history = 0 # tokens of past messages
for n in range(turns):
past = history
if window:
past = min(history, window * (user + reply) / 2)
total_in += system + past + user
history += user + reply
total_out = turns * reply
return total_in * 0.15 / 1e6 + total_out * 0.50 / 1e6
print(round(estimate(50), 4)) # full history
print(round(estimate(100), 4))
print(round(estimate(100, window=20), 4)) # sliding window of 20 messagesRunning it prints about 0.0551 for 50 turns, 0.2039 for 100 turns, and about 0.054 for 100 turns with the window. The window math is approximate because the first turns have less history than the window allows.
Measure real usage
Estimates are for planning. For billing and monitoring, read the real counts from the response.
from openai import OpenAI
IN_PRICE = 0.15 / 1_000_000 # dollars per input token
OUT_PRICE = 0.50 / 1_000_000 # dollars per output token
client = OpenAI(base_url="https://api.nsfwapihub.com/v1", api_key="YOUR_KEY")
def cost(usage):
return usage.prompt_tokens * IN_PRICE + usage.completion_tokens * OUT_PRICE
r = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Write a two-line greeting."}],
)
print(r.usage.prompt_tokens, r.usage.completion_tokens, round(cost(r.usage), 6))Log the numbers per user and per day. The X-Balance-USD response header shows your remaining credit, and balance can also be read from GET /v1/balance, so you can compare your own calculation with the actual drop. Failed or refused requests are not charged, which means retries after an error do not double-bill you.
Output-heavy and input-heavy workloads
Not every app is a chat. The same formula covers other shapes, and the dominant term changes.
- Story or scene generation. 500 tokens in, 2,000 tokens out. Cost: 500 x $0.15 / 1M + 2,000 x $0.50 / 1M = $0.000075 + $0.001 = $0.001075 per call. A thousand stories cost about $1.08. Output is the main cost here, so a length limit in the prompt saves money directly.
- Document summaries. 20,000 tokens in, 300 out. Cost: $0.003 + $0.00015 = $0.00315 per document, so a thousand documents cost $3.15. Input dominates.
When you plan a budget, find which side dominates in your app and optimise that side first. For chats it is input, so trim history. For generators it is output, so control reply length.
From per-chat cost to a monthly budget
Multiply by behaviour. Say a user has 20 sessions a month of 50 turns each. Cost per user: 20 x $0.0551 = $1.10. A thousand such users cost about $1,100 a month. If you also apply a sliding window, you cut the heavy tail, and a user with 100-turn sessions drops from $0.20 to $0.055 per session.
Plan for limits as well as money. A full key allows 8 concurrent requests and 300 requests per minute, and a 429 response with code rate_limited or concurrency tells you to queue. Streams occupy a slot until they end, so a slow stream counts against the 8 concurrent requests for its whole length.
Top-ups run from $20 to $500, with +5% from $50, +10% from $100 and +15% from $200, paid in crypto (USDT on TRC20 or USDC on Base). Credit never expires and there is no subscription. See pricing, and start with the trial credit from the API key page to measure your own numbers.
More guides
Questions and answers
How do I calculate the cost of one request?
Multiply input tokens by $0.15 and output tokens by $0.50, add them, and divide by 1,000,000.
Why does a long chat cost so much more per message?
Each turn resends the whole history, so input tokens grow every turn. Trim or summarise old messages to flatten the cost.
Are failed requests billed?
No. Failed or refused requests are not charged.
Does the calculator include a bonus on top-ups?
No. Bonuses of 5%, 10% and 15% from $50, $100 and $200 add credit, so your effective price per token is slightly lower.
Your key is one form away
Create an account, copy the key, change the base URL. That is the whole setup.