# billion-context request billing / 逐次请求计费

## English

**dsh-cost-meter 1.8.28 connects directly to original billion-context 0.1.181 or later. No companion archive is required.** Keep the original plugin installed, update dsh-cost-meter, and fully restart DSH so its local proxy starts with metering enabled.

### Automatic compatibility

The adapter checks the installed proxy and native request interfaces when the local proxy starts. Compatible later releases are accepted without a version allowlist. A source fingerprint verifies that the process loads the inspected code; it does not restrict supported release numbers.

- **0.1.180 or earlier:** a popup asks the user to update to **0.1.181 or later**.
- **Changed required interfaces:** metering adaptation is skipped, ordinary DSH billing continues, and a popup offers **Community group** and **Open an issue**. The group link opens this project's existing WeChat group section.
- **An older proxy is already running:** restart DSH and its local billion-context proxy. Reloading only the cost plugin cannot add observers to a process already started without them.
- Closing a notice suppresses repeats for the same version and reason. New problems can prompt again; a transient connection error alone is not classified as a code change.

The integration adds observers in memory when the verified local Node proxy starts. It does not rewrite billion-context's files, replace the installed plugin, set global `NODE_OPTIONS`, or change model routing. The original native interceptor remains in use. This integration requires Node 20.6 or later.

### What it records

- Consumed upstream requests, including compression, HTTP retries and additional model rounds.
- Uncached input, cache reads, cache writes, output and reasoning usage across OpenAI Chat Completions, Responses, Anthropic Messages and Google GenerateContent, including streams.
- Actual upstream origin/model, request identity, attempt, completion status, owning DSH conversation and active turn/step.

After durable proxy records are confirmed, those requests replace the corresponding ordinary DSH usage charge. Producer sequences and ledger receipts prevent repeated imports. Main/subagent totals, daily/week/all statistics, turn details and step views share these records and historical price versions. Inherited fork history is not charged again.

Main requests retain the configured DSH provider price. Internal models use their actual upstream/model. Zero usage, missing usage, partial responses and missing prices remain distinct. Charges are not inferred from savings percentages or balance changes. The correlation header is removed before forwarding to a model endpoint.

### Storage and coverage

Only billing metadata is saved under `<ledger>.billion-context`: no prompts, returned text, tool payloads, request headers or credentials. Restore these receipts together with a ledger backup. The producer and DSH must share local files; remote proxies and internal calls missing from historical logs cannot be recovered by this integration.

The old compatibility-archive generator remains a development utility for existing deployments. It is no longer a user installation step.

Checks use original npm packages, isolated local responses, both plugin loading orders, source-file hashes, compression/retries, concurrent agents and reloads. They do not call paid models. Future-version acceptance and changed-interface rejection use separate synthetic fixtures.

## 中文

**费用插件从 1.8.28 起直接接入原版 billion-context，支持 0.1.181 及更新版本，无需额外安装配套兼容包。**保留已安装的原版插件，更新费用插件，然后完全重启 DSH，让本地代理启动时自动接入计费。

### 自动兼容与提醒

接入时检查原版代理和原生请求拦截的实际接口，不再使用版本白名单。后续版本保持相关接口时自动接入；源码校验值只用于确认启动时加载的是已检查的代码，不限制版本号。

- **0.1.180 及更早版本：**弹窗提示升级到 **0.1.181 或更新版本**。
- **必需接口变化：**停止接入内部计费，保留普通 DSH 计费，并弹窗提供“交流群”和“提交 issue”。群入口跳转到本项目已有的微信群区域。
- **旧代理已经运行：**完全重启 DSH 及其本地 billion-context 代理。仅更新或重载费用插件，无法给已经启动且未接入的代理补装运行中的观察器。
- 同一版本、同一原因的提醒关闭后不再重复弹出；出现新问题时再次提醒。临时连接失败不会直接判定为接口变化。

接入只在已验证的本地 Node 代理启动时加载内存中的计费观察器，不修改原版插件文件，不替换安装包，不设置全局 `NODE_OPTIONS`，不改变模型路由。继续使用原版的请求拦截功能。此接入需要 Node 20.6 或更新版本。

### 统计内容

记录内部压缩、HTTP 重试和追加请求，以及未缓存输入、缓存读取、缓存写入、输出和推理用量；支持四种模型协议的 JSON 与流式响应。每条记录关联实际上游、模型、请求、重试次数、完成状态、所属 DSH 会话和轮次／步骤。

收到持久化回执后，逐次请求替换对应的普通 DSH 汇总记账。调用序号和账本入账凭据防止重复收费。会话金额、子代理、日／周／全部统计、单轮和步骤明细共用记录，并沿用保存的历史价格版本；分叉继承的历史不重复计费。

主请求沿用 DSH 渠道配置价格，内部模型按实际上游和模型匹配。零用量、缺失用量、部分回执和未定价分别显示，不按节省比例或余额差补算。关联标识在转发模型端点前删除。

### 数据范围

计费记录保存在 `<ledger>.billion-context`，不保存对话正文、工具内容、请求头或密钥。恢复账本时一并保留这些入账凭据。代理与宿主需要共享本地文件；远程代理及启用前缺失的内部记录无法通过此接入补齐。

旧的兼容包生成器仅保留为开发工具，已使用旧配套包的环境仍可继续工作；新用户和原版升级不需要使用它。

验证使用实际 npm 原包和隔离的本地响应，检查两种插件加载顺序、原包校验值、压缩与重试、并发子代理及重载。未来版本接入和接口变化拒绝使用模拟代码验证，没有调用付费模型。
