-
Notifications
You must be signed in to change notification settings - Fork 52.3k
fix(dashboard): query session_model_usage for accurate model analytics (#71778) #71802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JonthanaHanh
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
JonthanaHanh:fix/dashboard-models-analytics-session-model-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16814,24 +16814,54 @@ def _get_models_analytics(days: int = 30, profile: Optional[str] = None): | |
| try: | ||
| cutoff = time.time() - (days * 86400) | ||
|
|
||
| cur = db._conn.execute(""" | ||
| SELECT model, | ||
| billing_provider, | ||
| SUM(input_tokens) as input_tokens, | ||
| SUM(output_tokens) as output_tokens, | ||
| SUM(cache_read_tokens) as cache_read_tokens, | ||
| SUM(reasoning_tokens) as reasoning_tokens, | ||
| COALESCE(SUM(estimated_cost_usd), 0) as estimated_cost, | ||
| COALESCE(SUM(actual_cost_usd), 0) as actual_cost, | ||
| COUNT(*) as sessions, | ||
| SUM(COALESCE(api_call_count, 0)) as api_calls, | ||
| SUM(tool_call_count) as tool_calls, | ||
| MAX(started_at) as last_used_at, | ||
| AVG(input_tokens + output_tokens) as avg_tokens_per_session | ||
| FROM sessions WHERE started_at > ? AND model IS NOT NULL AND model != '' | ||
| GROUP BY model, billing_provider | ||
| ORDER BY SUM(input_tokens) + SUM(output_tokens) DESC | ||
| """, (cutoff,)) | ||
| # Query session_model_usage instead of sessions for accurate | ||
| # per-API-call model+provider attribution. The sessions table only | ||
| # records the *final* billing_provider after a /model switch, so | ||
| # mid-session switches misattribute all tokens to one provider. | ||
| # session_model_usage tracks every API call individually. | ||
| # (Fixes #71778) | ||
| try: | ||
| cur = db._conn.execute(""" | ||
| SELECT u.model, | ||
| u.billing_provider, | ||
| SUM(u.input_tokens) as input_tokens, | ||
| SUM(u.output_tokens) as output_tokens, | ||
| SUM(u.cache_read_tokens) as cache_read_tokens, | ||
| SUM(u.reasoning_tokens) as reasoning_tokens, | ||
| COALESCE(SUM(u.estimated_cost_usd), 0) as estimated_cost, | ||
| COALESCE(SUM(u.actual_cost_usd), 0) as actual_cost, | ||
| COUNT(DISTINCT u.session_id) as sessions, | ||
| SUM(COALESCE(u.api_call_count, 0)) as api_calls, | ||
| 0 as tool_calls, | ||
| MAX(u.last_seen) as last_used_at, | ||
| AVG(u.input_tokens + u.output_tokens) as avg_tokens_per_session | ||
| FROM session_model_usage u | ||
| JOIN sessions s ON s.id = u.session_id | ||
| WHERE s.started_at > ? AND u.model IS NOT NULL AND u.model != '' | ||
| AND u.task = '' | ||
| GROUP BY u.model, u.billing_provider | ||
| ORDER BY SUM(u.input_tokens) + SUM(u.output_tokens) DESC | ||
| """, (cutoff,)) | ||
| except Exception: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please narrow this fallback to the expected SQLite compatibility failure. Catching every exception hides defects in this new query and silently reverts users to the inaccurate |
||
| # Fallback for older DBs without session_model_usage table | ||
| cur = db._conn.execute(""" | ||
| SELECT model, | ||
| billing_provider, | ||
| SUM(input_tokens) as input_tokens, | ||
| SUM(output_tokens) as output_tokens, | ||
| SUM(cache_read_tokens) as cache_read_tokens, | ||
| SUM(reasoning_tokens) as reasoning_tokens, | ||
| COALESCE(SUM(estimated_cost_usd), 0) as estimated_cost, | ||
| COALESCE(SUM(actual_cost_usd), 0) as actual_cost, | ||
| COUNT(*) as sessions, | ||
| SUM(COALESCE(api_call_count, 0)) as api_calls, | ||
| SUM(tool_call_count) as tool_calls, | ||
| MAX(started_at) as last_used_at, | ||
| AVG(input_tokens + output_tokens) as avg_tokens_per_session | ||
| FROM sessions WHERE started_at > ? AND model IS NOT NULL AND model != '' | ||
| GROUP BY model, billing_provider | ||
| ORDER BY SUM(input_tokens) + SUM(output_tokens) DESC | ||
| """, (cutoff,)) | ||
| raw_rows = [dict(r) for r in cur.fetchall()] | ||
|
|
||
| # Add auxiliary usage as (model, provider) rows so aux-only models | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
session_model_usagehas no tool-call field, so this makes every main-agent Models card report zero and hides the UI statistic atModelsPage.tsx:520. Please preserve a deliberate session-level attribution (as Insights does) or remove/redesign the displayed metric explicitly.