AI API cost optimization: the complete guide to cutting your bill

Updated 2026-07-15

Most AI API bills come down to four levers: which model tier answers each call, how many tokens the prompt carries, whether repeat requests hit a cache, and what rate you pay per token once the first three are dialed in. The worked example below takes a real 10 million token month from $100.00 down to $15.66 by working through all four in order. The fourth lever, a lower per-token rate, is also the only one that needs no change to prompts or model choice, just a base URL.

Quick answer: what actually drives an AI API bill

A prototype answering a few hundred requests a day can run on pocket change, and the same workflow at production volume can turn into a five-figure monthly invoice without anyone changing the code. The gap is rarely the sticker price of the model itself. It is a handful of quiet defaults: the flagship model left as the default for every call, a system prompt that grew line by line over a year, no cache in front of repeat requests, and a rate that was never revisited after the first integration. Two pricing shapes cover almost everything a developer will meet. Chat and completion models bill per token, split into an input rate for what you send and a usually higher output rate for what comes back. A per-call image model such as GPT Image 2 charges a flat rate per generated image instead, independent of prompt length. For a typical text or agent workload, though, token pricing sets nearly all of the bill, so that is where the rest of this guide focuses.

Where the tokens actually go

None of this is exotic. It is the same arithmetic whether the workload is a support bot, a coding agent, or a batch labeling job: tokens in, tokens out, multiplied by a rate that depends entirely on which model answered. The next section prices one realistic month four different ways so the effect of each lever is visible instead of theoretical.

  • System prompts and tool definitions ride along on every single call, so a bloated prompt is a recurring charge, not a one-time cost.
  • Input tokens usually dominate a chat or agent workload: the running conversation, retrieved context, and tool output all count as input again on every follow-up call.
  • Output tokens are billed at a meaningfully higher rate than input on nearly every provider, so an unbounded response costs more per token than the prompt that triggered it.
  • A failed or timed-out request still consumes the tokens it already sent. The retry sends them again, so one flaky call can bill twice.
  • Flagship reasoning models commonly carry three to five times the per-token rate of a mid-tier model doing the same classification or extraction task.

Worked example: the same 10 million tokens, priced four ways

Take a workload that sends 10 million tokens a month, split roughly three-quarters input to one-quarter output, a realistic ratio for a chat or agent product with moderate conversation length. The table below prices the same volume first with no optimization at all, then with three levers applied one at a time. Each stage keeps the workload's actual behavior unchanged; only the routing, prompt size, cache usage, and rate change. The order matters less than doing all four. Model tiering accounts for the largest single jump because it removes the flagship rate from most of the traffic. Trimming and caching then shrink the volume that gets billed at all. The rate cut in the last row is the only step that changes nothing about the application itself.

Illustrative model mix: claude-opus-4-7 as the flagship tier, claude-haiku-4-5 as the budget tier, at official list prices. Your task mix will move the exact numbers.
StageInput tokensOutput tokensMonthly costvs. baseline
Flagship model only, official rate7.5M2.5M$100.00Baseline
+ Route routine calls to a budget-tier model7.5M2.5M$36.0064% lower
+ Trim prompts by about a quarter5.6M2.5M$32.6367% lower
+ Cache repeat and near-duplicate calls3.4M1.5M$19.5880% lower
+ Route through a lower per-token rate3.4M1.5M$15.6684% lower

Why the bill surprises people every month

Each of these is fixable without a rewrite, and none of them require negotiating a better headline rate first. Fixing them in the order below is what produced the worked numbers above.

  • Traffic outgrows the routing decisions made during the prototype, and "just call the best model" quietly becomes the production default.
  • Nobody sets a max_tokens ceiling, so a model that should answer in fifty tokens occasionally writes six hundred, at the higher output rate.
  • Retries double-bill by design: the timed-out attempt already spent its input tokens, and the retry spends them again.
  • System prompts grow one edge case at a time, and every added line becomes a permanent charge on every future call, not just the one that needed it.
  • Nobody looks at spend broken down by model or by feature, so the one workflow burning most of the budget stays invisible until finance asks about a number nobody can explain.

Comparison: what the same task costs on different model tiers

Model choice is the single biggest lever precisely because the spread between tiers is enormous for tasks that do not need a flagship model's reasoning depth. Classification, extraction, short-form drafting, and routing decisions rarely need the most expensive model on the list; they need a model that gets the answer right, cheaply, every time.

