outcome 將工作階段從對話提升為工作。您定義最終結果應該是什麼樣子以及如何衡量品質。代理程式會朝著該目標努力,自我評估並反覆迭代,直到達成成果為止。
當您定義成果時,框架會自動配置一個評分器(grader),根據評分標準(rubric)來評估產出物。評分器使用獨立的「context window」(上下文視窗),以避免受到主代理程式實作選擇的影響。
評分器會回傳一份說明,摘要哪些標準通過或未通過,或確認產出物符合評分標準。該回饋會傳回給代理程式,用於下一次迭代。
所有 Managed Agents API 請求都需要 managed-agents-2026-04-01 beta 標頭。SDK 會自動設定此 beta 標頭。
評分標準是一份描述各項標準評分方式的 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 上傳以便在多個工作階段中重複使用。
透過 Files API 上傳需要同時使用 managed-agents-2026-04-01 和 files-api-2025-04-14 兩個 beta 標頭。
rubric = client.beta.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,讓您可以啟動新的成果。一次僅支援一個成果,但您可以依序串接多個成果。要執行此操作,請在前一個成果的終止事件之後傳送新的 user.define_outcome 事件。
這是您傳送以啟動成果的事件。收到後會回傳相同內容,並包含 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...",
"processed_at": "2026-03-25T14:02:10Z"
}評分器完成一次迭代評估後發出。result 欄位指示接下來會發生什麼。
| 結果 | 下一步 |
|---|---|
satisfied | 工作階段轉換為 idle。 |
needs_revision | 代理程式開始新的迭代週期。 |
max_iterations_reached | 不再有進一步的評估週期。代理程式可能會在工作階段轉換為 idle 之前執行最後一次修訂。 |
failed | 工作階段轉換為 idle。當評分標準根本不符合任務時回傳,例如描述與評分標準相互矛盾。 |
interrupted | 僅在中斷前已觸發 outcome_evaluation_start 時發出。 |
{
"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/:id 並讀取 outcome_evaluations[].result:
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 擷取這些檔案:
# 列出此工作階段產生的檔案
files = client.beta.files.list(scope_id=session.id)
for f in files:
print(f.id, f.filename)
# 下載檔案
if files.data:
content = client.beta.files.download(files.data[0].id)
content.write_to_file("/tmp/output.txt")Was this page helpful?