A model that answers in 300ms, when the payment needed an answer in 200, didn't answer. The customer saw a spinner, the checkout timed out, and the clever model contributed nothing.
Real-time decisions have deadlines: approve or hold a payment, flag a transaction, match two people. I've deployed an ML scoring engine behind an API and lived with its tail latency, and the lesson was always the same. Treat the model as a dependency that will sometimes be slow and sometimes be down, and design so every request still gets a decision inside the budget.
Here's how, with the code in Python and FastAPI.
Start with a budget, not a model
Before writing any code, split the deadline between the hops. A 200ms p99 for a scoring call might look like this:
Two rules come out of this table. Every hop gets a hard timeout smaller than its share, and no hop gets "whatever time is left". The headroom is for garbage collection, a slow network moment, the things you can't plan for. If you spend it on purpose, you'll miss the deadline by accident.
Keep the model warm and close
Most of the latency in a model call isn't the model. It's everything around it.
- One long-lived HTTP client with a connection pool. A new TLS handshake per request can eat a third of your budget on its own.
- The model runs as its own service, loaded once at startup, in the same region and ideally the same zone as the API calling it.
- No cold starts. Keep a minimum number of instances warm. A scale-to-zero model server is fine for batch jobs and a disaster for a payment path.
- Precompute features. If scoring needs "transactions in the last 10 minutes" and "average amount over 30 days", don't compute them in the request. Keep rolling counters in Redis, updated as events arrive, and read them in one round trip.
- Don't batch on the real-time path. Batching helps throughput, and every millisecond a request waits for its batch comes out of its latency budget.
Hard timeouts
The model call gets its share and not a millisecond more. If it runs out, the request falls back. It never waits.
Two layers of timeout are on purpose. httpx bounds each phase of the request, and asyncio.timeout bounds the whole call, so a response that trickles in slowly can't sneak past either.
A circuit breaker
When the model service is down, every request paying 120ms to discover it again is wasted budget. A circuit breaker remembers: after enough failures it stops calling the model for a while and lets the rules decide straight away.
This one is deliberately small. A production breaker would let a single probe through after the cooldown, instead of the whole traffic stream at once, but the idea is the same: stop paying for a dependency you already know is down.
Rules that keep deciding
The fallback is the part people get wrong. "Allow everything when the model is down" is an open door, and "block everything" is an outage you caused yourself. The fallback has to be a real policy: simpler than the model, explainable, and conservative.
Notice what the fallback can't do: it can't block. When the model is down, risky transactions go to a person for review rather than being declined by a blunter instrument.
Some checks don't belong to the model at all. A sanctions or blocklist hit is a hard rule: it runs first, on every request, and it decides whether the model is up or not. You never want "the model was down" to be the reason a sanctioned account got through.
The endpoint
Record who decided
Every decision says how it was made: decided_by, model_version, the reasons, and the latency. Store it with the transaction. That serves two masters.
- Audit. In anything regulated, you'll be asked why a transaction was held. "The model said 0.83, version 14, top features X and Y" is an answer. "It was flagged" isn't.
- Operations. It's the raw material for the one metric that matters most here.
The fallback rate is your health metric
Latency percentiles tell you how fast you answered. The share of decisions made by rules tells you how well you answered. Watch it like an error rate:
- Alert when the fallback rate goes over a few percent for five minutes. It means the model is slow or down and you're running on the simpler policy.
- Track p99 per hop, not just end to end, so a slow feature store doesn't hide inside "the model is slow".
- Count model timeouts and breaker openings separately. They point at different problems.
Before you ship it
- Load test the whole path, model service included, at realistic concurrency. The p99 under load is the only number that matters.
- Test the fallback on purpose. Inject latency between the API and the model service and watch decisions keep flowing, with
decided_byflipping torules. A fallback you've never seen run doesn't work. - Run new models in shadow first. Score with the candidate in a background task with its own timeout, decide with the current model, and compare the two before switching. The shadow call must never sit on the decision path.
None of this is clever. It's a budget, a timeout at every hop, a breaker, rules worth falling back to, and a metric that tells you when you're flying on them. That's what turns a model into something you can put in front of real money.