Per-model token tracking for mid-session model switches#28842
Closed
yitang wants to merge 1 commit into
Closed
Conversation
Adds usage_by_model TEXT JSON column to sessions table that records per-model token/cost breakdown on every API call, enabling accurate cost attribution and display after mid-session /model switches. Closes NousResearch#28637
Contributor
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 free
to 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.
Closes #28637
Adds a
usage_by_model TEXTJSON column to thesessionstable thatrecords per-model token/cost breakdown on every API call, enabling
accurate cost attribution and display after mid-session
/modelswitches.
Changes
usage_by_model TEXTcolumn onsessions, auto-added toexisting DBs via
_reconcile_columns(), bump SCHEMA_VERSION to 12agent.session_usage_by_modeldict onAIAgentconversation_loop.py— tokens, cache, reasoning, API calls, andcost — then serializes to JSON and persists on every turn
/usage: per-model breakdown when multiple models detected;single-model fallback for legacy sessions
/usage: same breakdown in messaging platforms/insights: model breakdown usesusage_by_modelwhen present,falls back to
modelcolumn for backward compatusage_by_modelincluded in Python → TypeScript payload(display rendering not yet implemented — separate follow-up)
compat fallback, schema version bump
Backward compatibility
_reconcile_columns()adds the column on next startup — zero-downtimeinput_tokens,output_tokens, etc.) areunchanged