DSH Plugin Store
Back to home

kanallas

api-cost-meter

DeepSeek Harness API cost meter plugin: peak/off-peak spend badge, current unit prices, official account balance

Stars
0
Language
TypeScript
Created
Aug 14, 2026
Updated
Aug 14, 2026
Other
GitHub repo

Introduction

DeepSeek Harness API Cost Meter

一个显示 DeepSeek API 实际花费DeepSeek Harness 插件:在 Web 界面会话头部显示花费徽标,点开可见当前时段的单价(按 DeepSeek 峰谷定价)与官方账户余额

A DeepSeek Harness plugin that shows the real spend of your DeepSeek API: a badge in the session header, with a popover for current unit prices (peak/off-peak billing) and the official account balance.

gui-badge

功能 / Features

  • 徽标 / Badge(会话头部右侧):优先显示官方数据 —— 官方已消耗(充值总额 − 当前余额)或官方余额;无官方数据时显示本地估算并标注"估算"。 Prefers official data — spent (top-up total − current balance) or balance — and falls back to a clearly labeled local estimate.
  • 弹层 / Popover:
    • 官方账户:余额(充值/赠送)、已消耗;Official account balance and derived spend;
    • 当前时段:高峰(09:00–12:00、14:00–18:00 北京时间)或空闲折扣;Current peak/off-peak window;
    • 当前单价表(每百万 tokens:输入/输出/缓存命中,按时段折算);Current unit prices per 1M tokens, window-factored;
    • 本会话与累计的花费、调用次数。Session and cumulative spend and call counts.
  • 本地计价 / Local pricing:每条 assistant/message 的 usage 按其发生时刻的峰谷窗口计价;数据来自会话日志,可重放、跨重启持久化(含历史冷会话追赶)。Priced per event time under the peak/off-peak window; replayable and persisted.
  • 官方余额 / Official balance:轮询 GET /user/balance,与 LLM 路由共用同一凭据。Polls the official billing endpoint with the LLM route's own credential.

官方数据说明 / About official data

DeepSeek 公开 API 只提供 余额查询(Get User Balance),不提供按 API key / 时间查询的调用与费用明细 API —— 那是网页后台(platform.deepseek.com)的功能,走非公开网页接口。因此本插件的官方数据 = 余额 + 可选的充值总额配置反推已消耗;按 key/时间的调用明细仍以本地日志估算(标注"估算")。

The public DeepSeek API exposes only balance; per-key / per-time usage and cost detail is a web-console feature behind non-public endpoints, so this plugin's official data is balance plus an optional configured top-up total to derive spend. Per-key detail stays a clearly labeled local estimate.

默认价格来源 / Default price source

默认价格表来自官方定价页(模型 & 价格)的 2026-08-17 峰谷价(高峰基准):v4-flash 输入 ¥3 / 缓存命中 ¥0.10 / 输出 ¥9;v4-pro ¥9 / ¥0.30 / ¥27(每百万 tokens,空闲时段半价)。2026-08-17 之前官方对 V4 执行旧价(flash ¥1 / ¥0.02 / ¥2;pro ¥3 / ¥0.025 / ¥6,无峰谷拆分)——若在该时段精确对账,请在 cordis.patch.yml 覆盖 prices(并可将 offPeakFactor 设为 1 按平假计价)。

Defaults follow the official 2026-08-17 peak prices (peak basis) from the pricing page; before that date the V4 models billed at the legacy flat prices — override prices (and set offPeakFactor: 1) for exact reconciliation in that window.

安装 / Install

应用到 DeepSeek Harness 源码检出(root 下应用补丁):

git apply patch/api-cost-meter.patch
pnpm install
pnpm run build:lib
pnpm run build:web
pnpm dsh web        # 打开任意会话,头部右侧出现花费徽标

dsh-web-app bundle 已内置两个新行(api-cost host 服务 与 ui-api-cost 浏览器插件),无需额外配置。已发布安装(DSH_HOME 方式)的 dsh 需要让两个包可被 profile 解析(在 ~/.dsh/profiles/node_modules/@deepseek-ai/ 建立指向构建产物的 junction)。

配置 / Configuration

- id: api-cost
  name: '@deepseek-ai/dsh-api-cost'
  config:
    # 峰谷定价(默认对应 DeepSeek 官方 2026-08-17 价格表,高峰价;空闲自动半价)
    prices:
      deepseek-v4-flash:
        inputYuanPerMTok: 3        # 高峰输入 ¥3/百万(缓存未命中)
        outputYuanPerMTok: 9       # 高峰输出 ¥9/百万
        cacheReadYuanPerMTok: 0.1  # 高峰缓存命中 ¥0.10/百万
      deepseek-v4-pro:
        inputYuanPerMTok: 9
        outputYuanPerMTok: 27
        cacheReadYuanPerMTok: 0.3
    peakWindows:
      - { start: '09:00', end: '12:00' }
      - { start: '14:00', end: '18:00' }
    offPeakFactor: 0.5
    utcOffsetMinutes: 480
    # 官方余额(默认值即 DeepSeek 官方)
    officialBaseURL: https://api.deepseek.com
    officialApiKeyEnv: DEEPSEEK_API_KEY
    # 累计充值总额:配置后弹层显示官方口径"已消耗 = 充值总额 − 当前余额"
    # officialTopUpTotalYuan: 100
    officialRefreshMs: 60000

官方价格变动时请核对 DeepSeek API 定价

架构 / Architecture

包 / Package角色 / Role
packages/llm/api-cost(@deepseek-ai/dsh-api-cost)host 计量服务:纯定价模块、会话事件折叠、持久化全局累计、apiCost 会话投影、apiCost.snapshot Remote、官方余额轮询
packages/client/ui-api-cost(@deepseek-ai/dsh-client-ui-api-cost)浏览器:会话头部徽标与弹层,经 ctx.remote.apiCost.snapshot() 拉取数据

Host 侧数据流:usage-carrying assistant/message 会话事件 → 按 event.time 的峰谷窗口计价 → 逐会话水位增量折叠 → 持久化累计;官方余额由独立轮询读取。客户端 30s 轮询快照,并本地实时推导当前时段。

验证 / Verification

  • pnpm run build:lib 通过;54 个单元/组件测试全过;oxlint 与 pre-commit 门禁通过。
  • 真实数据实测:累计花费、当前时段、4 个模型单价、本会话/累计明细均正确显示;官方余额轮询与失败降级均有测试覆盖。
  • 截图 gui-badge.png 为运行中界面。

License

MIT(与 DeepSeek Harness 一致;插件依赖 @deepseek-ai/* workspace 包,需在 harness 检出内构建)。