定义结果
告诉智能体'完成'是什么样子,并让它迭代直到达成目标。
结果(outcome)告诉会话最终结果应该是什么样子,以及如何衡量其质量。智能体朝着该目标努力,进行自我评估和迭代,直到满足结果为止。
当您定义一个结果时,框架会自动配置一个评分器(grader),根据评分标准(rubric)评估产物。评分器使用单独的上下文窗口,以避免受到主智能体实现选择的影响。
评分器返回一个解释,总结哪些标准通过或失败,或确认产物满足评分标准。该反馈会交回给智能体用于下一次迭代。
创建评分标准
评分标准是一个描述每个标准评分的 markdown 文档。评分标准是必需的。
将评分标准构建为明确的、可评分的标准,例如"CSV 包含一个带有数值的价格列",而不是"数据看起来不错"。评分器独立地对每个标准评分,因此模糊的标准会产生嘈杂的评估。
如果您手头没有评分标准,可以尝试给 Claude 一个已知良好产物的示例,并要求它分析是什么使该内容良好,然后将该分析转化为评分标准。这种折中方法通常比从头编写标准产生更好的结果。
示例评分标准:
# DCF Model Rubric
## Revenue Projections
- Uses historical revenue data from the last 5 fiscal years
- Projects revenue for at least 5 years forward
- Growth rate assumptions are explicitly stated and reasonable
## Cost Structure
- COGS and operating expenses are modeled separately
- Margins are consistent with historical trends or deviations are justified
## Discount Rate
- WACC is calculated with stated assumptions for cost of equity and cost of debt
- Beta, risk-free rate, and equity risk premium are sourced or justified
## Terminal Value
- Uses either perpetuity growth or exit multiple method (stated which)
- Terminal growth rate does not exceed long-term GDP growth
## Output Quality
- All figures are in a single .xlsx file with clearly labeled sheets
- Key assumptions are on a separate "Assumptions" sheet
- Sensitivity analysis on WACC and terminal growth rate is included将评分标准作为内联文本传递给 user.define_outcome(参见创建带有结果的会话),或通过 Files API 上传以便在多个会话中重复使用。
import time
from pathlib import Path
from anthropic import Anthropic
client = Anthropic()
RUBRIC = """# DCF Model Rubric
## Revenue Projections
- Uses historical revenue data from the last 5 fiscal years
- Projects revenue for at least 5 years forward
## Output Quality
- All figures are in a single .xlsx file with clearly labeled sheets
"""
Path("/tmp/rubric.md").write_text(RUBRIC)
rubric = client.files.upload(file=Path("/tmp/rubric.md"))
print(f"Uploaded rubric: {rubric.id}")创建带有结果的会话
以下示例为现有的智能体和环境(两者均单独创建)创建一个会话,然后发送一个 user.define_outcome 事件。智能体立即开始工作。不需要额外的用户消息事件。
# 创建会话
session = client.beta.sessions.create(
agent=agent.id,
environment_id=environment.id,
title="Financial analysis on Costco",
)
# 定义结果 — 代理收到后即开始工作
client.beta.sessions.events.send(
session_id=session.id,
events=[
{
"type": "user.define_outcome",
"description": "Build a DCF model for Costco in .xlsx",
"rubric": {"type": "text", "content": RUBRIC},
# 或:"rubric": {"type": "file", "file_id": rubric.id},
"max_iterations": 5, # optional; default 3, max 20
}
],
)结果事件
面向结果的会话的进度会在事件流上显示。
agent.*事件(例如消息和工具使用)显示朝着结果的进度。span.outcome_evaluation_*事件仅针对面向结果的会话发出,显示迭代循环的次数和评分器的反馈过程。- 您也可以向面向结果的会话发送
user.message事件,以在智能体工作进展时引导其工作,但这不是必需的:智能体会自行朝着结果工作,迭代直到成功或用尽迭代次数。 user.interrupt事件会暂停当前结果的工作,并将span.outcome_evaluation_end.result标记为interrupted,允许您启动一个新的结果。- 在最终结果评估之后,会话可以作为对话式会话继续,或者可以启动一个新的结果。会话会保留先前结果的历史记录。
定义结果用户事件
这是您发送以启动结果的事件。它在接收时会被回显,包括 processed_at 时间戳和 outcome_id。
{
"type": "user.define_outcome",
"description": "Build a DCF model for Costco in .xlsx",
"rubric": { "type": "file", "file_id": "file_01..." },
"max_iterations": 5
}结果评估开始
一旦评分器在一个迭代循环上开始评估就会发出。iteration 字段是一个从 0 开始索引的修订计数器:0 是第一次评估,1 是第一次修订后的重新评估,依此类推。
{
"type": "span.outcome_evaluation_start",
"id": "sevt_01def...",
"outcome_id": "outc_01a...",
"iteration": 0,
"processed_at": "2026-03-25T14:01:45Z"
}结果评估进行中
评分器运行时发出的心跳。评分器的内部推理是不透明的:您看到它正在工作,但看不到它在想什么。
{
"type": "span.outcome_evaluation_ongoing",
"id": "sevt_01ghi...",
"outcome_id": "outc_01a...",
"iteration": 0,
"processed_at": "2026-03-25T14:02:10Z"
}结果评估结束
当结果评估周期结束时发出:在评分器完成对一次迭代的评估之后,或者当结果处于活动状态时会话被中断时。result 字段指示接下来会发生什么。
| Result | 下一步 |
|---|---|
satisfied | 会话转换为 idle。 |
needs_revision | 智能体开始一个新的迭代周期。 |
max_iterations_reached | 在会话转换为 idle 之前会有一个最终确认回合。不再运行进一步的评估。 |
failed | 会话转换为 idle。当评分标准不适用于交付物时返回,例如描述和评分标准相互矛盾。 |
interrupted | 当结果处于活动状态时会话被中断时发出,即使评估尚未开始。如果在中断之前没有触发 outcome_evaluation_start,则 outcome_evaluation_start_id 为空字符串。 |
{
"type": "span.outcome_evaluation_end",
"id": "sevt_01jkl...",
"outcome_evaluation_start_id": "sevt_01def...",
"outcome_id": "outc_01a...",
"result": "satisfied",
"explanation": "All 12 criteria met: revenue projections use 5 years of historical data, WACC assumptions are stated, sensitivity table is included...",
"iteration": 0,
"usage": {
"input_tokens": 2400,
"output_tokens": 350,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 1800
},
"processed_at": "2026-03-25T14:03:00Z"
}检查结果状态
您可以在事件流上监听 span.outcome_evaluation_end,或者轮询 GET /v1/sessions/{session_id} 并读取 outcome_evaluations[].result。在评估完成之前,result 报告 pending、running 或 evaluating:
session = client.beta.sessions.retrieve(session.id)
for outcome in session.outcome_evaluations:
print(f"{outcome.outcome_id}: {outcome.result}")
# outc_01a...: satisfied检索交付物
智能体将输出文件写入沙箱内的 /mnt/session/outputs/。要检索它们,请通过 Files API 以会话 ID 作为 scope_id 列出文件,然后按 ID 下载它们。按 scope_id 过滤需要在列表请求上使用 managed-agents-2026-04-01 beta 标头,因此 SDK 和 CLI 示例通过 beta 命名空间进行该调用并显式传递标头。文件在智能体完成写入后不久会出现在列表中,有时在会话变为空闲后几秒钟。如果您期望的文件尚未列出,请在短暂延迟后再次列出;一旦它出现在列表中,其上传就已完成。
# 列出此会话生成的文件
# scope_id 过滤要求在文件请求中启用 managed-agents beta
files = client.beta.files.list(scope_id=session.id, betas=["managed-agents-2026-04-01"])
for file in files:
print(file.id, file.filename)
# 下载文件
if files.data:
content = client.files.download(files.data[0].id)
content.write_to_file("/tmp/output.txt")后续步骤
Was this page helpful?