TokenTracker icon indicating copy to clipboard operation
TokenTracker copied to clipboard

zcode source: input_tokens includes cached tokens (inclusive semantics) — cache double-counted, same class as the Qoder normalizeQoderTokens fix

Open CuteSamurai24 opened this issue 3 months ago • 3 comments

Summary

The zcode source (Z.ai's coding agent, an OpenCode fork — parsed via readZcodeDbMessages → the shared opencode-DB path) reports inclusive token semantics: its input_tokens already includes cache-read tokens (and output_tokens includes reasoning). Upstream OpenCode's DB uses exclusive semantics. Because the shared parser only renames fields without subtracting, cached context is double-counted as fresh input for zcode — same bug class the project already fixed for Qoder in normalizeQoderTokens (src/lib/rollout.js ~L4750, comment: "Qoder's prompt_tokens already includes cached_tokens … otherwise cached context is counted twice in dashboards and cost calculations").

Environment

  • TokenTracker CLI tokentracker-cli 0.91.1, Windows 10, install at ~/.tokentracker
  • zcode v0.16.5 (Z.ai coding agent), GLM-5.3 via zcode plan; DB at ~/.zcode/cli/db/db.sqlite

Evidence that zcode's DB semantics are inclusive

All checks done read-only against ~/.zcode/cli/db/db.sqlite:

  1. part table, step-finish rows: total_tokens == input_tokens + output_tokens holds 400/400 sampled rows, with input_tokens >= cache_read_tokens in 400/400 — i.e. input contains cache-read. Sample row: total=140545, in=138174, out=2371, rw=6494, cr=136320.
  2. model_usage (31,695 completed rows): input_tokens < cache_read_tokens occurs 0 times; cache_write_tokens != 0 occurs 0 times; provider_total != input + output occurs 0 times.
  3. TokenTracker's own queue.jsonl: all 4,230 zcode rows satisfy cached <= input. By contrast, exclusive-semantics sources routinely have cached > input (e.g. dsh: 2,684 of 2,879 rows) — confirming zcode diverges from the rest of the opencode family.
  4. Observed cache-hit for GLM-5.3 over the last 7 days: cr/in ≈ 95.3% — consistent with inclusive input.

Impact

  • Any metric that treats input_tokens as fresh input is wrong. Example hourly bucket (2026-08-31T11:00, zcode/GLM-5.3): input=38,306,473, cached=37,289,088, output=80,503 → true fresh input is 1,017,385 (~1/38th of what the column implies).
  • Cost calculations that price cache-read cheaper than fresh input overcharge by cached × (input_price − cache_price). With ~95% cache hit, the input-side cost is inflated roughly 2× for zcode.
  • Downstream analytics (cache-hit rate, output/input yield) computed from the queue fields are distorted.

Suggested fix

Mirror the existing Qoder treatment for the zcode source, e.g. a normalizeZcodeTokens-style step (or subtract inside readZcodeDbMessages, src/lib/rollout.js ~L4417, which currently only filters provider IDs):

input_tokens: Math.max(0, input - cacheRead),   // report only the fresh remainder
cached_input_tokens: cacheRead,

total_tokens/billable stay consistent since inclusive input + output == (input - cr) + cr + output.

Happy to send a PR if the approach sounds right — the fix looks to be ~10 lines plus wiring, following normalizeQoderTokens as the template.

CuteSamurai24 avatar Aug 31 '26 16:08 CuteSamurai24

001-280

linear[bot] avatar Aug 31 '26 16:08 linear[bot]

Confirmed. ZCode's legacy OpenCode rows use inclusive parent counters: cache read/write are already included in input, and reasoning is already included in output. Feeding those rows into the shared OpenCode parser currently counts the subsets twice.

I have prepared a fix that:

  • normalizes both legacy and native ZCode rows into TokenTracker's disjoint token columns;
  • preserves the historical boundary introduced by #545;
  • runs a one-time repair that retracts previously queued ZCode totals, clears the ZCode cursor/buckets, and replays the corrected data without touching other providers.

The targeted parser/migration suite and the full Node suite pass locally. This is not shipped yet, so I am keeping the issue open until the patch lands and is released.

xiufengsun avatar Sep 02 '26 01:09 xiufengsun

Closing because the root cause is fixed and the parser/migration regression coverage passes. The change is validated locally but is not in a released build yet; release availability will follow the normal shipping workflow.

xiufengsun avatar Sep 02 '26 01:09 xiufengsun