Tutorials

How to Scale Claude Agents on AWS Bedrock

A technical breakdown of Anthropic's financial and compute strategy, paired with a practical tutorial on implementing high-throughput prompt caching for

AM
Alfian Majid
//6 min read
How to Scale Claude Agents on AWS Bedrock

Anthropic is betting its entire financial future on recursive self-improvement and high-margin enterprise contracts, locking in over $265 billion in non-cancelable compute commitments through 2045. When you examine the recent S-1 filings, the business reality becomes stark: generating $16.3 billion in revenue against massive fixed costs means your infrastructure cannot afford wasted tokens or inefficient inference loops. If you are deploying Claude agents at scale today, you need to manage your API spend and latency ruthlessly using architecture patterns like prompt caching on cloud providers like Amazon Bedrock.

How Does Prompt Caching Reduce Enterprise Inference Costs?

Running complex coding agents or cybersecurity analyzers means passing thousands of lines of system instructions, codebase context, and schema definitions into every single API call. Without prompt caching, you pay full input token price for that identical context on every turn of the conversation. Amazon Bedrock and Anthropic's native API allow you to cache static blocks of text-such as entire codebases or API documentation-so subsequent requests only pay a fraction of the cost for cache reads.

Consider an enterprise setup running Claude Opus 5.5 for automated code refactoring. If your system prompt and reference documentation total 50,000 tokens, sending that raw payload across 10,000 daily requests burns 500 million input tokens. By marking static blocks with cache control headers, cache write operations cost slightly more once, but cache reads drop by up to 90 percent. This economic reality explains how companies maintain a 900 percent net revenue retention rate without letting compute costs eat their margins alive.

What Environment Setup and Dependencies Do You Need?

Before writing the agent loop, verify your environment meets the strict requirements needed to interface with Amazon Bedrock using the modern SDK. You will need Python 3.11 or higher, an active AWS account with Bedrock model access enabled for Claude models, and the updated boto3 library.

  • Python Version: 3.11.4 or higher
  • AWS SDK: boto3>=1.38.0 and botocore>=1.31.0
  • Required Environment Variables: AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY
  • Model ID: anthropic.claude-sonnet-5-20260519-v1:0

Install the required packages in a dedicated virtual environment to prevent dependency conflicts with legacy machine learning libraries:

python3 -m venv venv
source venv/bin/activate
pip install boto3 botocore pydantic tenacity

How to Implement Prompt Caching in Python for Bedrock

To tap prompt caching on Amazon Bedrock, your message payload must structure the system prompt or reference documents as content blocks containing a cache_control ephemeral block. Below is a production-grade Python script that initializes a client, injects a large cached system context, and executes a multi-turn agent interaction loop.

import json
import boto3
from botocore.exceptions import ClientError

class BedrockClaudeAgent:
    def __init__(self, region="us-east-1"):
        self.client = boto3.client("bedrock-runtime", region_name=region)
        self.model_id = "anthropic.claude-sonnet-5-20260519-v1:0"

    def invoke_with_cache(self, static_context: str, user_prompt: str) -> str:
        try:
            response = self.client.invoke_model(
                modelId=self.model_id,
                contentType="application/json",
                accept="application/json",
                body=json.dumps({
                    "anthropic_version": "bedrock-2023-05-31",
                    "max_tokens": 4096,
                    "system": [
                        {
                            "type": "text",
                            "text": static_context,
                            "cache_control": {"type": "ephemeral"}
                        }
                    ],
                    "messages": [
                        {
                            "role": "user",
                            "content": user_prompt
                        }
                    ]
                })
            )
            response_body = json.loads(response.get("body").read())
            return response_body["content"][0]["text"]
        except ClientError as e:
            print(f"AWS Bedrock API Error: {e.response['Error']['Message']}")
            raise

if __name__ == "__main__()":
    agent = BedrockClaudeAgent()
    large_codebase_context = "# SYSTEM REPO CONTEXT\n" + ("def legacy_module(): pass\n" * 2000)
    result = agent.invoke_with_cache(large_codebase_context, "Refactor legacy_module to use async IO.")
    print(result)