Prices in USD per million tokens. Full catalog and current rates at /models and /pricing.
ModelInput $/1MOutput $/1MFits
claude-opus-4-7$4.00$20.00Hard reasoning, high-stakes writing, deep code review
claude-sonnet-4-6$2.40$12.00Balanced default for most production calls
gemini-3.5-flash$1.20$7.20Fast multimodal tasks, moderate volume
claude-haiku-4-5$0.80$4.00Classification, routing, extraction
gpt-5.4-mini$0.60$3.60General tasks, summaries, drafts
deepseek-v4-flash$0.126$0.252High-volume, simple, repetitive calls

Six fixes that actually move the number

The first five fixes are provider-agnostic; they work identically whether the request lands on a vendor's own endpoint or a gateway in front of it. The sixth is different, since it is the only lever here that touches billing rather than behavior. APIsRouter is one such gateway, an OpenAI-compatible endpoint with pay-as-you-go billing and no subscription; global models there are priced about 20% below official list, and Chinese-origin models are priced below their own official rates. Layered on top of the first five stages, a rate cut like that is exactly what took the worked example from $36.00 down to $15.66 in the earlier table.

  • Tier by task difficulty. Send classification, extraction, and short drafts to a budget model; reserve the flagship tier for reasoning that genuinely needs it.
  • Cap max_tokens on every call. An unbounded response can cost more in output than the entire input side of the same request.
  • Cache anything repeatable. Identical prompts, a static system prompt paired with short varying inputs, and classification with a small label set are the easiest wins.
  • Trim the system prompt on a schedule, not just once. Move rarely-needed instructions into a lookup the model requests only when relevant, instead of resending them on every call.
  • Batch what does not need a live answer. Nightly summarization, backfill labeling, and report generation do not need synchronous latency, and several providers price batch jobs lower than real-time calls.
  • Lower the rate itself once the call pattern is efficient. Routing routine traffic through a discounted OpenAI-compatible endpoint changes nothing about the prompts or the model IDs above; it only changes what you pay per token.

Point an OpenAI-compatible client at the lower rate

None of the fixes above require touching the request format. An OpenAI-compatible client only needs a new base_url and a matching API key; the model IDs, the message shape, and the streaming behavior stay exactly the same as a direct integration. Point the client at https://api.apisrouter.com/v1 with any model ID from the catalog above, and run the smoke test below before switching a production workload over.

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-key",
    base_url="https://api.apisrouter.com/v1",
)

response = client.chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Summarize this ticket in two sentences."}],
    max_tokens=200,
)
print(response.choices[0].message.content)

FAQ

How much can I realistically cut my AI API bill?

It depends how far your current setup is from the fixes on this page, but the worked example above takes a $100.00 baseline down to $15.66, an 84% reduction, by combining model tiering, prompt trimming, and caching before touching the rate at all. Teams starting from an unoptimized flagship-only setup usually see the largest single jump from tiering alone.

Is it ever worth paying for the flagship model?

Yes, for the minority of calls where reasoning depth actually changes the outcome: architecture decisions, gnarly debugging, or writing a customer will read closely. The mistake is not using a flagship model, it is leaving it as the default for every call regardless of difficulty.

Does caching really save money on LLM calls?

For workloads with repeat or near-duplicate requests, yes, meaningfully. Exact-match caching on identical prompts is cheap to implement and catches classification tasks, static-prompt lookups, and repeated boilerplate. Near-duplicate caching with an embedding similarity threshold catches more, at the cost of occasionally serving a slightly stale answer.

Why do output tokens cost more than input tokens?

Generating a token requires a full forward pass through the model for every token produced, while a large batch of input tokens can be processed together in parallel. That asymmetry shows up directly in pricing: on nearly every provider, the output rate runs several times the input rate for the same model.

What is the cheapest way to lower AI API costs without changing model behavior?

Routing through a pay-as-you-go OpenAI-compatible gateway such as APIsRouter, where global models are priced about 20% below official list and Chinese-origin models sit below their official rate, cuts the per-token rate without touching prompts, caching, or the models you already picked. It stacks on top of every other fix in this guide instead of replacing them, and switching over is a base_url change, not a rewrite.

Do batch APIs and streaming actually reduce cost?

Batch processing helps whenever a task does not need a synchronous answer: queue requests, submit them together, and collect results later at a lower rate than real-time calls. Streaming does not lower the per-token price, but it reduces wasted spend from timeouts, because a client can act on partial output instead of waiting, failing, and retrying the whole request.