Skip to content
Raisalon

Artificial Intelligence

How to cut LLM API costs with prompt caching, batching and routing

Prompt caching pays off from the second request, batch APIs halve the price of non-urgent work, and routing sends easy requests to cheaper models. See the numbers.

Author
4 min read

An LLM API bill is input tokens times the input price plus output tokens times the output price, summed over every request. Switching to a cheaper model is the obvious lever, but three others often save more without touching quality: caching the parts of the prompt that repeat, batching work that does not need an immediate answer, and routing each request to the smallest model that handles it well. How much 1,000 LLM chat requests cost shows how a bill builds up; this article is about cutting it.

Prompt caching: pay once for the part that repeats

Most production prompts start with the same block every time: system instructions, tool definitions, examples, and reference documents. Prompt caching lets the provider reuse the processed form of that prefix, and bills the reused tokens at a fraction of the normal input price.

Anthropic caching is opt-in with a cache_control field, either once at the top level of the request or on individual content blocks. Writing to the default 5-minute cache costs 1.25 times the base input price, writing to the optional 1-hour cache costs 2 times, and reading from the cache costs 0.1 times, with lower read prices on some newer models. Each use refreshes the cache lifetime at no extra cost. Anthropic prompt caching documentation

{
  "model": "claude-...",
  "max_tokens": 1024,
  "cache_control": { "type": "ephemeral" },
  "system": "Long, stable instructions and reference material...",
  "messages": [{ "role": "user", "content": "Today's question" }]
}

OpenAI enables prompt caching by default for supported models, so the work is in prompt structure rather than code. The minimum cacheable prompt is 1,024 tokens for GPT-5.6 and later, and cache usage is reported in usage.input_tokens_details.cached_tokens. OpenAI prompt caching guide

Both providers cache a prefix only when it is long enough. Anthropic’s minimum ranges from 512 to 4,096 tokens depending on the model, and a shorter prompt marked for caching is simply not cached, without an error. Check the usage fields in responses rather than assuming the cache works.

When does caching start to pay?

With Anthropic’s multipliers, the first request costs more than an uncached one because of the 1.25× write. Every later request inside the cache lifetime pays 0.1× for the prefix. This function compares the two for a 20,000-token prefix at an illustrative price of $3 per million input tokens; the percentages do not depend on the price.

def cost_without_cache(prefix_tokens, requests, price_per_mtok):
    return prefix_tokens * requests * price_per_mtok / 1_000_000


def cost_with_cache(prefix_tokens, requests, price_per_mtok, write=1.25, read=0.10):
    """One cache write, then cache reads for every later request inside the TTL."""
    if requests == 0:
        return 0.0
    multiplier = write + read * (requests - 1)
    return prefix_tokens * multiplier * price_per_mtok / 1_000_000


PREFIX = 20_000  # tokens of instructions and reference documents
PRICE = 3.00     # illustrative base input price, USD per million tokens

for n in (1, 2, 5, 20):
    plain = cost_without_cache(PREFIX, n, PRICE)
    cached = cost_with_cache(PREFIX, n, PRICE)
    print(f"{n:>2} requests: ${plain:.4f} vs ${cached:.4f} ({1 - cached / plain:+.1%} saved)")
 1 requests: $0.0600 vs $0.0750 (-25.0% saved)
 2 requests: $0.1200 vs $0.0810 (+32.5% saved)
 5 requests: $0.3000 vs $0.0990 (+67.0% saved)
20 requests: $1.2000 vs $0.1890 (+84.2% saved)

Caching loses money on a prefix used once and wins from the second use within the cache lifetime. A chatbot or agent that resends the same instructions on every turn crosses that line almost immediately.

Structure prompts so the cache can hit

A cache matches a prefix, so a single changed token near the start invalidates everything after it. OpenAI’s guidance is to put stable instructions and shared reference material first, and to move timestamps, user-specific content and other dynamic content to the end or into later messages. OpenAI prompt caching guide

  • Put fixed instructions, tool definitions and examples first, in a fixed order.
  • Keep the current date, user name and request ID out of the system prompt.
  • Append new conversation turns instead of rewriting earlier ones.

Batch APIs: half price for work that can wait

Nightly classification, document enrichment, evaluation runs and embedding backfills do not need an answer in two seconds. Both providers sell that patience at a discount. OpenAI’s Batch API costs 50% less than its synchronous APIs, and each batch completes within 24 hours. OpenAI Batch API guide Anthropic’s Message Batches API charges 50% of standard prices, results arrive when every request is done or after 24 hours, and most batches finish in under an hour. Anthropic batch processing documentation Anthropic also states that the batch discount stacks with prompt caching.

A useful rule: if a human is not waiting on the response, it belongs in a batch.

Advertisement

Model routing: the smallest model that passes

Many requests are easy: short classifications, extraction from clean text, routine rewrites. Sending them to a flagship model pays for capability that is not used. Routing sends each request to the cheapest model that handles that kind of request well, and escalates only when needed.

  1. Build a test set per request type with known good outputs.
  2. Run each candidate model on it and record quality, cost and latency.
  3. Route by request type, using the cheapest model that meets your quality bar.
  4. Escalate on failure: if the small model’s output fails validation, retry with a larger one.

Routing without step 1 is guessing. The measurement is what makes the saving safe, and it uses the same discipline as testing prompt regressions in CI.

Smaller wins that add up

  • Cap output length with the maximum-tokens setting. Output tokens usually cost more than input tokens.
  • Trim conversation history. Summarize or drop old turns instead of resending the whole transcript.
  • Retrieve fewer, better chunks in RAG. Every retrieved chunk is input on every request; RAG vs. fine-tuning shows how fast that grows.
  • Log tokens per request so you can see which feature drives the bill.

Next steps

Pull a week of usage logs and sort features by total tokens. Move anything that is not user-facing into a batch, restructure the biggest prompts so their stable part comes first, and measure the cached-token fields to confirm the cache is hitting before you count the savings.

Share

Written by

Advertisement

Advertisement