EN ▾
Try with trial credit

Build an OpenAI API Price Calculator in Python

An openai api price calculator needs only three inputs: how many requests you send, how many tokens each prompt has and how long each answer is. This article gives the formula, a 30-line script, a spreadsheet version and a table of scenarios, all priced at $0.15 per 1M input and $0.50 per 1M output tokens.

Last updated

A glass bead calculator with token stacks sliding along rails into a coin tray, picturing an API cost estimate

Key takeaways

  • Monthly cost = requests x days x (prompt tokens x input rate + answer tokens x output rate) / 1,000,000.
  • Measure prompt and answer sizes from the usage field of real replies instead of guessing from word counts.
  • Add headroom for chat history growth; a multiplier of 1.2 to 1.5 is a sensible start.
  • Check peak load too: a full key allows 8 concurrent requests and 300 per minute.

The formula behind every calculator

Language model APIs bill by tokens, with different prices for input and output. Everything a calculator does comes from that:

cost per request = (prompt tokens x $0.15 + answer tokens x $0.50) / 1,000,000

Multiply by requests per day and by days to get a monthly figure. The hard part is not the arithmetic, it is choosing honest inputs. Prompt tokens include the system message and any chat history you resend. Answer tokens include everything the model writes. Billing uses the real counts reported in each response, so your estimate is only as good as your token numbers.

The rates above are this API's. To compare another provider, replace the two constants with the numbers on that provider's current pricing page and keep the rest unchanged. We do not list other vendors' prices, because they change and are theirs to state.

A small script you can run today

Save this as estimate.py. It takes the three inputs from the command line and prints cost per request, per day and for the period you choose.

# estimate.py
"""Estimate LLM API cost from request volume, prompt size and answer length."""
import argparse

INPUT_PER_M = 0.15   # dollars per 1M input tokens
OUTPUT_PER_M = 0.50  # dollars per 1M output tokens


def request_cost(prompt_tokens: float, answer_tokens: float) -> float:
    return (prompt_tokens * INPUT_PER_M + answer_tokens * OUTPUT_PER_M) / 1_000_000


def main() -> None:
    p = argparse.ArgumentParser()
    p.add_argument("--requests-per-day", type=float, required=True)
    p.add_argument("--prompt-tokens", type=float, required=True)
    p.add_argument("--answer-tokens", type=float, required=True)
    p.add_argument("--days", type=int, default=30)
    p.add_argument("--headroom", type=float, default=1.0,
                   help="multiplier for retries and growth, e.g. 1.2")
    a = p.parse_args()

    per_request = request_cost(a.prompt_tokens, a.answer_tokens) * a.headroom
    per_day = per_request * a.requests_per_day
    print(f"per request : ${per_request:.6f}")
    print(f"per day     : ${per_day:.2f}")
    print(f"{a.days} days    : ${per_day * a.days:.2f}")


if __name__ == "__main__":
    main()

Try it with a modest chat app: 1,000 requests a day, 800 prompt tokens and 300 answer tokens.

python estimate.py --requests-per-day 1000 --prompt-tokens 800 --answer-tokens 300
# per request : $0.000270
# per day     : $0.27
# 30 days    : $8.10

Check by hand: 800 x $0.15 = 120 and 300 x $0.50 = 150, which is 270 divided by 1,000,000 = $0.00027 per request. A thousand requests make $0.27 a day, and thirty days make $8.10. The script and the hand calculation agree, which is the test every calculator should pass before you trust it.

Where to get honest token numbers

Do not estimate prompt size from the length of the last message. Pull the numbers from real traffic. Every reply carries a usage object, so a few calls tell you what your prompts really cost:

from openai import OpenAI

client = OpenAI(base_url="https://api.openaipriceapi.com/v1", api_key="YOUR_KEY")
reply = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Write a two-line greeting for a tavern bot."}],
)
print(reply.usage.prompt_tokens, reply.usage.completion_tokens)

Run the call with your real system prompt and a typical user message, then average over twenty or thirty samples. If you do not have an account yet, the API key page lets you claim $0.50 of trial credit with one click. No card is needed, and it lasts 7 days. That is enough for hundreds of test calls.

If you only have word counts, a rough rule is a bit over one token per word for English text. Treat it as a first guess and replace it with measured usage as soon as you can.

Scenario table: three typical workloads

The table uses the same formula for different shapes of traffic. All figures are for 30 days.

WorkloadRequests per dayPrompt tokensAnswer tokensPer day30 days
Support-style chat1,000800300$0.27$8.10
Roleplay with long character card5006,000400$0.55$16.50
Batch tagging job20,000500150$3.00$90.00

