Skip to content

Add a Grok collector to the agents widget - #11539

Closed
ConsultingFuture4200 wants to merge 2 commits into
omacom:quattrofrom
ConsultingFuture4200:add-grok-usage-collector
Closed

ConsultingFuture4200 wants to merge 2 commits into
omacom:quattrofrom
ConsultingFuture4200:add-grok-usage-collector

Conversation

@ConsultingFuture4200

Copy link
Copy Markdown

The agents panel gains a tab for any record that lands in the usage directory, so adding Grok is adding a collector — no plugin code changes. This follows the pattern the widget's own README describes:

Adding an agent therefore never touches this plugin: ship a collector that prints the record contract.

Where the numbers come from

Local stats come from the SQLite session index under ~/.grok/sessions, with grok usage <id> per session for tokens, models, and cost. Only local sessions carry token data — grok sessions list reports server-side sessions that grok usage cannot answer for.

Per-session totals are cached in ~/.local/state/omarchy/agents/grok-usage-cache.json and re-read only when a session's own updated_at moves, so a refresh costs one CLI call per changed session rather than per session. The plan and signed-in identity come from ~/.grok/auth.json, and GROK_HOME is honored the way CODEX_HOME is.

Two deliberate omissions

No limit meters. A grok.com subscription does have allowances, but nothing exposes them to a collector. Grok's own status-line documentation lists a rate-limit summary among the fields it deliberately does not send, calling it "a number it cannot source honestly." The record carries no limits rather than a meter no one can stand behind.

No balance. grok usage prices each session at API list rates, but a subscription bills nothing per token, so that figure is notional rather than money spent. It travels as notionalCostUsd and is deliberately not published as a balance — that section draws a prepaid credit ledger, and publishing one rendered "Prepaid credits $0.00" for an account that is neither prepaid nor at zero.

Notes for review

  • Day totals sum cache traffic the way the model rows do. Grok's own totalTokens counts input and output only, so TOKENS BY DAY and TOKENS BY MODEL would otherwise disagree.
  • The access token's tier claim is a plain integer with no published mapping. Only a value confirmed against a real account is named (1 → SuperGrok, verified on a $30 subscription); anything else falls back to the generic label rather than stating a guessed plan as fact. Happy to drop the mapping entirely if you'd rather not carry an unpublished constant.
  • The collector always prints valid JSON, including when the CLI is missing or signed out, so a failure shows as "Waiting for auth" rather than a broken record.
  • grok is resolved via shutil.which with a mise fallback, since version managers keep it off the PATH a bar widget inherits. Open to a better convention if one exists.

Testing

Verified on Omarchy 4.0.3-1 against a real signed-in SuperGrok account:

  • bin/omarchy-agent-usage-update --force grok writes grok.json; the panel picks it up and renders the tab with no plugin changes.
  • --force and --limits-only both produce valid records.
  • Signed-out (GROK_HOME=/nonexistent) yields ready=false, "Waiting for auth", and the grok login hint.
  • TOKENS BY DAY and TOKENS BY MODEL agree (24,799 tokens across grok-4.6-build).

I don't have a second Grok account or another tier to test against, so the tier mapping is confirmed for one value only.

🤖 Generated with Claude Code

The agents panel gains a tab for any record that lands in the usage
directory, so adding Grok is adding a collector — no plugin code changes.

Local stats come from the SQLite session index under ~/.grok/sessions, with
`grok usage <id>` per session for tokens, models, and cost. Only local
sessions carry token data: `grok sessions list` reports server-side sessions
that `grok usage` cannot answer for. Per-session totals are cached and
re-read only when a session's own timestamp moves, so a refresh costs one CLI
call per changed session. The plan and signed-in identity come from
~/.grok/auth.json, and GROK_HOME is honored the way CODEX_HOME is.

Two deliberate omissions:

Grok reports no rate limits. A grok.com subscription does have allowances,
but nothing exposes them to a collector — Grok's status-line documentation
lists a rate-limit summary among the fields it deliberately omits, as "a
number it cannot source honestly". The record carries no limits rather than
an invented meter.

`grok usage` prices sessions at API list rates, but a subscription bills
nothing per token, so that figure is notional. It travels as
notionalCostUsd and is not published as a balance, which would have drawn a
prepaid credit ledger for an account that has none.

Day totals sum cache traffic the way the model rows do, since Grok's own
totalTokens counts input and output only and the panel's two sections would
otherwise disagree.

The tier claim in the access token is a plain integer with no published
mapping, so only a value confirmed against a real account is named; anything
else falls back to the generic label.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Three corrections after running this against a real account for two days.

A failed `grok usage` call now keeps the session's last good numbers and
leaves its timestamp alone, so the next run retries rather than treating the
carried-forward reading as fresh. Only a session with nothing cached counts
as unread, and then the record says the totals are incomplete instead of
implying an idle week.

The tab also says what it can and cannot see. Only Grok CLI sessions on this
machine leave token data behind; work done on grok.com, in the Grok app, or
on another machine is invisible here, and `grok usage` cannot answer for the
server-side sessions `grok sessions list` reports. An empty week now says so
rather than showing zeros that read as broken tracking.

The tier mapping is gone. The access token's `tier` claim is not a stable
plan id: one account read 1 and, two days later with no plan change to
explain it, 7. With no published mapping, an unmapped tier falls back to the
panel's generic label, which is never wrong, rather than putting a guessed
plan name in someone's bar.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CRqM55mgaBbMgkkYzPNHqi
@ConsultingFuture4200

Copy link
Copy Markdown
Author

Pushed three corrections after running this against a real account for a couple of days.

A failed read no longer looks like an idle week. If grok usage doesn't answer for a session, the collector keeps that session's last good numbers and leaves its timestamp alone so the next run retries. Only a session with nothing cached counts as unread, and the record then says the totals are incomplete rather than reporting zeros.

The tab now states its scope. Only Grok CLI sessions on the local machine leave token data behind — work done on grok.com, in the Grok app, or on another machine is invisible, and grok usage returns "not found" for the server-side sessions grok sessions list reports. An empty week says so explicitly instead of showing zeros that read as broken tracking. This came from a real report: a maxed-out subscription showed an empty tab because none of that work went through the CLI on that machine.

The tier mapping is removed. I'd mapped tier: 1 to a plan name on one account's say-so. The same account later read tier: 7 with no plan change that would explain it, so that claim isn't a stable plan id. With no published mapping, an unmapped tier now falls back to the panel's generic label rather than putting a guessed plan name in someone's bar.

Behaviour otherwise unchanged; the record contract is the same.

🤖 Generated with Claude Code

@bjarneo

bjarneo commented Sep 20, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #9498, which covers the same Grok usage collector. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

@bjarneo bjarneo closed this Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants