Near Real-Time Per-User Cost Controls for Amazon Bedrock
Deploy serverless per-user spending limits for Amazon Bedrock with ~30–60 second enforcement. This CDK sample uses model invocation logging, CloudWatch subscription filters, and session-scoped IAM deny policies to track running costs — including prompt cache tokens — and automatically block users who exceed configurable daily or monthly budgets. Designed for enterprises scaling AI coding assistants and agentic workloads who need cost governance faster than AWS Budgets can provide.
Enterprises adopting AI coding assistants and agentic workloads on Amazon Bedrock are facing a familiar challenge in a new domain: how do you give developers the freedom to move fast while maintaining financial governance? For organizations piloting tools like Claude Code on Bedrock across engineering teams, the answer isn't obvious — and the consequences of getting it wrong can be measured in thousands of dollars within a single business day.
Today, Amazon Bedrock doesn't offer a native mechanism for per-user hard spending caps. AWS Budgets provides account-level cost alerting and can trigger enforcement actions, but cost data can lag 8–12 hours behind actual usage. Service Quotas control throughput (tokens per minute), but they don't translate to dollar-based limits. This leaves a gap: a single developer running heavy agentic workloads — recursive tool calls, large cached contexts, multi-step code generation — can accumulate significant uncontrolled costs before any existing guardrail fires.
This concern is consistently cited by enterprise FinOps and platform teams as a blocker to promoting AI developer tooling from sandbox into production. They need controls that enforce within seconds, not hours.
In this post, we introduce a serverless solution that closes this gap — enforcing per-user dollar-based spending limits on Amazon Bedrock in near real-time (~30–60 seconds) using model invocation logging, Amazon CloudWatch, AWS Lambda, Amazon DynamoDB, and AWS Identity and Access Management (IAM). The solution is available as a deployable CDK sample at aws-samples/sample-bedrock-cost-controls .
Solution overview
This solution is a detective control with automated response. It processes Bedrock model invocation logs as they arrive in CloudWatch Logs, calculates a running per-user dollar cost (including prompt cache reads and writes), and automatically attaches a session-scoped IAM deny policy when a configurable budget threshold is breached. Other users sharing the same IAM role are unaffected — enforcement is scoped to the individual session.
Key characteristics:
- Enforcement latency: ~30–60 seconds end-to-end (vs. 8–12 hours with AWS Budgets)
- Granularity: Per-user and per-team dollar-based limits
- Pricing accuracy: Meters all four token price components — input, output, cache read, and cache write — and refreshes rates automatically from the AWS Price List API
- Blast radius: Session-scoped deny policies mean one user hitting their limit doesn't disrupt others
It's important to understand what this solution does not do:
- It is not a preventive control. There is a 30–60 second reaction window where spend can overshoot before the deny policy lands.
- It does not cover the
bedrock-mantleendpoint (which serves OpenAI-compatible and Anthropic-compatible APIs). Model invocation logging only covers thebedrock-runtimeendpoint.
- It does not replace a purpose-built AI gateway for organizations that need hard real-time enforcement.
Architecture
The following diagram shows the end-to-end architecture:

