跳过主要内容
A

dsh-task-budget

andromder/dsh-task-budget

按任务统计模型调用费用(峰/谷价),把剩余预算换算成每轮提示词里的调用/文件额度,并在超出上限时暂停任务等待用户确认。默认每个任务的上限为 ¥0.5。

安装

dsh plugin --profile web add github:andromder/dsh-task-budget

README

Task budget (dsh-task-budget)

A DeepSeek Harness plugin with a left-sidebar panel for per-task spend, a per-task cost cap, and a live budget brief that is generated for the model on every turn.

What it does

  • Left sidebar → 任务预算 / Task budget. Lists every task (session) with its token totals, actual CNY cost, a budget bar, and status. Click a row to see:
    • every model call: time, peak/off-peak tier, model, input / cache / output tokens, that call's cost, and the running total;
    • the exact budget brief the model sees for that task (collapsible).
  • Per-task cap. One number (default ¥0.5) applies to each new task or conversation. It is never shared across tasks.
  • Hard stop at the cap. The next step is forced to ask you with ask_user_question, and every tool except ask_user_question is denied until a human answers. One reply of yours buys a full continuation allowance: the cap becomes at least spent + continuationStepCny.
  • Approvals survive reloads. Grants are persisted in $DSH_HOME/budget-guard/grants.json, so a plugin/Host restart does not ask again for a task you already approved.

How the budget reaches the model

The prompt has two sections:

  1. Rules (budget-guard:rules, static): real rates, the money scale (¥0.05 ≈ 1 short answer, ¥0.15 ≈ 2-3 calls, ¥0.30 ≈ 5 calls, ¥0.50 ≈ 8-10 calls, ¥1 ≈ 15 calls), and the shortest-path discipline.

  2. Task brief (budget-guard:brief, dynamic, per answering agent): rebuilt for every step with live numbers:

    【本任务预算简报 · 自动生成,优先级高于其他计划】
    上限 ¥2.306|已花 ¥1.306|剩余 ¥1.000|谷时|平均每次调用 ¥0.050(约可再调用 20 次)
    本轮请求:把左侧栏入口的价格显示改成“元/百万 token”…
    判定:小改动(精简执行)|本轮上限:4 次调用 / 1 个文件
    策略:精简执行。直接改完并做 1 次最小验证;最多 4 次调用、1 个文件。
    硬约束:以上次数/文件数是硬上限…;不扫描全仓库…
    

The budget → prompt algorithm

All local and free (no extra model call):

  1. Classify the request from the latest substantive user message: length, keywords (重构/迁移/全量 weigh 2; 新增/实现/修复/测试/文档 weigh 1), multi-file phrases, and whether it is a pure question. Result: answer | small | feature | large.
  2. Measure the real burn rate: average CNY per model call from this task's own committed usage (default ¥0.06 before the first call).
  3. Convert money into a work envelope: affordable = floor(remaining / avg) and pick a strategy — full, lean, minimal, diagnose (diagnose only, no edits), answer (no tools), or stop (not enough for one call).
  4. Emit hard limits into the prompt: maximum model calls, maximum files, search/read limits, the required one-line budget estimate, and the stop rule when less than one call's money remains.

The model therefore sees not just "the cap is ¥0.5", but "you have ¥1.00 left, about 20 calls, this request is a small change, use at most 4 calls and 1 file".

Settings

The panel writes $DSH_HOME/budget-guard/settings.json:

{
  "enabled": true,
  "capCny": 0.5,
  "continuationStepCny": 0.5,
  "warnRatio": 0.8,
  "inputIncludesCache": true,
  "peakWindows": [{ "start": 1, "end": 4 }, { "start": 6, "end": 10 }],
  "holidays": [],
  "models": {}
}
  • peakWindows uses UTC hours (1–4 and 6–10 are 09:00–12:00 and 14:00–18:00 Beijing).
  • models overrides rates, e.g. {"deepseek-flash": {"offPeak": {"cacheHit": 0.02, "cacheMiss": 1, "output": 4}, "peak": {"cacheHit": 0.04, "cacheMiss": 2, "output": 8}}}. A model entry with only cacheHit/cacheMiss/output is priced flat.
  • inputIncludesCache follows DeepSeek accounting: inputTokens already contains cache-hit tokens, so the miss count is input - cacheRead - cacheWrite.

Notes

  • Cost is folded from committed assistant/message usage events, priced at each call's own timestamp (peak/off-peak), so it is reconstructable from the log. Historical sessions are read on demand (the newest 12 are queued when the panel opens; older rows compute when opened) and cached in memory.
  • Real prices: off-peak CNY 0.02 / 1 / 4 per 1M tokens (cache hit / miss / output); peak is double. Peak = Beijing Mon–Fri 09:00–12:00 and 14:00–18:00; weekends and holidays are off-peak all day.
  • Subagent sessions are separate tasks and are listed with a 子任务 badge.

相关插件