How running out of API credits taught us to put every LLM call behind one gateway

2026-11-24来源 dev.toEnglish

This is a story about a failure that never showed up as an error. Our LLM credits ran out at one provider, nothing crashed, and users got stuck in a loop we didn't know about. Fixing it led us to route every model call through a single gateway, OpenRouter. Soon after that move we found a second silent failure: prompt caching wasn't working at all.

If you call more than one LLM provider in production, or you rely on prompt caching to keep costs down, I hope this saves you from both mistakes.

The situation

I'm the founder of CV by JD. People upload their resume, paste a job description, and we rewrite the resume for that job. Then a separate scoring step gives the new resume a match score from 0 to 100, with tips on what's still missing.

Behind one "optimize" click there are several separate model calls:

  • a chat model rewrites the resume
  • a model pulls the requirements out of the job description
  • a model writes tips specific to this resume
  • a vision model reads text from uploaded images
  • a fact-checking model checks each line of the rewrite against the original resume, so we never add something the person didn't say

Here's how those calls reached the models before the change, and after it:

 Before After ────── ───── Our server ──▶ OpenAI Our server ──▶ OpenRouter ──▶ OpenAI ──▶ Google Vertex AI ──▶ DeepSeek hosts ──▶ OpenRouter ──▶ others ──▶ Qwen hosts (a key, a bill and an error format (one key, one bill, one error format) for each provider) 

A provider is a company that runs models and sells access to them, such as OpenAI. A gateway like OpenRouter sits in front of many providers and gives you one API for all of them.

For most of these calls we keep an ordered list of models, cheapest first. If the first model fails, the next one tries. I'll call that list the ladder and each model on it a tier.

Model names in this post reflect our setup in November 2026. We change models often as we learn what works, so treat them as examples, not recommendations.

How we got here

The setup grew one feature at a time. Each time we added a step, we picked whichever model looked best for it and added that provider's client library (SDK). After a few months, our server talked to OpenAI directly, to Google's Vertex AI directly, and to OpenRouter for a few tiers.

Each provider came with its own API key, its own way of authenticating, its own billing page and its own error format. Most steps had backup models from other providers, so they could survive one provider failing. Two steps had no backup at all: pulling requirements out of the job description, and writing resume-specific tips. Both used a single OpenAI model, and together they produce the tips users see next to their score.

The problem

One day our OpenAI credits ran out.

The two single-model steps started failing. The code around them handled the failure the way it was written to: it fell back to generic tips. Those tips looked like this:

Improve your keywords. [Fix this]

The "Fix this" button sends the tip to the model that rewrote the resume and asks it to apply the change. But "Improve your keywords" doesn't name any specific missing requirement, so applying it changed nothing the score looks at. The score stayed flat, the same tip came back, and the user could click it again, and again.

From the user's side, the product looked broken. From our side, everything looked fine. There were no 500s and no error pages, and no alert fired, because a billing failure at one provider wasn't wired to any alert. We found out from user feedback.

The real problem was that billing failures were invisible. We had several providers, several dashboards and no alert.

What we researched

We went through every model call in the codebase and asked three questions of each one:

  1. Which provider does it use, and what happens if that provider fails? Most calls could fall back to another provider. A couple couldn't.
  2. How would we find out about a failure? For a billing failure, we mostly wouldn't. Each provider reported it in its own format, on its own dashboard.
  3. What else do we need from the provider layer? Per-request privacy settings, such as asking that the request isn't stored or used for training. Access to the fact-checking model, which we call through OpenRouter. And the ability to try a new model without adding another client library.

The options we found

Option Good Bad
Keep the direct providers and add billing alerts to each one No migration Still several keys, bills and error formats. Every new provider adds more.
Run our own proxy (for example, LiteLLM) Full control over routing and logging Another service to deploy, secure and keep up to date
Move to a different managed gateway Similar benefits We'd still need OpenRouter for the fact-checking model, so we'd have two gateways
Put everything behind OpenRouter Already in use for part of the ladder and for the fact-checking model. One key, one bill, one error format. Every model call now depends on one company

What we chose, and why

We moved every call to OpenRouter. For us it was the simplest way to get down to one key, one bill and one error format without running another service ourselves, and we already depended on it for the fact-checking model. The ladder still uses models from several vendors (DeepSeek, Qwen and OpenAI), but all of them are reached through one API.

