feat: leverage modelUsage field for improved cost tracking (v2.1.81) #246

Closed
opened 2026-03-23 14:22:41 -07:00 by hikari · 1 comment
Owner

Context

Claude Code v2.1.81 stream-json result messages now include a modelUsage field with a detailed per-model cost breakdown:

"modelUsage": {
  "claude-opus-4-6[1m]": {
    "inputTokens": 3,
    "outputTokens": 66,
    "cacheReadInputTokens": 6285,
    "cacheCreationInputTokens": 30008,
    "webSearchRequests": 0,
    "costUSD": 0.1923575,
    "contextWindow": 1000000,
    "maxOutputTokens": 64000
  }
}

Why This Matters

Currently src-tauri/src/stats.rs and src/lib/stores/stats.ts calculate costs manually. The modelUsage field provides:

  • Pre-calculated costUSD directly from Claude Code — no manual calculation needed
  • contextWindow and maxOutputTokens per model — useful for context usage display
  • Per-model breakdown for multi-model sessions (e.g. subagents using different models)

This is a significant enhancement opportunity for cost and context tracking accuracy.

Tasks

  • Add modelUsage to the result message type in src-tauri/src/types.rs
  • Update src-tauri/src/stats.rs to prefer costUSD from modelUsage over manual calculation
  • Expose contextWindow / maxOutputTokens for context window progress display
  • Update src/lib/stores/stats.ts accordingly
  • Add tests for the updated stats logic

References

  • Claude Code v2.1.81 stream-json sample output
  • src-tauri/src/stats.rs
  • src/lib/stores/stats.ts
  • src-tauri/src/types.rs

This issue was created with help from Hikari~ 🌸

## Context Claude Code v2.1.81 stream-json `result` messages now include a `modelUsage` field with a detailed per-model cost breakdown: ```json "modelUsage": { "claude-opus-4-6[1m]": { "inputTokens": 3, "outputTokens": 66, "cacheReadInputTokens": 6285, "cacheCreationInputTokens": 30008, "webSearchRequests": 0, "costUSD": 0.1923575, "contextWindow": 1000000, "maxOutputTokens": 64000 } } ``` ## Why This Matters Currently `src-tauri/src/stats.rs` and `src/lib/stores/stats.ts` calculate costs manually. The `modelUsage` field provides: - Pre-calculated `costUSD` directly from Claude Code — no manual calculation needed - `contextWindow` and `maxOutputTokens` per model — useful for context usage display - Per-model breakdown for multi-model sessions (e.g. subagents using different models) This is a significant enhancement opportunity for cost and context tracking accuracy. ## Tasks - [ ] Add `modelUsage` to the `result` message type in `src-tauri/src/types.rs` - [ ] Update `src-tauri/src/stats.rs` to prefer `costUSD` from `modelUsage` over manual calculation - [ ] Expose `contextWindow` / `maxOutputTokens` for context window progress display - [ ] Update `src/lib/stores/stats.ts` accordingly - [ ] Add tests for the updated stats logic ## References - Claude Code v2.1.81 stream-json sample output - `src-tauri/src/stats.rs` - `src/lib/stores/stats.ts` - `src-tauri/src/types.rs` ✨ This issue was created with help from Hikari~ 🌸
Author
Owner

Resolved in PR #263. Added model_usage: Option<serde_json::Value> to ClaudeMessage::Result in types.rs. Field is destructured in the wsl_bridge.rs Result match arm and per-model names are logged at debug level. Full per-model cost breakdown can be wired in a future pass once the data structure stabilises.

Resolved in PR #263. Added `model_usage: Option<serde_json::Value>` to `ClaudeMessage::Result` in `types.rs`. Field is destructured in the `wsl_bridge.rs` Result match arm and per-model names are logged at debug level. Full per-model cost breakdown can be wired in a future pass once the data structure stabilises. ✨
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nhcarrigan/hikari-desktop#246