Claude Managed Agents 用托管基础设施替换了你手写的代理循环。本页介绍了从使用 Messages API 构建的自定义循环或从 Claude Agent SDK 迁移时的变化。
所有 Managed Agents API 请求都需要 managed-agents-2026-04-01 beta 头。SDK 会自动设置 beta 头。
如果你通过在 while 循环中调用 messages.create、自己执行工具调用并将结果附加到对话历史来构建代理,那么大部分代码都会消失。
| 之前 | 之后 |
|---|---|
| 你维护对话历史数组并在每个回合都将其传回。 | 会话在服务器端存储历史。发送事件,接收事件。 |
你解析 stop_reason: "tool_use",执行工具,并使用 tool_result 消息循环回来。 | 预构建的工具在容器内自动执行。你只需通过 agent.custom_tool_use 事件处理自定义工具。 |
| 你为运行代理生成的代码配置自己的沙箱。 | 会话容器处理代码执行、文件操作和 bash。 |
| 你决定循环何时完成。 | 当代理没有更多事情要做时,会话发出 session.status_idle。 |
之前(Messages API 循环,简化版):
messages = [{"role": "user", "content": task}]
while True:
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=messages,
tools=tools,
)
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason == "end_turn":
break
for block in response.content:
if block.type == "tool_use":
result = execute_tool(block.name, block.input)
messages.append(
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": block.id,
"content": result,
}
],
}
)之后(Claude Managed Agents):
agent = client.beta.agents.create(
name="Task Runner",
model="claude-opus-4-7",
tools=[{"type": "agent_toolset_20260401"}],
)
session = client.beta.sessions.create(
agent={"type": "agent", "id": agent.id, "version": agent.version},
environment_id=environment.id,
)
with client.beta.sessions.events.stream(session.id) as stream:
client.beta.sessions.events.send(
session.id,
events=[{"type": "user.message", "content": [{"type": "text", "text": task}]}],
)
for event in stream:
if event.type == "session.status_idle":
breakagent.custom_tool_use 事件。参见 会话事件流。如果你使用 Claude Agent SDK 构建,你已经在使用代理、工具和会话作为概念。区别在于它们在哪里运行:SDK 在你操作的进程中执行,而 Managed Agents 在 Anthropic 的基础设施中运行。大部分迁移是将 SDK 配置对象映射到其 API 端等效项。
| Agent SDK | Managed Agents |
|---|---|
ClaudeAgentOptions(...) 每次运行时构造 | client.beta.agents.create(...) 一次;代理在服务器端持久化和版本化。参见 代理设置。 |
async with ClaudeSDKClient(...) 或 query(...) | client.beta.sessions.create(...) 然后发送和接收 事件。 |
@tool 装饰的函数由 SDK 自动分派 | 在代理上声明为 {"type": "custom", ...};你的客户端处理 agent.custom_tool_use 事件并用 user.custom_tool_result 回复。参见 工具。 |
| 内置工具在你的进程中针对你的文件系统运行 | {"type": "agent_toolset_20260401"} 在会话容器内针对 /workspace 运行相同的工具。 |
cwd、add_dirs 指向本地路径 | 上传或挂载 文件 作为会话资源。 |
system_prompt 和 CLAUDE.md 层次结构 | 代理上的单个 system 字符串。每次更新都会产生新的服务器端版本;将会话固定到特定版本以推广或回滚而无需部署。参见 代理设置。 |
mcp_servers 在一个地方配置和认证 | 在代理上声明服务器;通过会话上的 Vault 提供凭证。 |
permission_mode、can_use_tool | 每个工具的 permission_policy;对 always_ask 工具的 user.tool_confirmation 事件做出响应。 |
之前(Agent SDK):
from claude_agent_sdk import (
ClaudeAgentOptions,
ClaudeSDKClient,
create_sdk_mcp_server,
tool,
)
@tool("get_weather", "Get the current weather for a city.", {"city": str})
async def get_weather(args: dict) -> dict:
return {"content": [{"type": "text", "text": f"{args['city']}: 18°C, clear"}]}
options = ClaudeAgentOptions(
model="claude-opus-4-7",
system_prompt="You are a concise weather assistant.",
mcp_servers={
"weather": create_sdk_mcp_server("weather", "1.0", tools=[get_weather])
},
)
async with ClaudeSDKClient(options=options) as agent:
await agent.query("What's the weather in Tokyo?")
async for msg in agent.receive_response():
print(msg)之后(Managed Agents):
from anthropic import Anthropic
client = Anthropic()
agent = client.beta.agents.create(
name="weather-agent",
model="claude-opus-4-7",
system="You are a concise weather assistant.",
tools=[
{
"type": "custom",
"name": "get_weather",
"description": "Get the current weather for a city.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}
],
)
environment = client.beta.environments.create(
name="weather-env",
config={"type": "cloud", "networking": {"type": "unrestricted"}},
)
session = client.beta.sessions.create(
agent={"type": "agent", "id": agent.id, "version": agent.version},
environment_id=environment.id,
)
def get_weather(city: str) -> str:
return f"{city}: 18°C, clear"
with client.beta.sessions.events.stream(session.id) as stream:
client.beta.sessions.events.send(
session.id,
events=[
{
"type": "user.message",
"content": [{"type": "text", "text": "What's the weather in Tokyo?"}],
}
],
)
for ev in stream:
if ev.type == "agent.message":
print("".join(b.text for b in ev.content))
elif ev.type == "agent.custom_tool_use":
result = get_weather(**ev.input)
client.beta.sessions.events.send(
session.id,
events=[
{
"type": "user.custom_tool_result",
"custom_tool_use_id": ev.id,
"content": [{"type": "text", "text": result}],
}
],
)
elif ev.type == "session.status_idle" and ev.stop_reason.type == "end_turn":
break代理和环境创建一次并在会话中重复使用。工具函数仍在你的进程中运行;区别在于你读取 agent.custom_tool_use 事件并显式发送结果,而不是 SDK 为你分派它。
代理循环由 Anthropic 运行的权衡是 SDK 自动处理的一些事情变成了你的客户端的责任。
| SDK 功能 | Managed Agents 方法 |
|---|---|
| 计划模式 | 首先运行仅计划会话,然后运行第二个会话来执行。 |
| 输出样式、斜杠命令 | 在发送 user.message 之前或接收 agent.message 之后在你的客户端中应用。 |
PreToolUse / PostToolUse 钩子 | 你的客户端已经在响应之前看到每个 agent.custom_tool_use 事件;将逻辑放在那里。对于内置工具,使用 permission_policy: always_ask。 |
max_turns | 在客户端计算回合。 |
sessions.create 和 sessions.stream 替换你的循环。resources。agent.custom_tool_use 事件的响应。当发布新的 Claude 模型时,迁移 Claude Managed Agents 集成通常是一个字段的更改:更新 代理定义 上的 model,更改在你创建的下一个会话上生效。
ant beta:agents update \
--agent-id "$AGENT_ID" \
--version "$AGENT_VERSION" \
--model claude-opus-4-7Messages API 迁移指南 中记录的大多数模型级行为变化不需要你采取行动:
max_tokens 默认值、thinking 配置)由 Claude Managed Agents 运行时处理。这些字段不在代理定义上公开。agent.custom_tool_use 事件之前由运行时解析。你看到的是结构化数据,而不是原始字符串。Messages API 指南中的行为描述(模型的不同之处)仍然适用。迁移步骤(如何更改你的请求代码)不适用。
Was this page helpful?