Was this page helpful?
このガイドでは、エージェントの作成、環境のセットアップ、セッションの開始、エージェントレスポンスのストリーミングについて説明します。
| Concept | Description |
|---|---|
| Agent | The model, system prompt, tools, MCP servers, and skills |
| Environment | Configuration for where sessions run: an Anthropic-managed cloud container, or a self-hosted sandbox on your own infrastructure |
| Session | A running agent instance within an environment, performing a specific task and generating outputs |
| Events | Messages exchanged between your application and the agent (user turns, tool results, status updates) |
インストールを確認します:
ant --versionAPIキーを環境変数として設定します:
export ANTHROPIC_API_KEY="your-api-key-here"すべてのManaged Agents APIリクエストにはmanaged-agents-2026-04-01ベータヘッダーが必要です。SDKはベータヘッダーを自動的に設定します。
ユーザーイベントを送信すると、Claude Managed Agentsは:
session.status_idleイベントを発行しますエージェントを作成する
モデル、システムプロンプト、利用可能なツールを定義するエージェントを作成します。
ant beta:agents create \
--name "Coding Assistant" \
--model '{id: claude-opus-4-7}' \
--system "You are a helpful coding assistant. Write clean, well-documented code." \
--tool '{type: agent_toolset_20260401}'agent_toolset_20260401ツールタイプは、事前構築されたエージェントツールのフルセット(bash、ファイル操作、ウェブ検索など)を有効にします。完全なリストとツールごとの設定オプションについてはツールを参照してください。
返されたagent.idを保存してください。作成するすべてのセッションで参照します。
環境を作成する
環境は、エージェントが実行されるコンテナを定義します。
ant beta:environments create \
--name "quickstart-env" \
--config '{type: cloud, networking: {type: unrestricted}}'返されたenvironment.idを保存してください。作成するすべてのセッションで参照します。
セッションを開始する
エージェントと環境を参照するセッションを作成します。
session = client.beta.sessions.create(
agent=agent.id,
environment_id=environment.id,
title="Quickstart session",
)
print(f"Session ID: {session.id}")メッセージを送信してレスポンスをストリーミングする
ストリームを開き、ユーザーイベントを送信し、到着したイベントを処理します:
with client.beta.sessions.events.stream(session.id) as stream:
# Send the user message after the stream opens
client.beta.sessions.events.send(
session.id,
events=[
{
"type": "user.message",
"content": [
{
"type": "text",
"text": "Create a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt",
},
],
},
],
)
# Process streaming events
for event in stream:
match event.type:
case "agent.message":
for block in event.content:
print(block.text, end="")
case "agent.tool_use":
print(f"\n[Using tool: {event.name}]")
case "session.status_idle":
print("\n\nAgent finished.")
breakエージェントはPythonスクリプトを作成し、コンテナ内で実行し、出力ファイルが作成されたことを確認します。出力はこのようになります:
I'll create a Python script that generates the first 20 Fibonacci numbers and saves them to a file.
[Using tool: write]
[Using tool: bash]
The script ran successfully. Let me verify the output file.
[Using tool: bash]
fibonacci.txt contains the first 20 Fibonacci numbers (0 through 4181).
Agent finished.ネットワークとコンテナの設定をカスタマイズします