Cost-Optimized, Robust LLMs in Production
Fallbacks, budgets, local models and safe agents. Here are the twelve checks from the talk, the slides, a script you can run, and every source behind the numbers on the slides.
Slides and code
The line every talk starts from: client.models.generate_content(model="gemini-2.5-pro", contents=prompt). It quietly decides who answers, what the call may cost, where it runs and what it may touch. The practices below take those decisions out of the code and put them in one place.
-
Slides (PDF)llms-in-production/slides.pdfAll 19 slides: the 16 from the talk and three appendix slides with code (practices 10–12).
-
The 402 demo, as a scriptllms-in-production/chain.pyA fallback chain over two local mock vendors:
python3 chain.py naivegives up on 402,python3 chain.py ownersmoves on. No network, no keys. -
Keystone (KTMS)https://github.com/ai-process/ktmsThe open-source gateway where we do most of these practices: Apache 2.0, one Go binary. A star helps a small project.
Twelve checks for every LLM call site
Pick one service, find every call to a model, and go down the list. Practices 1–9 were on stage; 10–12 are in the slides’ appendix.
-
1 · Say what the call needs, not which modelThe code sends a topic and a level (medical: basic); one place picks the closest model that meets it, then the cheapest. Check:
grep -rn 'gemini-\|gpt-\|claude-' src/finds nothing outside one config file. -
2 · Prove the next model on shadow trafficA test set first (a few hundred real prompts from your logs, with the answers you expect), then a share of live calls mirrored to the candidate with a judge, then a canary keyed by user ID. Check: no switch without a week of judge results: primary, shadow, tie.
-
3 · Log the model that answeredThe model you ask for is not always the one that answers, and you pay for the one that does. Check:
GROUP BY requested, answered; if they never differ, you log the wrong field. -
4 · Thinking tokens are output tokensSet the thinking level per need, the lowest that still passes your test set. The output limit counts the thinking too: set it too low, and you pay for thinking with an empty answer. Check: thinking tokens as a share of output, per need.
-
5 · Cache the stable prefix, batch what can waitNo timestamp or request ID in the first line of the prompt; anything that can wait a day goes to a batch API. Check: cached tokens as a share of input tokens, per service.
-
6 · 402 means switch vendors, not give up400, 413, 422: return it. 401, 402, 403: next vendor and an alert. 429, 5xx, timeout: retry, then the next vendor. Check: a fake vendor that answers 402 gives an answer from the next vendor and exactly one alert.
-
7 · Put the RAM you already bought to workLaptops join a pool with the owner’s schedule, a pause and a usage log; the cloud takes over when no laptop is free. Check: close a laptop’s lid during a load test: errors stay at zero, the cloud’s share goes up.
-
8 · Restricted data never falls back to the cloudThe call says what data it carries; restricted calls run only on machines you own, or fail. Check: stop the local models, make a restricted call, expect an error, in CI.
-
9 · Break one leg of the lethal trifectaPrivate data, untrusted content, a way to talk to the outside: no agent gets all three. Check: one row per agent, three columns for the legs, no row with three ticks.
-
10 · Retry with a budget, then move onOne deadline per call; a
Retry-Afterlonger than the time left means the next vendor; request errors don’t open the breaker. Check: breaker state per vendor on a dashboard. -
11 · A key per service, who and why on every callOne usage event per call: service, team, user, action, needs, the model that answered, four kinds of tokens. Check: name the feature behind yesterday’s most expensive hour in under five minutes.
-
12 · Limits that say no before the invoice doesMoney per key, output tokens per call, steps and tool calls per agent, and a written fail-open or fail-closed decision. Check: a one-euro cap in staging returns the right error.
Sources
Every number on the slides, with the page it came from. Checked between November 12 and December 1, 2026.
-
Models change under youGemini API: deprecations OpenAI: deprecations and notice periods Anthropic: model deprecations and API versions Gemini API: modelVersion in the response
-
The billGemini: thinking, pricing and token limits Gemini: implicit caching Gemini: Batch API FinOps Foundation: State of FinOps 2026 OWASP LLM10:2025 Unbounded Consumption
-
Local models“RAM Altman”, November 2026 TrendForce: DRAM contract prices, Q1 2026 Gemma 4: memory at 4-bit llama.cpp: Apple Silicon benchmark Harry Potter word counts OpenAI: tokens and words
-
Failures and agentsGoogle Cloud: Service Control incident, June 2025 Anthropic: a postmortem of three recent issues Simon Willison: the lethal trifecta Invariant Labs: GitHub MCP toxic agent flow
-
Gateways comparedOur benchmark, October 20, 2026 LiteLLM: auto routing LiteLLM: judging the auto router on shadow traffic LiteLLM: traffic mirroring LiteLLM: tag routing LiteLLM: MCP permissions Portkey: fallbacks Kong AI Gateway: AI Proxy Advanced, failover criteria Bifrost: retries and fallbacks Apache APISIX: ai-proxy-multi Envoy AI Gateway: inference pools
AI disclosure. Idea: human. Text: human. Spelling and timing check: AI*. Graphics and slides: AI*.
* Antigravity + Claude, talking to each other through the KTMS inbox service.