Then we fixed the things that had let the failure go unnoticed:

  • Every step now has a ladder. No call depends on a single model.
  • Billing failures are recognised and alerted on. Providers don't send a special "you're out of credit" signal. You get an ordinary-looking error. So we treat HTTP 402 as "out of money", and we also treat a 401, 403 or 429 that mentions credit or quota in its message the same way. That sends an alert, rate-limited per key so it doesn't flood the channel.
  • Each key has a spend limit. A bug in a loop can't run up an unlimited bill.
  • Privacy settings travel with the request. The fact-checking calls ask for no data collection and zero data retention, and aren't allowed to move to another host.
  • Model versions are pinned. OpenRouter offers floating aliases such as ~deepseek/deepseek-flash-latest, which move to new versions automatically. We use the dated ID instead (deepseek/deepseek-v4.1-flash-1017), so the model only changes when we deploy the change.

We accepted one real trade-off. OpenRouter is now a single point of failure. If it goes down, every tier goes down together. Before, we looked redundant, but two steps had no backup, so one provider failing could still break the product without anyone hearing about it. A single dependency we alert on has been easier to live with than several we didn't.

The second problem: prompt caching quietly stopped working

The situation

Our chat system prompt is over 20,000 tokens and identical on every request. Providers such as DeepSeek cache a prompt prefix they've seen recently and charge much less to reuse it. With a prompt this large, caching makes up a big part of the cost of every request.

After consolidating, we made DeepSeek V4.1 Flash the first model on the ladder, partly because its cached input is so cheap.

The problem

We record the number of cached prompt tokens on every request, and it was close to zero. We were paying full price for the same 20,000 tokens on every call.

What we researched

OpenRouter's documentation on provider routing explained it. A model whose weights are public, like this one, is served by many different hosting companies, about 50 when we checked, and each host keeps its own cache. By default OpenRouter spreads requests across them, so consecutive requests usually landed on different hosts, and each one found a cold cache.

Here's a simple way to picture it. Imagine 50 coffee shops that each remember your order, but only if you go back to the same shop. If you're sent to a random shop every morning, nobody ever remembers you.

The options we found

Option Effect
Accept it Full input price on every request
Send a session ID, so OpenRouter keeps a conversation on one host Cache hits inside a conversation, but every new conversation starts cold
Pin a provider order, so every request goes to the same one or two hosts All users share one warm cache for the shared system prompt

What we chose, and why

Our big, expensive prefix is the system prompt, and it's the same for every user. Pinning gives everyone the same warm cache, so we chose that:

Here, deepinfra/fp8 is one hosting company's build of the model, and deepseek is DeepSeek's own API:

modelKwargs: { provider: { order: ['deepinfra/fp8', 'deepseek'], allow_fallbacks: true, }, }, 

On the next request, 22,272 of the roughly 22,400 prompt tokens (about 99%) came from the cache instead of being billed at full price. At DeepSeek's cached-token price, that made input on our most common call roughly ten times cheaper.

Two details make this work:

  • allow_fallbacks: true means that if both pinned hosts are down, OpenRouter may use any other host for the same model. Only if that also fails does our ladder move on to a different model.
  • The system prompt has to stay exactly the same. Anything that changes per request, such as today's date, the user's resume or the job description, goes in a later message. A single timestamp at the top of the system prompt is enough to make every request a cache miss.

What you can take from this

  • A fallback that hides a failure can be worse than the failure. Our generic tips kept the page working and made the problem invisible.
  • Treat "out of credit" as its own kind of error, with its own alert. It doesn't look like an outage, so normal error monitoring misses it.
  • Look for steps that depend on a single model. One of them will be the step that breaks.
  • On a multi-host gateway, prompt caching may not work by default. Pin the provider, keep the prefix constant, and measure cached tokens instead of assuming.
  • If you consolidate on one gateway, decide on purpose that you accept the single point of failure.

If you run production traffic through a model gateway, I'm curious how you handle that single point of failure. Did you find a setup that was worth the extra complexity?

原文:https://www.a8q9.cn/en/article/how-running-out-of-api-credits-taught-us-to-put-every-llm-call-behind-one-gatewa-a66efa.html

相关文章