成果(outcome)告訴工作階段最終結果應該是什麼樣子,以及如何衡量其品質。代理程式會朝著該目標努力,自我評估並反覆迭代,直到達成成果為止。
當您定義成果時,harness(執行框架)會自動配置一個評分器(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?