Skip to content

[Bug][claude] #1315 drops usage.cache_creation (ephemeral_5m/1h breakdown) → Claude Code VS Code prompt-cache countdown icon disappears #4072

Closed

Description

@vinhch

Description *

Since routing Claude Code through bili, the VS Code extension no longer shows the 5-minute prompt-cache countdown icon. Prompt caching itself still works (cache_read_input_tokens stays high). Only the TTL breakdown is lost. Not compression-related: it happens on every turn, with or without a fold.

Expected: the response usage keeps usage.cache_creation.{ephemeral_5m_input_tokens, ephemeral_1h_input_tokens} from the upstream, so the countdown icon renders.

Repro:

  1. Point Claude Code (VS Code extension) at bili in proxy mode: ANTHROPIC_BASE_URL=http://127.0.0.1:<port>/bili/<upstream>
  2. Send any message that creates or reads prompt cache
  3. Countdown icon is missing. In the transcript (~/.claude/projects/<proj>/<session>.jsonl) every usage row has ephemeral_5m_input_tokens: 0, ephemeral_1h_input_tokens: 0 even when cache_creation_input_tokens > 0

How the extension uses it. The webview derives the TTL only from the breakdown object:

// anthropic.claude-code-2.1.304 webview/index.js (minified)
function Pl0($){let J=$?.cache_creation;if(!J)return;
  if((J.ephemeral_1h_input_tokens??0)>0)return"1h";
  if((J.ephemeral_5m_input_tokens??0)>0)return"5m";return}

No cache_creation object, or both fields 0, gives a TTL of undefined. The countdown state becomes unknown and the icon is hidden.

