Skip to content

fix(responses): emit input_tokens_details / output_tokens_details in Responses usage#229

Merged
dwgx merged 1 commit into
dwgx:masterfrom
warelik:fix/usage-details
Jul 25, 2026
Merged

fix(responses): emit input_tokens_details / output_tokens_details in Responses usage#229
dwgx merged 1 commit into
dwgx:masterfrom
warelik:fix/usage-details

Conversation

@warelik

@warelik warelik commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

中文 TL;DR

Responses usage 之前只有 input/output/total,缺 *_tokens_details。补齐
input_tokens_details / output_tokens_details(text 由 total 减 cached/reasoning 推导,
上游若给了显式 text_tokens 则优先)。

Summary

Spec-completeness for the OpenAI Responses API usage object, which strict
consumers (Grok Build, codex-style clients) expect to carry the *_tokens_details
breakdown.

Fix

  • src/handlers/responses.js mapUsage: emit input_tokens_details
    (text / audio / image / cached) and output_tokens_details
    (text / audio / reasoning), deriving text_tokens by subtracting
    cached / reasoning from the totals and preferring an explicit upstream
    text_tokens when present.

Test plan

  • test/responses.test.js +2: non-zero cached/reasoning subtraction, explicit
    text_tokens precedence; existing usage assertions updated to the richer shape
  • Full suite: npm test2763 pass / 0 fail

Branch is on current master (v3.5.0), applies cleanly.

The Responses usage object only carried input/output/total tokens; strict
consumers (Grok Build, codex-style) expect the *_tokens_details breakdown.

- src/handlers/responses.js: mapUsage now emits input_tokens_details
  (text/audio/image/cached) and output_tokens_details (text/audio/reasoning),
  deriving text_tokens by subtracting cached/reasoning from the totals and
  preferring an explicit upstream text_tokens when present
- test/responses.test.js: +2 — non-zero cached/reasoning subtraction, explicit
  text_tokens precedence; existing usage assertions updated to the richer shape
@dwgx
dwgx merged commit 581927c into dwgx:master Jul 25, 2026
5 checks passed
dwgx added a commit that referenced this pull request Jul 25, 2026
- contributors.json: 新增 6 条(#224 S / #225 A+ / #226 S / #227 A / #228 B+ / #229 A)
- README 中英: warelik 段落补本轮六个 PR 的技术叙述
dwgx added a commit that referenced this pull request Jul 25, 2026
@dwgx

dwgx commented Jul 25, 2026

Copy link
Copy Markdown
Owner

已合并,谢谢 🙏 这个 PR 有个你可能还不知道的巧合:它和我这边同期做的一件事正好是一条链的两半

我前两天用付费 teams 账号校准了 DEVIN_CONNECT 响应里 cache 计数的 wire tag(issue #220):cache_read_tokens 是 metadata #7 的 tag 5 —— 免费账号上这个值是 0、protobuf 不编码零值,所以字段根本不出现,一直没法校准。校准之后缓存命中数进得了 chat 层的 prompt_tokens_details.cached_tokens,但当时它到不了 Responses API,因为 mapUsage 只映射三个总数。你这个 PR 正好把出口补上了。

合并后我用实测的真实数字串了一遍端到端:

输入: prompt_tokens=3554, cached_tokens=3328   (真实 A/B 数据)
输出: input_tokens_details.cached_tokens = 3328  ✅
      input_tokens_details.text_tokens   = 226   ✅ (3554-3328)

text_tokens 优先用上游显式值、没有才用减法推导 —— 这个优先级设计对,因为将来上游真给了 text_tokens,减法推导就未必等价了(可能还有别的 token 类别)。

顺带:既然现在出口通了,我把 DEVIN_CONNECT_BILLING_TAGS 的默认值改成了 cache_read_tokens=5(原本 opt-in)。不然默认部署下 cached_tokens 永远是 0,你补的这两个 details 对象就白瞎了。免费账号上零值不编码,所以默认开是安全的。

已加入致谢名单,评级 A。

dwgx added a commit that referenced this pull request Jul 25, 2026
合入 warelik 六连(#224-#229):429 reset window 双链根因(resetMs 传输层透传 +
冷却落到 account-wide,原先 model-scoped 对 modelKey=null 的池选择结构性不可见)、
客户端断连不再罚账号(abort 统一识别,流式/非流式全路径不 failover 不烧配额,499)、
Grok/xAI self-ID 中和(a6-grok)、thinking signature 空串改省略、Responses usage
补 input/output_tokens_details、pair-chain 会话连续性新模块(600 行零依赖,默认关)。

合入 forrinzhao #219 的 codex content-policy 发现:落地为规则 (a7),并修根因 ——
工具描述 preamble 原本注入在 neutralizeClientIdentity 之后,导致 native 路径上
任何经由工具描述进来的触发词都绕过 a1-a6 整条防线。identity-neutralize 现覆盖
Claude Code / Cline / Grok / codex 四个客户端。

DEVIN_CONNECT cache_read_tokens=5 付费校准并改为默认解码(#220):缓存命中不再被
当作新鲜输入计费,与 #229 合起来端到端可见;'off' 可全关。setup.sh 在无 LS 二进制
的主机(macOS 典型)默认写 DEVIN_CONNECT=1 + 回环绑定。

2778/0 绿。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants