Skip to content

[Runtime] Add run budgets and runaway-agent safeguards #17

Description

@helsome

Background

Deep Research / Agent 属于长链路任务,单次任务可能持续进行模型调用、搜索、检索和工具调用。如果缺少统一的运行预算与循环检测,错误规划、provider 异常或重复搜索可能造成任务长时间不结束、成本失控、重复请求和不可解释的卡死。

当前需要把“Agent 最多可以做多少事”变成明确的 runtime contract,而不是依赖每个 workflow 自己约束。

Requirements

建立统一的 Run Budget,至少支持:

  • wall-clock duration;
  • model call count;
  • tool call count;
  • search / retrieval iteration count;
  • input/output token budget(provider 可提供时);
  • estimated / actual cost budget(provider 可提供时)。

同时提供 runaway detection:

  • 连续重复相同 tool call;
  • 相同或近似相同 search query 循环;
  • 无新增 evidence / progress 的连续迭代;
  • retry storm。

运行达到预算或命中 runaway 条件时:

  • 不能直接把任务表现为普通 success;
  • 返回稳定、machine-readable 的 stop_reason
  • 尽可能保存已有 evidence、partial report、trace 和运行状态;
  • 用户可以看见任务为何停止,以及已完成到哪里;
  • 支持合理的默认预算,并允许显式 per-run override;
  • override 仍应有系统级 hard ceiling,避免无限运行。

#14 对接:Langfuse trace / evaluation 中应能记录 budget、consumption、stop_reason 和 loop-detection event。

Acceptance Criteria

  • Agent / Deep Research 使用统一 budget contract,而不是各 workflow 独立 hardcode。
  • 至少能限制 duration、model calls、tool calls、search/retrieval iterations。
  • token / cost 数据可获取时纳入 budget 和 summary。
  • 对明显重复 tool/search loop 有自动检测和终止。
  • 达到 budget 后保留 partial result,并明确标识 stop_reason=budget_exhausted 或等价状态。
  • 正常任务不会因 telemetry/provider 无法提供 token/cost 数据而异常失败。
  • 有 unit/integration tests 覆盖每类 budget 与 loop detector。
  • 必须用真实 provider + production Agent path 跑一个故意设置极低 budget 的任务,确认能够真实终止;另跑一个可诱发重复搜索/工具调用的 case,确认不会无限循环。只 mock 不算验收。
  • PR 附真实运行的调用次数、耗时、stop reason 和 partial result。

Non-goals

  • 不是简单给单个 HTTP request 加 timeout。
  • 不是仅在 UI 增加“停止”按钮;重点是 runtime 自身具备可验证的硬约束。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

claimedClaimed by a contributor and currently in progress

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions