CMA:大规划小执行
先做整体规划再逐步执行的策略
🌐 查看英文原文 | 源码 Notebook
协调器模式:大模型用于规划,小模型用于执行
简介
大多数 Agent 工作负载内部有两个非常不同的工作:少量的规划和判断,以及大量的机械性阅读和执行。网络研究是极端情况,也是本 notebook 使用的例子:根据权威来源验证二十个事实意味着通过模型拉取数十万 token 的网页,而以前沿速率计算,该阅读费用占主导地位。
协调器模式将两个工作负载分开。前沿模型规划研究并综合答案,但它从不接触原始网页——廉价的工作器在各自的并行上下文窗口中完成所有阅读,并报告提炼后的发现。本 notebook 诚实地衡量了这种分割:它在同一问题上运行了现实的替代方案——一个具有相同工具、遵循相同验证标准的前沿 Agent——并比较了真实的费用和实际耗时。在作者的运行中,两个方案阅读的量大致相同,团队方案便宜约 2.5 倍、快约 3 倍,84-98% 的输入 token 以工作器费率计费。
通过本 cookbook,你将能够:
- 使用
multiagent协调器字段配置双模型团队:前沿协调器和廉价搜索工作器 - 通过 session 事件流实时跟踪委托(
thread_created、thread_message_sent、thread_message_received) - 运行严格匹配的单前沿 Agent 对照组并比较真实费用
- 使用类型化的每线程累计
usage对每个线程进行计量
相同的经济原理适用于任何廉价模型可以承担 token 密集型工作的负载:文档审查、日志分析、代码库扫描。

