Skip to content

Commit d3f6a32

Browse files
authored
Merge pull request #403 from Portkey-AI/feat/circuit_breaker
feat: circuit breaker config documentation
2 parents 0f0b811 + 61dd2e7 commit d3f6a32

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

docs.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"product/ai-gateway/cache-simple-and-semantic",
6767
"product/ai-gateway/fallbacks",
6868
"product/ai-gateway/automatic-retries",
69+
"product/ai-gateway/circuit-breaker",
6970
"product/ai-gateway/realtime-api",
7071
"product/ai-gateway/load-balancing",
7172
"product/ai-gateway/canary-testing",
@@ -1007,6 +1008,7 @@
10071008
"product/ai-gateway/cache-simple-and-semantic",
10081009
"product/ai-gateway/fallbacks",
10091010
"product/ai-gateway/automatic-retries",
1011+
"product/ai-gateway/circuit-breaker",
10101012
"product/ai-gateway/realtime-api",
10111013
"product/ai-gateway/load-balancing",
10121014
"product/ai-gateway/canary-testing",
@@ -2849,6 +2851,10 @@
28492851
"source": "/key-features/automatic-retries",
28502852
"destination": "/product/ai-gateway/automatic-retries"
28512853
},
2854+
{
2855+
"source": "/key-features/circuit-breaker",
2856+
"destination": "/product/ai-gateway/circuit-breaker"
2857+
},
28522858
{
28532859
"source": "/key-features/custom-metadata",
28542860
"destination": "/product/observability/metadata"

introduction/feature-overview.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Connect to 250+ AI models using a single consistent API. Set up load balancers,
2323
<Card title="🔁 Automatic Retries" href="/product/ai-gateway/automatic-retries">
2424
Implement automatic retries for improved resilience
2525
</Card>
26+
<Card title="🔌 Circuit Breaker" href="/product/ai-gateway/circuit-breaker">
27+
Configure per-strategy circuit protection and failure handling
28+
</Card>
2629
<Card title="⚖️ Load Balancing" href="/product/ai-gateway/load-balancing">
2730
Distribute workload efficiently across multiple models
2831
</Card>

product/ai-gateway.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ description: The world's fastest AI Gateway with advanced routing & integrated G
3535
Setup automatic retry strategies
3636
</Card>
3737

38+
<Card title="Circuit Breaker" href="/product/ai-gateway/circuit-breaker">
39+
Configure per-strategy circuit protection and failure handling
40+
</Card>
41+
3842
<Card title="Load Balancing" href="/product/ai-gateway/load-balancing">
3943
Load balance between various API Keys to counter rate-limits
4044
</Card>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: 'Circuit Breaker'
3+
description: 'Configure per-strategy circuit protection and failure handling'
4+
---
5+
6+
<Info>
7+
This feature is available on all Portkey [plans](https://portkey.ai/pricing).
8+
</Info>
9+
10+
## Circuit Breaker Config Schema
11+
12+
Each `strategy` in a config may define a `cb_config` with the following fields:
13+
14+
- **failure_threshold**: Number of failures after which the circuit opens.
15+
- **failure_threshold_percentage** *(optional)*: Percentage failure rate to trip the circuit.
16+
- **cooldown_interval**: Time (in milliseconds) to wait before allowing retries. A minimum of 30 seconds is enforced.
17+
- **failure_status_codes** *(optional)*: Specific HTTP status codes considered as failures. If not provided, all status codes >500 are considered as failures.
18+
- **minimum_requests** *(optional)*: Minimum number of requests required before failure rate is evaluated.
19+
20+
<Info>
21+
- Strategies can inherit `cb_config` from a parent if not set at their level.
22+
- Targets inherit `cb_config` from their parent strategy.
23+
</Info>
24+
25+
<Warning>
26+
Strategies using the `conditional` mode are **not considered** in circuit breaker logic.
27+
</Warning>
28+
29+
## Example Config
30+
31+
```json
32+
{
33+
"retry": {
34+
"attempts": 3
35+
},
36+
"strategy": {
37+
"mode": "fallback",
38+
"cb_config": {
39+
"failure_threshold_percentage": 20,
40+
"minimum_requests": 10,
41+
"cooldown_interval": 60000,
42+
"failure_status_codes": [
43+
401,
44+
429,
45+
500,
46+
]
47+
}
48+
},
49+
"targets": [
50+
{
51+
"virtual_key": "virtual-key-1"
52+
},
53+
{
54+
"virtual_key": "virtual-key-2"
55+
}
56+
]
57+
}
58+
```
59+
60+
## Circuit State Evaluation
61+
62+
Circuit breaker logic is evaluated per strategy path. It tracks:
63+
64+
- Number of failures and successes.
65+
- The time of the first failure.
66+
- Whether the total requests meet the `minimum_requests` threshold and calculates failure rate as percentage
67+
68+
The circuit is **OPEN**(unhealthy) if:
69+
- The failure count exceeds the `failure_threshold`, or
70+
- The failure rate exceeds `failure_threshold_percentage`.
71+
72+
Once **OPEN**, requests to the affected targets are blocked until the `cooldown_interval` has passed.
73+
74+
When the cooldown period ends, the circuit is **CLOSED** automatically, and failure counters are reset.
75+
76+
## Runtime Behavior
77+
78+
- At each strategy level, the circuit breaker evaluates the status of all sibling targets.
79+
- Unhealthy targets (circuit OPEN) are removed from the target list before strategy execution.
80+
- If no healthy targets remain, circuit breaker is bypassed and all targets are used for routing.
81+
82+
```mermaid
83+
flowchart TD
84+
A[User sends request] --> B[Gateway selects a strategy]
85+
B --> C[Strategy has multiple targets]
86+
C --> D{All targets OPEN?}
87+
D -->|Yes| E[Ignore circuit breakers]
88+
E --> F[Use all targets for routing]
89+
D -->|No| G["Use only CLOSED (healthy) targets"]
90+
F --> H[Route to best target]
91+
G --> H
92+
H --> I[Process target]
93+
```

0 commit comments

Comments
 (0)