Vercel AI Gateway Failover Budget
Admission control for AI Gateway calls that counts the money the gateway actually billed rather than the tokens you happened to receive. The gateway records a generation's final status server-side and injects the generation id on the first content chunk, which means a stream that dies mid-flight bills you and returns no usage object: a ledger that meters result.usage after a successful await counts zero for exactly the calls most likely to be expensive, and a wrapper that releases the hold on error reads healthy while the invoice climbs. The same fact governs failover, which is why both halves live here. GatewayError carries isRetryable and an optional generationId, so a chain that routes on hand-written status codes walks all five fallbacks on a 401 that will fail identically everywhere, and a chain that releases a failed attempt whose generationId proves it reached a provider loses that spend entirely: a failed attempt with a generation id must be reconciled, not released, and only an attempt without one was genuinely free. Holds are taken per attempt before the call so a runaway agent loop is refused admission instead of becoming a billing incident, cost is reconciled against GatewayGenerationInfo.totalCost in USD rather than inferred from token counts (fallbacks and sort cost mean the model that served the request is chosen at request time and is not the one you priced), and the ledger is integer micro-dollars internally because a few thousand additions of 0.0021 do not sum to what you expect. Pinned to @ai-sdk/[email protected].
npx shadcn@latest add https://ui.aryank.space/r/vercel-ai-gateway-failover-budget.jsonInstalls from ui.aryank.space. To add it by hand, copy the files in Files below, or register the @compronents namespace via the docs.
The first attempt streams 900 tokens and then the provider times out. The wrapper does what every retry wrapper does and releases the hold, but the error carried a generationId, which means a provider was reached and the generation was billed. The failover succeeds, the ledger reports the cost of one call, and the invoice shows two.
@ai-sdk/[email protected]