Cost, Latency and Prompt Caching
Once a B2B project is live, "cost per job" and "processing time" are the first two numbers customers and managers ask about.
Core concepts
| Technique | Saves | When it fits |
|---|---|---|
| Tiered model routing | Cost | Small models for simple tasks (classification, extraction), large models for complex reasoning |
| Prompt caching | Cost + time to first token | Every request carries the same long prefix (system prompt, documents, tool definitions) |
| Context trimming | Cost + latency | Include only relevant chunks (RAG) instead of the whole document |
| Batch API | Cost (async, not real-time) | Overnight bulk processing that doesn't need an immediate response |
| Streaming output | Perceived latency | User-facing generation tasks |
How it works: prompt caching (Anthropic as the example)
- The prefix is what gets cached: a request forms a prefix in the order
tools → system → messages. Mark a content block withcache_controland everything before that point is cached. Later requests hit the cache only if their prefix is exactly the same. - Two ways to enable it: automatic caching with a top-level
cache_controlon the request, or manual breakpoints on specific content blocks. - TTL: 5 minutes by default (refreshed on each hit), 1 hour optional.
- Price multipliers (relative to the base input price): 5-minute cache write 1.25×, 1-hour cache write 2×, cache read 0.1×.
- Minimum length: below the model's minimum cacheable token count nothing is cached and no error is raised. Check
cache_creation_input_tokens/cache_read_input_tokensin the responseusageto confirm.
OpenAI also has prompt caching: it applies automatically to sufficiently long repeated prefixes with no manual marking (see the official docs for details).
Cost estimation formula
Cost per call = input tokens × input price + output tokens × output price
(with caching: input splits into "regular input + cache writes + cache reads", each priced separately)
Cost per task = Σ the LLM call cost of every step in the task (so log task_id on every call)In practice
python
# Put stable long content first with a cache marker, and changing content after it
resp = await client.messages.create(
model=MODEL,
max_tokens=1024,
system=[
{"type": "text", "text": AUDIT_RULES_LONG_TEXT, "cache_control": {"type": "ephemeral"}},
],
messages=[{"role": "user", "content": f"Review the following contract fields: {fields_json}"}],
)
u = resp.usage
log.info("cache", write=u.cache_creation_input_tokens, read=u.cache_read_input_tokens, input=u.input_tokens)Ordering rule when designing a prompt: what never changes (tool definitions, rules, examples) → what rarely changes (customer config) → what changes every time (the current document, the question).
Common pitfalls
- Timestamps or request IDs in the prefix: they differ every time, so the cache never hits.
- Thinking you're cached when you aren't: content below the minimum length is silently not cached. Always check the usage fields.
- Looking at unit price but not call count: one agent task may make a dozen calls. Cost only means something when measured per task.
- Routing to a small model with no evaluation safety net: compare accuracy on an eval set before switching to a smaller model.
Common interview questions
- How does prompt caching work? When does it stop working?
- How would you structure a prompt to maximize the cache hit rate?
- How do you measure and control the LLM cost of a task?
- What do you base tiered model routing on? How do you prove a smaller model isn't worse?
How it relates to my projects
- AI Document Review Pipeline: the long review-rule text is a good fit for caching; the
llm_calltable records usage, so cost can be aggregated per task on the tracing dashboard.
To be verified / may go stale
- The minimum cacheable length differs per model and can change, so this page gives no specific numbers. Use the table in the Anthropic docs.
- The price multipliers (1.25× / 2× / 0.1×) were checked against the official docs on 2026-11-24; pricing rules may change.
Sources
- Anthropic Docs: Prompt caching — automatic caching and breakpoints, 5-minute / 1-hour TTL, price multipliers, minimum length, usage fields
- Anthropic Docs: Batch processing
- OpenAI Docs: Prompt caching
- Anthropic Pricing · OpenAI Pricing
