Skip to content

Add a Grok usage collector to the agents panel - #9498

Closed
Thunderhawke wants to merge 1 commit into
omacom:quattrofrom
Thunderhawke:agents-grok-usage-collector
Closed

Thunderhawke wants to merge 1 commit into
omacom:quattrofrom
Thunderhawke:agents-grok-usage-collector

Conversation

@Thunderhawke

Copy link
Copy Markdown

Adds a grok collector to the agents panel, so the Grok CLI gets a tab
alongside Claude, Codex and the rest instead of silently missing from the bar
for anyone whose subscription is Grok.

What it reads

Grok ships no usage or rate-limit endpoint — the CLI has no usage
subcommand and never exposes the account allowance — so every number comes
from the CLI's own session logs at
~/.grok/sessions/<encoded-cwd>/<session-id>/updates.jsonl, where one
turn_completed update is written per prompt carrying that turn's token
usage broken down by model.

That makes the record local-stats-only: hasLocalStats and hasPromptStats
are true, limits stays empty, and tierLabel/usageStatusText stay empty
so heroMeta() renders the neutral "Subscription" line rather than a warning
card. This is the same shape Claude's collector already falls back to when it
can't reach Anthropic's endpoint, so the panel needs no changes to draw it.

Details worth a reviewer's eye

Token buckets are de-overlapped. Grok's counters nest:
cachedReadTokens and cacheCreationTokens are both counted inside
inputTokens (records satisfy totalTokens == inputTokens + outputTokens).
The panel wants four disjoint buckets, so plain input is what remains after
subtracting the two cache buckets. Reasoning tokens stay folded into output,
matching how the Codex collector treats them. Verified against hand-summed
raw turns.

Forked sessions are deduped. grok --fork-session replays prior turns
into the new session's log, so turns are deduped on prompt_id and a fork
doesn't inflate prompt counts or token totals.

One prompt per turn. A turn_completed served by more than one model
counts once as a prompt, with its tokens split across models in
todayTokensByModel / modelUsage.

Scan cache, mirroring the Codex collector: flock-guarded, atomic write,
{schemaVersion, scanDate, stats} envelope so today's counters don't survive
midnight, and it degrades to a direct scan on any cache failure. Without it
the widget rereads every session file on each refresh. --force bypasses it.
The scan window is 30 days by mtime, so "all time" carries the same caveat
the README already documents for the other scanners.

GROK_HOME is honored, the way the Codex collector honors CODEX_HOME.

Not included: the icon

I deliberately did not add shell/plugins/agents/assets/grok.svg — I wasn't
going to approximate xAI's mark by hand and ship it. Grok falls back to the
default bar glyph, which the plugin already supports. Happy to add the real
asset if a maintainer wants to drop one in, or to take a patch on the branch.

Tests

test/shell.d/agent-usage-grok-scanner-test.sh — 10 assertions, all passing:
valid record with no sessions present, the bucket split, a second model in
its own bucket, today's prompts/sessions/tokens, the seven-day chart,
totals/active days, forked-session dedup, a multi-model turn counting as one
prompt, the scan-window skip, and surviving malformed lines. Fixtures are
built with jq -nc under TZ=UTC with GROK_HOME/XDG_CACHE_HOME
redirected, so nothing touches the runner's real state.

./test/all shows no new failures.

Grok exposes no usage or rate-limit endpoint, so the record carries local
stats only: tokens by day and model, prompts, and sessions, read from the
turn_completed updates the CLI writes per prompt.
@bjarneo

bjarneo commented Sep 21, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #6485, which covers the same Grok usage collector with broader scope (brand icons, weekly credit meter via ACP billing, live verifications). I keep that one open and close this one to consolidate review.

@bjarneo bjarneo closed this Sep 21, 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