Per MillionLLM API prices, tracked daily

LLM Cost Calculator Python: Build One in 5 Steps (2026)

By Per Million · Published · 20 min read

You can build an LLM cost calculator in Python in five steps: count tokens, store prices in a dictionary, compute the cost of each call, log every call and add up the totals by month. The script below uses October 2026 list prices from OpenAI, Anthropic and Google. It reads token counts from each API's own usage fields, so your estimates come from the same numbers the provider bills on. Copy it, change the model list to the ones you use, and check the prices against the official pages before you trust any total.

Which option is right for you?

Choose by situation. A rough estimate is enough to pick a model, but forecasting a monthly bill means logging real usage.

Which option is right for you?. Source: the LiteLLM price list (MIT License)
Your situationWhat to pick firstFirst page to open
Beginner who wants a rough monthly numberThe price dictionary and call_cost() below, with the "4 characters ≈ 1 token" ruleOpenAI API pricing
You call the API every day in a productThe SQLite logger below, filled from the usage object of each responseAnthropic token counting docs
Tight budget, high volumegpt-5-nano, gemini-2.5-flash-lite or claude-haiku-5-5, plus Batch where latency allowsGemini API pricing
Heavy Claude user deciding between a subscription and the APIRun the calculator on one month of logs, then compare it with the plan price/claude-api-pricing-vs-max-plan-break-even-table-for-2026/

Why these picks: beginners need a number quickly, and the chars/4 rule gets within a reasonable range. Daily users need billing-grade counts, and only the API response gives you those. Budget users save the most by changing the model tier, and the Batch discount comes second.

How Much Does an LLM Cost?

LLM APIs charge per million tokens, with separate rates for input and output. As of October 2026, list prices go from $0.05 per million input tokens to $50 per million output tokens across the three big providers (check the official pages for current rates).

All three vendors bill the same way: input tokens at one rate, output tokens at a higher rate, and a discount for input that is served from a cache. Here are the anchor prices from the official pages:

How Much Does an LLM Cost?. Source: the LiteLLM price list (MIT License)
Model (API ID)Input $/1MCached input $/1MOutput $/1MSource
gpt-5-nano0.050.0050.40OpenAI
gpt-5-mini0.250.0252.00OpenAI
gpt-5.42.500.2515.00OpenAI
gpt-5.55.000.5030.00OpenAI
claude-haiku-5-50.10 (≤100K)0.010.50 (≤100K)Anthropic
claude-sonnet-5-52.000.1010.00Anthropic
claude-opus-5-54.000.2020.00Anthropic
claude-fable-5-110.000.2550.00Anthropic
gemini-2.5-flash-lite0.10n/a0.40Google
gemini-3.8-flash0.750.0753.75Google
gemini-3.1-pro-preview2.00 (≤200K)0.2012.00 (≤200K)Google

Prices as of October 2026; check the official pages.

Two things in this table catch people out. First, output tokens cost 4 to 8 times as much as input tokens. Second, some models charge more once a prompt passes a size limit. Claude Haiku 5.5 rises to $0.50 input and $2.50 output above 100K tokens (Anthropic pricing). Gemini 3.1 Pro Preview rises to $4.00 and $18.00 above 200K (Google pricing).

Example (illustrative numbers): a support bot that sends 1,000 input tokens and gets 300 output tokens back, 100,000 times a month, uses 100M input and 30M output tokens. On gpt-5-mini that costs 100 × $0.25 + 30 × $2.00 = $85 a month. On claude-opus-5-5 it costs 100 × $4 + 30 × $20 = $1,000. For a wider comparison across five providers, see LLM API Pricing Comparison 2026.

What Is an LLM Cost Calculator and Why Build Your Own?

An LLM cost calculator multiplies token counts by per-model prices to estimate what you will spend on the API. If you build your own, the prices stay current and the usage numbers are yours instead of a template's.

