How to add LLM circuit breakers in Node.js in 2026
Per-provider LLM circuit breakers stop cascade failures early. Closed, open, half-open state machine with auto-reset: working Node.js code.
Tag · infra
Per-provider LLM circuit breakers stop cascade failures early. Closed, open, half-open state machine with auto-reset: working Node.js code.
Wire Anthropic and OpenAI into one Next.js API route: primary call, automatic provider switch on 429 or 5xx, and the real latency cost of switching mid-request.
Four signals every LLM call should log: tokens, cost, latency, and a quality proxy. An async Node.js middleware that adds under 2ms of overhead.
Three production patterns for detecting stalled LLM streams in Node.js: chunk watchdogs, wall-clock gates, and partial output buffering, with working code.
Three production patterns for streaming LLM responses in Node.js, Next.js, and FastAPI, with the mid-stream error catch most tutorials skip.
LiteLLM has multiple routing strategies. These 4 matter in production: weighted, fallback, semantic, and budget-capped, each with production-ready YAML.
Prompt caching cuts LLM API costs up to 90%, but Anthropic, OpenAI, and Gemini implement it differently. Here's how each vendor's billing actually works.
Route each prompt to the cheapest model that handles it well. When quality falls short, escalate silently. Here's the pattern with working Node.js code.
Add OpenRouter model fallbacks to a Node.js app: setup, the models array, response.model tracking, and four pitfalls that catch you on week two.
Semantic caching reduces LLM API spend by 20-70% in production. Here's how embedding-based, prompt-hash, and hybrid caching each break in practice.
Four production patterns for LLM rate limits: jitter, token pre-checks, circuit breakers, and provider failover. Backoff alone won't save you in 2026.
Prompt caching cuts LLM costs 90% on Anthropic and 50% on OpenAI, but only when your workload fits. Here's the exact break-even math per provider.
One model going down shouldn't take your AI feature with it. Here's how to build a fallback chain using LiteLLM, OpenRouter, and LLMTest.