Root cause (dist/index.js 0.1.202):

  1. neutralizeStartUsage() (fix: anthropic stitched terminal as the sole usage authority (#1310) #1315) replaces message_start.message.usage with {input_tokens:0, output_tokens:0}. Anthropic puts the cache_creation object in exactly this frame, so it is discarded.
  2. buildTerminal() re-emits input_tokens, output_tokens, cache_read_input_tokens, and cache_creation_input_tokens, but not the cache_creation object.
  3. The string ephemeral_ occurs nowhere in dist/index.js.

#1315 anticipated this class of breakage ("If any host … reads usage ONLY from message_start … its usage display would now read 0"). Here the lost piece is the breakdown, not the totals.

Evidence (transcript usage rows, same upstream):

Session Via bili cache_creation_input_tokens cache_read_input_tokens ephemeral_5m ephemeral_1h
A no 151696 0 151696 0
A no 417 153690 417 0
B yes 66756 107541 0 0
C yes 7551 77565 0 0

Suggested fix:

Test idea: extend the #1310 non-stitched companion test to assert that the terminal carries the same cache_creation object as the upstream start frame.

bili version *

0.1.202

Usage mode *

Proxy mode (client manually points its base URL at bili)

Client and version *

claude code — VS Code extension 2.1.304 (win32-x64)

Operating system *

Windows 11 Pro (not WSL2)

Upstream and model *

Anthropic-compatible relay (localhost) · claude-opus-5-5 · 1M

Compression-related config (optional)

# env only, no config-file changes
BILI_NO_CACHE_CONTROL: "1"

Log excerpt (strongly recommended)


/acp or acp_status output (optional)


Activity

  1. ranxianglei commented on Oct 21, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (ai: vllm-qwen/qwen3.8-27b)

    查重: 不重复。 按失败签名(cache_creation / ephemeral_5m / countdown icon)检索 open+closed issues/PR:相关条目只有 #1315(引入本缺陷的改动,已合并)和 #1310(原始双计问题),均不覆盖「breakdown 对象丢失」这个缺陷本身。

    标签: bug, claude-code, scope/wiring, p2(缓存功能本身正常,仅 TTL 倒计时图标消失——明显可见但有绕行;非缓存泄露类,不升 p1)

    审核结论(代码级核验,你的根因分析完全成立)

    对照 v0.1.202 源码逐条复现了你列的三个事实:

    1. neutralizeStartUsage()(fix: anthropic stitched terminal as the sole usage authority (#1310) #1315 所加)确实把 message_start.message.usage 整体改写为 {input_tokens:0, output_tokens:0}(src/loop/adapter-anthropic.ts)。Anthropic 恰好把 cache_creation breakdown 对象放在这一帧里 → 随中性化一起被丢弃。
    2. buildTerminal() 只重发 input_tokens / output_tokens / cache_read_input_tokens / cache_creation_input_tokens,不重发 cache_creation 对象。
    3. 全仓 grep ephemeral_ 零命中——这个字段从未被建模过。

    层次判断:#1315 要修的是「总量双计」(start 帧与合成 terminal 各带一份 totals,host 相加爆窗),所以把 start 帧用量置零、totals 归 terminal 独享。 casualties 是另一个数据维度:breakdown 对象在 wire 上没有第二个载体,start 一置零它就没了。你的修法方向正中要害——loop 本来就取最后一轮的 usage 进 terminal(stitched 场景下就是 fold 后重发那轮),把这个对象沿同一条路原样带过去即可,stitching 语义免费正确;且 start 帧保持中性,#1310/#1315 的双计保护不受影响。

    开工中,计划改动面:

    • src/loop/adapter-anthropic.ts:parseStream 捕获 usage.cache_creation(start 帧原样取;delta 不完整帧仅在含正值时采纳,防 relay echo 全零 schema 形状污染);buildTerminal 增加该字段,紧邻 cache_creation_input_tokens 发出。
    • src/loop/core.ts:usage 事件/完成选项类型加一个可选字段(纯类型管道)。
    • 测试:扩展 [bug] claude native: stitched-stream usage double count after in-stream compress locks session ('Prompt is too long') — #300 variant #1310 non-stitched companion test(terminal 携带与 upstream start 帧相同的 cache_creation 对象)+ stitched 变体(末轮胜出)+ 两个负例(upstream 无 breakdown → terminal 不带该键;全零 echo → 不采纳)。

    完成后在本帖回 PR 链接。

  2. ranxianglei commented on Oct 21, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (ai: vllm-qwen/qwen3.8-27b)

    修复完成:PR #4074(Closes #4072),分支 2026-10-21_anthropic-cache-creation-breakdown,已 rebase 到最新 master,等 CI。

    结构化八问

    1. 是什么问题:经 bili 代理后,每一轮响应的 usage 都丢失 Anthropic 的 cache_creation breakdown 对象(ephemeral_5m_input_tokens / ephemeral_1h_input_tokens)。Claude Code VS Code 的 prompt-cache TTL 倒计时图标只从该对象推导 → 图标消失(缓存命中本身正常)。
    2. 什么原因导致的:fix: anthropic stitched terminal as the sole usage authority (#1310) #1315 把 message_start.message.usage 中性化为零值,让合成 terminal 成为唯一 usage 权威(防总量双计)。但 breakdown 对象只存在于这一帧,而 terminal 重发时只带了四个数字字段、没给它留载体——它随中性化一起被丢弃。不是「总量被过度清零」,是「这个数据维度没有载体」。
    3. 影响范围与严重程度:所有经 bili proxy 模式走 anthropic 上游的轮次(有折叠无折叠都触发);受影响的是读 breakdown 的宿主(已确认 Claude Code VS Code 倒计时图标);usage 其余字段不受影响。严重度:功能降级但有绕行(缓存照常工作,仅图标/TTL 展示丢失)→ p2。
    4. 怎么修复的:每轮从 start 帧原样捕获 usage.cache_creation(无条件);不完整 delta 帧仅在对象含正值时采纳(与数字字段同族的 relay 全零 echo 防护,Redesign #408 host-usage backfill: post-fold by default, baseline opt-in (opencode gap + whack-a-mole exemptions) #660 同类);terminal 在 cache_creation_input_tokens 旁重发该对象;stitched 场景取末轮——与 totals 的选择规则一致;从不合成:上游没有就不带这个键。start 帧保持中性不变。
    5. 是否有回归问题:逐项审过既有行为——① [bug] claude native: stitched-stream usage double count after in-stream compress locks session ('Prompt is too long') — #300 variant #1310/fix: anthropic stitched terminal as the sole usage authority (#1310) #1315 双计保护:start 帧仍是 {input_tokens:0, output_tokens:0},terminal 仍是唯一带 usage 的帧(测试断言钉住 usageFrames.length === 1 且 start 上无 cache_creation 键);② 数字字段采纳规则(>0 门、完整对象原子采纳)未动,新字段独立跟随同一规则;③ openai/responses/google 车道:字段可选且恒为 undefined,输出零差异;④ 错误路径 emitError 不传该参数,行为不变。结论:无回归。
    6. 是否破坏缓存:不涉及前缀缓存/重试/增量游标——cache_control 标记、请求字节、折叠判定全部未动;改变的只是客户端可见 usage JSON 里一个新增键。
    7. 是否改了设计方向:没有。「fix: anthropic stitched terminal as the sole usage authority (#1310) #1315 单一 usage 权威」的设计原样保留,只是在该设计下给 breakdown 维度补上缺失的载体,没有引入新数据流或新权威源。
    8. 是否引入新配置:没有。无 env/config/CLI 变更。

    行为变更披露(additive):anthropic 车道的合成 terminal usage 在上游提供 breakdown 时新增携带 cache_creation 对象(与上游 start 帧逐字节一致)。旧值:键不存在 → 新值:上游提供时存在。读它的客户端受益,忽略未知键的客户端无感。

    验证状态:本地定向测试全绿(loop-adapters 38/38 含 3 个新用例+扩展的 #1310 companion、host-usage-postfold 27/27、issue1766-terminal-extra + proxy-stream-error + plugin-terminal-usage 23/23),typecheck clean;全量回归与真实客户端 e2e 交给 CI。CI 绿即可合并 PR #4074。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions