AI engineering path · Models in your system · Topic 8 of 12

Cost and latency

Not started

With hosted models you pay per token, separately for input and output, and the model you pick sets the rate. Latency follows the same drivers: how much the model must read, how much it must write, and how big it is. That makes both predictable. Log the token counts and timing of every call, and you can estimate what a feature costs per user and where the seconds go.

Why it matters: An LLM feature that works in a demo can be too slow or too expensive at real traffic. Both cost and latency are driven by tokens and model choice, so you can measure them per request and design them down.

  1. 1Measure per request

    Log input tokens, output tokens, cache hits, model and latency for every call. You cannot optimise what you do not record.

  2. 2Output is the slow part

    Input is processed in parallel; output is produced one token at a time. Shorter answers cut latency the most.

  3. 3Reuse the stable prefix

    Prompt caching makes repeated leading content (system prompt, tools, documents) cheaper and faster, if it stays byte-for-byte the same at the start.

  4. 4Right-size each step

    Use smaller, faster models for easy steps, batch work that can wait, and keep the large model for what needs it.

1

How it works

The explanation, with examples you can run.

Where the time goes

A response has two phases. Processing the input (often called prefill) happens largely in parallel and shows up as time to first token. Generating the output (decode) is sequential: one token after another. For most chat-style requests the output dominates, so the strongest levers are:

  • ask for shorter answers and set an output token limit that fits the task,
  • stream, so users read while the rest is generated,
  • run independent calls in parallel rather than one after another,
  • use a smaller model where quality allows.

Token budgets

Decide a budget per feature: how many input tokens (system prompt, history, retrieved passages) and output tokens a request may use. Enforce it in code: trim or summarise old conversation turns, cap the number of retrieved chunks, and cap tool rounds in agent loops. Also limit per user and per tenant, since one script calling your endpoint in a loop can run up a large bill. Providers enforce their own rate limits in requests and tokens per minute; handle 429 responses with backoff.

Prompt caching

When many requests start with the same content, such as a long system prompt, tool definitions or a reference document, providers can reuse the work done on that prefix. Cached input tokens are billed at a reduced rate and processed faster. The details differ: some providers cache automatically, others ask you to mark cache breakpoints; some charge a little extra for the request that writes the cache; entries expire after a period of disuse (at the time of writing, defaults range from a few minutes to about half an hour, with longer options on some APIs); and there is a minimum prompt length below which nothing is cached. Check your provider's page, and confirm hits in the usage numbers each response reports. What is common is the rule that the cache matches a prefix: put stable content first, and keep it identical. A timestamp or user name near the top of the system prompt defeats it.

Separately, you can cache whole answers in your own application (keyed by a hash of model, prompt and settings) when identical requests are common and the answer may be reused.

Batching and model routing

Work that nobody is waiting for (nightly summaries, backfills, evaluation runs) can go through a provider's batch interface: you submit many requests and collect results later, typically at a discount and with a completion window measured in hours. Without one, use a queue with a fixed number of workers so you stay within rate limits.

Not every step needs the largest model. Classification, routing, extraction from short text and simple rewrites often work well on smaller, faster, cheaper models; keep the large model for hard reasoning or long, nuanced writing. Decide with your evaluation set, not by assumption: run each candidate model on it and compare quality, cost and latency together.

The calculator uses made-up relative prices (units per thousand tokens), not real ones. Change the numbers to your provider's current rates to see what caching and routing do to a feature's cost.

⌘/Ctrl + Enter
OutputNot run yet

Press Run to see what your code prints, in order.

Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.

Watch the totals, not just the averages

Track cost and latency per feature and per tenant, with percentiles (p50, p95, p99), not only averages: long answers, large documents and agent loops create a heavy tail. Alert on sudden changes, which often mean a prompt change made answers longer or a loop stopped terminating.

2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.You want prompt caching to work well. How should you order the prompt?
  2. 2.A summarisation job over 200,000 documents must finish by tomorrow morning. Users are not waiting on it. What fits best?
  3. 3.Which change usually reduces time-to-complete-answer the most for a chat reply?

0 of 3 right so far. Get all three to finish this topic.