Skip to content

Studio: track API token usage in Profile - #9320

Merged
Etherll merged 22 commits into
unslothai:mainfrom
Etherll:codex/issue-8974-api-profile-stats
Aug 26, 2026
Merged

Etherll merged 22 commits into
unslothai:mainfrom
Etherll:codex/issue-8974-api-profile-stats

Conversation

@Etherll

@Etherll Etherll commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

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

@Etherll

Etherll commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator Author

UI evidence

Clean two-install run using merge base 92d3f8705 (BEFORE, :8990) and live head 84db4253b (AFTER, :8991), with login and server identity verified on both. The same content-free scalar API receipt was seeded into each isolated home.

Profile API usage summary — BEFORE vs AFTER

Profile API activity details — BEFORE vs AFTER

Observed: total tokens 0 -> 1,500, API tokens missing -> 1,500, Studio Chat tokens missing -> 0, Activity insights 0 -> 1, and api-only-model appears. The AFTER breakdown shows 1.2K sent, 300 generated, and 1.5K total. No prompt, reply, API key, model weight, or GPU data was used.

@Etherll
Etherll marked this pull request as ready for review August 20, 2026 02:10
@Etherll
Etherll requested a review from danielhanchen as a code owner August 20, 2026 02:10
@Etherll

Etherll commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex preview

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@Etherll

Etherll commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex preview

@chatgpt-codex-connector

Copy link
Copy Markdown

Preview

The 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

  • ✅ git status --short --branch — working tree is clean on the PR head.
  • ✅ git log -5 --oneline — verified the checkout is at commit 6c64303, matching the supplied PR head SHA.

View task →

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: bc7a3ecc1e

ℹ️ 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".

@mahiatlinux

Copy link
Copy Markdown
Contributor

Review and fix report

Confirmed defect

The API usage writer drained before _stop_post_warm_thread() and hardware detection retirement. Backend CI on Python 3.13 caught the shutdown race: 2 warm-window tests failed after 30,322 tests passed.

Fix

Commit 3048fbbae moves the writer drain after both synchronous warm-worker retirement steps. The push was a normal fast-forward.

Verification

  • Python 3.12: both failed shutdown tests pass; all 180 API monitor, API usage route, and Profile stats tests pass.
  • Python 3.13: both failed shutdown tests pass; the same 180 usage tests pass.
  • Exact A/B run: the merge base records no API receipt; the PR head persists and aggregates the receipt without changing Studio Chat message counts.
  • Frontend head and merge-ref checks pass: focused tests, typecheck, strict parity across 12 locales, and production build.
  • Chromium 151 and WebKit 26.5 show 1.5K total tokens, 0 Studio Chat tokens, 1.5K API tokens, one active day, and api-model.
  • A forced Profile stats 503 shows the server error and a working Try again action.

UI evidence

Profile API usage summary before and after

Profile API activity details before and after

The Profile stats components are unchanged between the published image head 84db4253b and current head 3048fbbae. The later frontend change only adds unrelated Remote & LAN localization.

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 3048fbbae.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 3048fbbae0

ℹ️ 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".

@mahiatlinux
mahiatlinux self-requested a review August 25, 2026 08:31
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 72c7086b1b

ℹ️ 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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +214 to +218
rows = conn.execute(
"""
SELECT model, prompt_tokens, completion_tokens, total_tokens, created_at
FROM api_usage_events
WHERE subject = ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from mahiatlinux Aug 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from mahiatlinux Aug 25, 2026
@unslothai unslothai deleted a comment from mahiatlinux Aug 25, 2026
@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread studio/backend/routes/inference.py Outdated
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 9fe4b4c110

ℹ️ 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".

@Etherll
Etherll merged commit c21b057 into unslothai:main Aug 26, 2026
52 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Track API Token Usage in Profile

3 participants