Scaling AI inference: how we route to the right model
Not all AI tasks are equal. Here's how we built a model routing layer that balances quality, speed, and cost based on your content type.
CDNZero runs multiple AI models for image generation, video generation, and content processing. Early on, we sent every request to the same model. It was simple and it worked — until it didn't. Here's how we built a routing layer that handles quality, speed, and cost trade-offs automatically.
The problem with a single model
Different generation tasks have very different requirements. A quick thumbnail preview needs to be fast and cheap — a 512px draft image that the user can approve or reject in seconds. A final export for a campaign asset needs to be high quality — worth waiting 10 seconds for a 2048px result.
Running both through the same large model means your preview requests are expensive and slow. Running both through the same small model means your exports look bad. You need routing.
The routing decision tree
Our routing layer makes four decisions for every request: which model family to use, which model size within that family, whether to use GPU or CPU inference, and which datacenter to route to based on current queue depths.
The input to these decisions comes from: the request type (preview vs export vs batch), the output parameters (resolution, duration, quality setting), the user's plan tier (free plans get smaller models by default), and real-time queue metrics from each inference worker pool.
Latency vs quality trade-offs
We maintain three tiers: Fast (small model, under 3 seconds, used for previews), Balanced (medium model, under 10 seconds, the default for standard exports), and Quality (large model, up to 30 seconds, available on Pro/Enterprise for high-stakes assets).
Users don't pick the tier directly in most flows — they pick a quality setting (draft, standard, high) and the router maps that to the appropriate tier based on their plan. This keeps the UX simple while letting us optimise infrastructure behind the scenes.
Queue management
Each model tier has its own worker pool with autoscaling. When a pool's queue depth exceeds a threshold, new requests are either held in a short queue or — if the wait would exceed a target latency — routed to the next tier up.
This means a Fast-tier request that would otherwise wait 8 seconds in a busy queue gets automatically promoted to a Balanced-tier worker instead. The cost is slightly higher, but the latency stays predictable. We absorb the cost difference on our end rather than passing unpredictable wait times to users.
Cost impact
After implementing routing, our average cost per image generation request dropped by 38%. Most of that saving came from correctly routing preview requests to smaller models rather than defaulting to the large model for everything.
The quality of final exports didn't change — those still go through the large model. But previews, draft iterations, and batch thumbnail jobs now run on infrastructure that's sized for the task.
What's next
We're experimenting with user-specific routing profiles: users who consistently approve their first generation get routed to larger models earlier in the flow (they're not iterating, so quality matters more than speed). Users who generate many drafts before approving get routed to faster, cheaper models for the iteration phase.
We're also evaluating speculative execution: generate a Fast-tier preview and a Balanced-tier result in parallel, then cancel whichever the user doesn't need. The cost of the cancelled generation is low; the latency improvement for users who would have waited for the Balanced result is significant.
More Engineering articles
← All articlesStart hosting files the simple way
Create a workspace, upload your first asset and copy its CDN URL. The free plan includes 3 GB of storage and 15 GB of transfer.
No credit card required · Cancel anytime