DEV Community

Cover image for Part 4: Multi-Endpoint Arbitrage & Context Caching — Slashing LLM Bills Without Changing Models
zeeshan mushtaq lone
zeeshan mushtaq lone

Posted on Edited on AI-assisted

Part 4: Multi-Endpoint Arbitrage & Context Caching — Slashing LLM Bills Without Changing Models

Description: How to cut LLM API bills by 50–85% using prompt caching math, batch APIs, and multi-endpoint host routing—without downgrading model quality.

In Part 3, we looked at hybrid model routing and output token engineering—sending lightweight classification or extraction prompts to fast economy models while reserving frontier models for multi-step reasoning.

There is a second, often overlooked lever that cuts spend without changing a single model weight or rewriting your prompts: multi-endpoint arbitrage and context caching.

When teams calculate LLM spend, they usually look up the official first-party rate card, plug in their token volume, and treat that number as fixed. In practice, the exact same open-weight or frontier model can vary by 2x to 5x in effective monthly cost depending on:

  1. Which inference host or router serves the request
  2. How much of your prompt prefix hits KV-cache reads
  3. Whether non-interactive workloads run through asynchronous batch queues

Let's break down the math and architecture behind all three.


1. The Real Math Behind Prompt Caching

Most production LLM calls repeat the same prefix on every request:

  • System instructions and JSON schemas (800–2,500 tokens)
  • Few-shot examples (1,500–4,000 tokens)
  • Retrieved RAG context or codebase snapshots (5,000–30,000+ tokens)

Without caching, you pay the full Input / 1M rate on that shared prefix every single time. With prompt caching enabled, providers charge a small cache write premium once (or zero write premium on automatic prefix caches), and subsequent hits read from memory at 10% to 50% of the base input price.

The 30-Day Cache Break-Even Formula

Suppose a workload sends R requests per month, with T_prefix shared prefix tokens, T_dynamic uncached user tokens, and T_out output tokens. At cache hit rate h (between 0 and 1):

Monthly Cost = (R / 1,000,000) * [
  T_prefix * (h * P_cached + (1 - h) * P_write) +
  T_dynamic * P_in +
  T_out * P_out
]
Enter fullscreen mode Exit fullscreen mode

Look at what happens on a realistic RAG or coding-agent workload:Volume: 10,000 calls/day (300,000 calls/month)Tokens per call:

Cache Hit Rate (h) Monthly Input + Cache Spend Monthly Output Spend Total 30-Day Bill Savings vs. Uncached
0% (No Caching) $4,875.00 $1,500.00 $6,375.00 —
50% Hit Rate $2,850.00 $1,500.00 $4,350.00 -31.8%
85% Hit Rate $1,432.50 $1,500.00 $2,932.50 -54.0%
95% Hit Rate $1,027.50 $1,500.00 $2,527.50 -60.4%

Notice the key takeaway: output tokens stay constant at $1,500.00, so once your cache hit rate crosses ~85%, output generation—not prompt size—becomes your dominant expense.

Three Rules to Maximize Cache Hits in Code
Keep dynamic timestamps and user IDs out of the system prompt: Placing Current time: 2026-12-05T14:32:10Z at line 1 invalidates the entire prefix hash on every request. Put static instructions, tool definitions, and schemas first; append timestamps and user variables at the very end.

Stabilize tool-array ordering: If your agent dynamically sorts or filters JSON tool definitions per request, prefix hashes miss. Sort tool schemas deterministically by name.

Batch requests per tenant or repository: Grouping requests that share the same document or codebase context inside a 5-to-10-minute window keeps the warm KV cache alive before TTL eviction.

  1. Multi-Endpoint Arbitrage (Same Model, Different Bill) For open-weight models—and increasingly for multi-cloud frontier models—you are rarely locked into a single endpoint. Different inference hosts optimize for different hardware topologies (FP8 quantization vs. BF16, speculative decoding, regional GPU spot capacity), creating real spread across:

Per-1M-token input and output rates

Cached read support (some third-party hosts advertise low base input rates but offer no prompt caching, making them more expensive than first-party APIs on high-prefix workloads)

Throughput (tok/s) and Time-to-First-Token (TTFT)

Before switching endpoints, always compare effective workload cost rather than headline base rates:

interface EndpointQuote {
  host: string;
  inputPer1M: number;
  cachedInputPer1M: number | null;
  outputPer1M: number;
  batchDiscountPct?: number;
}

export function estimate30DayCost(
  quote: EndpointQuote,
  params: {
    dailyCalls: number;
    prefixTokens: number;
    dynamicInputTokens: number;
    outputTokens: number;
    cacheHitRate: number; // 0 to 1
    isBatch?: boolean;
  }
): number {
  const monthlyCalls = params.dailyCalls * 30;
  const cachedRate = quote.cachedInputPer1M ?? quote.inputPer1M;
  const batchMultiplier =
    params.isBatch && quote.batchDiscountPct
      ? 1 - quote.batchDiscountPct / 100
      : 1;

  const effectivePrefixRate =
    params.cacheHitRate * cachedRate +
    (1 - params.cacheHitRate) * quote.inputPer1M;

  const inputCost =
    (monthlyCalls *
      (params.prefixTokens * effectivePrefixRate +
        params.dynamicInputTokens * quote.inputPer1M)) /
    1_000_000;

  const outputCost =
    (monthlyCalls * params.outputTokens * quote.outputPer1M) / 1_000_000;

  return (inputCost + outputCost) * batchMultiplier;
}
Enter fullscreen mode Exit fullscreen mode

When you run that exact function across endpoints, a host with a slightly higher base input rate but a 90% cached-read discount frequently beats a bare-bones endpoint with no cache support. You can test these multi-endpoint spreads and 30-day cache curves interactively using the PromptBill 30-Day LLM API Spend Calculator or inspect per-host rate cards in the PromptBill Multi-Endpoint Pricing Table.

  1. Asynchronous Batch Queues for Offline Pipelines Not every LLM call needs a sub-second streaming response. Nightly evals, document indexing, catalog enrichment, embeddings backfills, and bulk classification pipelines can tolerate a 1-to-24-hour completion window.

Most major providers discount asynchronous batch endpoints by 40% to 50% across both input and output tokens:

Interactive synchronous call: Full input + full output rate, immediate response

Batch API queue: ~50% off input and output tokens, higher rate-limit ceilings, zero retry-storm throttling

A clean production pattern is separating your queue by SLA:

Real-time user-facing traffic (SLA < 3s): Routes to the fastest low-TTFT endpoint with warm prefix caching.

Background enrichment & eval jobs (SLA < 12h): Serializes prompts into JSONL chunks and submits to batch endpoints at half price.

  1. Putting the Cost Optimization Stack Together Across this four-part series, the compounding stack looks like this:

Measure baseline token ratios (Part 1): Separate input, cached prefix, and output volume instead of guessing from monthly invoice totals.

Constrain output verbosity (Part 2): Enforce concise JSON keys and strict max_tokens caps because output tokens cost 3x–5x more than input tokens.

Route by task complexity (Part 3): Send deterministic classification and extraction to sub-$0.20/1M economy models; escalate hard reasoning to frontier models.

Optimize endpoint & cache topology (Part 4): Pin static prefixes for >85% cache hits, compare multi-endpoint rate cards on effective cached cost, and move async jobs to batch queues.

If you want to benchmark specific model pairs before refactoring your router—such as comparing frontier models against high-efficiency open-weight endpoints—check out the PromptBill Head-to-Head Model Comparator and the Hybrid Routing Simulator.

Top comments (0)