Uh oh!
There was an error while loading. Please reload this page.
Add per-turn token usage tracking with cost analytics - #37
Merged
Conversation
- New migration v020: session_usage table storing per-turn token counts - New UsageStore mixin with aggregate queries (by period, source, cache hit rate) - engine.py: persist usage per turn + accumulate total_cost_usd on sessions - Diagnostics endpoint: 7-day usage summary, daily chart data, source breakdown - Frontend: Usage & Cost section in DiagnosticsPage with bar chart and source table - Session sidebar: show estimated cost under session title when > $0.01 - Cascade delete session_usage rows when sessions are deleted
…on-cached The SDK's input_tokens field only counts fresh (non-cached) input. Total input = input_tokens + cache_read + cache_creation. The old formula divided cache_read by just input_tokens (6), giving 7878900%.
- Fix context bar: total used now includes cached tokens (was under-counting) - Add Cost section to hover: this turn's estimated cost, session total, cache hit rate - Rename "Input tokens" to "Fresh input" for clarity
The SDK reports cumulative tokens across all API sub-calls in a turn (each tool use triggers a new call). A turn with 3 tool calls shows ~2.4M cache_read even though each call only uses ~700K of the 1M window. Estimate the number of sub-calls from total/max ratio and divide to approximate single-call context occupancy. Shows ~N sub-calls in hover when turn involved multiple API round-trips.
- Migration v021: add model, cost_usd, duration_ms, duration_api_ms, num_turns, web_search/fetch columns to session_usage - Capture ResultMessage fields (total_cost_usd, durations, num_turns) and AssistantMessage.model in engine.py - Use SDK-provided cost when available, fall back to model-specific pricing (8 tiers matching Claude Code's modelCost.ts) - Fix context bar: divide by num_turns (not broken ceil heuristic), exclude output tokens from context window estimation - Add per-model usage breakdown to diagnostics endpoint and UI - Diagnostics uses SDK cost_usd directly instead of re-estimating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
session_usagetable stores per-turn token counts (input, output, cache creation, cache read, max context) — one row per agent turn, ~60 bytes eachtotal_cost_usdfinally populated: The dead column from v003 now accumulates estimated cost per session using Opus 4 pricing ($15/M input, $75/M output, $1.50/M cache read, $18.75/M cache write)$X.XXcost under session title when cost > $0.005session_usagerows cleaned up when sessions are deletedPurely additive — no existing behavior changes. One INSERT per agent turn (~1ms on SQLite).
Test plan