One gateway for every model call
Keep provider keys, routes, limits and usage receipts in one gateway: pinned models never fall back silently, and calls retry only before output is committed.
Keep provider keys, routes, limits and usage receipts in one gateway: pinned models never fall back silently, and calls retry only before output is committed.
Make the model a setting, not a dependency: a thin interface, evals per task and a tested fallback. Route across models only where cost or residency pays.
Prompt caching reuses the processed start of a repeated prompt, so it bills at a tenth or less of the input price. How Claude, OpenAI and Gemini differ.
A recurring report. A slow review. A prototype that needs to work in the real world.
Provider credentials, routing rules, concurrency caps and usage receipts belong in one place: a model gateway sitting between your application and every model provider. In the Company Twin, that gateway is the only part of the stack that holds a provider key. The executor running agents holds none, and neither does anything else. Each caller can use only the routes its class allows, a pinned model is the model that answers, and every call ends with a receipt.
Moving roughly fifteen scattered callers behind one service took less time than we expected and added about 2 ms before the first token. The hard parts were streaming retries, proving that a provider actually finished generating, and fitting our own self-hosted GPUs behind the exact same contract.
Key takeaways
When we audited our codebase, product code called model providers from roughly fifteen places: the executor's agent turns and tools, the tenant API's routers and background jobs, the build worker through a bridge, the runtime for our self-hosted models, and the app's dictation route. Each held its own credentials, ran its own retry and timeout logic, and logged usage, or didn't. Rotating a key in one place broke another; fixing a retry bug in one caller left the rest broken.
Putting every caller behind one gateway turned the security question from "which of fifteen processes could leak a key?" into "one role that nothing else can read". It also gave us one place to enforce policy and one stream of receipts. We migrated callers one family at a time, stripping each caller's credentials in the same commit.
| Caller class | Allowed routes |
|---|---|
| Executor | Interactive agent routes, the executor's tool routes and production self-hosted models |
| Tenant API | Routers, titles, organisation style, provenance, proposers and transcription |
| Build worker | The authoring route only |
| Evaluation | Maintenance and evaluation pools, and pinned evaluator routes |
Each class authenticates with its own credential, and any caller can ask the gateway which routes it's allowed to use.
upstream) or from the gateway itself, plus the failure kind: unreachable, interrupted mid-stream, timed out, unavailable, rate-limited, refused credential, or a request the provider rejected. Only the first five are worth trying again. The gateway itself retries only before any output reaches the caller (below); a stream cut off after that ends as a failure. The tenant can then rerun the whole turn as a new attempt, discarding the partial answer, only for a retryable provider error, and never once the attempt has run a tool with side effects.The rule we guard hardest is zero silent fallback. If a pinned model is down, the call fails and explains why. A service that quietly answers with a different model has made a decision nobody signed off on, and every evaluation you ran against the pinned model is now measuring something else. That's the argument in our guide to multi-model AI without lock-in: make the model a setting, then make sure the setting is what actually runs.
Streaming makes retries dangerous. Once the opening words of a reply, or the start of a tool call, have reached the caller, retrying would stitch a second, different answer onto the first. So the gateway draws one hard line: the moment output is committed.
Before that line, a transport error can be retried a fixed number of times under the same request identity, with usage counted once. Where a route has a second credential path to the same model, switching is permitted only here, and the losing attempt is aborted before the replacement starts. After the line, nothing gets retried or switched; a failure comes back as a failure, with its origin attached.
Every admitted call carries a gateway request ID (generated by the caller and reused on its own retries) and produces a receipt: route, provider, model, attempts, timings and provider-reported usage. Calls belonging to a run also record the tenant, organisation, run, attempt and call role, tying every receipt back to the turn that triggered it. Receipts never hold prompt or response text.
Usage uses one schema across every route, regardless of provider. Input tokens exclude prompt-cache reads and writes (counted separately), output tokens include reasoning, and a separate context field captures everything the model read. Keeping those apart matters: with caching on, uncached input is often a tiny fraction of what the model actually saw, and summing the wrong columns throws usage figures off by multiples.
A resilience audit in October showed the gateway didn't yet guarantee one accurate receipt per call, and fixing it changed how we treat usage. A provider can return HTTP 200 and close the stream cleanly without the model having finished, so a clean EOF no longer counts as completion. Early counts in a stream's opening event aren't final either. Usage now counts as final only after the provider's terminal event; anything else is flagged uncertain and reconciled later instead of billing on stale numbers. When a response is too large for the gateway to scan to the end, the receipt explicitly marks usage as truncated rather than passing off a partial count as complete.
We serve open-weight models on GPUs we rent and control under Project Venus, for reasons covered in why we serve our own open-weight models. To a caller, a Venus route looks identical to any other route: resolved, pinned, capped and receipted by the gateway. Four rules keep it clean.
The gateway also carries a residency switch. Turned on, a server admits only in-region inference in London through Amazon Bedrock, answering 403 on every other model route. Proving residency at the single choke point every call passes through is far simpler than trusting fifteen callers to remember it.
Less than we worried. We benchmarked a fake provider streamed directly (as callers used to) against streaming through the gateway over loopback.
| Case | Direct, p50 (p95) | Through the gateway, p50 (p95) |
|---|---|---|
| First text, 64 chunks sent at once, 200 calls | 0.85 ms (2.39) | 2.82 ms (6.75) |
| Whole call, same | 1.40 ms (3.52) | 2.89 ms (7.46) |
| First text, 32 chunks 5 ms apart, 40 calls | 1.31 ms (2.59) | 2.91 ms (5.80) |
| Whole call, same | 178.2 ms (184.4) | 175.7 ms (181.0) |
| 16 concurrent calls, 32 chunks 5 ms apart | 173.1 ms | 185.3 ms |
Apple silicon, Node 25, September 2026. Real provider latency sits in the hundreds of milliseconds to seconds and dwarfs these numbers.
The gateway adds roughly 2 ms before the first text and relays chunks as they arrive, so a single paced stream takes no longer end to end. At sixteen paced streams in parallel, a round took about 12 ms longer at the median (roughly 7%).
One piece remains on our list: replacing the executor's class credential with per-run grants signed by the tenant API, binding every model call to a specific claimed run rather than to the executor as a whole.
Most teams we meet have model keys tucked into half a dozen services, a fallback someone hacked in during an outage, and usage reconstructed from monthly invoices. A gateway is usually the highest-leverage piece of AI infrastructure to build first, because everything after it (evaluation, cost control, residency, swapping models) depends on knowing for certain which model answered which call.
Putting a gateway in front of your models
For the governance case behind keeping the model layer separate, see LLM-agnostic by design.
Every model call in the Company Twin passes through this gateway, hosted or self-hosted, with a receipt naming the exact model that answered. See how the Twin routes models →