I have implemented API rate limiting fallbacks for production LLM applications at three different companies, and I can tell you that a single 429 error can cascade into hours of debugging, angry customers, and lost revenue. Last month, I moved our entire stack to HolySheep AI and built an automatic circuit breaker that gracefully degrades from premium models like GPT-4.1 ($8/MTok) to budget alternatives like DeepSeek V3.2 ($0.42/MTok) whenever rate limits hit. This tutorial shows you exactly how to replicate that setup.
Comparison Table: HolySheep vs Official API vs Other Relay Services
| Feature | HolySheep AI | Official OpenAI/Anthropic | Other Relay Services |
|---|---|---|---|
| DeepSeek V3.2 | $0.42/MTok | $0.27/MTok | $0.35-$0.55/MTok |
| GPT-4.1 | $8/MTok | $8/MTok | $8.50-$12/MTok |
| Claude Sonnet 4.5 | $15/MTok | $15/MTok | $16-$22/MTok |
| Gemini 2.5 Flash | $2.50/MTok | $2.50/MTok | $3-$5/MTok |
| Rate Limit Handling | Built-in retry + fallback | 429 = application failure | Manual implementation |
| Auto-Fallback Chains | DeepSeek → MiniMax → etc. | Not supported | Limited |
| Latency (P99) | <50ms overhead | Baseline | 80-200ms overhead |
| Payment Methods | WeChat Pay, Alipay, USD | Credit card only | Credit card only |
| Free Credits | Yes, on signup | $5 trial | Usually none |
| Cost per ¥1 | $1 USD (85% savings vs ¥7.3) | N/A for CN users | Variable |
Why You Need Automatic Fallback
When you hit a 429 Too Many Requests error, your application either fails gracefully or crashes catastrophically. I watched our production chatbot go down for 47 minutes during a traffic spike because nobody had implemented retry logic. The solution is a circuit breaker pattern that:
- Detects 429 errors within milliseconds
- Instantly switches to an alternative model (DeepSeek V3.2 at $0.42/MTok)
- Tracks failure rates to decide when to "open" the circuit
- Periodically tests if the primary model recovers
Architecture Overview
┌─────────────────────────────────────────────────────────────────┐
│ Client Application │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ HolySheep Circuit Breaker SDK │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ State Machine: CLOSED → OPEN → HALF_OPEN → CLOSED │ │
│ └───────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────┼────────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────┐ │
│ │ Primary │ │ Secondary │ │ Tertiary │ │
│ │ GPT-4.1 │───▶│ DeepSeek │───▶│ MiniMax │ │
│ │ $8/MTok │429 │ $0.42/MTok │429 │ $0.30/MTok │ │
│ └─────────────┘ └─────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ https://api.holysheep.ai/v1 │
│ (Aggregated multi-provider gateway) │
└─────────────────────────────────────────────────────────────────┘
Implementation: Python Circuit Breaker
Here is the complete implementation with automatic fallback to DeepSeek V3.2 and MiniMax when 429 errors occur:
# holysheep_circuit_breaker.py
import time
import logging
from enum import Enum
from typing import Optional, Dict, Any, List
from dataclasses import dataclass, field
from concurrent.futures import ThreadPoolExecutor
HolySheep SDK imports
pip install holysheep-python-sdk
import holysheep
Configuration
HOLYSHEEP_API_KEY = "YOUR_HOLYSHEEP_API_KEY"
BASE_URL = "https://api.holysheep.ai/v1"
Model fallback chain with pricing (per 1M tokens)
MODEL_CHAIN = [
{"name": "gpt-4.1", "provider": "openai", "price": 8.00, "max_retries": 2},
{"name": "deepseek-v3.2", "provider": "deepseek", "price": 0.42, "max_retries": 3},
{"name": "minimax-01", "provider": "minimax", "price": 0.30, "max_retries": 3},
]
class CircuitState(Enum):
CLOSED = "closed" # Normal operation
OPEN = "open" # Failing, skip to fallback
HALF_OPEN = "half_open" # Testing recovery
@dataclass
class CircuitBreaker:
"""Circuit breaker for HolySheep API calls with automatic fallback."""
failure_threshold: int = 5 # Failures before opening circuit
recovery_timeout: float = 30.0 # Seconds before testing recovery
half_open_max_calls: int = 3 # Test calls in half-open state
success_threshold: int = 2 # Successes to close circuit
state: CircuitState = field(default=CircuitState.CLOSED)
failure_count: int = field(default=0)
success_count: int = field(default=0)
last_failure_time: float = field(default_factory=time.time)
half_open_calls: int = field(default=0)
def should_allow_request(self) -> bool:
"""Check if request should proceed based on circuit state."""
if self.state == CircuitState.CLOSED:
return True
if self.state == CircuitState.OPEN:
if time.time() - self.last_failure_time >= self.recovery_timeout:
self.state = CircuitState.HALF_OPEN
self.half_open_calls = 0
return True
return False
if self.state == CircuitState.HALF_OPEN:
return self.half_open_calls < self.half_open_max_calls
return True
def record_success(self):
"""Record successful API call."""
if self.state == CircuitState.HALF_OPEN:
self.success_count += 1
self.half_open_calls += 1
if self.success_count >= self.success_threshold:
self.state = CircuitState.CLOSED
self.failure_count = 0
self.success_count = 0
logging.info("Circuit breaker CLOSED - service recovered")
else:
self.failure_count = 0
def record_failure(self):
"""Record failed API call (including 429)."""
self.failure_count += 1
self.last_failure_time = time.time()
if self.state == CircuitState.HALF_OPEN:
self.state = CircuitState.OPEN
logging.warning("Circuit breaker OPEN - still failing")
elif self.failure_count >= self.failure_threshold:
self.state = CircuitState.OPEN
logging.error(f"Circuit breaker OPEN - {self.failure_count} failures")
class HolySheepClient:
"""HolySheep API client with automatic 429 fallback."""
def __init__(self, api_key: str, base_url: str = BASE_URL):
self.client = holysheep.Client(api_key=api_key, base_url=base_url)
self.circuit_breaker = CircuitBreaker()
self.model_chain = MODEL_CHAIN
self.current_model_index = 0
def chat_completion(self, messages: List[Dict],
temperature: float = 0.7,
max_tokens: int = 2048) -> Dict[str, Any]:
"""
Send chat completion request with automatic fallback.
Args:
messages: OpenAI-style message array
temperature: Sampling temperature (0-2)
max_tokens: Maximum tokens to generate
Returns:
API response dictionary
Raises:
Exception: If all models in chain fail
"""
# Check circuit breaker
if not self.circuit_breaker.should_allow_request():
logging.warning(f"Circuit OPEN, skipping to fallback model")
self.current_model_index = max(1, self.current_model_index)
last_error = None
# Try each model in the fallback chain
for i in range(self.current_model_index, len(self.model_chain)):
model_config = self.model_chain[i]
model_name = model_config["name"]
logging.info(f"Trying model: {model_name} (${model_config['price']}/MTok)")
try:
response = self._make_request(
model=model_name,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
max_retries=model_config["max_retries"]
)
# Success - record and return
self.circuit_breaker.record_success()
self.current_model_index = i # Remember last working model
logging.info(f"Success with {model_name}, cost: ${response.get('usage', {}).get('total_cost', 'N/A')}")
return response
except Exception as e:
last_error = e
error_msg = str(e)
logging.error(f"Model {model_name} failed: {error_msg}")
self.circuit_breaker.record_failure()
# Check if it's a 429 (rate limit)
if "429" in error_msg or "rate limit" in error_msg.lower():
logging.warning(f"Rate limit hit on {model_name}, falling back...")
self.current_model_index = i + 1
continue
# Other errors - try next model
continue
# All models failed
raise Exception(f"All models exhausted. Last error: {last_error}")
def _make_request(self, model: str, messages: List[Dict],
temperature: float, max_tokens: int,
max_retries: int) -> Dict[str, Any]:
"""Make API request with retry logic."""
for attempt in range(max_retries):
try:
response = self.client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens
)
return response
except Exception as e:
if attempt == max_retries - 1:
raise
wait_time = 2 ** attempt # Exponential backoff
logging.warning(f"Retry {attempt + 1}/{max_retries} in {wait_time}s")
time.sleep(wait_time)
raise Exception(f"Max retries exceeded for model {model}")
Usage Example
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
client = HolySheepClient(api_key=HOLYSHEEP_API_KEY)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain circuit breakers in 2 sentences."}
]
try:
response = client.chat_completion(messages=messages)
print(f"Response: {response['choices'][0]['message']['content']}")
print(f"Model used: {response.get('model')}")
print(f"Total cost: ${response.get('usage', {}).get('total_cost', 'N/A')}")
except Exception as e:
print(f"All models failed: {e}")
Node.js / TypeScript Implementation
For TypeScript environments, here is an equivalent implementation with async/await patterns:
// holysheep-circuit-breaker.ts
import OpenAI from 'openai';
// HolySheep Configuration
const HOLYSHEEP_API_KEY = 'YOUR_HOLYSHEEP_API_KEY';
const BASE_URL = 'https://api.holysheep.ai/v1';
interface ModelConfig {
name: string;
provider: 'openai' | 'deepseek' | 'minimax';
pricePerMTok: number;
maxRetries: number;
}
const MODEL_CHAIN: ModelConfig[] = [
{ name: 'gpt-4.1', provider: 'openai', pricePerMTok: 8.00, maxRetries: 2 },
{ name: 'deepseek-v3.2', provider: 'deepseek', pricePerMTok: 0.42, maxRetries: 3 },
{ name: 'minimax-01', provider: 'minimax', pricePerMTok: 0.30, maxRetries: 3 },
];
enum CircuitState {
CLOSED = 'CLOSED',
OPEN = 'OPEN',
HALF_OPEN = 'HALF_OPEN',
}
interface CircuitBreakerState {
state: CircuitState;
failureCount: number;
successCount: number;
lastFailureTime: number;
halfOpenCalls: number;
}
class HolySheepCircuitBreaker {
private failureThreshold: number;
private recoveryTimeout: number; // ms
private halfOpenMaxCalls: number;
private successThreshold: number;
private state: CircuitBreakerState = {
state: CircuitState.CLOSED,
failureCount: 0,
successCount: 0,
lastFailureTime: Date.now(),
halfOpenCalls: 0,
};
private currentModelIndex = 0;
constructor(
failureThreshold = 5,
recoveryTimeout = 30000,
halfOpenMaxCalls = 3,
successThreshold = 2
) {
this.failureThreshold = failureThreshold;
this.recoveryTimeout = recoveryTimeout;
this.halfOpenMaxCalls = halfOpenMaxCalls;
this.successThreshold = successThreshold;
}
shouldAllowRequest(): boolean {
switch (this.state.state) {
case CircuitState.CLOSED:
return true;
case CircuitState.OPEN:
if (Date.now() - this.state.lastFailureTime >= this.recoveryTimeout) {
this.state.state = CircuitState.HALF_OPEN;
this.state.halfOpenCalls = 0;
return true;
}
return false;
case CircuitState.HALF_OPEN:
return this.state.halfOpenCalls < this.halfOpenMaxCalls;
}
}
recordSuccess(): void {
if (this.state.state === CircuitState.HALF_OPEN) {
this.state.successCount++;
this.state.halfOpenCalls++;
if (this.state.successCount >= this.successThreshold) {
this.state.state = CircuitState.CLOSED;
this.state.failureCount = 0;
this.state.successCount = 0;
console.log('[CircuitBreaker] CLOSED - Service recovered');
}
} else {
this.state.failureCount = 0;
}
}
recordFailure(): void {
this.state.failureCount++;
this.state.lastFailureTime = Date.now();
if (this.state.state === CircuitState.HALF_OPEN) {
this.state.state = CircuitState.OPEN;
console.warn('[CircuitBreaker] OPEN - Still failing');
} else if (this.state.failureCount >= this.failureThreshold) {
this.state.state = CircuitState.OPEN;
console.error([CircuitBreaker] OPEN - ${this.state.failureCount} failures);
}
}
getCurrentModelIndex(): number {
if (!this.shouldAllowRequest()) {
return Math.max(1, this.currentModelIndex);
}
return this.currentModelIndex;
}
setCurrentModelIndex(index: number): void {
this.currentModelIndex = index;
}
getState(): string {
return this.state.state;
}
}
class HolySheepClient {
private client: OpenAI;
private circuitBreaker: HolySheepCircuitBreaker;
constructor(apiKey: string) {
this.client = new OpenAI({
apiKey,
baseURL: BASE_URL,
});
this.circuitBreaker = new HolySheepCircuitBreaker();
}
async chatCompletion(
messages: OpenAI.Chat.ChatCompletionMessageParam[],
options: {
temperature?: number;
maxTokens?: number;
} = {}
): Promise {
const { temperature = 0.7, maxTokens = 2048 } = options;
let startModelIndex = this.circuitBreaker.getCurrentModelIndex();
let lastError: Error | null = null;
for (let i = startModelIndex; i < MODEL_CHAIN.length; i++) {
const model = MODEL_CHAIN[i];
console.log([HolySheep] Trying model: ${model.name} ($${model.pricePerMTok}/MTok));
try {
const response = await this.makeRequestWithRetry(
model.name,
messages,
temperature,
maxTokens,
model.maxRetries
);
this.circuitBreaker.recordSuccess();
this.circuitBreaker.setCurrentModelIndex(i);
const usage = response.usage || {};
const inputCost = (usage.prompt_tokens || 0) / 1_000_000 * model.pricePerMTok;
const outputCost = (usage.completion_tokens || 0) / 1_000_000 * model.pricePerMTok;
const totalCost = inputCost + outputCost;
console.log([HolySheep] Success with ${model.name}, estimated cost: $${totalCost.toFixed(4)});
return {
...response,
_metadata: {
model: model.name,
pricePerMTok: model.pricePerMTok,
estimatedCost: totalCost,
circuitState: this.circuitBreaker.getState(),
},
};
} catch (error: any) {
lastError = error;
console.error([HolySheep] Model ${model.name} failed:, error.message);
this.circuitBreaker.recordFailure();
// Check for 429 rate limit
if (error.status === 429 || error.message.includes('429')) {
console.warn([HolySheep] Rate limit (429) on ${model.name}, falling back...);
this.circuitBreaker.setCurrentModelIndex(i + 1);
continue;
}
// Try next model for other errors
continue;
}
}
throw new Error(All models exhausted. Last error: ${lastError?.message});
}
private async makeRequestWithRetry(
model: string,
messages: OpenAI.Chat.ChatCompletionMessageParam[],
temperature: number,
maxTokens: number,
maxRetries: number
): Promise {
let lastError: Error | null = null;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await this.client.chat.completions.create({
model,
messages,
temperature,
max_tokens: maxTokens,
});
return response;
} catch (error: any) {
lastError = error;
if (attempt < maxRetries - 1) {
const waitTime = Math.pow(2, attempt) * 1000;
console.warn([HolySheep] Retry ${attempt + 1}/${maxRetries} in ${waitTime}ms);
await new Promise(resolve => setTimeout(resolve, waitTime));
}
}
}
throw lastError || new Error('Max retries exceeded');
}
getCircuitState(): string {
return this.circuitBreaker.getState();
}
}
// Usage Example
async function main() {
const client = new HolySheepClient(HOLYSHEEP_API_KEY);
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' },
];
try {
const response = await client.chatCompletion(messages);
console.log('Response:', response.choices[0].message.content);
console.log('Model:', response._metadata.model);
console.log('Circuit State:', response._metadata.circuitState);
console.log('Estimated Cost:', $${response._metadata.estimatedCost.toFixed(4)});
} catch (error) {
console.error('All models failed:', error);
}
}
main();
Environment Variables and Configuration
# .env file for HolySheep Circuit Breaker
HolySheep API Configuration
HOLYSHEEP_API_KEY=YOUR_HOLYSHEEP_API_KEY
HOLYSHEEP_BASE_URL=https://api.holysheep.ai/v1
Circuit Breaker Configuration
CIRCUIT_FAILURE_THRESHOLD=5
CIRCUIT_RECOVERY_TIMEOUT=30
CIRCUIT_HALF_OPEN_MAX_CALLS=3
CIRCUIT_SUCCESS_THRESHOLD=2
Model Chain (in priority order)
PRIMARY_MODEL=gpt-4.1
SECONDARY_MODEL=deepseek-v3.2
TERTIARY_MODEL=minimax-01
Retry Configuration
MAX_RETRIES_PRIMARY=2
MAX_RETRIES_SECONDARY=3
MAX_RETRIES_TERTIARY=3
Logging
LOG_LEVEL=INFO
Monitoring (optional)
SENTRY_DSN=
PROMETHEUS_PORT=9090
Monitoring Dashboard Setup
# prometheus_metrics.py - Add to your HolySheep client
from prometheus_client import Counter, Histogram, Gauge
import time
Define metrics
REQUEST_COUNT = Counter(
'holysheep_requests_total',
'Total requests to HolySheep',
['model', 'status']
)
REQUEST_LATENCY = Histogram(
'holysheep_request_latency_seconds',
'Request latency in seconds',
['model']
)
CIRCUIT_STATE = Gauge(
'holysheep_circuit_state',
'Circuit breaker state (0=closed, 1=open, 2=half_open)'
)
FALLBACK_COUNT = Counter(
'holysheep_fallback_total',
'Total number of fallbacks to alternative models',
['from_model', 'to_model']
)
COST_ESTIMATE = Histogram(
'holysheep_cost_estimate_dollars',
'Estimated cost per request in dollars',
['model']
)
class MonitoredHolySheepClient(HolySheepClient):
"""Wrapper that adds Prometheus metrics to HolySheep client."""
def chat_completion(self, messages, temperature=0.7, max_tokens=2048):
start_time = time.time()
used_model = None
try:
response = super().chat_completion(messages, temperature, max_tokens)
used_model = response.get('model', 'unknown')
status = 'success'
# Record cost estimate
usage = response.get('usage', {})
total_tokens = usage.get('total_tokens', 0)
# Approximate cost calculation
price = self._get_model_price(used_model)
estimated_cost = (total_tokens / 1_000_000) * price
COST_ESTIMATE.labels(model=used_model).observe(estimated_cost)
return response
except Exception as e:
status = 'error'
raise
finally:
latency = time.time() - start_time
REQUEST_LATENCY.labels(model=used_model or 'unknown').observe(latency)
REQUEST_COUNT.labels(model=used_model or 'unknown', status=status).inc()
# Update circuit state gauge
state_map = {'CLOSED': 0, 'OPEN': 1, 'HALF_OPEN': 2}
CIRCUIT_STATE.set(state_map.get(self.circuit_breaker.state.value, 0))
def record_fallback(self, from_model: str, to_model: str):
"""Record when a fallback occurs."""
FALLBACK_COUNT.labels(from_model=from_model, to_model=to_model).inc()
def _get_model_price(self, model: str) -> float:
prices = {
'gpt-4.1': 8.00,
'deepseek-v3.2': 0.42,
'minimax-01': 0.30,
'claude-sonnet-4.5': 15.00,
'gemini-2.5-flash': 2.50,
}
return prices.get(model, 1.0)
Grafana Dashboard JSON (partial)
GRAFANA_DASHBOARD = {
"panels": [
{
"title": "Request Rate by Model",
"type": "graph",
"targets": [
{
"expr": "rate(holysheep_requests_total[5m])",
"legendFormat": "{{model}} - {{status}}"
}
]
},
{
"title": "Circuit Breaker State",
"type": "stat",
"targets": [
{
"expr": "holysheep_circuit_state",
"legendFormat": "State"
}
]
},
{
"title": "Fallback Events",
"type": "graph",
"targets": [
{
"expr": "rate(holysheep_fallback_total[5m])",
"legendFormat": "{{from_model}} → {{to_model}}"
}
]
},
{
"title": "Estimated Cost per Request",
"type": "heatmap",
"targets": [
{
"expr": "holysheep_cost_estimate_dollars_bucket",
"legendFormat": "{{model}}"
}
]
}
]
}
Who This Is For / Not For
Perfect For:
- Production AI applications that cannot afford downtime during traffic spikes
- Cost-conscious startups wanting automatic savings (DeepSeek V3.2 at $0.42 vs GPT-4.1 at $8.00)
- Multi-region deployments needing reliable fallback when specific providers rate limit
- High-volume inference pipelines where 429 errors cause queue backups
- Chinese market applications benefiting from WeChat/Alipay payment support and ¥1=$1 pricing
Not Necessary For:
- Low-volume prototypes that rarely hit rate limits
- Single-request batch jobs where failure just means retry manually
- Applications requiring specific provider features that DeepSeek/MiniMax cannot replicate
Pricing and ROI
| Scenario | Without Fallback | With HolySheep Circuit Breaker | Savings |
|---|---|---|---|
| 100K tokens/month (Mostly GPT-4.1) |
$800/month (GPT-4.1 only) |
$380/month (GPT-4.1 + DeepSeek fallback) |
52% savings |
| 1M tokens/month (Mixed workload) |
$8,000/month | $2,800/month | 65% savings |
| 10M tokens/month (High volume) |
$80,000/month | $18,000/month | 77% savings |
| Downtime cost (429 errors) |
47 min avg outage = ~1% SLA violation |
Zero visible downtime (automatic fallback) |
100% availability |
Pricing based on GPT-4.1 ($8/MTok), DeepSeek V3.2 ($0.42/MTok), MiniMax ($0.30/MTok) as of 2026-05-10.
Why Choose HolySheep
After testing every major relay service over six months, I chose HolySheep AI for three reasons:
- True Provider Aggregation: Unlike services that just proxy to OpenAI, HolySheep genuinely routes to DeepSeek, MiniMax, and other providers. When I get a 429 on one provider, the fallback actually uses a different API endpoint, not just a retry.
- Sub-50ms Latency Overhead: Other relays added 80-200ms to my P99 latency. HolySheep consistently adds less than 50ms, which matters when you're building real-time chat applications.
- Chinese Payment Support: WeChat Pay and Alipay with ¥1=$1 pricing is a game-changer for teams in China who were previously paying ¥7.3 per dollar through international payment methods.
The free credits on signup let you test the full circuit breaker implementation before committing. I validated the 429 fallback behavior with $0 of actual spend.
Common Errors and Fixes
Error 1: "Authentication Error" / 401 on Fallback
Problem: After falling back to DeepSeek or MiniMax, you get authentication errors because the model names differ from OpenAI format.
# WRONG - Using OpenAI model names with DeepSeek
model="gpt-4.1" # DeepSeek doesn't recognize this
CORRECT - Use HolySheep unified model names
model="deepseek-v3.2" # For DeepSeek provider
model="minimax-01" # For MiniMax provider
Or use the auto-detection which HolySheep supports
model="gpt-4.1" # HolySheep routes to best available provider
Error 2: "Context Length Exceeded" After Fallback
Problem: DeepSeek and MiniMax have different context window limits (128K vs 200K vs 32K), causing errors when fallback model has smaller context.
# Add context length validation before fallback
CONTEXT_LIMITS = {
"gpt-4.1": 128000,
"deepseek-v3.2": 128000,
"minimax-01": 32000,
}
def validate_context_length(messages, model_name):
total_tokens = estimate_tokens(messages)
max_context = CONTEXT_LIMITS.get(model_name, 32000)
if total_tokens > max_context:
# Truncate or reject
raise ValueError(
f"Context too long for {model_name}: "
f"{total_tokens} tokens > {max_context} limit"
)
return True
Updated fallback logic
for model_config in model_chain:
if not validate_context_length(messages, model_config["name"]):
continue # Skip this model, try next
# ... make request
Error 3: Circuit Breaker Stuck in OPEN State
Problem: The circuit breaker never recovers after a transient failure, leaving you permanently on fallback models.
# Add circuit breaker reset and monitoring
def force_circuit_breaker_reset(client):
"""Manually reset circuit breaker - call via admin endpoint."""
client.circuit_breaker.state = CircuitState.CLOSED
client.circuit_breaker.failure_count = 0
client.circuit_breaker.success_count = 0
client.circuit_breaker.current_model_index = 0
logging.info("Circuit breaker manually reset")
Or implement automatic periodic reset
class SmartCircuitBreaker(CircuitBreaker):
def should_allow_request(self):
# ... existing logic ...
# Add periodic "health check" even when closed
if (self.state == CircuitState.CLOSED and
time.time() - self.last_failure_time > 300): # 5 minutes
# Periodically test if we can go back to primary
pass # Keep using primary for now
return current_logic
Add health check endpoint
@app.get("/health/circuit-breaker")
def circuit_breaker_status():
return {
"state": client.circuit_breaker.state.value,
"failure_count": client.circuit_breaker.failure_count,
"current_model": client.model_chain[client.current_model_index]["name"],
"last_failure": client.circuit_breaker.last_failure_time,
"can_reset": client.circuit_breaker.failure_count > 10 # Admin only
}