How it works:
- An IAM principal invokes a Bedrock model on the
bedrock-runtimeendpoint — this could be a developer using Claude Code, an application calling the Converse API, or a CI pipeline running automated code reviews.
- Amazon Bedrock delivers the model invocation log to a CloudWatch Logs log group. Delivery latency is typically 5–30 seconds. The log contains the caller's identity ARN, the model ID, and full token usage including prompt cache breakdowns.
- A CloudWatch Logs subscription filter asynchronously triggers the Cost Enforcement Lambda on each log event.
- The Cost Enforcement Lambda performs the following steps:
- Parses the invocation log and extracts the IAM principal identity, model ID, and token counts across all four components (input, output, cache read, cache write)
- Looks up per-model, per-component pricing from a DynamoDB Pricing Table
- Calculates the dollar cost for the invocation
- Atomically increments the user's running spend totals in a DynamoDB Usage Table (daily and monthly accumulators)
- Compares the updated totals against the user's configured budget thresholds from a DynamoDB Budget Config Table
- If spend exceeds the threshold: attaches a session-scoped IAM inline deny policy that blocks
bedrock:InvokeModelandbedrock:InvokeModelWithResponseStream,bedrock:Converse, andbedrock:ConverseStreamfor that user only
- Publishes alerts to an SNS topic at configurable warning thresholds (default: 50% and 80%)
- A Budget Reset Lambda runs on an EventBridge schedule (daily and monthly), resetting the accumulators and removing any deny policies for users whose budget period has turned over.
- A Pricing Sync Lambda runs daily, refreshing token and cache pricing from the AWS Price List API so rates stay current without manual intervention.
Key design decisions
Prompt cache metering
This is the single most important accuracy consideration and the reason for the four-component cost model. The invocation log's
inputTokenCount field excludes tokens read from or written to the prompt cache. Per AWS documentation:total input tokens = inputTokens + cacheReadInputTokens + cacheWriteInputTokensMetering only the headline field therefore measures only the residual — for a coding assistant with a large cached system prompt, this can undercount actual cost by 3–5x. The solution extracts cache token counts from
cacheDetails (Converse API) or cache_creation / cache_read (Anthropic native format) in the response body, and prices each component at its documented rate.Session-scoped deny policies
In many enterprise Bedrock deployments — particularly those using IAM Identity Center with SSO — multiple developers may share the same IAM role, differentiated only by their session name. The solution attaches deny policies scoped to the specific role session (
aws:userid condition key), ensuring that one developer exceeding their budget doesn't block their colleagues.Automated pricing
Rather than maintaining a static pricing table that goes stale when new models launch or rates change, the solution includes a Pricing Sync Lambda that queries the AWS Price List API daily. It resolves both standard token rates and cache-specific multipliers, tagged with the pricing source so you can audit which rates are derived vs. API-sourced.
Idempotency and deduplication
CloudWatch Logs subscription filters provide at-least-once delivery. The solution uses the invocation log's
requestId as a deduplication key (via a DynamoDB table with TTL) to prevent double-counting if the same event is delivered twice.Scope and limitations
Before deploying, be aware of the following constraints:
Limitation | Effect |
|---|---|
bedrock-mantle endpoint | Not captured by invocation logging. Traffic through OpenAI/Anthropic-compatible APIs is invisible to this solution. Recommend restricting via SCP if metering is critical. |
Image and video models | No token counts in the log — priced at $0.00. |
Provisioned Throughput | Billed per model-unit-hour, not per token. |
30–60 second reaction window | A single large-context streaming request can overshoot before the deny lands. |
Unmatched model IDs | New model IDs or application inference profile ARNs fall back to Sonnet-tier pricing (conservative). |
For organizations needing hard preventive enforcement (zero-latency, inline blocking) specifically for Claude Code, the Claude Apps Gateway for AWS provides built-in per-user dollar caps with real-time request rejection. This solution is complementary — it covers broader Bedrock usage across all models and API surfaces while the Gateway handles Claude-specific developer tooling.
Getting started
Prerequisites
- AWS CDK v2 installed and bootstrapped in your target account/region
- Python 3.12+
- Bedrock model invocation logging enabled with CloudWatch Logs as a destination
- Access to the Bedrock models you want to meter
Deployment
git clone https://github.com/aws-samples/sample-bedrock-cost-controls.git
cd sample-bedrock-cost-controls
pip install -r requirements.txt
cdk deployDeployment takes approximately 2 minutes and creates all required resources (Lambda functions, DynamoDB tables, subscription filter, EventBridge rules, and SNS topic).
Testing
The repository includes a Jupyter notebook that walks you through a complete demo — configuring a test user's budget, generating Bedrock invocations, observing the cost accumulator incrementing in real time, and verifying that the deny policy is attached when the threshold is exceeded — all without impacting production users.
Cleanup
To remove all resources created by this solution:
cdk destroyNote that any IAM deny policies attached by the solution during enforcement should be removed before destroying the stack. The Budget Reset Lambda handles this automatically on its next scheduled run, or you can invoke it manually.
Conclusion
While native per-user budget enforcement for Amazon Bedrock is on the AWS roadmap (via AgentCore Gateway), enterprises scaling AI developer tooling today need controls that work now. This solution bridges that gap — reducing enforcement latency from hours to seconds and giving FinOps teams the confidence to expand Bedrock access beyond small pilot groups.
The architecture is intentionally simple: three Lambda functions, four DynamoDB tables, one subscription filter. It deploys in minutes, costs less than $50/month to operate for 100 developers, and the worst-case overshoot on a budget breach is approximately $1–2 (one additional request in the latency window).
Get started with the sample at aws-samples/sample-bedrock-cost-controls and let us know how it works for your organization.
Darren Roback is a Senior Solutions Architect with Amazon Web Services based in St. Louis, Missouri. He has a background in Data Analytics, Generative AI, and IoT. At AWS, Darren partners with energy and utility customers to help them solve business challenges with AWS technology.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article