N
NexusDigitalLabs
All articles
LLM CostJun 2025·7 min read

How to Reduce OpenAI API Costs: A Practical Guide

Six concrete techniques to cut your GPT-4o and Claude API spend without sacrificing output quality — covering token compression, model routing, caching, and batching.

LLM API costs have a way of creeping up faster than expected. A prototype that costs a few dollars a month in development can easily reach hundreds of dollars once deployed to real users. The good news is that most production LLM systems are spending 30–60% more than they need to — not because the models are inefficient, but because of avoidable engineering choices upstream of the API call.

This guide covers six techniques you can apply immediately, ordered from lowest effort to highest. None of them require switching models or reducing the quality of your output.

1. Strip Unnecessary Tokens From Your Prompts

The single most underestimated source of API cost is token waste inside your prompt templates. Developers write prompts in readable editors where whitespace is invisible — but every trailing space, redundant blank line, Windows-style \r\n line ending, and repeated boilerplate instruction costs real tokens.

A systematic prompt normalization step applied before every API call typically reduces token count by 8–22% with zero change to model output. The normalization pipeline should, at minimum:

  • Strip trailing whitespace from every line (/[ \t]+$/gm)
  • Normalize line endings to Unix \n
  • Collapse consecutive blank lines to a single blank
  • Remove BOM characters from file-sourced content
  • Trim the final assembled prompt string

You can measure the impact of these changes with a token counter before and after. Our free Prompt Architect tool does this measurement live in your browser without sending your prompts to any server.

2. Route Requests to the Right Model

GPT-4o is priced significantly higher than GPT-4o mini ($5 vs $0.15 per million input tokens at time of writing). Claude Haiku costs a fraction of Claude Sonnet. For many tasks in a production system, the smaller model is not just "good enough" — it is actually faster and produces the same quality output because the task does not require the reasoning depth that justifies the cost premium.

A simple routing layer can classify requests by complexity before they reach the API:

  • Short classification tasks, intent detection, and single-field extraction → small model
  • Summarization of structured content, constrained generation → small model
  • Multi-step reasoning, code generation, nuanced analysis → large model
  • Fallback: if the small model returns a low-confidence result, retry with the large model

Teams that implement even a basic two-tier routing system report 40–70% reductions in monthly API spend with no measurable degradation in end-user output quality.

3. Cache Repeated Prompts and Responses

If your application sends the same or structurally similar prompts repeatedly — FAQ answering, document processing pipelines, code review on similar files — you are almost certainly paying for the same computation multiple times.

Semantic caching

A semantic cache stores the embedding of each prompt and its response. On each new request, it compares the incoming prompt embedding against stored entries. If the cosine similarity exceeds a threshold (typically 0.92–0.97 depending on your tolerance), it returns the cached response without calling the API. Libraries like GPTCache and LangChain's built-in caching layer implement this pattern.

Prefix caching

Both OpenAI and Anthropic have introduced prompt prefix caching at the API level. If the beginning of your prompt (system prompt, document context, static instructions) is identical across requests, the providers cache the KV state for that prefix and charge a discounted rate for cached tokens. Structuring your prompts so the stable content comes first and the variable content comes last maximises cache hit rate automatically.

4. Compress Your Context Window

In agentic and chat applications, the context window grows with each turn. By the tenth turn of a conversation, you may be paying to re-send the full history of every previous message. Two patterns address this:

  • Rolling summary: After every N turns, call a cheap model to produce a 3–5 sentence summary of the conversation so far. Replace the raw history with this summary. This keeps context informative but compact.
  • Selective retrieval: Instead of sending the full conversation, embed all turns and retrieve only the K most relevant turns for the current query using vector similarity. The current message gets the most relevant historical context, not all of it.

5. Batch Non-Real-Time Requests

OpenAI's Batch API offers a 50% discount on all supported models for requests that can tolerate a 24-hour turnaround. If your use case involves background processing — document ingestion, content moderation, scheduled analytics, dataset enrichment — moving those workloads to the Batch API cuts costs in half with no change to output quality.

Anthropic offers a similar Message Batches API at a 50% discount for Claude models. Any pipeline that does not require a real-time response is a candidate for batching.

6. Reduce Output Token Length

Most prompts ask the model to "explain" or "describe" without specifying output length. Models default to verbose responses, which costs output tokens. Output tokens are priced at 2–4× the rate of input tokens on most providers, making this a high-leverage area to optimise.

Specific instructions that reduce output token waste:

  • "Respond in 2–3 sentences only."
  • "Return a JSON object only. No explanation, no preamble."
  • "Use bullet points. Maximum 5 items."
  • "If the answer is not in the provided context, respond with exactly: NOT_FOUND"

In structured extraction tasks, specifying an exact JSON schema (rather than asking for a natural-language description) typically reduces output tokens by 30–50% while simultaneously making the output machine-parseable.

Putting It Together

None of these techniques require rebuilding your architecture. A prompt normalization step and explicit output length constraints can be implemented in an afternoon. Model routing and caching are medium-effort changes with the highest return on investment for high-volume systems.

Start by measuring your current token distribution: how many tokens are input vs. output, how much variance is there in prompt length across request types, and what percentage of requests are structurally similar. That measurement tells you which of the six techniques above will have the highest impact for your specific workload.

Free tool

Prompt Architect — Token Counter & Cost Estimator

Measure your prompt's token count, strip whitespace waste, and estimate API costs for GPT-4o, Claude, and Gemini — entirely in your browser.

Open Prompt Architect
Buy me a coffee?