Multi-Provider Token Marketplace Gateway: Routing, Circuit Breaking, and Retry
A multi-provider Token aggregation gateway lives or dies by its routing layer — dynamically distributing requests by price, latency, and availability, with automatic failover. This article covers the layered gateway architecture, routing algorithm selection, circuit breaker engineering, and idempotent retry handling.
The Bottom Line: A Multi-Provider Token Gateway Is Not About “Calling Multiple APIs” — It Is About Real-Time Routing Decisions
The AI Token platform architecture article covered the overall architecture. This article focuses on the routing and resilience layer — the most critical and most underestimated part of a multi-provider gateway.
1. Layered Gateway Architecture
A production-grade multi-provider Token gateway has three layers:
Client → Access Layer → Routing Layer → Provider Adapter Layer → AI Provider APIs
Access Layer: Unified entry point for authentication, rate limiting, and billing logs. Exposes a single API to all clients, hiding backend provider differences.
Routing Layer: The core. Each request is evaluated against real-time provider metrics: price, latency (p50/p95/p99), availability, quota remaining, and request constraints (model type, priority).
Provider Adapter Layer: Converts the internal request format to each provider’s API format, handling authentication differences, response format variations, and error code mapping. Adding a new provider means adding one adapter.
2. Routing Algorithm Selection
Algorithm 1: Price-Weighted (Recommended)
Weight_i = BasePrice / CurrentPrice_i
SelectionProbability_i = Weight_i / Sum(AllWeights)
Cheaper providers get more traffic. Simple and intuitive, but:
- Price-only ignores quality: the cheapest provider may have the worst latency
- Price changes can cause traffic oscillation: a price drop → traffic surge → latency spike → poor experience
Algorithm 2: Price-Weighted + Latency Penalty (Recommended)
Weight_i = (BasePrice / Price_i) × clamp(1 - (p99_i - p99_baseline) / p99_baseline, 0.3, 1.0)
Adds a latency penalty factor based on the last 5 minutes of p99 latency. Providers with higher latency get lower weight, but there is a floor (0.3) to prevent starvation.
Advantages:
- No prediction model needed, minimal computation
- Automatic price-quality balance
- Traffic oscillation is dampened by the latency penalty
Algorithm 3: Weighted Round-Robin + Circuit Breaker
For throughput-sensitive scenarios (batch processing), use weighted round-robin with a circuit breaker — when a provider’s error rate exceeds a threshold, the breaker opens and traffic bypasses it entirely.
3. Circuit Breaking and Degradation
Circuit Breaker State Machine
Closed (normal forwarding)
→ Error rate exceeds threshold (e.g., 5 consecutive failures or > 30% in 5 min)
→ Open (fail fast, no forwarding)
→ Half-open (after timeout window, probe with少量 requests)
→ Success → Closed, Failure → Open
Circuit breakers are per-provider. One provider failing does not affect others. Breaker states must be exposed on the monitoring dashboard.
Degradation Strategy
When all providers are unavailable or rate-limited, the gateway should degrade gracefully:
- Degrade to cache: return cached results for non-realtime requests
- Degrade to queue: return an estimated queue position, client polls for results
- Degrade to friendly error: return “capacity exhausted, please retry later” instead of a bare 502
4. Idempotent Retries
Retries are the most subtle trap in multi-provider gateways. A request goes to Provider A, times out, retries to Provider B — but Provider A actually executed it. The user gets charged twice.
Solution: request_id Deduplication
The gateway assigns a globally unique request_id (UUID or Snowflake ID) to each request. The provider promises:
Same request_id → execute at most once
If the provider does not support deduplication, the gateway falls back to:
- Read operations (balance checks) → safe to retry
- Write operations (inference requests) → application-level deduplication, or gateway-level short-term dedup (5-min window)
5. Quota Management
Each provider has rate and total limits. The gateway maintains a quota tracking table:
| Provider | Model | Per-Minute Quota | Used | Remaining | Reset Time |
|---|---|---|---|---|---|
| A | GPT-4 | 1000 | 342 | 658 | 12:00 |
| B | Claude-3 | 800 | 800 | 0 | 12:00 |
The routing layer excludes providers with exhausted quotas. Five minutes before a quota reset, start pre-allocating requests to the recovering provider to avoid a traffic spike.
Summary
| Module | Key Decision | Common Mistake |
|---|---|---|
| Routing | Price-weighted + latency penalty | Price-only, ignoring quality |
| Circuit breaker | Per-provider + half-open state | Global breaker, one failure kills all |
| Retry | request_id deduplication | No idempotency guarantee, double billing |
| Quota | Real-time tracking + pre-allocation | Only error on exhaustion |
| Adapter | Adapter pattern | Mixing routing and adapter logic |
The complexity of a multi-provider gateway is not in connecting to APIs — it is in making real-time decisions between them. Routing, circuit breaking, retry, and quota management: each is simple in isolation, but together they are the real engineering challenge.
Related reading
- AI Token Trading Platform Architecture — the overall platform architecture and the gateway’s role
- Observability for AI Applications — latency and error monitoring at the gateway layer
Need a multi-provider Token aggregation gateway? Contact us — tell us your provider list and traffic scale, feasibility within 24 hours.
FAQ
How is a multi-provider Token gateway different from a regular API gateway?
A regular API gateway uses static routing — the path-to-service mapping is determined at deploy time. A Token gateway uses dynamic routing — every request is routed based on real-time metrics: price, latency, availability, and quota remaining across all providers. It is more of a real-time trading layer than a proxy.
Round-robin or price-weighted routing?
Round-robin only works when providers are fully homogeneous, which is rare. The recommended approach is price-weighted + latency penalty: base weight is inversely proportional to price, with a penalty factor based on p99 latency over the last 5 minutes. A provider with rising latency automatically gets less traffic. Simple to implement and stable in practice.
How do you detect and switch away from a failing provider?
Two-stage detection: passive — count request timeouts and 5xx errors, mark "suspected" after a threshold (e.g., 5 consecutive failures); active — send health checks every 10s to marked providers, restore after 3 consecutive successes. When switching, gradually reduce weight rather than removing entirely — this prevents traffic spikes from overwhelming the remaining providers.
How do you ensure idempotent retries?
Assign a globally unique request_id at the gateway layer. The provider echoes it back in the response. On retry, the gateway sends the same request_id, and the provider deduplicates by ID. This requires provider cooperation. If the provider does not support deduplication, the gateway can only offer at-least-once semantics, and the business layer must handle duplicates.
This article comes from AI Enable Harness front-line delivery practice. Need a similar system or optimization service?
Subscribe to Updates
Get notified when new articles are published. No spam, occasional updates only.
Subscribe →