iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🫠

Integrating Claude Agent SDK (Python) with Amazon Bedrock

に公開

Introduction

In this article, I will introduce the steps to use the Claude Agent SDK (Python version) in integration with Amazon Bedrock.
A benefit of integrating with Amazon Bedrock is that if you have an AWS environment, you can use it with AWS's pay-as-you-go billing.

■ Claude Agent SDK

The Claude Agent SDK is a software development kit for building AI agents developed by Anthropic.
While Claude Code runs AI agents on the CLI, the Claude Agent SDK allows you to define and execute AI agents programmatically.
https://docs.anthropic.com/en/docs/claude-code/sdk/sdk-python
https://github.com/anthropics/claude-agent-sdk-python

1. Prerequisites

■ Issuing an Amazon Bedrock API Key

The steps introduced here use an Amazon Bedrock API key.
Issue an API key from the Amazon Bedrock console in advance.
*Note: If the LLM model you want to use has access controls, please ensure access is granted before proceeding.

■ Installing claude-agent-sdk

Install the claude-agent-sdk using the following command.

pip install claude-agent-sdk

2. Integrating Claude Agent SDK (Python version) with Amazon Bedrock

This time, we will use the sample program provided in the official documentation as a base.

import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def main():
    options = ClaudeAgentOptions(
        system_prompt="You are an expert Python developer",
        permission_mode='acceptEdits',
        cwd="/home/user/project"
    )

    async for message in query(
        prompt="Create a Python web server",
        options=options
    ):
        print(message)


asyncio.run(main())

■ Defining environment variables required for Amazon Bedrock integration

Next, we will define the environment variables required for integration with Amazon Bedrock in the source code mentioned above. The required environment variables are as follows:

Environment Variable Value Description
CLAUDE_CODE_USE_BEDROCK 1 Explicitly specifies that Amazon Bedrock will be used.
AWS_BEARER_TOKEN_BEDROCK API Key Sets the Amazon Bedrock API key. Be sure not to hardcode it in actual production.
AWS_REGION ap-northeast-1 The region of the LLM model to be used. Here, we use the Tokyo region.

We will apply these environment variables to the previous source code and make some minor adjustments. In claude-agent-sdk, you can define environment variables in the env argument of ClaudeAgentOptions.

sample.py
import asyncio
import os

from claude_agent_sdk import ClaudeAgentOptions, query

# Get the current directory
CWD = os.path.dirname(os.path.abspath(__file__))


async def main():
    options = ClaudeAgentOptions(
        system_prompt="You are an excellent system engineer. Please respond in Japanese.",
        permission_mode="acceptEdits",
        cwd=CWD,
        model="sonnet",
        env={ 
            "CLAUDE_CODE_USE_BEDROCK": "1",
            "AWS_BEARER_TOKEN_BEDROCK": "Amazon Bedrock API Key",
            "AWS_REGION": "ap-northeast-1",
        },
    )

    async for message in query(
        prompt="Please create a README for the current project.",
        options=options,
    ):
        print(message)


asyncio.run(main())

■ Trying out the Claude Agent SDK

Let's try running the Claude Agent SDK.

python sample.py

When executed, the logs for the Claude Agent SDK's processing status (print(message)) will be output to the console. After a while, a README.md file like the one below was created.

Discussion