CMDC.Plugin.Builtin.CostGuard (cmdc v0.5.3)

Copy Markdown View Source

P1 成本硬限制插件 — Token 和成本预算控制。

在每次 LLM 请求前(before_request)检查累计 token/cost 是否超预算。 超限时立即 abort Agent,防止成本失控。

配置

{CMDC.Plugin.Builtin.CostGuard,
  max_tokens: 100_000,     # 单次会话最大 token(nil 不限制)
  max_cost_usd: 1.0,       # 单次会话最大成本 USD(nil 不限制)
  warn_threshold: 0.8      # 用量达 80% 时 emit 告警(0.0-1.0)
}

触发 Hook

{:before_request, messages} — 发送 LLM 请求前触发。