Skip to content

#17: surface cache_write_tokens so cost reconciles from shown tokens - #149

Merged
anilmurty merged 2 commits into
mainfrom
fix/17-cache-write
Jun 19, 2026
Merged

anilmurty merged 2 commits into
mainfrom
fix/17-cache-write

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

Closes #17 (from the v0.3.5 smoke test #141 — MEDIUM; highest end-user value).

On cache-heavy Opus traffic, cache-write is ~91% of cost but was invisible
above the DB
: the traces API omitted the field and the UI / tj cost never
showed cache tokens, so a displayed cost couldn't be derived from its own
columns. The reporter's span:

input 2 · output 465 · cache_read 243597 · cost $1.442306  (claude-opus-4-8)
→ ~209k cache-WRITE tokens account for the missing ~$1.31

Fix

  • traces API (_span_to_dict) returns cache_write_tokens alongside cache_tokens (read).
  • UI trace-detail panel + waterfall tooltip render Cache read + Cache write — per-span cost now reconciles on screen.
  • Aggregate: CostRow + get_cost_summary sum cache-read/write; /api/v1/cost rows carry both + window totals; the UI Cost table and tj cost gain CACHE R / CACHE W columns (tool grouping stays cost-only).

Tests

Trace detail + /api/v1/cost expose cache-write (the reporter's exact span reconciles); UI guard for the rendered fields. Full suite green (651); ruff/mypy clean.

Note: built in an isolated git worktree to avoid the shared-HEAD collisions we'd been hitting.

anilmurty and others added 2 commits June 18, 2026 17:15
On cache-heavy Opus traffic, cache-write is ~91% of cost but was invisible
above the DB — the traces API omitted the field and the UI/tj cost never showed
cache tokens, so a displayed cost couldn't be derived from its own columns
(a trust problem for a cost tool).

- traces API (_span_to_dict) now returns cache_write_tokens alongside
  cache_tokens (read).
- UI trace-detail panel + waterfall tooltip render Cache read + Cache write.
- CostRow + get_cost_summary sum cache-read and cache-write; /api/v1/cost rows
  carry both + window totals; the UI Cost table and
                             TOKENS     TOKENS
  DATE       AGENT   MODEL   IN         OUT       CACHE R   CACHE W   COST
 ──────────────────────────────────────────────────────────────────────────────
  2026-06…   -       -       128.3k     1.1M      494.6M    0         $358.26…
  2026-06…   -       -       172.4k     1.6M      483.9M    2.8M      $361.65…
  2026-06…   -       -       5.5k       682.1k    395.7M    36.9k     $274.00…
  2026-06…   -       -       125        66.9k     38.5M     0         $47.9370
  2026-06…   -       -       635        180.7k    99.6M     0         $85.3886
  2026-06…   -       -       366        164.5k    65.7M     0         $66.3781
  2026-06…   -       -       45         30.8k     27.3M     0         $21.3287
                             307.4k     3.8M      1605.2M   2.9M      $1214.9…
                                                                                 table gain
  CACHE R / CACHE W columns (tool grouping stays cost-only — no token dimension).

Tests: trace detail + /cost expose cache-write (the reporter's exact span
reconciles); UI guard for the rendered fields. Suite green; ruff/mypy clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@anilmurty
anilmurty merged commit 71f1a2c into main Jun 19, 2026
4 checks passed
@anilmurty
anilmurty deleted the fix/17-cache-write branch June 19, 2026 00:38
HoomanDgtl pushed a commit to HoomanDigital/tokenjam that referenced this pull request Jun 20, 2026
Two bugs surfaced by the v0.4.0 pre-release test pass against an
install with the daemon running, plus the testing-infra MD files that
caught them.

## Bugs fixed

**`tj cost` cache columns silently 0 with daemon up.** PR Metabuilder-Labs#149 added
`cache_tokens` + `cache_write_tokens` to the DB row, API response, and
CLI rendering — but missed the `ApiBackend` shim that the CLI uses
when `tj serve` is running. The shim dropped the cache fields on the
floor, so they defaulted to 0 even though the JSON response carried the
real values. Two-line addition to mirror the wider contract.

**`tj optimize` silently skips the Downsize section when no candidates
exist.** The Optimize web tab fixed this in PR Metabuilder-Labs#130 (issue Metabuilder-Labs#126) by
rendering an explicit "no candidates" state. The CLI render path was
overlooked. When other analyzers find content (cache, script, trim, …),
the catch-all "No candidates flagged" message doesn't fire either, so
Downsize disappears without explanation — reviewers think the analyzer
didn't run. Now mirrors the UI: explicit "Downsize: no candidates in
this window — sessions don't match the smaller-model shape" empty
state. Guarded by the positional `findings` filter so
`tj optimize cache` (asking for just cache) doesn't add Downsize noise.

## Testing infrastructure (new)

- `tests/manual-pre-release-runner.md` — generic instructions for a
  sub-agent: find the release-specific checklist, walk each step,
  capture output, write a structured log to `tests/results/`.
- `tests/manual-pre-release-v0.4.0.md` — focused 12-step checklist for
  the v0.4.0 release. Each step has the structured shape the runner
  expects (Setup / Test / Expected / Assertions).
- `.gitignore`: `tests/results/` — per-run logs are local artifacts,
  not committed.

## Verification

- `pytest tests/unit/ tests/integration/`: 659 passed.
- Live with daemon: `tj cost --since 30d --group-by model` now shows
  `CACHE R 698.8M`, `1530.9M`, etc. instead of `0`.
- Live with daemon: `tj optimize --since 30d` prints "① Downsize: no
  candidates in this window …" when downsize finds nothing.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
HoomanDgtl pushed a commit to HoomanDigital/tokenjam that referenced this pull request Jun 20, 2026
TokenJam v0.4.0 — major release. Significant new product surface beyond
the 0.3.x patch cadence:

- TokenJam Lens UI rebrand: Overview triage front door, Optimize detail
  tab, real charts (uPlot, vendored offline), URL-state filters,
  plan-tier-aware framing on all dollar-bearing surfaces.
- Reuse — the 5th analyzer + `tj report --reuse` artifact export
  (HTML + Markdown skeleton sidecars).
- New `core/framing.py` — single source of truth for plan-tier
  rendering, consumed by both CLI and REST API.
- `estimated_recoverable_usd` contract on every savings analyzer
  (Metabuilder-Labs#111), normalized to a single time basis (Metabuilder-Labs#122) so Overview tiles
  are directly comparable.
- `cache_write_tokens` surfaced through DB → API → CLI → UI (Metabuilder-Labs#149) —
  fixed the ~91% hidden cost driver on cache-heavy workloads.
- Security: `.tj/config.toml` untracked (Metabuilder-Labs#145) + CI guard
  (`tests/unit/test_no_tracked_dev_secrets.py`).
- 17 v0.3.5 post-release findings closed (Metabuilder-Labs#141) + 5 Lens UI bug rounds
  (Metabuilder-Labs#126-Metabuilder-Labs#136) + onboard `--plan` honored on plain path (Metabuilder-Labs#148).

## Also in this commit

**Step 6 in the pre-release checklist** is corrected to match real
behavior: `tj report --reuse` with the daemon running exits with the
"needs direct database access" hint (workaround: `tj stop`). Long-term
fix tracked as Metabuilder-Labs#154 for v0.4.1.

**Test outputs committed.** `tests/results/` was gitignored; flipping
it so per-release pre-release test logs ship as part of the repo for
historical reference. Two v0.4.0 result logs included: the first pass
(which surfaced two bugs fixed in PR Metabuilder-Labs#153) and the clean re-run after
those fixes landed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.

1 participant