How to Reduce AI API Costs: 8 Token Optimization Strategies
Updated June 2026 · 8 min read
AI API costs can seem negligible when you're prototyping — a few cents per request barely registers. But once you move to production and start handling thousands or tens of thousands of requests per day, the bill grows fast. A single GPT-4o conversation with a 500-token system prompt and a 400-token response costs about $0.006. Multiply that by 50,000 daily users and you're looking at $300/day — nearly $110,000 per year — just for one feature.
The good news: most teams overspend by 40–70% on AI API costs without realizing it. The strategies in this guide — from choosing the right model to enabling prompt caching — can cut that bill dramatically without sacrificing output quality. Before diving in, it helps to understand what AI tokens are and how they map to cost. You can also use our free AI Token Calculator to measure your current usage and see exactly where your budget is going.
AI API pricing at a glance
Before optimizing, know your baseline. Here is how the major models compare on cost as of mid-2026. Output tokens typically cost 3–4× more than input tokens, so reducing response length has an outsized impact on your bill.
| Model | Input / 1M tokens | Output / 1M tokens | Relative cost |
|---|---|---|---|
| Gemini 2.0 Flash | $0.10 | $0.40 | 1× |
| GPT-4o mini | $0.15 | $0.60 | 1.5× |
| Claude Haiku 3.5 | $0.80 | $4.00 | 8× |
| GPT-4o | $2.50 | $10.00 | 25× |
| Claude Sonnet 4 | $3.00 | $15.00 | 30× |
| Gemini 2.5 Pro | $7.00 | $21.00 | 52× |
The difference between the cheapest and most expensive models is more than 50×. Even modest optimizations — like routing 60% of requests to a cheaper model — can reduce total cost by half overnight. Want to run the numbers on your own prompts? Plug them into our Token Calculator to see the cost breakdown across every major model.
Strategy 1: Choose the right model for the task
The single highest-impact change most teams can make is model routing — using a powerful (and expensive) model only when the task genuinely requires it, and routing simpler work to a cheaper model. The temptation is to default to GPT-4o or Claude Sonnet for everything because the outputs feel better, but for many production tasks the cheaper models perform comparably.
Good tasks for cheap models (GPT-4o mini, Gemini 2.0 Flash, Claude Haiku): classification, short-form summarization, data extraction from structured input, FAQ answering with retrieval augmentation, translation, sentiment analysis, and simple question answering.
Reserve expensive models (GPT-4o, Claude Sonnet, Gemini Pro) for: complex reasoning, multi-step planning, nuanced writing, code generation for complex logic, and tasks where output quality directly drives revenue. A practical approach is to run a sample of your production requests through both tiers, measure quality with a simple rubric or human review, and route everything that scores the same on both models to the cheaper tier. Many teams find 60–80% of requests qualify.
Strategy 2: Shorten your prompts
Long, verbose prompts are one of the most common sources of wasted tokens — and they are entirely within your control. Every extra word in your system prompt multiplies across every single API call. A 200-word system prompt sent 100,000 times per day costs more than $6/day on GPT-4o in input tokens alone. Trim it to 80 words and you save $3.60/day — over $1,300/year — without touching your model choice.
Before (verbose):
"Please carefully read the following customer support message and then provide a helpful, friendly, and professional response that addresses all of the customer's concerns. Make sure your tone is empathetic and that you offer actionable next steps." (42 tokens)
After (tight):
"Reply to this support message. Be friendly, address all concerns, give actionable next steps." (17 tokens — 60% reduction)
Practical trimming tactics: remove filler phrases ("please carefully", "make sure to"), replace prose instructions with bullets, cut redundant context, and use examples only when they genuinely change the output. Understanding how many tokens are in each word helps you prioritize which sections to trim first.
Strategy 3: Summarize context instead of repeating it
In multi-turn conversations, the naive approach is to pass the full message history on every turn. As a conversation grows, this gets extremely expensive. A 20-turn conversation with 100-token average messages means turn 20 sends ~2,000 tokens of history on top of the new message — 20× the cost of a single-turn request.
Three better approaches: sliding window — keep only the last N turns and discard older ones. Summarization — after 5–10 turns, call the API to produce a compact summary of what has been discussed, then replace the raw history with that summary. Selective history — keep only turns that contain facts the model still needs (user preferences, decisions made, entities referenced) and drop the conversational filler.
A practical rule of thumb: if your average conversation exceeds 2,000 tokens of accumulated history, implement a summarization step. The summarization call costs tokens, but one 200-token summary replacing 1,500 tokens of raw history pays back within two subsequent turns.
Strategy 4: Control output length with max_tokens
By default, most models generate as many tokens as they judge appropriate. This means a request for "a brief summary" might return 400 tokens when 150 would suffice — and since output tokens cost 3–4× more than input tokens, that padding is expensive. Always set an explicit max_tokens parameter that reflects the actual maximum useful length for each endpoint in your application.
Beyond the parameter, include length instructions directly in your prompt. Phrases like "respond in 2–3 sentences", "reply in under 100 words", or "return only the JSON object with no explanation" reliably shrink output size. For structured extraction tasks, defining a strict schema and asking for JSON output instead of prose can cut token usage by 50–70% compared to a conversational response covering the same information.
A tiered approach works well: set max_tokens to your soft limit, use prompt instructions to target even lower, and monitor actual output token counts over time to catch any drift. Our Token Calculator can help you estimate the cost difference between output sizes before you ship.
Strategy 5: Use prompt caching
Prompt caching is one of the most powerful — and underused — cost reduction techniques available today. Both Anthropic and OpenAI offer discounted rates when a prefix of your prompt is identical across multiple requests, allowing the provider to reuse computed key-value states from the first request rather than reprocessing the same tokens from scratch.
Anthropic (Claude): Pass "cache_control": {"type": "ephemeral"} on the content block you want cached. Cached input tokens cost $0.30/1M (vs $3.00/1M standard) — a 90% discount on those tokens. Cache entries last 5 minutes and refresh on each hit.
OpenAI (GPT-4o and later): Prompt caching is automatic for prompts longer than 1,024 tokens. Cached tokens are billed at 50% of the standard input rate, with no code changes required. The cache is keyed on the exact prefix, so keep stable content (system prompts, few-shot examples, retrieved context) at the top of your messages array.
The savings compound with scale. A 1,000-token system prompt sent 1,000,000 times/month costs $3,000 uncached on Claude Sonnet but only $300 with caching — saving $2,700/month on that one prompt alone. For more on tokens and why these savings are so significant, see our guide on what AI tokens are.
Strategy 6: Batch requests where possible
If your application processes requests that do not need a real-time response — nightly data enrichment, bulk document analysis, generating product descriptions, running evals — batch processing offers a straightforward 50% cost reduction.
OpenAI Batch API: Submit a .jsonl file of requests and receive results within 24 hours. All models available via the standard API are supported, and batched requests are billed at exactly 50% of the standard per-token rate. A data pipeline processing 500,000 documents/month that would cost $2,500 on standard API costs $1,250 via batch — saving $1,250/month with no change to the model or prompt.
Anthropic (Claude): The Message Batches API supports asynchronous processing with a similar 50% input token discount. Batches accept up to 10,000 requests and can run in parallel, making them well-suited for large-scale offline jobs.
The key discipline: audit your application for endpoints that are latency-tolerant. Analytics pipelines, background enrichment jobs, offline eval suites, and scheduled content generation are all strong candidates for batching.
Strategy 7: Cache results in your application
API-level prompt caching reduces the cost of reprocessing identical prompt prefixes — but application-level result caching goes further by avoiding the API call entirely for identical or near-identical requests. If two users ask "What is the return policy?" on the same e-commerce platform, there is no reason to call the API twice. Cache the result and serve it from your own store.
Exact-match caching: Hash the full prompt (including system prompt and conversation state) and store the response. Works best for stateless queries with a small vocabulary of possible inputs, like FAQ responses or fixed classification tasks. Use a TTL appropriate for how often your underlying data changes — 1–24 hours for most use cases.
Semantic caching: For more varied natural-language input, embed the query and retrieve cached responses for semantically similar past queries above a cosine similarity threshold (typically 0.92–0.97). Libraries like GPTCache make this straightforward. Cache hit rates of 30–60% are common for customer-facing chatbots, and each hit is pure cost elimination.
Even a 20% cache hit rate on a $1,000/month API budget saves $2,400/year. Combine this with prompt caching (Strategy 5) for maximum impact — application-level caching eliminates the call entirely, while prompt caching reduces the cost of the calls that do get through. Use the AI Token Calculator Guide to estimate the value of your cache hit rate before building the infrastructure.
Strategy 8: Monitor and alert on token usage
You cannot optimize what you do not measure. Surprisingly many teams deploy AI features without per-request token logging, meaning they have no visibility into cost anomalies, prompt bloat over time, or individual features that are burning disproportionate budget.
At minimum, log prompt_tokens, completion_tokens, and total_tokens from every API response alongside the feature name, user ID, and timestamp. Most AI SDKs return this in the response object. Aggregate these into a dashboard and track: average tokens per request by feature, total daily spend, and p95/p99 token counts to catch outliers.
Set up budget alerts through your provider's dashboard — both OpenAI and Anthropic support email and webhook alerts when spend crosses a threshold. For more granular control, implement soft limits in your application layer: if a single request's prompt exceeds a token threshold (say, 4,000 tokens), log a warning and investigate whether the context-building logic has a bug. Knowing that one endpoint averages 3,200 tokens when it should average 800 is often enough to identify a summarization step that was accidentally disabled. Our Token Calculator is useful for benchmarking what "normal" looks like for a given prompt structure.
Use our free AI Token Calculator to measure your current prompt sizes and compare costs across models before and after optimization.
Open Token Calculator →Frequently asked questions
How much does the ChatGPT API cost?
The ChatGPT API (OpenAI's API) pricing varies by model. As of mid-2026: GPT-4o costs $2.50/1M input tokens and $10.00/1M output tokens. GPT-4o mini costs $0.15/1M input and $0.60/1M output — about 17× cheaper for most workloads. GPT-3.5 Turbo is $0.50/1M input and $1.50/1M output. A single average API call (500 input + 300 output tokens) costs roughly $0.004 on GPT-4o and $0.0003 on GPT-4o mini. Use our Token Calculator to estimate cost for your specific prompts and volumes.
What is the cheapest AI API?
As of mid-2026, Gemini 2.0 Flash ($0.10/1M input, $0.40/1M output) and GPT-4o mini ($0.15/1M input, $0.60/1M output) are the cheapest capable models available via API. For many tasks — classification, extraction, short-form summarization, FAQ answering — these models perform comparably to the premium tier at 25–50× lower cost. The right answer depends on your task: run a quality comparison on a sample of your production data before committing to the cheapest option.
Does prompt caching actually work?
Yes, and the savings are substantial when your architecture supports it. Anthropic's cache_control feature provides a genuine 90% discount on cached input tokens, and OpenAI's automatic prompt caching gives 50% off. The requirement is that the cached prefix must be byte-identical across requests — so it works best for static system prompts, fixed few-shot examples, and large retrieved context blocks that do not change between users. For conversational applications where every message history is unique, caching the system prompt prefix alone can still save 20–40% on total input costs depending on how large your system prompt is relative to user messages.
How do I track my OpenAI API usage?
OpenAI provides a usage dashboard at platform.openai.com/usage showing daily token consumption and spend broken down by model. For programmatic tracking, log the usage object returned in every API response — it contains prompt_tokens, completion_tokens, and total_tokens. You can also set hard monthly spending limits and soft alert thresholds in the billing section of your account settings. For team accounts, project-level usage tracking lets you attribute costs to specific features or services.
Can I set a spending limit on my OpenAI account?
Yes. In your OpenAI account under Settings → Billing → Usage limits, you can set a hard monthly spend limit — the API will return errors once you hit it, protecting you from runaway costs. You can also set a softer notification threshold that sends an email alert when spend reaches a specified amount. For Anthropic (Claude), the equivalent is found in the Console under Settings → Usage. Setting both a notification threshold at 80% of your budget and a hard cap at 100% is good practice for any production deployment.
Related reading
- Free AI Token Calculator — estimate cost for any prompt
- What Are AI Tokens? A Simple Explanation
- AI Token Calculator: Complete Guide
- How Many Tokens Per Word? (With Examples)
- ChatGPT vs Claude: Cost and Context Comparison
Want more GitHub stars for your open source project?
Star-Swap helps developers grow their GitHub repos through community collaboration.
Try Star-Swap Free →