Everyone talks about cost-per-million-tokens like it's the only number that matters. They'll pick the cheapest provider, wire it into their stack, and call it a day. Then the pager goes off at 3 AM because the "cheap" endpoint is returning 429s, and your fallback chain is burning through your GPT-4 budget. The real cost isn't on the pricing page; it's in the retry logic and the cascading failure scenarios you didn't model.
I built a tool to analyze this after a particularly expensive month where our "cost-optimized" routing logic failed spectacularly. We were using a mix of providers (Claude, GPT-4o, Command R+) with automatic failover. The problem? A surge in traffic caused latency spikes on our primary, which triggered retries, which then also failed over, creating a thundering herd against our fallback providers. We paid for every single attempted token, successful or not, and the latency went through the roof.
The calculator simulates request flows under different conditions. You feed it your provider list, their per-token costs, their expected latency distributions (P50, P90, P99), and your retry/fallback policy. It then runs Monte Carlo simulations to show you the *effective* cost-per-successful-request and the realistic latency profile.
Here's a simplified version of the configuration it uses:
```yaml
providers:
- name: "openai:gpt-4o"
cost_input: 5.00 # per 1M tokens
cost_output: 15.00 # per 1M tokens
latency_p50_ms: 250
latency_p90_ms: 800
error_rate: 0.005 # 0.5%
- name: "anthropic:claude-3-5-sonnet"
cost_input: 3.00
cost_output: 15.00
latency_p50_ms: 300
latency_p90_ms: 1200
error_rate: 0.003
routing_policy:
primary: "openai:gpt-4o"
fallbacks: ["anthropic:claude-3-5-sonnet"]
retries: 2
retry_timeout_ms: 5000
fallback_on: ["5xx", "429", "timeout"]
```
The key outputs are:
* **Effective Cost per Successful 1K Tokens:** Incorporates the cost of all retry and fallback attempts. A provider with a low base cost but high error rate can become more expensive than a stable, pricier one.
* **Latency at Percentile (Actual):** Your P99 latency isn't the provider's P99. It's your provider's P99 *plus* the time spent failing over after N retries. This is what your users actually feel.
* **Budget Risk Profile:** Shows which provider volatility (error rates, latency spikes) most directly impacts your monthly spend.
The biggest surprises from my own analysis:
* Using a very fast but slightly less reliable provider as a primary can be cheaper *if* your retry logic is aggressive and your fallback is significantly slower. The retries succeed quickly most of the time, avoiding the slower fallback cost.
* Adding more fallbacks doesn't linearly increase resilience; it creates a complex cost surface. A second fallback is often only worth it if the first is fundamentally unstable, not just slow.
* For batch processing jobs, tuning for absolute lowest cost-per-token makes sense. For user-facing requests, you must optimize for the *product* of cost and high-percentile latency. Spending 20% more on tokens to cut your P99 latency in half is almost always the right trade-off.
Stop looking at static price sheets. Model the actual request flow, with all its retries and errors. The numbers you get will change your architecture.
This is exactly the kind of modeling more teams need to be doing. The hard part is getting realistic latency distributions, especially for P99. Did you find those numbers stable enough across different times of day and days of the week to feed into your simulation, or did you have to build in separate distributions for peak/off-peak?
That "pay for every attempted token" part is brutal. Does your calculator also factor in the cost of those retries hitting different model tiers? Like if your primary is GPT-4o-mini but the fallback is full GPT-4o, a cascade suddenly gets really expensive.
Still learning
Absolutely, that's a critical detail. Our first version missed it, and we modeled a surge where the fallback was ten times the price per token. The difference wasn't just a spike, it looked like a permanent budget shift.
I'd love to know, what's your strategy for setting the price tiers in the model? Do you use the list prices or factor in any committed use discounts you might have?
Wow, this sounds incredibly complex. So you're simulating the entire request flow with all those variables? How do you even start building something like that? Do you need a ton of historical data first?
You don't need a ton of data to start, but you absolutely need *some*. The initial model is going to be wrong anyway.
I've seen teams get stuck in analysis paralysis. Start with the simplest assumption: a fixed percentage of requests fail and retry, using your bill's actual pricing tiers. That'll already give you a nasty surprise compared to the naive "cost per million tokens" math. Then you layer in the latency distributions and cascade probabilities.
Otherwise you're just building a beautifully complex simulator that's divorced from the reality of your invoices.
Show me the bill
That's a great point about starting simple. We tried to model everything at once and got overwhelmed.
Our first "nasty surprise" came from just tracking retry counts for a week against our primary tier. It was way higher than we guessed, enough to justify changing providers before we even built the full simulator.
How did you pick that initial fixed failure percentage? Just a guess, or based on something like your error logs?