Skip to content

Part 6 · Application-Layer Efficiency

Parts 3 through 5 assumed you own the model: you set the batch size, you size the KV cache, you quantize the weights. Most teams shipping features don’t. They call an API — Anthropic’s Messages API, OpenAI, or a hosted endpoint — and a meter runs on every token in and every token out. You can’t touch the kernels, the scheduler, or the precision. So this part asks a different question: when the model is a black box behind a per-token bill, what does each call cost in latency, context budget, and dollars — and which levers, all of them outside the model, make it cheaper?

When you own the model, cost is amortized hardware. When you call an API, cost is brutally direct and per-request:

cost_per_call ≈ (input_tokens × input_price) + (output_tokens × output_price)

Two facts dominate everything in this part. First, output tokens cost several times more than input tokens — commonly around 5× on the Claude model lineup. Second, every token in your prompt is paid on every single call: a system prompt, your few-shot examples, the retrieved document you stuffed in — all of it re-billed each time, forever. A 2,000-token preamble across a million calls is two billion input tokens you chose to pay for.

“Memory” in this part isn’t HBM — that’s the provider’s problem. Your memory budget is the context window: a finite token allowance per request that you fill, and every token you spend filling it adds prefill latency and dollars (and, as we’ll see, can hurt quality).

You can’t change the model. You can change what you send it, how often you send it, which model you send it to, and what shape you demand back.

your request ─┐
├─ Prompt economy ── send fewer, denser tokens
├─ Caching ── stop re-paying for repeated prefixes
├─ RAG ── retrieve a little, not stuff a lot
├─ Routing ── send easy work to the cheap model
└─ Structured out ── force valid output, kill retries
└──▶ provider (black box)
LeverWhat it cutsPaid in
Prompt & context economyinput tokens on every calla little prompt-engineering effort
Cachingre-billing of stable prefixescache-write premium + staleness risk
RAG efficiencytokens stuffed per queryretrieval infra + latency budget
Model routingdollars on easy requestsmis-routing risk → eval cost
Structured outputretries + over-long repliesschema design, slight rigidity

Why this is where the money actually leaks

Section titled “Why this is where the money actually leaks”

Owning-the-model efficiency is a one-time engineering investment. Application-layer waste is recurring and compounds with scale. A fat prompt doesn’t cost more to write — it costs more on call one million. The encouraging flip side: these levers need no GPUs, no retraining, and no infra migration. They are mostly prompt edits, a cache header, a retriever, and a routing if-statement. The cheapest efficiency win you will ever ship lives in this part.

  1. Prompt & context economy — you pay per input and output token; context isn’t free; trim, compress, cap.
  2. Caching — prefix/prompt caching, semantic caching, and exact response caching; when each applies.
  3. RAG efficiency — retrieve a little well instead of stuffing a lot; chunk size, top-k, reranking.
  4. Model routing & cascades — not every request needs the biggest model; cheap-first, escalate on need.
  5. Structured output & constrained decoding — valid JSON the first time means no retries, and retries cost tokens.
  1. What fundamentally changes about cost when you call a model API instead of owning the model?
  2. In the per-call cost formula, which tends to be more expensive — input or output tokens — and roughly by how much on the Claude lineup?
  3. Why is a long system prompt a recurring cost rather than a one-time one?
  4. In this part, what plays the role that “memory” played when you owned the model?
  5. Name the five application-layer levers and give a one-line reason each one saves money.
Show answers
  1. Cost becomes direct and per-request — you pay a metered amount for every input and output token on every call — instead of being amortized fixed hardware you control.
  2. Output tokens are more expensive, commonly around 5× the input-token price on the Claude models. (Always check current pricing.)
  3. Because every token in the prompt is re-sent and re-billed on every call; a fixed preamble multiplied by N calls is a cost that grows with traffic, not a one-time charge.
  4. The context window — a finite per-request token budget you fill, where every token spent adds prefill latency and dollars (and can hurt quality).
  5. Prompt economy (send fewer/denser tokens), caching (stop re-paying for stable prefixes), RAG (retrieve a little instead of stuffing a lot), routing (send easy work to the cheap model), structured output (force valid output so you don’t pay for retries).