Repository navigation
Studio: export token usage from Profile stats as CSV - #14213
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2a02d80248
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| {hasUsage ? <ExportUsageButton /> : null} | ||
| </header> | ||
|
|
||
| <StatsHighlights stats={stats} /> |
There was a problem hiding this comment.
Hide export when no token rows exist
When a profile contains only user messages or assistant messages from before token metadata was recorded, stats.totals.messages makes hasUsage true even though every candidate row has zero token counts and export_usage_csv() filters all of them out. The newly displayed button therefore downloads a header-only file; gate it on the token fields that the exporter uses (prompt, completion, or total tokens) rather than the general message-based hasUsage flag.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Keeping this as is: a header-only file is the correct export for a history with no recorded token counts, nothing is lost or wrong, and the button already hides when there is no usage at all.
| rows: list[tuple[int, str, str, int, int, int, int]] = [] | ||
|
|
||
| def collect(*row) -> None: | ||
| if row[3] or row[4] or row[5]: | ||
| rows.append(row) |
There was a problem hiding this comment.
Stream unbounded lifetime exports
For accounts with long-lived chat or API histories, this retains every exported record in rows and then builds a second full in-memory CSV in StringIO; Starlette subsequently encodes another complete response buffer, while the frontend reads it as a string and Desktop encodes it again for IPC. Since the lifetime history has no row cap, sufficiently large but valid profiles can exhaust backend or Desktop memory during export; emit the CSV through a streaming response or a bounded temporary-file path and use the existing streaming download infrastructure.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Measured on a synthetic 30k-response history: 1.77 MB of CSV in about 1 s, so even the 260k-message history noted in the stats route is roughly 15 MB. The Profile stats fold already walks the same history in memory, so this does not add a new memory ceiling worth a streaming path.
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
[skip ci]
Adds an Export CSV button to Settings > Profile > Your stats that downloads the token usage behind the stats, one row per chat response or API request.
Fixes #7214 (the export part; the usage limiter in that issue is not part of this PR).
What the file looks like
time/dateare in the browser's timezone, the same bucketing the activity chart uses, so a spreadsheet pivot by day, month, year or model matches the Profile numbers.sourceischat(Studio chat) orapi(authenticated local API requests).How
GET /api/profile/stats/export(new, read-only) returnstext/csvwith an attachment filename._fold_messages,_fold_api_usage), through an optional per-row callback. No new tables, no second parser, and the counting rules (forks, compare mode panes, llama.cppserverTimingsfallback) stay identical, so the CSV total equals the "Lifetime tokens" figure./api/profile/stats: chat history is install-wide (those rows have no owner), API receipts only for the calling subject, and the per-account database under multi-user installs.=,+,-,@is prefixed with'to stop it running as a spreadsheet formula.downloadFile, which uses the native save dialog in Unsloth Desktop and a normal download in the browser. The button only shows when there is usage to export.exportCsv/exportErrorstrings are added to all 14 locales (i18n:check:strictpasses).Nothing else on the Profile page changes; the stats request and payload are unchanged.
Tests
tests/test_profile_stats.py: exported rows (ordering, local time, chat vs api, formula escaping), the other subject's API receipts excluded, CSV token sum equalscompute_profile_statstotalTokens, repeat export byte-identical, header-only file for empty history.tests/test_api_profile_usage_routes.py: the route forwards the authenticated subject and timezone and responds as a CSV attachment.Frontend:
npm run typecheckandnpm run i18n:check:strictclean.Before / after
Settings > Profile > Your stats on the merge base vs this branch, with the same seeded chat history (two replies, 4,500 tokens). After: the Export CSV button sits beside the heading, and the browser saves
unsloth-token-usage-<date>.csvwith 2 rows summing to 4,500 tokens, equal to the Profile total. The export endpoint answers 404 before and 200 after.