AI Gateway falls back to another provider only when a request fails outright. But a provider can return a 200, bill you for output tokens, and deliver nothing usable, no text, no tool call. Since the request technically succeeded, nothing falls back, and you're charged for an answer that doesn't exist.
In vercel/ai#20932, Baseten did exactly this on zai/glm-5.3-flash: billed the final answer but streamed no content in 22 of 51 tool-calling requests.
I built provider-guard to catch this. It's an AI SDK middleware, CLI, and dashboard:
```ts import { gateway } from '/gateway' import { streamText, wrapLanguageModel } from 'ai' import { guard } from 'provider-guard'
const model = wrapLanguageModel({ model: gateway('zai/glm-5.3-flash'), middleware: guard() }) const result = streamText({ model, prompt }) ```
What it does:
- Detects a billed-but-empty response and retries once on a different provider, spliced into the same stream
- Lets you exclude one provider per model per request (
exclude()), since AI Gateway currently acceptsexcludebut silently ignores it (vercel/ai#20934) - Records metadata-only call records (provider, tokens, timing, never prompts or responses) and gives you a CLI report + local dashboard to see which provider is actually flaky
- Zero runtime dependencies
Live demo replaying real data from the issues above: https://provider-guard-demo.vercel.app
npm: https://www.npmjs.com/package/provider-guard Source: https://github.com/syedali9210/provider-guard
Would genuinely like feedback, especially from anyone who's hit this with a different provider, or if the routing metadata shape I'm reading (providerMetadata.gateway.routing) doesn't match what you're seeing.
