结果(outcome)告诉会话最终成果应该是什么样子,以及如何衡量其质量。智能体朝着该目标工作,自我评估并迭代,直到满足结果要求。
当您定义一个结果时,运行框架会自动配置一个评分器(grader),根据评分标准(rubric)评估产出物。评分器使用独立的上下文窗口,以避免受到主智能体实现选择的影响。
评分器会返回一段说明,总结哪些标准通过或未通过,或确认产出物满足评分标准。该反馈会交回给智能体用于下一次迭代。
评分标准(rubric)是一个描述逐项评分的 markdown 文档。评分标准是必需的。
评分标准示例:
# 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 事件。智能体会立即开始工作。不需要额外的用户消息事件。
# Create a session
session = client.beta.sessions.create(
agent=agent.id,
environment_id=environment.id,
title="Financial analysis on Costco",
)
# Define the outcome — agent starts working on receipt
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},
# or: "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 字段指示接下来会发生什么。
| 结果 | 下一步 |
|---|---|
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 获取它们。
# List files produced by this session
# scope_id filtering requires the managed-agents beta on the files request
files = client.beta.files.list(scope_id=session.id, betas=["managed-agents-2026-04-01"])
for file in files:
print(file.id, file.filename)
# Download a file
if files.data:
content = client.files.download(files.data[0].id)
content.write_to_file("/tmp/output.txt")Was this page helpful?