Skip to content

Measure prompt-cache usage per turn - #180

Merged
BunsDev merged 9 commits into
mainfrom
sticky-routing
Oct 12, 2026
Merged

BunsDev merged 9 commits into
mainfrom
sticky-routing

Conversation

@BunsDev

@BunsDev BunsDev commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Why

Auto picks a model on every turn and treats a switch as free. On a long thread a switch
re-reads the whole history without the prompt cache (Anthropic's cache is scoped to one model),
so the "cheaper" model is often the more expensive turn. Before changing routing, Modex needs to
see what each turn actually read from the cache. This is part 1 of
sticky Auto routing
(plan). Routing behavior is unchanged.

What changes

  • TurnResult.usage (backends/usage.ts): the first model call's cache read / cache write /
    uncached input, the last call's total input (what the next turn re-reads), output tokens and
    call count.
    • Claude: message.usage on each assistant stream message, each message id counted once,
      sub-agent messages (parent_tool_use_id) excluded, follow-up turns after background work
      included; output from each result.
    • Codex: thread/tokenUsage/updated for the turn's own thread; output is the change in the
      thread's running total, so a repeated notification cannot double count.
  • Thread.lastTurn: model, effort, fast mode, end time and context size of the last turn that
    reached a model. Written after any turn that made a call (or completed without usage);
    cleared on a backend switch.
  • Auto's routing records (routing-fit.json) add idle time and context size at decision time
    and the turn's first-call cache figures afterwards.
  • Fixtures are trimmed real captures (ids and token counts only) from claude 2.1.296 and
    codex-cli 0.162.1.

Evidence gathered on the way

  • A resumed claude -p session keeps its session id. Claude Code writes 1-hour cache entries.

  • Codex inputTokens includes cached tokens; one notification per response.

  • Cache probe (same session, one change per turn):

    CLI same settings effort only model fast
    Claude (opus → sonnet) warm after the first resume kept (36,905 of 36,972 read) rebuilt (10,584 read, 26,734 written) not measurable: --settings {"fastMode":true} reported speed: standard
    Codex (gpt-6.1-sol → gpt-6-astra) 53,760 of 53,950 read kept (53,760 read) rebuilt (8,960 of 64,082 read) default model has no fast tier

Verification

  • npm run build && npm test: 857 tests, 844 passed, 13 skipped (Windows-only), 0 failed
  • npm run test:e2e -w @modex/desktop: 242 passed, 3 skipped (real OpenKnowledge cases, environment-gated)

Auto picks a model on every turn and treats a switch as free. On a long
thread a switch re-reads the whole history without the prompt cache, so
the "cheaper" model is often the more expensive turn. The spec measures
cache usage first, then holds the model while the cache is warm.

Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
Coven-Harness: Claude Code / claude-opus-5-5
@vercel

vercel Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
modex Ready Ready Preview Oct 12, 2026 6:38am UTC

Request Review

@BunsDev
BunsDev merged commit ef17fc8 into main Oct 12, 2026
9 of 11 checks passed
@BunsDev
BunsDev deleted the sticky-routing branch October 13, 2026 12:48

This branch was successfully deployed

1 active deployment
Preview — 276d02af Deployed Oct 12, 2026 by vercel[bot]
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