前提条件
在遵循本指南之前,请确保你具备:
所需知识: - Python 基础知识 - 熟悉 Managed Agents 基础知识——agents、environments、sessions 和流式事件循环(CMA_iterate_fix_failing_tests.ipynb 介绍了所有这些)
所需工具: - Python 3.11 或更高版本 - Anthropic API 密钥(在此获取),具有 Managed Agents beta 访问权限
如果你之前没有见过 multiagent 字段,CMA_coordinate_specialist_team.ipynb 用一个异构专家团队介绍了它。本 notebook 使用尽可能简单的团队——一种工作器类型——因为这里的重点是成本结构,而不是团队设计。
环境配置
%%capture
%pip install -qU anthropic python-dotenvimport os
import time
import anthropic
from dotenv import load_dotenv
load_dotenv()
BETAS = ["managed-agents-2026-04-01"]
# The frontier model plans and synthesizes; the cheap model reads the web.
COORDINATOR_MODEL = os.environ.get("COOKBOOK_COORDINATOR_MODEL", "claude-fable-5")
WORKER_MODEL = os.environ.get("COOKBOOK_WORKER_MODEL", "claude-sonnet-5")
client = anthropic.Anthropic()1. 团队:廉价的阅读器,昂贵的思考者
两个 agent 定义构成了整个团队。
工作器——在文档术语中,是协调器可以从其名册中生成的子 agent——是一个普通的 agent:一个模型、一个范围缩小到 web_search + web_fetch 的工具集,以及一个系统提示。每个工作器实例在自己的 session 线程中研究一个集中的子问题,因此它读取的巨大网页永远不会进入其他任何人的上下文。
协调器没有自己的工具——只有一个命名工作器的 multiagent 名册。这一个字段就是使它成为协调器的原因:服务器自动给它 create_agent、send_to_agent、wait_for_agents 和 list_agents,工作器以同样的方式获得 submit_result 和 send_to_parent。你永远不需要定义这些工具。
关于这种关系有两点需要了解。首先,名册在协调器创建或更新时被快照——如果你更改工作器的定义,请更新或重新创建协调器。其次,不太明显的是:协调器看不到其名册 agent 的任何信息——既看不到它们的提示,也看不到它们的名称或描述。它的 create_agent 工具接受一个简单的 agent 名称和任务字符串。协调器对其工作器的所有认知都来自它自己的系统提示,因此请保持该描述与工作器的实际提示一致——服务器上没有任何东西强制执行这一点。(对于单 agent 名册,任何请求的名称都会解析到那个工作器;对于多种工作器类型,请在协调器的提示中明确命名它们。)
worker = client.beta.agents.create(
name="search-worker",
model=WORKER_MODEL,
# Everything off except the two web tools: the worker's job is
# reading, and scoping keeps the cheap model from wandering into
# bash or the filesystem. It's also the security boundary: workers
# read arbitrary (untrusted) web pages, so a worker that can only
# search, fetch, and report back is the blast radius you want for
# that input — and the coordinator reading the reports has no
# tools at all.
tools=[
{
"type": "agent_toolset_20260401",
"default_config": {"enabled": False},
"configs": [
{"name": "web_search", "enabled": True},
{"name": "web_fetch", "enabled": True},
],
}
],
system=(
"You are a search worker researching one focused sub-question for "
"a coordinator. Use web_search and web_fetch to find the answer. "
"Be thorough: try multiple query phrasings, follow promising "
"links, and cross-check facts across sources. Report back with "
"the specific answer you found and the evidence (URLs, quotes) "
"that supports it. If you could not find a definitive answer, say "
"exactly what you did find and what remains uncertain. Always "
"finish by calling submit_result."
),
betas=BETAS,
)
coordinator = client.beta.agents.create(
name="search-coordinator",
model=COORDINATOR_MODEL,
multiagent={
"type": "coordinator",
"agents": [{"type": "agent", "id": worker.id}],
},
system=(
"You are coordinating a team of search workers to answer a hard "
"web-research question. Your workers have web_search and "
"web_fetch; you do not. Break the question into focused "
"sub-questions and delegate each to a worker via create_agent. "
"Run several workers in parallel on independent sub-questions, "
"and ALWAYS call wait_for_agents after spawning before drawing "
"any conclusion. When a worker reports, decide whether its "
"findings answer the sub-question or whether to send a follow-up "
"with send_to_agent. If a worker returns an infrastructure error "
"(rate limit, timeout) instead of findings, re-assign the same "
"sub-question to a fresh worker. Once you have enough evidence, "
"synthesize the workers' findings into a single final answer to "
"the original question."
),
betas=BETAS,
)
print(f"worker {worker.id}")
print(f"coordinator {coordinator.id}")worker agent_01YDJ3havNfM7FUu2X4xE5UF
coordinator agent_015MHQnJYMc51iXmhVDVrWZm
2. 运行一个研究问题
为协调器创建一个 environment 和一个 session,将问题作为 user.message 发送,然后流式传输。session 级别的流是协调器的主线程——整个运行的精简视图。工作器线程在其中显示为委托流量:生成工作器时的 session.thread_created,协调器交给一个工作器子问题时的 agent.thread_message_sent,以及发现返回时的 agent.thread_message_received。
这个问题是一个覆盖任务——二十个事实(10 个公园 × 2 个属性),每个都必须根据特定的权威来源进行验证。覆盖问题是该模式大放异彩的地方,因为阅读是强制性的:没有人可以从记忆中回答,所以唯一的问题是阅读以什么费率计费以及是否并行发生。(发现问题——在大搜索空间中找到一个隐藏的答案,类似于 BrowseComp 等基准测试的风格——更多奖励前沿模型的搜索直觉,差距会缩小。)
env = client.beta.environments.create(
name="research-fanout",
config={"type": "anthropic_cloud", "networking": {"type": "unrestricted"}},
)
session = client.beta.sessions.create(
agent=coordinator.id,
environment_id=env.id,
betas=BETAS,
)
QUESTION = (
"For each of the ten largest national parks in the contiguous United "
"States by area, find: the current standard private-vehicle entrance "
"fee, and whether the park currently requires a timed-entry or "
"day-use reservation for peak season. Each fact must be verified "
"against that park's official nps.gov pages (fees page and alerts/"
"reservations page) - not from third-party summaries. Give park, fee, "
"reservation requirement, and the nps.gov URLs you used."
)
t_start = time.monotonic()
client.beta.sessions.events.send(
session.id,
betas=BETAS,
events=[{"type": "user.message", "content": [{"type": "text", "text": QUESTION}]}],
)
def text_of(content):
return "".join(b.text for b in content or [] if b.type == "text")
def clip(s, n=160):
return s[:n] + ("..." if len(s) > n else "")
final_answer = ""
with client.beta.sessions.events.stream(session.id, betas=BETAS) as stream:
for ev in stream:
match ev.type:
case "agent.message":
if text := text_of(ev.content).strip():
final_answer = text
print(f"[coordinator] {clip(text, 200)}")
case "session.thread_created":
print(f"[spawn] {ev.agent_name} ({ev.session_thread_id})")
case "agent.thread_message_sent":
print(f"[delegate -> {ev.to_agent_name}] {clip(text_of(ev.content))}")
case "agent.thread_message_received":
print(f"[report <- {ev.from_agent_name}] {clip(text_of(ev.content))}")
case "session.status_idle":
break
print(f"\n[team finished in {time.monotonic() - t_start:.0f}s]")
print("=" * 70)
print(final_answer)[spawn] search-worker (sthr_01F32moz43gj3cxRPrgS67jf)
[delegate -> search-worker] You are a web research worker. Research Death Valley National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or ...
[spawn] search-worker (sthr_01R1qqTRXyARsqGebN1zqer1)
[delegate -> search-worker] You are a web research worker. Research Yellowstone National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or n...
[spawn] search-worker (sthr_01UBFmhdgLajS4MR4Lukk1YD)
[delegate -> search-worker] You are a web research worker. Research Everglades National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or ne...
[spawn] search-worker (sthr_01TdgcdgMm4J9ugpvTxADCq5)
[delegate -> search-worker] You are a web research worker. Research Grand Canyon National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or ...
[spawn] search-worker (sthr_01PVtf1smDJMTEyw5f6Yx4rc)
[delegate -> search-worker] You are a web research worker. Research Glacier National Park (Montana) using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs...
[spawn] search-worker (sthr_01PNQou4A6DVaLZkRsamEJkz)
[delegate -> search-worker] You are a web research worker. Research Olympic National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or news ...
[spawn] search-worker (sthr_012wGNTHaSLEKox6qzL3r2cn)
[delegate -> search-worker] You are a web research worker. Research Big Bend National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or news...
[spawn] search-worker (sthr_01SLZ1PDc2W5VqsSNGaQGMQm)
[delegate -> search-worker] You are a web research worker. Research Joshua Tree National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or n...
[spawn] search-worker (sthr_01S5TWC3pjmAfYYSSPZBzv4k)
[delegate -> search-worker] You are a web research worker. Research Yosemite National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or news...
[spawn] search-worker (sthr_01UrnhFkeooc8bSQPzXKXyGB)
[delegate -> search-worker] You are a web research worker. Research Kings Canyon National Park using ONLY official nps.gov pages (no third-party summaries like Wikipedia, travel blogs, or ...
[coordinator] I've identified the ten largest national parks in the contiguous U.S. by area (Death Valley, Yellowstone, Everglades, Grand Canyon, Glacier, Olympic, Big Bend, Joshua Tree, Yosemite, and Kings Canyon)...
[report <- search-worker] Research complete — Big Bend National Park (nps.gov official sources only):
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE: $30.00
- "Private Vehicle $30.00 — Adm...
[coordinator] Big Bend is confirmed: **$30 private-vehicle fee (7 days), no timed-entry or day-use reservation required** — verified on the park's official fees page and alerts page. Nine parks still in progress; I...
[report <- search-worker] Research complete — both facts verified directly on official nps.gov pages.
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE: $35.00
- This covers one private, non-com...
[coordinator] Yellowstone is confirmed: **$35 private-vehicle fee (7 days), no timed-entry/vehicle reservation required** — the fees page explicitly states vehicle reservations are not needed to enter. Eight parks ...
[report <- search-worker] Research complete — both facts verified directly on nps.gov (fetched pages below).
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE: $35.00
- Source: https://www.nps.g...
[coordinator] Grand Canyon is confirmed: **$35 private-vehicle fee (7 days, South Rim), no timed-entry or day-use reservation required** — the fees page explicitly says "No reservation needed for entry, no timed en...
[report <- search-worker] Research complete — both facts verified directly on nps.gov (no third-party sources used).
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE
- $30.00 — "Private Vehicle...
[coordinator] Joshua Tree is confirmed: **$30 private-vehicle fee (7 days), no timed-entry or day-use reservation required** — only campgrounds, Keys Ranch tours, and backcountry permits require reservations. Six p...
[report <- search-worker] RESEARCH RESULTS: Kings Canyon / Sequoia & Kings Canyon National Parks (nps.gov "seki" — Kings Canyon is jointly administered with Sequoia NP under the NPS unit...
[coordinator] Kings Canyon is confirmed: **$35 private-vehicle fee (valid 1–7 days, covers both Sequoia & Kings Canyon, which are jointly administered), no timed-entry or day-use reservation required for entry**. F...
[report <- search-worker] Research complete on Glacier National Park (nps.gov only sources):
**1) Standard private-vehicle entrance fee**
- Current standard vehicle fee: $35.00 (summer ...
[coordinator] Glacier is confirmed: **$35 private-vehicle fee ($25 winter rate Nov 1–Apr 30), and no vehicle reservations required in any area of the park for 2026** — a notable change, since Going-to-the-Sun Road ...
[report <- search-worker] YOSEMITE NATIONAL PARK — Official nps.gov Research Results
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE: $35.00
- Verified on https://www.nps.gov/yose/planyourvisi...
[coordinator] Yosemite is confirmed: **$35 private-vehicle fee (7 days), and no timed-entry reservation for 2026** — the park's own Entrance Reservations page announces it discontinued the timed-reservation system ...
[report <- search-worker] RESEARCH RESULTS: Death Valley National Park (nps.gov only)
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE: $30.00
- Source verified: https://www.nps.gov/deva/planyo...
[coordinator] Death Valley is confirmed: **$30 private-vehicle fee (7 days), no timed-entry or day-use reservation required**. Two parks still pending: Everglades and Olympic.
[report <- search-worker] EVERGLADES NATIONAL PARK — RESEARCH RESULTS (nps.gov only)
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE: $35.00 (7-day pass, admits passholder + all passengers in ...
[coordinator] Everglades is confirmed: **$35 private-vehicle fee (7 days, all entrances), no timed-entry or day-use reservation required** — the worker also flagged and correctly discounted a stale legacy page show...
[report <- search-worker] OLYMPIC NATIONAL PARK — RESEARCH RESULTS (all sourced from official nps.gov pages only)
1) STANDARD PRIVATE-VEHICLE ENTRANCE FEE
- Fee: $30.00 USD for a "Priva...
[coordinator] All ten workers have reported, with each fact verified directly against official nps.gov pages. Here is the complete picture:
## The 10 Largest National Parks in the Contiguous U.S. — Fees & Entry Re...
[team finished in 194s]
======================================================================
All ten workers have reported, with each fact verified directly against official nps.gov pages. Here is the complete picture:
## The 10 Largest National Parks in the Contiguous U.S. — Fees & Entry Reservations
| # | Park | Private-Vehicle Fee | Timed-Entry / Day-Use Reservation? |
|---|------|--------------------|-----------------------------------|
| 1 | **Death Valley** | $30 (7 days) | **No** |
| 2 | **Yellowstone** | $35 (7 days) | **No** — fees page explicitly states vehicle reservations are not needed |
| 3 | **Everglades** | $35 (7 days, all entrances) | **No** |
| 4 | **Grand Canyon** | $35 (7 days) | **No** — "No reservation needed for entry, no timed entry" (note: North Rim closed for 2025 season due to Dragon Bravo Fire) |
| 5 | **Glacier** | $35 summer / $25 winter (Nov 1–Apr 30) | **No** — "No vehicle reservations are required in any areas of the park in 2026" (reservations were used 2021–2024 on Going-to-the-Sun Road, now discontinued) |
| 6 | **Olympic** | $30 (7 days) | **No** — "No reservations are required for entrance to the park" |
| 7 | **Big Bend** | $30 (7 days) | **No** — "There are no reservations required for entering Big Bend National Park" |
| 8 | **Joshua Tree** | $30 (7 days) | **No** — "No advance reservations are needed to enter Joshua Tree National Park" |
| 9 | **Yosemite** | $35 (7 days) | **No** — park announced it will **not** use its timed-reservation system in 2026 (a reversal; reservations were required in 2025 peak season) |
| 10 | **Kings Canyon** (jointly administered with Sequoia as "SEKI") | $35 (1–7 days, valid for both parks) | **No** — "Reservations are not required to enter the parks" |
**Headline finding:** Currently, **none** of the ten largest contiguous-U.S. parks requires a timed-entry or day-use reservation for general entry — including the two with recent reservation systems (Glacier and Yosemite), both of which have officially discontinued them per their own nps.gov pages. Reservations remain required only for ancillary activities (campgrounds, wilderness permits, cave/ranch tours, tram tours), not park entry.
### Official nps.gov sources used per park
- **Death Valley**: [fees](https://www.nps.gov/deva/planyourvisit/fees.htm) · [alerts/conditions](https://www.nps.gov/deva/planyourvisit/conditions.htm) (no reservation language anywhere; no reservations page exists)
- **Yellowstone**: [fees](https://www.nps.gov/yell/planyourvisit/fees.htm) (contains the "Vehicle Reservations Not Required" callout) · [conditions](https://www.nps.gov/yell/planyourvisit/conditions.htm)
- **Everglades**: [fees](https://www.nps.gov/ever/planyourvisit/fees.htm) · [permits & reservations](https://www.nps.gov/ever/planyourvisit/permits-and-reservations.htm) · [conditions](https://www.nps.gov/ever/planyourvisit/conditions.htm)
- **Grand Canyon**: [fees](https://www.nps.gov/grca/planyourvisit/fees.htm) (contains the "no timed entry" statement) · [conditions](https://www.nps.gov/grca/planyourvisit/conditions.htm)
- **Glacier**: [fees](https://www.nps.gov/glac/planyourvisit/fees.htm) (contains the 2026 no-reservations statement) · [reservations](https://www.nps.gov/glac/planyourvisit/reservations.htm) · [conditions](https://www.nps.gov/glac/planyourvisit/conditions.htm)
- **Olympic**: [fees](https://www.nps.gov/olym/planyourvisit/fees.htm) · [plan your visit](https://www.nps.gov/olym/planyourvisit/index.htm) (entry-reservation statement) · [conditions](https://www.nps.gov/olym/planyourvisit/conditions.htm)
- **Big Bend**: [fees](https://www.nps.gov/bibe/planyourvisit/fees.htm) (contains the no-reservations statement) · [conditions](https://www.nps.gov/bibe/planyourvisit/conditions.htm)
- **Joshua Tree**: [fees](https://www.nps.gov/jotr/planyourvisit/fees.htm) · [permits & reservations](https://www.nps.gov/jotr/planyourvisit/permitsandreservations.htm) · [conditions](https://www.nps.gov/jotr/planyourvisit/conditions.htm)
- **Yosemite**: [fees](https://www.nps.gov/yose/planyourvisit/fees.htm) · [entrance reservations](https://www.nps.gov/yose/planyourvisit/reservations.htm) (2026 discontinuation announcement) · [conditions](https://www.nps.gov/yose/planyourvisit/conditions.htm)
- **Kings Canyon/SEKI**: [fees](https://www.nps.gov/seki/planyourvisit/fees.htm) · [permits & reservations](https://www.nps.gov/seki/planyourvisit/permits.htm) · [conditions](https://www.nps.gov/seki/planyourvisit/conditions.htm)
**Caveats worth noting:**
- Fee ranges like "$15–$35" on some fees pages span motorcycle/per-person tiers; the figures above are specifically the **private-vehicle** rate in every case.
- Glacier and Yosemite's no-reservation status is a recent policy change framed as applying "for 2026" — worth rechecking close to a travel date, as these programs have toggled year to year.
- Everglades has a stale legacy page showing an outdated $10 fee; the current, internally consistent fees page confirms $35.
需要注意的形状:每个 [delegate ->] 行都是一条小消息,每个 [report <-] 行都是提炼的摘要。产生这些报告的数兆字节的搜索结果和获取的页面从未跨越协调器的上下文。这种分离就是整个成本故事。为了公平定价,下一节运行现实的替代方案,然后我们计量两者。
3. 运行对照组:一个前沿 Agent,相同的验证标准
如果没有该模式,这会花费多少?现实的替代方案是一个具有相同两个 Web 工具的单个前沿 Agent。一个微妙之处使这个比较公平或毫无价值:独立 Agent 必须遵守相同的验证标准。 按自己的判断行事,前沿模型是经济的——它每个事实读取一个来源,成本很低,但那是一个较低严谨度的产品,不是不同价格的相同工作。因此,下面的独立提示要求团队已经做的事情:每个事实从两次独立获取中验证,冲突重新检查并标记。
相同的问题,安静的流。
solo = client.beta.agents.create(
name="solo-researcher",
model=COORDINATOR_MODEL,
tools=[
{
"type": "agent_toolset_20260401",
"default_config": {"enabled": False},
"configs": [
{"name": "web_search", "enabled": True},
{"name": "web_fetch", "enabled": True},
],
}
],
system=(
"You research hard web questions with audit-grade rigor. Use "
"web_search and web_fetch. For EVERY fact you report, verify it "
"from at least two independent fetches (the authoritative page "
"plus one corroborating source), and re-fetch when two sources "
"disagree. Never carry a fact forward on one source or from "
"memory. In your answer, give each fact with both source URLs, "
"and explicitly flag any fact where sources conflicted. Before "
"finishing, audit your own answer: list each claim and check it "
"has two cited sources."
),
betas=BETAS,
)
t_solo = time.monotonic()
solo_session = client.beta.sessions.create(agent=solo.id, environment_id=env.id, betas=BETAS)
client.beta.sessions.events.send(
solo_session.id,
betas=BETAS,
events=[{"type": "user.message", "content": [{"type": "text", "text": QUESTION}]}],
)
solo_answer = ""
with client.beta.sessions.events.stream(solo_session.id, betas=BETAS) as stream:
for ev in stream:
match ev.type:
case "agent.message":
if text := text_of(ev.content).strip():
solo_answer = text
case "session.status_idle":
break
print(f"[solo finished in {time.monotonic() - t_solo:.0f}s]")
print(clip(solo_answer, 300))[solo finished in 608s]
All facts verified. Here are the results, with every fee and reservation status confirmed against at least two official nps.gov pages.
## Which parks made the list
By official NPS acreage, the ten largest national parks in the contiguous U.S. are: Death Valley, Yellowstone, Everglades, Grand Canyo...
4. 计量和定价两次运行
成本归因内置于 API 中:每个 session 线程携带一个类型化的累计 usage,而 session.usage 总计整个团队。列出线程,将主线程(parent_thread_id is None)作为协调器,子线程是工作器——独立 session 根本没有子线程。
(如果你需要每个请求的详细信息,它在每个线程自己的事件源中——session 级别的源只携带主线程的 span.model_request_end 事件。)
价格来自撰写时的定价页面(Sonnet 5 显示其初始费率)。只需配置输入和输出费率:5 分钟缓存写入按输入费率的 1.25 倍计费,1 小时写入按 2 倍计费,缓存读取按 0.1 倍计费——而 /v1/models 报告功能但不报告定价,因此每个模型的两个数字存在于你的代码中。
# $ / MTok input and output from the pricing page. Sonnet 5 is its
# introductory rate ($2/$10 through Aug 31, 2026; standard $3/$15
# after — update these numbers then).
PRICES = {
"claude-fable-5": {"input": 10.0, "output": 50.0},
"claude-sonnet-5": {"input": 2.0, "output": 10.0},
}
def total_input(u):
cache = u.cache_creation # None on threads with no cache activity
return (
u.input_tokens
+ u.cache_read_input_tokens
+ (cache.ephemeral_5m_input_tokens if cache else 0)
+ (cache.ephemeral_1h_input_tokens if cache else 0)
)
def cost(u, model):
p = PRICES[model]
cache = u.cache_creation
return (
u.input_tokens * p["input"]
+ (cache.ephemeral_5m_input_tokens if cache else 0) * p["input"] * 1.25
+ (cache.ephemeral_1h_input_tokens if cache else 0) * p["input"] * 2.0
+ u.cache_read_input_tokens * p["input"] * 0.1
+ u.output_tokens * p["output"]
) / 1e6
def report(session_id, primary_model, worker_model):
threads = list(client.beta.sessions.threads.list(session_id, betas=BETAS))
primary = next(t.usage for t in threads if t.parent_thread_id is None)
workers = [t.usage for t in threads if t.parent_thread_id is not None]
workers_in = sum(total_input(u) for u in workers)
total = cost(primary, primary_model) + sum(cost(u, worker_model) for u in workers)
print(
f" primary thread ({primary_model}): {total_input(primary):>9,} in / {primary.output_tokens:>6,} out"
)
if workers:
print(
f" {len(workers)} worker(s) ({worker_model}): {workers_in:>9,} in / {sum(u.output_tokens for u in workers):>6,} out"
)
print(f" workers' share of input: {workers_in / (workers_in + total_input(primary)):.0%}")
print(f" total cost: ${total:.2f}")
return total
print("split team (fable coordinator + sonnet workers):")
split_cost = report(session.id, COORDINATOR_MODEL, WORKER_MODEL)
print("\nsolo frontier agent:")
solo_cost = report(solo_session.id, COORDINATOR_MODEL, COORDINATOR_MODEL)
# The counterfactual that isolates the rate split: this run's team
# workload with every token billed at the frontier rate.
threads = list(client.beta.sessions.threads.list(session.id, betas=BETAS))
frontier_team_cost = sum(cost(t.usage, COORDINATOR_MODEL) for t in threads)
print(f"\nsolo / split cost ratio on this pair of runs: {solo_cost / split_cost:.1f}x")
print(f"the split team's workload at all-frontier rates: ${frontier_team_cost:.2f}")split team (fable coordinator + sonnet workers):
primary thread (claude-fable-5): 169,391 in / 6,484 out
10 worker(s) (claude-sonnet-5): 908,392 in / 34,164 out
workers' share of input: 84%
total cost: $1.61
solo frontier agent:
primary thread (claude-fable-5): 1,259,624 in / 38,479 out
total cost: $4.00
solo / split cost ratio on this pair of runs: 2.5x
the split team's workload at all-frontier rates: $5.06
两次运行做了几乎相同的阅读——这就是匹配验证标准的意义。不同的是阅读计费的费率和工作形状:团队的二十次查找以廉价费率作为并行工作器线程运行,而独立 Agent 在一个以前沿定价的上下文中串行处理它们。在作者的运行中,团队便宜约 2.5 倍、快约 3 倍,84-98% 的团队输入 token 以工作器费率计费。Token 量因运行而异,因此将任何打印的单个比率视为一个样本;结构是稳定的部分。
四个诚实的注意事项,都是在构建本 notebook 时观察到的:
- 保持比较的严谨度匹配。 独立前沿 Agent 按自己的判断行事阅读量少得多(每个事实一个来源),比团队便宜——但那是一个不同的、较低严谨度的产品。当验证标准固定时,分割的成本优势是真实的。
- 委托有最低成本。 每个工作器线程支付固定的设置开销。将相同的工作分成更多、更窄的简报反而增加了我们的费用而不是降低它——简报粒度有一个最优值。
- 验证标准只覆盖你放入其中的内容。 提交运行中的两个方案都根据 nps.gov 验证了所有二十个事实——而且两者都根据模型记忆构建了公园列表,将 Kings Canyon 放在了实际上属于 Great Smoky Mountains 的第 10 位(按面积 Kings Canyon 是第 12 位)。事实被审计了;问题分解没有。如果前提重要,再花一次委托让工作器验证它。
- 协调器只知道你告诉它的。 服务器上没有任何东西向它展示工作器的提示(见第 1 节),因此经济性也取决于你在协调器提示中准确描述工作器的行为。
分割什么时候不划算?在狭窄的问题上,阅读量太少,不值得套利。如果协调器根据自己的知识回答(没有委托),你为一个前沿往返什么也没得到——注意没有 [spawn] 行的运行。如果任务需要对原始材料本身进行前沿判断(细微的文档分析而不是事实查找),廉价的阅读器可能会把恰好重要的内容摘要掉。
回顾
在本指南中,你构建了多 Agent 团队最便宜的有用形状并衡量了它带来的收益:
- 配置了双模型团队 — 一个唯一能力是其
multiagent名册的前沿协调器,以及一个范围限定到 Web 工具的廉价工作器 - **通过 session 流上的
thread_created/thread_message_sent/thread_message_received实时跟踪了委托 - 运行了严格匹配的单前沿 Agent 对照组 并比较了真实的费用和实际耗时,而不是估计值
- 使用每个 session 线程携带的类型化累计
usage计量了每个线程(session.usage是整个团队的总计)
要进一步探索:
- 添加具有范围化工具集的专家工作器类型——
CMA_coordinate_specialist_team.ipynb展示了一个三角色团队以及为什么按角色限定范围很重要 - 将每线程计量放入你的生产遥测中:线程级别的使用量是你按委托归因支出的方式,而不仅仅是按 session
- 在你自己的 token 密集型工作负载上尝试相同的分割——文档审查和日志分类具有与网络研究相同的阅读密集型、覆盖形状的特征
参考:多 Agent 会话文档。
中文读者提示:本章节的代码和输出为英文原文。如需理解具体实现细节,可参考上方中文导读和代码注释。如有疑问,欢迎在 GitHub Issues 讨论。