Web calculators help with a first guess. They tend to fail in three ways, though. Some are missing new models, such as Claude Opus 5.5 (released with retirement "not sooner than " per Anthropic's models overview). Many have no fields for cached input or batch pricing. And most can't see your real input/output ratio, which drives the bill more than anything else.

A script of about 100 lines fixes all three. It runs where your API calls already happen, so it can read the exact token counts that come back in each response. It also lets you plan for a known price change. Google's pricing page says Gemini 3.8 Flash costs $0.75 input and $3.75 output through , then $1.50 and $7.50 from (Gemini API pricing). A generic calculator won't warn you that your Flash bill doubles in ten weeks. Your own price table can hold both rates with a switch-over date.

Pitfalls to avoid when you build one:

If you only use OpenAI, the shorter formula in OpenAI API Pricing Calculator: 2026 Formula + Cost Table may be enough.

How to Count Tokens in Python for Cost Estimation

Use tiktoken for OpenAI models, the free count_tokens endpoints for Claude and Gemini, and the API response's usage fields for billing-grade numbers. The "4 characters ≈ 1 token" rule works for rough planning.

Each provider has its own tokenizer, so the same paragraph produces a different count on each. Here are your options:

How to Count Tokens in Python for Cost Estimation. Source: the LiteLLM price list (MIT License)
ProviderBefore the call (estimate)After the call (billed)Cost of counting
OpenAItiktoken locally (GitHub)response.usageFree, offline
Anthropicclient.messages.count_tokens()response.usageFree, rate-limited (docs)
Googleclient.models.count_tokens()response.usage_metadataAPI call (docs)

Step 1: Install the libraries

pip install tiktoken openai anthropic google-genai

Step 2: Count OpenAI tokens offline

import tiktoken

def count_openai(text: str, model: str = "gpt-4o") -> int:
    try:
        enc = tiktoken.encoding_for_model(model)
    except KeyError:
        enc = tiktoken.get_encoding("o200k_base")  # fallback for newer model names
    return len(enc.encode(text))

The tiktoken README shows both tiktoken.encoding_for_model("gpt-4o") and tiktoken.get_encoding("o200k_base") (tiktoken on GitHub). The KeyError fallback matters because a tiktoken release may not know a model ID that came out after it. This count covers the text only. Chat formatting and tool definitions add a few tokens on top, which is why the logged usage numbers are the ones to trust.

Step 3: Count Claude tokens with the API

import anthropic
client = anthropic.Anthropic()

resp = client.messages.count_tokens(
    model="claude-sonnet-5-5",
    system="You are a support agent.",
    messages=[{"role": "user", "content": "Where is my order?"}],
)
print(resp.input_tokens)

Anthropic says token counting is "free to use" but has per-minute limits: 5,000 RPM on the Start tier, 10,000 on Build and 20,000 on Scale. The result is labeled an estimate (Anthropic token counting).

The same page has a warning that matters for cost: Claude Opus 4.7 and later models use a newer tokenizer that produces "approximately 30 percent more tokens" for the same text. Anthropic tells you to recount prompts against the model you plan to use (same source). So don't reuse token counts from Claude Sonnet 4.6 when you budget for Sonnet 5.5.

Step 4: Count Gemini tokens

from google import genai
client = genai.Client()
r = client.models.count_tokens(model="gemini-3.8-flash", contents="Where is my order?")
print(r.total_tokens)

Google's docs give the rule of thumb that "a token is equivalent to about 4 characters" and that 100 tokens is about 60-80 English words. They also list fixed costs for media: an image up to 384 pixels is 258 tokens, video is about 100 tokens per second and audio about 32 tokens per second (Gemini token docs).

Step 5: A no-API fallback

def rough_tokens(text: str) -> int:
    return max(1, len(text) // 4)

This fallback is fine for comparing models before you write any integration code. Don't use it for invoices. It undercounts code, non-English text and, on Claude 4.7+ models, ordinary English prose.

How to Build a Python Price Table for OpenAI, Anthropic, and Gemini Models

Keep input, cached-input and output prices per million tokens in one dictionary keyed by the exact API model ID, and add a dated comment showing when you last checked them. The table below matches the official pages as of October 2026.

Save this as llm_cost.py:

# llm_cost.py: USD per 1M tokens. Verify against official pages before use.
from decimal import Decimal, ROUND_HALF_UP

PRICES_CHECKED = "2026-10-23"

PRICES = {
    # OpenAI: developers.openai.com/api/docs/pricing (Standard tier)
    "gpt-5.5":       {"in": 5.00, "cached": 0.50,  "out": 30.00},
    "gpt-5.4":       {"in": 2.50, "cached": 0.25,  "out": 15.00},
    "gpt-5.4-mini":  {"in": 0.75, "cached": 0.075, "out": 4.50},
    "gpt-5.4-nano":  {"in": 0.20, "cached": 0.02,  "out": 1.25},
    "gpt-5-mini":    {"in": 0.25, "cached": 0.025, "out": 2.00},
    "gpt-5-nano":    {"in": 0.05, "cached": 0.005, "out": 0.40},
    "gpt-4.1":       {"in": 2.00, "cached": 0.50,  "out": 8.00},
    "gpt-4o":        {"in": 2.50, "cached": 1.25,  "out": 10.00},
    "gpt-4o-mini":   {"in": 0.15, "cached": 0.075, "out": 0.60},
    "o4-mini":       {"in": 1.10, "cached": 0.275, "out": 4.40},

    # Anthropic: claude.com/pricing (cache_write = 5-minute cache write)
    "claude-fable-5-1":  {"in": 10.00, "cached": 0.25, "cache_write": 12.50, "out": 50.00},
    "claude-opus-5-5":   {"in": 4.00,  "cached": 0.20, "cache_write": 5.00,  "out": 20.00},
    "claude-sonnet-5-5": {"in": 2.00,  "cached": 0.10, "cache_write": 2.50,  "out": 10.00},
    "claude-haiku-5-5":  {"in": 0.10,  "cached": 0.01, "cache_write": 0.125, "out": 0.50,
        "long": {"above": 100_000, "in": 0.50, "cached": 0.05,
                 "cache_write": 0.625, "out": 2.50}},

    # Google: ai.google.dev/gemini-api/docs/pricing (paid tier)
    "gemini-3.8-flash": {"in": 0.75, "cached": 0.075, "out": 3.75,
        "from_2027_01_01": {"in": 1.50, "cached": 0.15, "out": 7.50}},
    "gemini-3.1-pro-preview": {"in": 2.00, "cached": 0.20, "out": 12.00,
        "long": {"above": 200_000, "in": 4.00, "cached": 0.40, "out": 18.00}},
    "gemini-2.5-pro":   {"in": 1.25, "cached": 0.125, "out": 10.00,
        "long": {"above": 200_000, "in": 2.50, "cached": 0.25, "out": 15.00}},
    "gemini-2.5-flash":      {"in": 0.30, "cached": 0.03, "out": 2.50},
    "gemini-2.5-flash-lite": {"in": 0.10, "out": 0.40},
}

Why it's built this way:

Prices are stored as plain floats so they're easy to read. The cost function converts them to Decimal before doing any arithmetic.

How to Calculate Cost Per API Call in Python

Write one function that takes the model name and the token counts, picks the right price tier, applies any batch discount and returns a rounded dollar amount. Then add three small adapters, one per provider, because each SDK reports usage differently.

The core function

Add this to llm_cost.py:

from datetime import date

MILLION = Decimal(1_000_000)

def _rates(model: str, prompt_tokens: int, on: date | None = None) -> dict:
    if model not in PRICES:
        raise KeyError(f"No price for {model!r}. Add it to PRICES (checked {PRICES_CHECKED}).")
    p = dict(PRICES[model])
    if on and on >= date(2027, 1, 1) and "from_2027_01_01" in p:
        p.update(p["from_2027_01_01"])
    if "long" in p and prompt_tokens > p["long"]["above"]:
        p.update(p["long"])
    return p

def call_cost(model, uncached_in=0, cached_in=0, cache_write=0, output=0,
              batch=False, on=None) -> Decimal:
    p = _rates(model, uncached_in + cached_in + cache_write, on)
    d = lambda k: Decimal(str(p.get(k, p["in"])))
    total = (uncached_in * d("in") + cached_in * d("cached")
             + cache_write * d("cache_write") + output * d("out")) / MILLION
    if batch:
        total *= Decimal("0.5")
    return total.quantize(Decimal("0.000001"), rounding=ROUND_HALF_UP)

Decimal avoids float noise such as 0.30000000000000004 building up over a million logged rows. The batch=True flag applies the 50% Batch discount, which all three vendors list: OpenAI (pricing), Anthropic (claude.com/pricing) and Google (Gemini pricing). OpenAI's page also says the Flex tier matches Batch rates for most models, so use batch=True for Flex calls too. Batch rules differ by provider in other ways, which are covered in Batch API Discount 2026: OpenAI vs Claude vs Gemini.

Provider adapters

The three SDKs don't define "input tokens" the same way, and the adapters fix that:

def cost_openai(model, usage, batch=False):
    # usage from the Responses API; cached tokens are part of input_tokens
    cached = getattr(getattr(usage, "input_tokens_details", None), "cached_tokens", 0) or 0
    return call_cost(model, uncached_in=usage.input_tokens - cached,
                     cached_in=cached, output=usage.output_tokens, batch=batch)

def cost_anthropic(model, usage, batch=False):
    # input_tokens excludes cache reads and cache writes
    return call_cost(model,
        uncached_in=usage.input_tokens,
        cached_in=getattr(usage, "cache_read_input_tokens", 0) or 0,
        cache_write=getattr(usage, "cache_creation_input_tokens", 0) or 0,
        output=usage.output_tokens, batch=batch)

def cost_gemini(model, um, batch=False):
    # usage_metadata; thinking tokens are billed at the output rate
    cached = um.cached_content_token_count or 0
    out = (um.candidates_token_count or 0) + (um.thoughts_token_count or 0)
    return call_cost(model, uncached_in=um.prompt_token_count - cached,
                     cached_in=cached, output=out, batch=batch)

The field meanings come from each vendor's caching docs. OpenAI reports cached_tokens as a subset of input tokens (OpenAI prompt caching). Anthropic reports cache reads and writes in separate fields from input_tokens (Anthropic prompt caching). Google's price table is headed "Output price (including thinking tokens)" (Gemini pricing). Field names can change between SDK versions, so print one raw usage object before you rely on an adapter.

Quick check (example values)

print(call_cost("gpt-5-mini", uncached_in=1_000, output=300))      # 0.000850
print(call_cost("claude-haiku-5-5", uncached_in=150_000, output=500))  # long tier applies
print(call_cost("gemini-3.8-flash", uncached_in=1_000, output=300,
                on=date(2027, 1, 15)))                                 # 2027 rate

The first line works out as 1,000 × $0.25/1M + 300 × $2.00/1M = $0.00025 + $0.0006 = $0.00085.

How to Track Monthly LLM Spend Across Multiple Models

Log each call's model, token counts and cost to SQLite with a timestamp. Then group by month to see what you've spent so far and project the full bill. That is enough to catch runaway usage within a day instead of at invoice time.

Steps

  1. Create the log table once:
import sqlite3, datetime as dt

db = sqlite3.connect("llm_costs.db")
db.execute("""CREATE TABLE IF NOT EXISTS calls(
    ts TEXT, model TEXT, feature TEXT,
    uncached_in INT, cached_in INT, cache_write INT, output INT,
    batch INT, cost TEXT)""")
  1. Write one row after every API call:
def log_call(model, feature, cost, uncached_in=0, cached_in=0,
             cache_write=0, output=0, batch=False):
    db.execute("INSERT INTO calls VALUES (?,?,?,?,?,?,?,?,?)",
        (dt.datetime.now(dt.timezone.utc).isoformat(), model, feature,
         uncached_in, cached_in, cache_write, output, int(batch), str(cost)))
    db.commit()

The feature column (for example "chat", "summarize", "classify") shows which product feature is spending the money. Model-level totals can't tell you that.

  1. Get the month-to-date total and a straight-line projection:
import calendar

def month_report(month: str | None = None):
    today = dt.date.today()
    month = month or today.strftime("%Y-%m")
    rows = db.execute("""SELECT model, feature, SUM(CAST(cost AS REAL))
                         FROM calls WHERE substr(ts,1,7)=?
                         GROUP BY model, feature ORDER BY 3 DESC""", (month,)).fetchall()
    spent = sum(r[2] for r in rows)
    days = calendar.monthrange(today.year, today.month)[1]
    projected = spent / today.day * days if month == today.strftime("%Y-%m") else spent
    for m, f, c in rows:
        print(f"{m:24} {f:12} ${c:,.2f}")
    print(f"Spent: ${spent:,.2f}  Projected month: ${projected:,.2f}")
  1. Add a guard. For example, if projected goes over your monthly budget, raise an exception in a scheduled job or send the result to whatever alerting you already run. The calculator only reports numbers; deciding what happens when a limit is hit is up to your code.

Pitfalls

Check the logged total against the vendor's billing console once a month. If the two differ by more than a few percent, look first for a price change or a model alias you haven't added to the table.

GPT-4o vs Claude vs Gemini: Which Model Is Cheapest for Your Use Case?

For high-volume simple tasks, the cheapest models are gpt-5-nano, gemini-2.5-flash-lite and claude-haiku-5-5, at $17-$25 for the example workload below. Compare cost per task, though, not just the price per token.

Example workload (illustrative): 100,000 requests a month, each with 1,000 input and 300 output tokens, no caching, standard tier. That's 100M input and 30M output tokens. Costs come from the October 2026 list prices above:

GPT-4o vs Claude vs Gemini: Which Model Is Cheapest for Your Use Case?. Source: the LiteLLM price list (MIT License)
ModelMonthly costGood fit
gpt-5-nano$17.00Classification, routing, short extraction
gemini-2.5-flash-lite$22.00Same, plus cheap multimodal input
claude-haiku-5-5$25.00Anthropic describes it as for "classification, extraction, and routing" (overview)
gpt-4o-mini$33.00Existing apps already tuned for it
gpt-5-mini$85.00General chat with better reasoning
gemini-3.8-flash$187.50 (→ $375.00 from Jan 2027)Fast general model; plan for the price rise
claude-sonnet-5-5$500.00Coding and agent work
gpt-4o$550.00Legacy integrations
gemini-3.1-pro-preview$560.00Hard reasoning, long documents
gpt-5.4$700.00Strong general reasoning
claude-opus-5-5$1,000.00Long-running agentic coding
gpt-5.5$1,400.00OpenAI's top tier in this list
claude-fable-5-1$2,500.00Hardest reasoning, long-horizon agents

Prices as of October 2026; check the official pages.

This table understates some models, for two reasons. Reasoning models add thinking tokens, which are billed as output on Gemini (Gemini pricing). Anthropic lists Claude Opus 5.5 and Fable 5.1 as "Adaptive (always on)" for thinking (models overview). Also, the same text produces about 30% more tokens on Claude 4.7+ models (token counting docs). The table gives every model the same token counts. Real Claude counts for the same prompts would be higher.

How to decide:

  1. Take 50-100 real prompts from your logs.
  2. Run them through two or three candidate models and record usage with the adapters above.
  3. Score the answers against your own pass/fail check.
  4. Divide the total cost by the number of passing answers. The lowest cost per passing answer wins.

A cheap model that fails 30% of the time and needs a retry on a pricier model can cost more than going to the pricier model first. For per-token rankings across more vendors, see Cheapest LLM API in 2026: Real Cost per 1M Tokens Table. If you're prototyping on Google, also read Gemini API Pricing Free Tier 2026. Several Flash and Flash-Lite models are listed with a free tier (Gemini pricing).

Common Pitfalls When Estimating LLM Costs

Most calculators drift from the real bill for a few reasons: output is underestimated, cache and batch rates are ignored, and the price table gets old. Each one has a simple fix.

Common Pitfalls When Estimating LLM Costs. Source: the LiteLLM price list (MIT License)
MistakeWhat happensFix
Guessing output lengthOutput costs 4-8× input, so a 2× miss on output can double the billLog real output_tokens and use the 90th percentile in forecasts
Ignoring reasoning/thinking tokensHidden tokens are billed as outputInclude thoughts_token_count (Gemini) and use the billed output totals
Reusing old Claude token countsClaude 4.7+ uses about 30% more tokens for the same text (source)Recount with count_tokens on the target model
Double-counting cached tokens on OpenAICached tokens are already inside input_tokensSubtract cached_tokens before pricing uncached input
Missing Claude cache writesWrites cost 1.25× input for 5-minute caching (pricing)Price cache_creation_input_tokens separately
Ignoring long-context tiersPrompts over 100K (Haiku 5.5) or 200K (Gemini Pro) cost moreUse the long key in the price table
Stale pricesGemini 3.8 Flash doubles on (source)Store the date you checked prices and recheck monthly
Unknown model IDs counted as $0New aliases silently cost nothing in your reportRaise KeyError, as _rates() does

The last row is the cheapest to fix and causes the worst surprises. A calculator that returns 0 for an unknown model gives you a confident but wrong total.

Next steps

You can have the calculator running today. If you'd rather not maintain one, a managed tool is the alternative.

Do it yourself today:

  1. Save llm_cost.py, then delete every model from PRICES that you don't call. A short table is easier to keep current.
  2. Wrap your main API call with the matching cost_* adapter and log_call(), and tag each call with a feature name.
  3. After 24 hours, run month_report() and compare it with the usage page in your provider's console.

If you'd rather not build it: an LLM observability service such as Langfuse Cloud records traces with token and cost tracking. Plans are Hobby (free, 50k units/month), Core ($29/month) and Pro ($199/month), each paid plan with 100k units/month included (Langfuse pricing; as of October 2026, check the official page). It suits teams that also want prompt tracing and dashboards without building them. It suits solo developers who only need one monthly number less well, because the script above does that for free. A limitation: any hosted tool's price list can also fall behind new models, so check its numbers against vendor pricing pages too. A free fallback is each vendor's own billing console, which shows billed usage but won't give you a combined view across providers.

Related guides on Per Million:

FAQ

How much does an LLM cost?

LLM APIs bill per million tokens, with output priced higher than input. As of October 2026, gpt-5-nano costs $0.05 input and $0.40 output per million tokens. claude-fable-5-1 costs $10 input and $50 output. A typical small app on a mini-tier model can run under $100 a month, but your token volume decides the total.

How do I count tokens in Python?

For OpenAI models, use tiktoken.encoding_for_model() locally. For Claude, call client.messages.count_tokens(), which is free but rate-limited. For Gemini, use client.models.count_tokens(). For billing-grade numbers, read the usage fields that come back with every response.

How do I calculate API cost per call?

Multiply uncached input, cached input and output tokens by their per-million rates, add them up and divide by 1,000,000. Apply 0.5 if the call went through a Batch API. The call_cost() function above does this with Decimal and handles the long-context tiers.

Is GPT-4o cheaper than Claude or Gemini?

Not at the list prices on the official pages as of October 2026. gpt-4o costs $2.50 input and $10 output per million tokens. claude-sonnet-5-5 costs $2 and $10, and gemini-3.8-flash costs $0.75 and $3.75 through December 2026. For simple tasks, gpt-4o-mini, gemini-2.5-flash-lite and claude-haiku-5-5 all cost far less.

Sources

Spotted something out of date or wrong? Tell us at shigoto.ai.note@gmail.com. We check every guide against its official sources.