Repository navigation
Studio: track API token usage in Profile - #9320
Conversation
UI evidenceClean two-install run using merge base Observed: total tokens |
|
@codex preview |
|
To use Codex here, create an environment for this repo. |
|
@codex preview |
PreviewThe PR comment already includes before/after UI screenshots demonstrating the Profile API-token usage changes. No additional code change was requested or necessary, so I did not create a commit or a follow-up pull request. Checks
|
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
Review and fix reportConfirmed defect The API usage writer drained before Fix Commit Verification
UI evidence The Profile stats components are unchanged between the published image head Compatibility gaps Native Firefox automation hung during session startup. Safari 27 is installed, but remote automation is disabled. No native Firefox or Safari pass is claimed. GitHub CI restarted on |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bf4aeecda2
ℹ️ 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".
|
|
||
| def __init__(self, sink: Callable[[ApiUsageReceipt], bool] = record_api_usage): | ||
| self._sink = sink | ||
| self._queue: queue.Queue[object] = queue.Queue() |
There was a problem hiding this comment.
Bound the usage-writer backlog
When SQLite remains busy while authenticated API traffic continues, _run() retries the head receipt indefinitely while every completed request is still accepted into this unbounded queue. A long transaction or external process holding studio.db can therefore make memory grow with request volume until Studio is killed. Use a bounded or disk-backed backlog with an explicit overflow policy so best-effort accounting cannot exhaust the server.
Useful? React with 👍 / 👎.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ed3d310e72
ℹ️ 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".
| rows = conn.execute( | ||
| """ | ||
| SELECT model, prompt_tokens, completion_tokens, total_tokens, created_at | ||
| FROM api_usage_events | ||
| WHERE subject = ? |
There was a problem hiding this comment.
Compact API receipts before lifetime scans
On a long-lived, high-throughput authenticated API server, this query scans every receipt ever recorded whenever Profile statistics need recomputing. Each request inserts a permanent row and invalidates the cache, and a repo-wide search for api_usage_events finds no retention, deletion, or rollup path, so studio.db and Profile load time grow linearly with lifetime request count. Periodically compact receipts into daily/model aggregates or apply a bounded retention policy while preserving lifetime totals.
Useful? React with 👍 / 👎.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7d0110f68c
ℹ️ 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".
| return selected | ||
| if not isinstance(prior.get("usage"), dict) or not isinstance(selected.get("usage"), dict): | ||
| return selected | ||
| return _summed_tool_loop_stats(prior, selected) |
There was a problem hiding this comment.
Account with the chronologically last retry usage
When the nudge retry returns 200 but still has no promotable call, both callers pass retry_data as prior and retain first_data as selected. This helper then feeds the original response to _summed_tool_loop_stats as the later turn, so that function keeps the original prompt count rather than the retry's larger prompt count; for example, the added 7-token/20-token test case reports 7 input tokens and 15 total instead of the same 20 input and 28 total used when the retry succeeds. Preserve the original response content separately, but aggregate usage in chronological order so failed healing retries are not omitted from API accounting.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 9fe4b4c. Retry stats are now aggregated in chronological order, while the client response still keeps the selected attempt content. The discarded-retry regression now asserts original content, the retry prompt count, and both completion counts.
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
Preserve internal usage accounting and caller-visible usage filtering while marking failed streams so preview ownership is not claimed after an error.


Summary
Persist content-free token usage receipts for authenticated external API requests and include them in Profile totals, daily activity, streaks, and model rankings. Show separate Studio Chat and API token totals, including for API-only profiles, without counting internal Studio traffic.
Closes #8974