Building Circuit Breakers with Step Functions
This lesson shows how to build a circuit breaker with AWS Step Functions, which stops calling a dependency that is down for minutes instead of wasting time, tokens and capacity on retries.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the closed, open and half-open states.
- Store circuit state in DynamoDB for a Step Functions workflow.
- Choose thresholds and a narrow scope for each circuit.
The circuit breaker pattern
A circuit breaker has three states. In the closed state, calls pass through normally and failures are counted. When failures exceed a threshold within a time window, the circuit opens: calls are not attempted, and a fallback response is returned immediately. After a cool-down period, the circuit becomes half-open and allows a trial call; success closes the circuit, failure opens it again. The pattern protects users from long waits, protects the failing dependency from load and protects budgets from wasted tokens.
The following diagram shows the three states and the transitions between them.

Circuit breaker states
Building it with Step Functions and DynamoDB
In AWS Step Functions, the circuit state is stored outside the workflow so all executions share it, typically in a DynamoDB item per dependency (for example per model or per tool) with the state, failure count and an expiry time. Each execution starts with a GetItem task (a direct DynamoDB service integration, no Lambda needed) and a Choice state. If the circuit is open, the workflow goes straight to a fallback state. If it is closed, or no item exists yet for the dependency, a Task state invokes the model with the Bedrock service integration or a Lambda function. The Choice state checks that the state attribute is present before comparing it, because a Choice rule that reads a missing path fails the execution.
The model task has a Retry field for transient errors and a Catch field. On failure after retries, the Catch path increments the failure count with an UpdateItem and opens the circuit when the threshold is reached, setting an expiry time. A DynamoDB time to live attribute or a timestamp comparison returns the circuit to half-open after the cool-down. On success, the workflow resets the counter.
"CheckCircuit": {
"Type": "Task", "Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {"TableName": "circuit-state", "Key": {"dependency": {"S": "summarizer-model"}}},
"ResultPath": "$.circuit", "Next": "IsOpen"
},
"IsOpen": {
"Type": "Choice",
"Choices": [{"And": [{"Variable": "$.circuit.Item.state.S", "IsPresent": true},
{"Variable": "$.circuit.Item.state.S", "StringEquals": "OPEN"}],
"Next": "Fallback"}],
"Default": "InvokeModel"
},
"InvokeModel": {
"Type": "Task", "Resource": "arn:aws:states:::bedrock:invokeModel",
"Parameters": {"ModelId": "amazon.nova-lite-v1:0", "Body": {"messages.$": "$.messages"}},
"Retry": [{"ErrorEquals": ["Bedrock.ThrottlingException"], "IntervalSeconds": 2, "BackoffRate": 2, "MaxAttempts": 3, "JitterStrategy": "FULL"}],
"Catch": [{"ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "RecordFailure"}],
"Next": "ResetCircuit"
}The following diagram shows the workflow states that implement the breaker.

A circuit breaker workflow
Choosing thresholds and scope
A circuit should have a narrow scope: one per model, per tool or per downstream API, so one failing dependency does not stop unrelated work. Thresholds balance sensitivity and stability; for example, open after five failures in one minute and stay open for 60 seconds. Only failures that indicate an unhealthy dependency should count: throttling and service errors count, but validation errors caused by a bad request do not. Every state change should be logged and published as a metric so operators see when circuits open.
Step Functions suits this pattern because the visual workflow, execution history and built-in retries make the behaviour easy to audit. The same logic can be implemented in application code or a GenAI gateway when Step Functions is not on the request path. The following table summarises the states.
| State | Behaviour | Transition |
|---|---|---|
| Closed | Calls pass through; failures counted | Opens when failures reach the threshold |
| Open | Calls skipped; fallback returned | Becomes half-open after the cool-down |
| Half-open | One trial call allowed | Closes on success; reopens on failure |
A failing tool in an agent workflow
An order-support workflow calls an FM and then an inventory API. When the inventory API is down, each execution waits through retries for 40 seconds and the model is still invoked, wasting tokens. The company wants fast, cheap failure handling.
Adding more retries makes it worse. A circuit breaker for the inventory API, with state in DynamoDB checked at the start of the workflow, lets executions skip the API and the model call while it is down and return a message that stock information is temporarily unavailable, with a metric and alarm on the circuit opening.
Why is circuit state stored in DynamoDB rather than in the state machine execution?
Show answer
So that every execution shares it. Once the circuit opens, all new executions skip the failing dependency instead of each discovering the failure separately.
Exam Tips
Remember for the exam
- Repeated failures of a dependency: circuit breaker, not more retries.
- Store circuit state in DynamoDB so all executions share it.
- Use Retry for transient errors and Catch to record failures and open the circuit.
- Count only failures that indicate an unhealthy dependency.