Working the middle row: 500 requests x 6,000 tokens is 3,000,000 input tokens, which costs 3 x $0.15 = $0.45. The same 500 requests produce 200,000 output tokens, which cost 0.2 x $0.50 = $0.10. The day totals $0.55, and 30 days come to $16.50. Notice that input is more than four times the output cost there. Long prompts, not long answers, drive that bill, so shortening the character card saves more than capping the reply.

The last row shows the opposite: 20,000 short jobs, with input and output costing $1.50 each per day.

Add headroom for the things a formula misses

Real usage drifts above the clean estimate. Three effects matter most.

  • History growth. In a chat, each turn resends all earlier messages. Use the average prompt size across a whole conversation, not turn one.
  • Retries. A retried request is a new request. The upside is that failed or refused requests are not charged on this API, so only completed calls count against your credit.
  • Prompt edits. Teams add instructions over time. A prompt that starts at 400 tokens often ends up near 1,000.

The --headroom flag in the script handles this. With 1.3, the chat example becomes $0.27 x 1.3 = $0.351 a day and $10.53 a month. Pick the multiplier from your own logs after the first week of traffic.

Price a feature before you build it

The best moment to run the numbers is before the feature exists. Here is a short method.

  1. Write the real system prompt and three typical user messages.
  2. Send twenty calls and record the median prompt and answer tokens from usage.
  3. Estimate users, actions per user per day, and how many of those actions call the model.
  4. Run the calculator, then apply a headroom multiplier.

Example: a companion chat feature with 300 daily users, 20 messages each, which makes 6,000 requests a day. Say the median call has 1,500 prompt tokens (system message, character card and a short history) and 250 answer tokens. Per request: (1,500 x $0.15 + 250 x $0.50) / 1,000,000 = (225 + 125) / 1,000,000 = $0.00035. Per day: 6,000 x $0.00035 = $2.10. Over 30 days: $63.00. With a 1.3 headroom multiplier, the budget is $81.90.

A $100 top-up gives $110 of credit, so it would last a little over a month at that level. If the figure looks high, the cheapest fix is a shorter history window or a leaner character card. Cutting 500 prompt tokens from every call saves 6,000 x 500 x $0.15 / 1,000,000 = $0.45 a day, or $13.50 a month.

The same calculator in a spreadsheet

Not everyone wants a script. In a sheet, put prompt tokens in B2, answer tokens in C2 and requests per day in D2. The monthly cost is:

=(B2*0.15 + C2*0.50) / 1000000 * D2 * 30

Add one row per workload and a total at the bottom. A column for headroom makes the sheet useful for planning: multiply the result by it. Because the rates live in the formula, you can copy the sheet and swap in another provider's rates when you compare.

From estimate to top-up, and a peak-load check

Credit is prepaid, so the monthly figure tells you what to buy. The three workloads above need $8.10, $16.50 and $90.00 a month. Top-ups are any whole amount from $20 to $500, paid in crypto (USDT on TRC20 or USDC on Base), with +5% credit from $50, +10% from $100 and +15% from $200. A $100 top-up gives $110 of credit, which covers the roleplay workload for more than six months. Credit never expires, so buying ahead has no cost. The pricing page has the current tiers.

Then check the peak. Concurrent requests are roughly the arrival rate times the average duration of a call. A burst of 5 requests per second, with calls that last 4 seconds each, puts about 20 in flight. A full key allows 8 at once and 300 per minute (60 and 2 for a trial key), so that burst needs a queue on your side or a lower rate. Measure your real call duration, because we do not publish latency figures. A streamed request holds its slot until the stream ends.

To track real spend against the estimate, read the X-Balance-USD header on responses or call GET /v1/balance. The docs cover the endpoints and request fields, and the Python call above works with any OpenAI SDK once the base URL points at this API.

Common questions

What is the formula for an LLM API cost estimate?

Cost per request equals prompt tokens times the input rate plus answer tokens times the output rate, divided by 1,000,000. Multiply by requests and days for a monthly figure.

How accurate is a calculator based on word counts?

It gives a rough first guess. English runs a bit above one token per word, but the real counts in the usage field of each reply are what you are billed on.

How much does a 1,000 requests per day chat app cost?

With 800 prompt tokens and 300 answer tokens per request, it is $0.27 a day and $8.10 over 30 days at $0.15 and $0.50 per 1M tokens.

How much headroom should I add?

Start with a multiplier between 1.2 and 1.5 for history growth and prompt changes, then adjust after a week of real usage data.

Get your API key in steps

Register, copy the token, update your base URL. That is the entire configuration.

Enter email and password; receive your key instantly.