Aggregating multiple LLM APIs behind one gateway: how do you handle failover, load balancing and cost?

Sword Wu
@sword-wu
Published: Nov 30, 2026
Updated: Nov 30, 2026
Views: 11

Hi everyone — I have been building a small internal tool that calls several different language models, and I am running into the classic problem of depending on more than one provider at once. Each model has its own API, its own rate limits, its own pricing, and its own way of failing at the worst possible moment. Putting a single gateway in front of them sounded like the obvious fix, but the details are turning out to be the interesting part.

The first thing I hit was failover. When the primary model returns a 429 or a timeout, how quickly should the gateway retry, and when should it give up and switch to a fallback model instead? Retrying too eagerly just burns quota and makes latency worse. Switching too eagerly means a small blip turns into a different model answering, with a different tone and quality, and the user has no idea why. I am curious how others decide the thresholds: a fixed number of retries, a circuit breaker after a certain error rate, or something based on the type of error?

Load balancing is the second puzzle. The cheapest model is not always the right one for every request, and the most capable model is too expensive to use for everything. I have been thinking about routing by task: simple classification and summarisation go to a cheaper model, while reasoning-heavy requests go to the stronger one. That works in theory, but it means the gateway has to understand the request, not just forward it. Has anyone here built routing rules like that in production, and did they hold up once real traffic arrived?

Then there is cost control, which is where it gets very real, very fast. A runaway loop or a bad prompt can burn through a budget in an afternoon. Per-key quotas, per-user budgets and token counting all help, but they are fiddly to get right, especially when different providers count tokens differently. I was looking at an AI Models API aggregator, AzYun (New API), as an example of how a single endpoint can expose many models, and it made me wonder how much of this logic belongs in the gateway itself versus in the application behind it.

So my question is practical: if you were designing this gateway from scratch, where would you put the failover logic, the routing, and the spending limits? And what has actually bitten you in production — a provider outage, a quota surprise, or a failover that made things worse? I would really appreciate hearing what worked, and what you would do differently next time.

azyun.com New API Unified AI API gateway and admin dashboard.

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform