LLMOps: model gateways, caching, limits, observability and costs in production
What happens to an LLM application after the demo – a gateway, prompt caching, retries and fallbacks, per-customer limits, token and cost tracking, prompt versioning and safe rollouts. With code in Python, TypeScript and C#.
An LLM application prototype is a single API call. Production is everything around it: what happens when the provider returns a 429 on a Friday evening, who pays for the customer who sent 40,000 messages, why answers suddenly slowed down and which feature eats half the budget. LLMOps is the set of practices and infrastructure that answers these questions – for models you don't train, but on which your product depends.
- 0.05×
- of the input price is what you pay for a cache read – Claude Opus 5.5 and Sonnet 5.5 (as of November 2026)
- 50%
- discount for batch mode – Message Batches API
- 2
- automatic retries – default in the official SDKs (429, 5xx, network errors)
The problem: Friday, 7:30 pm, the pierogi chat "catches fire"
A social media campaign worked too well. Within an hour, traffic on Halina's Pierogi chat grew tenfold and the trouble started:
- some requests get a 429 (rate limit exceeded), and the application shows customers a raw error,
- one "customer" – a competitor's bot – sends thousands of price questions and eats everyone's budget,
- response times grow, because every request sends 8,000 tokens of terms and menu all over again,
- on Monday accounting asks how much review summarization cost compared with the chat – and nobody can answer.
None of these problems is about model quality. All of them are about operations.
Architecture: a gateway between the application and the models
Instead of calling the model API from ten places in the code, all services talk to one layer: an LLM gateway. It can be a library in your code, a separate service or a ready-made product (e.g. LiteLLM Proxy, Portkey, Kong AI Gateway, Cloudflare AI Gateway, Azure API Management with model policies).
| Responsibility | Why | Example tools |
|---|---|---|
| Limits and quotas | protect the budget and the provider from a single customer | per-tenant token bucket in Redis, API Management policies |
| Routing | the right model for the task, easy model changes | gateway configuration, feature flags |
| Reliability | retries, timeouts, fallbacks, circuit breaker | SDK (built-in retries), Polly, resilience4j, the gateway |
| Caching | cost and latency | provider prompt caching, response cache, semantic cache |
| Observability | debugging, costs, quality | OpenTelemetry (gen_ai.* conventions), Langfuse, Helicone, Arize Phoenix |
| Security | PII, keys, policies | data masking, secrets in a vault, guardrails |
Caching on three levels
1. Prompt caching (at the provider). The model processes the prompt prefix and keeps its computed state. The next request with a byte-for-byte identical prefix pays the cache-read rate for those tokens – for Claude Opus 5.5 that is USD 0.20 instead of USD 4 per million. Writing to the cache costs 1.25× the input price (5-minute TTL) or 2× (1-hour TTL), so it pays off from the second request. The render order is tools → system → messages: stable things first, variable things last.
Prompt caching and verifying cache hits
import anthropic
client = anthropic.Anthropic()
# Stable prefix: identical bytes on every request (no dates, no user ids, no random order).
STABLE_SYSTEM = [
{"type": "text", "text": open("policies.md", encoding="utf-8").read()},
{"type": "text", "text": open("menu.md", encoding="utf-8").read(),
"cache_control": {"type": "ephemeral"}}, # explicit breakpoint at the end of the static part
]
def chat(history: list[dict], user_message: str) -> str:
history.append({"role": "user", "content": user_message})
r = client.messages.create(
model="claude-opus-5-5",
max_tokens=2048,
system=STABLE_SYSTEM,
cache_control={"type": "ephemeral"}, # automatic breakpoint for the growing conversation tail
messages=history,
)
history.append({"role": "assistant", "content": r.content})
u = r.usage
# Verify the cache actually works: reads should dominate after the first request.
print(f"cache_read={u.cache_read_input_tokens} cache_write={u.cache_creation_input_tokens} "
f"uncached={u.input_tokens} output={u.output_tokens}")
return "".join(b.text for b in r.content if b.type == "text")
history: list[dict] = []
chat(history, "Do you deliver to Mokotów?")
chat(history, "And how long does it take on Friday evening?")import Anthropic from "@anthropic-ai/sdk";
import { readFileSync } from "node:fs";
const client = new Anthropic();
// Stable prefix: identical bytes on every request (no dates, no user ids, no random order).
const STABLE_SYSTEM: Anthropic.TextBlockParam[] = [
{ type: "text", text: readFileSync("policies.md", "utf8") },
{ type: "text", text: readFileSync("menu.md", "utf8"), cache_control: { type: "ephemeral" } },
];
async function chat(history: Anthropic.MessageParam[], userMessage: string): Promise<string> {
history.push({ role: "user", content: userMessage });
const r = await client.messages.create({
model: "claude-opus-5-5",
max_tokens: 2048,
system: STABLE_SYSTEM,
cache_control: { type: "ephemeral" }, // automatic breakpoint for the growing conversation tail
messages: history,
});
history.push({ role: "assistant", content: r.content });
const u = r.usage;
// Verify the cache actually works: reads should dominate after the first request.
console.log(`cache_read=${u.cache_read_input_tokens} cache_write=${u.cache_creation_input_tokens} ` +
`uncached=${u.input_tokens} output=${u.output_tokens}`);
return r.content.map((b) => (b.type === "text" ? b.text : "")).join("");
}
const history: Anthropic.MessageParam[] = [];
await chat(history, "Do you deliver to Mokotów?");
await chat(history, "And how long does it take on Friday evening?");using Anthropic;
using Anthropic.Models.Messages;
AnthropicClient client = new();
// Stable prefix: identical bytes on every request (no dates, no user ids, no random order).
var stableSystem = new List<TextBlockParam>
{
new() { Text = File.ReadAllText("policies.md") },
new() { Text = File.ReadAllText("menu.md"), CacheControl = new CacheControlEphemeral() },
};
var r = await client.Messages.Create(new MessageCreateParams
{
Model = "claude-opus-5-5",
MaxTokens = 2048,
System = stableSystem,
Messages = [new UserMessageParam { Content = "Do you deliver to Mokotów?" }],
});
// Verify the cache actually works: reads should dominate after the first request.
var u = r.Usage;
Console.WriteLine($"cache_read={u.CacheReadInputTokens} cache_write={u.CacheCreationInputTokens} " +
$"uncached={u.InputTokens} output={u.OutputTokens}");The rest of this article is for members
Create a free account or sign in to read it all. Still ahead:
- Reliability: retries, timeouts and fallbacks
- Observability: what to log on every call
- Cost control: levers in order of payoff
- Rollouts: a prompt is code too
- Pre-production checklist
- Test yourself
- What's next