DEV Community

Xinzhi Sherry Zhu
Xinzhi Sherry Zhu

Posted on • Originally published at zenn.dev

Using Claude Sonnet 4's 1M Context Window on Amazon Bedrock

This is the English translation of my original Japanese article: https://zenn.dev/arvehisa/articles/anthropic-claude-1m-context-window-on-bedrock

On August 13, 2025, Claude Sonnet 4 added support for a 1M token context window. This feature is also available on Amazon Bedrock.

Reference links:
https://www.anthropic.com/news/1m-context
https://aws.amazon.com/jp/about-aws/whats-new/2025/08/anthropic-claude-sonnet-bedrock-expanded-context-window/
https://docs.anthropic.com/en/api/claude-on-amazon-bedrock#1m-token-context-window
https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html

Important Notes

  1. This feature is currently available only in US Oregon, Virginia, and Ohio regions on Amazon Bedrock.
  2. Be aware that using the 1M Context Window increases the cost both on Amazon Bedrock and Anthropic API.
Prompt Size Input Output
≤ 200K tokens $3 / MTok $15 / MTok
> 200K tokens $6 / MTok $22.50 / MTok

Python SDK (Boto3) + Converse API Sample Code

import boto3

# Initialize Bedrock client
bedrock = boto3.client(
    service_name='bedrock-runtime',
    region_name='us-west-2'
)

# Currently only Claude Sonnet 4 supports 1M Context Window
model_id = "us.anthropic.claude-sonnet-4-20250514-v1:0"

# Prepare large text
large_text = "Sample text " * 50000

# Build request
request_body = {
    "messages": [
        {
            "role": "user",
            "content": [{"text": f"Please summarize this text: {large_text}"}]
        }
    ],
    "additionalModelRequestFields": {
        "anthropic_beta": ["context-1m-2025-08-07"]  # Enable 1M context
    },
    "inferenceConfig": {
        "maxTokens": 500
    }
}

# API call
response = bedrock.converse(
    modelId=model_id,
    **request_body
)

# Display results
print(f"Input tokens: {response['usage']['inputTokens']:,}")
print(f"Output tokens: {response['usage']['outputTokens']:,}")
print(response['output']['message']['content'][0]['text'])
Enter fullscreen mode Exit fullscreen mode

TypeScript SDK + Converse API Sample Code

import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";

// Initialize Bedrock client
const bedrock = new BedrockRuntimeClient({
    region: "us-west-2"
});

// Currently only Claude Sonnet 4 supports 1M Context Window
const modelId = "us.anthropic.claude-sonnet-4-20250514-v1:0";

async function main() {
    try {
        // Prepare large text
        const largeText = "Sample text ".repeat(50000);

        // Build request
        const requestBody = {
            modelId: modelId,
            messages: [
                {
                    role: "user" as const,
                    content: [
                        {
                            text: `Please summarize this text: ${largeText}`
                        }
                    ]
                }
            ],
            additionalModelRequestFields: {
                anthropic_beta: ["context-1m-2025-08-07"]  // Enable 1M context
            },
            inferenceConfig: {
                maxTokens: 500
            }
        };

        // API call
        const command = new ConverseCommand(requestBody);
        const response = await bedrock.send(command);

        // Display results
        console.log(`Input tokens: ${response.usage?.inputTokens?.toLocaleString()}`);
        console.log(`Output tokens: ${response.usage?.outputTokens?.toLocaleString()}`);
        console.log(response.output?.message?.content?.[0]?.text);

    } catch (error) {
        console.error("Error:", error);
    }
}

// Execute
if (require.main === module) {
    main();
}

export { bedrock, modelId };
Enter fullscreen mode Exit fullscreen mode

TypeScript SDK + InvokeModel Sample Code

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";

// Initialize Bedrock client
const bedrock = new BedrockRuntimeClient({
    region: "us-west-2"
});

// Currently only Claude Sonnet 4 supports 1M Context Window
const modelId = "us.anthropic.claude-sonnet-4-20250514-v1:0";

async function main() {
    try {
        // Prepare large text
        const largeText = "Sample text ".repeat(50000);

        // Build request
        const requestBody = {
            anthropic_version: "bedrock-2023-05-31",
            anthropic_beta: ["context-1m-2025-08-07"], // Enable 1M context
            max_tokens: 500,
            messages: [
                {
                    role: "user",
                    content: [
                        {
                            type: "text",
                            text: `Please summarize this text: ${largeText}`
                        }
                    ]
                }
            ]
        };

        // API call
        const command = new InvokeModelCommand({
            modelId: modelId,
            contentType: "application/json",
            accept: "application/json",
            body: JSON.stringify(requestBody)
        });

        const response = await bedrock.send(command);
        const responseBody = JSON.parse(new TextDecoder().decode(response.body));

        // Display results
        console.log(`Input tokens: ${responseBody.usage.input_tokens.toLocaleString()}`);
        console.log(`Output tokens: ${responseBody.usage.output_tokens.toLocaleString()}`);
        console.log(responseBody.content[0].text);

    } catch (error) {
        console.error("Error:", error);
    }
}

// Execute
if (require.main === module) {
    main();
}

export { bedrock, modelId };
Enter fullscreen mode Exit fullscreen mode

Example Execution Results

Input tokens: 400,018
Output tokens: 184
I apologize, but the provided text consists only of the phrase "Sample text" repeated many times...
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. Amazon Bedrock only supports US regions, so you may get unsupported errors depending on the region you use.
  2. Claude Sonnet 4 only supports cross-region inference with instance profiles, and as of August 15, 2025, it only works when specifying the model ID us.anthropic.claude-sonnet-4-20250514-v1:0. In this case, you need to enable model access for all regions that might be used for cross-region inference, otherwise you may get the error You don't have access to the model with the specified model ID.
  3. When you get the error Too many tokens, please wait before trying again., this is throttling, not a context window issue. Please check your AWS Account Quota.

Summary

Two key points:

  1. Specify us.anthropic.claude-sonnet-4-20250514-v1:0 as the model ID
  2. Add anthropic_beta: ["context-1m-2025-08-07"] to additionalModelRequestFields

This enables you to use up to 1M tokens of context.

Top comments (0)