What Terminal Output Should Your Agent Generate?

When you execute the caching script against Amazon Bedrock, the initial request writes the cache blocks to ephemeral storage, resulting in a slightly higher token usage report in the AWS CloudWatch logs. Subsequent requests within the TTL window read directly from the cache. Here is what a successful execution and metric verification look like in your terminal.

$ python agent_runner.py
[INFO] 2026-07-07 14:22:01 - Initializing Bedrock runtime client for us-east-1
[INFO] 2026-07-07 14:22:02 - Payload constructed. System context size: 54,210 characters.
[INFO] 2026-07-07 14:22:04 - Model invocation successful. Status 200.
[INFO] 2026-07-07 14:22:04 - Token Metrics -> Input: 12,400 | Cache Read: 12,000 | Output: 342
Refactored legacy_module into an asynchronous handler using asyncio loops...

How to Fix Common Bedrock and Caching Errors?

Deploying high-throughput LLM pipelines at scale exposes subtle API quirks and infrastructure limits. Here are three common failure modes encountered when scaling Claude agents on AWS Bedrock, along with their exact terminal fixes.

  • ValidationException: Cache control blocks not supported for model version
    Root Cause: You are passing the cache_control dictionary to an older model ID or an endpoint that has not rolled out caching support.
    Fix: Update your modelId string to point explicitly to the latest version like anthropic.claude-sonnet-5-20260519-v1:0.
  • ThrottlingException: Rate exceeded for model invocation
    Root Cause: Your account hit the default AWS service quotas for tokens per minute (TPM) or requests per minute (RPM).
    Fix: Submit a quota increase request via the AWS Service Quotas console for Bedrock, and implement exponential backoff retry logic using the tenacity library in Python.
  • AccessDeniedException: User is not authorized to perform: bedrock:InvokeModel
    Root Cause: The IAM role or user profile running the script lacks the necessary permissions or model access has not been requested in the Bedrock console.
    Fix: Navigate to the Amazon Bedrock console, click Model Access, enable Claude 5 models, and attach an IAM policy granting bedrock:InvokeModel on the specific resource ARN.

Are There Production Security and Rate Limit Considerations?

Managing multi-million token pipelines requires strict adherence to security best practices and cost governance. Never hardcode AWS credentials or Anthropic API tokens inside your application source code. Instead, inject secrets via AWS Secrets Manager or secure container environment variables running inside ECS or EKS clusters.

On top of that, monitor cache hit ratios rigorously through Amazon CloudWatch metrics. If your cache expiration TTL lapses between requests due to idle agent states, your application will continuously pay cache-write penalties instead of enjoying cache-read discounts. Structure your agent polling intervals to fire within the 5-minute to 1-hour cache retention windows provided by the infrastructure.

What Should You Build Next with Your Agent Infrastructure?

Once you have established a reliable caching layer for single-turn code analysis, you can expand your architecture into a multi-agent swarm. Integrate frameworks like LangGraph or CrewAI to orchestrate specialized Claude agents where one agent handles security scanning, another manages unit test generation, and a third handles documentation updates. Because all agents share the same cached system codebase context, your overall token expenditure remains flat even as your agent team scales horizontally.

Bottom Line

Stop sending unoptimized, raw text payloads to your LLM endpoints if you want your AI engineering projects to survive financial scrutiny. Implement prompt caching on Bedrock immediately, monitor your cache hit metrics daily, and structure your system architecture to assume compute resources cost real money. Use it, optimize it, or watch your cloud bills consume your operating margins.

Share this article

Alfian Majid
Alfian Majid/Founder & Editor-in-Chief

Solo developer and blogger from Indonesia. Runs CogitoDaily as a passion project - covering AI news, testing tools, and writing guides. Background in web development and game tech. When not writing about AI, you'll find me deep in anime or gaming.