Token Cost Claude Calculator: Estimate Before You Build
A token cost calculator is a dozen lines of code: estimate tokens, multiply by the per-million rates, add the two sides. Here is a version you can paste, plus how to check its estimates against the real numbers.
Last updated

Table of contents
Key takeaways
- Estimate tokens at roughly four characters each for English, then replace the guess with the usage field.
- Cost is input tokens times the input rate plus output tokens times the output rate, per 1M.
- Plan for resent chat history, system prompts and tool schemas; they dominate long sessions.
- Rates here are $0.15 and $0.50 per 1M tokens; put any other price list into the same function to compare.
The formula in one box
| Piece | Value at our rates |
|---|---|
| Input rate | $0.15 per 1M tokens, or $0.00000015 per token |
| Output rate | $0.50 per 1M tokens, or $0.0000005 per token |
| 1,000 input + 500 output | $0.00015 + $0.00025 = $0.0004 |
| 10,000 input + 1,000 output | $0.0015 + $0.0005 = $0.002 |
Anyone typing token cost claude calculator into a search box wants that table as a tool: paste a prompt, get a price. A calculator needs only three inputs: how many tokens go in, how many come out, and the two rates. The rest of this page builds that tool and shows where its estimates go wrong.
Step 1: estimate tokens from text
Without the exact tokenizer you can still get close. For English prose, divide the character count by four. For code, JSON and non-English text, divide by three, because those take more tokens per character. Neither rule is exact, and both are only for planning.
def estimate_tokens(text: str, chars_per_token: float = 4.0) -> int:
"""Rough planning estimate. Real counts come from the API's usage field."""
return max(1, round(len(text) / chars_per_token))
prompt = open("prompt.txt", encoding="utf-8").read()
print(estimate_tokens(prompt)) # English prose
print(estimate_tokens(prompt, 3.0)) # code or JSONIf you want a closer local number, a tokenizer library such as tiktoken gives a decent approximation for many models, but it is not this model's own tokenizer, so treat its output as a second estimate and not as a billing figure.
Step 2: turn tokens into dollars
Keep the rates in a dictionary so you can add other price lists later. Read the other providers' rates from their pricing pages yourself; we do not state them.
RATES = {
# USD per 1M tokens: (input, output)
"this-api": (0.15, 0.50),
# "other": (..., ...), # fill in from that provider's pricing page
}
def call_cost(in_tokens: int, out_tokens: int, plan: str = "this-api") -> float:
in_rate, out_rate = RATES[plan]
return in_tokens / 1e6 * in_rate + out_tokens / 1e6 * out_rate
def monthly(in_tokens, out_tokens, calls_per_day, plan="this-api"):
return call_cost(in_tokens, out_tokens, plan) * calls_per_day * 30
print(f"{call_cost(2000, 500):.6f}") # 0.000550
print(f"{monthly(2000, 500, 1000):.2f}") # 16.50Check the two printed values by hand. 2,000 input tokens cost $0.0003 and 500 output tokens cost $0.00025, so one call is $0.00055. A thousand calls a day for 30 days is 30,000 calls, which is $16.50.
Step 3: model a conversation, not a single call
The single-call number understates chat apps. Each new turn resends everything before it, so input grows with every message. Model it with a loop:
def chat_cost(turns, system=400, user=150, reply=350, plan="this-api"):
total_in = total_out = 0
history = 0
for _ in range(turns):
total_in += system + history + user
total_out += reply
history += user + reply
return total_in, total_out, call_cost(total_in, total_out, plan)
for t in (5, 10, 20, 40):
i, o, c = chat_cost(t)
print(t, i, o, f"{c:.5f}")With those defaults a 20-turn chat sends 106,000 input tokens in total across all its calls and receives 7,000 output tokens. The cost is $0.0159 + $0.0035 = $0.0194. A 40-turn chat does not cost twice as much; at 412,000 input and 14,000 output tokens it costs $0.0688, about 3.5 times as much, because later turns carry more history. That quadratic growth is the thing a flat per-message estimate misses, and it is why trimming old turns pays off.
Step 4: replace the estimate with real usage
Estimates get you to a budget. For the real figure, read the usage block the API returns and compare it with your guess. The same calculator then works on actual numbers.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.tokencostclaude.com/v1",
apiKey: process.env.API_KEY,
});
const IN_RATE = 0.15, OUT_RATE = 0.5; // USD per 1M tokens
const text = "Explain how prepaid API credit works in three sentences.";
const guess = Math.round(text.length / 4);
const res = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: text }],
});
const u = res.usage;
const cost = (u.prompt_tokens / 1e6) * IN_RATE + (u.completion_tokens / 1e6) * OUT_RATE;
console.log({ guess, actual: u.prompt_tokens, out: u.completion_tokens, cost });Expect actual to be a little higher than guess, because the API adds a few tokens of message framing around your text. Log the ratio over a few hundred real requests and use it to correct your four-characters rule. The docs list the response fields.
Where calculator estimates go wrong
A calculator is only as good as its inputs. Four sources of error show up again and again.
- Character-based counts. The four-characters rule can be off by 10 to 30 percent for code, tables and non-English text. Calibrate it with real usage figures, as in step 4.
- Message framing. Each message carries a few extra tokens of structure. With a long history of short messages this adds up.
- Output length. You do not know the reply length in advance. Budget on a high percentile, not the average.
- Retries and loops. Agents that call tools run several requests per task. Multiply by the typical number of rounds.
The output point deserves a number. Suppose a request has 2,000 input tokens, the median reply is 300 tokens and the 90th percentile reply is 900 tokens. The median call costs $0.0003 + $0.00015 = $0.00045. The 90th-percentile call costs $0.0003 + $0.00045 = $0.00075, two-thirds more. A budget built on the median will run low in a bad week.
For reference, here are four common shapes priced by the function from step 2:
| Request | Input / output tokens | Cost |
|---|---|---|
| Support reply | 1,000 / 300 | $0.0003 |
| Document summary | 12,000 / 400 | $0.002 |
| Chat turn with history | 3,000 / 250 | $0.000575 |
| Story chapter | 500 / 3,000 | $0.001575 |
Comparing against a different API
The reason to build the calculator with a rate table is comparison. Run the same prompts, record input and output token counts, and apply each provider's rates. Two cautions apply. First, tokenizers differ, so the same text yields different counts on different models; measure on each. Second, price is only one axis. Anthropic's Claude models and this service's open-weight model are different products, and this service is not Anthropic's API. If Claude's model suits your task and its price fits, use it. If you need a model that does not refuse lawful adult fiction or roleplay, price per token is not the deciding question.
For the background on how the two sides of the bill work, see the existing guide to pricing Claude-style workloads.
Using the calculator for a top-up decision
Credit is prepaid, from $20 to $500 in whole dollars, with +5% from $50, +10% from $100 and +15% from $200. Divide your top-up by the cost per call to see how long it lasts. At $0.0004 per call (1,000 input, 500 output) a $20 top-up covers 50,000 calls. At $0.00055 per call it covers 36,363 calls. Credit never expires, so you can start with the minimum and add more once the real numbers settle. The pricing page has the bonus table, and the API key page offers a $0.50 trial claim, valid 7 days, to run your first measurements for free.
If you share the calculator with a team, put the rates in a config file with a date next to them, and review it whenever a provider announces a change. Stale rates are the most common reason a budget spreadsheet is wrong.
More guides
Common questions
How many tokens is a prompt?
For English prose, about one token per four characters is a fair planning estimate. Code and non-English text run higher. The exact figure is in the usage field of the response.
Does the calculator work for any model?
The formula does. Put each provider's input and output rate per 1M tokens into the rate table and feed in measured token counts from that model.
Why is my real bill higher than my estimate?
Usually because chat history, system prompts and tool schemas are resent on every call. Model a whole conversation, not one message.
Can I check cost after a call without math?
Yes. The X-Balance-USD response header shows your remaining credit, so the difference between two calls is the cost of the call between them.
Begin with trial credits
Simple email/password signup. Key displays instantly.
No subscription model. Prepaid credits do not expire.