Published · Updated
Preventing Runaway Bedrock Agent Loops: Token Budgets, Circuit Breakers and Signed Stops
Bound agent work before model calls, measure asynchronous containment and design a separately authorized Nerve alert and signed-stop path.
A recursion limit in a prompt is not a spending control. The execution layer must bound steps, retries, concurrency, time and token exposure before it dispatches another model or tool call. Billing alerts and an operator stop are additional response paths, with measurable delay and a remaining in-flight cost.
| Decision input | Collect or demonstrate | Acceptance evidence | Unresolved condition |
|---|---|---|---|
| Work budget | Allowed models, token categories and conservative rates | Rejected over-budget reservation | Unknown price or uncontrolled direct caller |
| Execution bound | Max steps, retries, deadline and concurrency | Recursive fixture stops at the configured boundary | Child task escapes the shared budget |
| Emergency response | Scoped stop authority and operating owner | Timed test with actual receiver and target | Alert delivered but no authorized stop |
| Recovery | Reviewed release of the stopped workload | Approval, audit receipt and bounded restart | Stale or replayed command |
Architecture and data flow
Authenticated job -> atomic quota reservation -> bounded model/tool dispatch
| |
reject or stop reconcile usage
|
minimal incident signal -> operator
|
separately authorized bounded stop
Reserve before invoking
Keep one trusted budget record for the job and its descendants. Before each dispatch, reserve a conservative upper bound for the selected model, input, permitted output and other applicable meters. Enforce per-job and tenant limits atomically with concurrency limits and idempotent request keys. Unknown model/rate or unavailable budget storage should follow an explicit failure policy rather than silently allowing unlimited work.
admit(job, request):
require job.active and now < job.deadline
require job.steps < max_steps and active_calls < concurrency_limit
estimate = upper_bound(selected_model, input, max_output, other_meters)
atomically reserve estimate using request_id in shared budget
if no reservation: reject before provider request
dispatch once, bounded retries using the same accounting rules
reconcile actual usage when known; retain uncertain reservations
This is an admission-control design, not runnable production code or a provider-enforced dollar cap. Token estimation, tool costs, cache categories and streaming cancellation can leave uncertainty. Releasing a reservation immediately after a timeout can allow a duplicate request while the first is still running. Keep unknown outcomes reserved until bounded reconciliation resolves them; avoid double-spending reservations under concurrent workers.
Set maximum output tokens, execution deadline, step count, retry count and queue depth independently. Every recursive child must inherit the same accounting domain or a reserved sub-budget. For credential abuse outside the application path, combine least-privilege roles, quota controls, detection and response; an application wrapper does not constrain an attacker using a different valid credential route.
Measure asynchronous containment separately
AWS Budgets guidance explains that billing updates and alerting have delay. Budgets is not an inline admission gate. CloudWatch anomaly detection, EventBridge automation and emergency IAM policy changes likewise have observation, evaluation and propagation delays. Measure the actual chain and the cost of calls already in flight.
A WAF rule applies to a supported attached ingress resource. It does not universally intercept direct Bedrock SDK calls or every internal agent/tool path. Use it for the application boundary it actually protects, then test bypass routes. A deny policy may contain new requests after propagation; do not describe it as an instantaneous hardware fuse or assume it cancels every existing stream.
Add a separately authorized Nerve response path
The Nerve encrypted alerting security model distinguishes the send-only signal credential from an agent credential and signed command execution. A sender DSN cannot stop an agent. Send a minimal incident identifier, severity and approved review link; keep prompts, keys, CUI and detailed logs in the approved source system.
A proposed signed stop integration requires a receiver and a bounded wrapper on the intended host or control service. Bind the command to its target, permitted action, request/incident ID, expiry and replay protection. The wrapper must authorize the action, record an idempotent outcome and reject arbitrary shell text. Alert transport, signature validation, target authorization and the actual stop are separate acceptance checks.
This article describes an integration design based on the documented security model. It does not establish that a particular mobile receiver or stop wrapper is installed or verified. Before operational use, test actual notification receipt, acknowledgement, expired/replayed command rejection, target isolation, stop latency and a fallback operator route. Loss of notification delivery must not disable the inline execution budget.
Production Pitfalls
Test a recursive tool fixture, simultaneous workers exhausting one budget, provider timeout after dispatch, unknown usage, a stale price record, unavailable quota storage and a stop arriving after completion. Record detection, dispatch rejection, remaining active requests and recovery as separate times. A successful alert without a bounded stop receipt is an incomplete response test.
The existing CloudTrail inspection example and scope manifest remain a bounded starting observation. They do not install an inline budget or stop workload credentials. Pair them with the serving cost model and MCP identity evidence.
An Evidence Sprint at $9,500–$14,500 can scope the control, evidence and response tests; hardening starts at $2,950. A Nerve integration is agreed explicitly with the target, approval policy and acceptance criteria, not implied by a link or an alert arriving.
Continue with the AI infrastructure topic, the AI / LLM observability and MCP security kit and the source documentation for the retained inspection.
Frequently asked questions
Can a budget alarm guarantee no agent overspend?
No. Billing and alerting are asynchronous. Reserve work before dispatch, enforce execution limits and measure the exposure of calls already in flight.
Does WAF stop all Bedrock or MCP calls?
No. WAF controls supported attached ingress resources. Direct SDK requests and internal paths need their own authorization, admission and containment controls.
Can a Nerve sender DSN execute a kill switch?
No. The documented sender is send-only. A separately authorized agent, signed bounded command and tested target wrapper are needed for a stop integration.
Continue reading
- Bedrock in FedRAMP and NIST 800-53 Environments: Guardrails and Audit Evidence
Map Bedrock network, identity, Guardrails and logging evidence to owned NIST controls, with authorized-scope checks and negative fixtures.
- Bedrock Knowledge Base Costs: OpenSearch Serverless OCUs vs Aurora pgvector
Compare classic and NextGen OpenSearch capacity with Aurora pgvector, then stage a Bedrock knowledge-base migration with retrieval and rollback checks.
- Bedrock Model Invocation Logging: Direct S3, Athena and CloudWatch Cost Boundaries
Use a reviewed S3-only Bedrock invocation-log route, verify endpoint coverage and build an auditable Athena pipeline without assuming universal CloudWatch rates.
Written by
Tymur Chmeruk
Cloud Security & Infrastructure Engineer · Baltimore–Washington Metro · [email protected]