Repository navigation
Add a Grok usage collector to the agents panel - #9498
Closed
Thunderhawke wants to merge 1 commit into
Closed
Thunderhawke wants to merge 1 commit into
Thunderhawke wants to merge 1 commit into
Conversation
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.
This was referenced Sep 20, 2026
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
grokcollector to the agents panel, so the Grok CLI gets a tabalongside 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
usagesubcommand 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 oneturn_completedupdate is written per prompt carrying that turn's tokenusage broken down by model.
That makes the record local-stats-only:
hasLocalStatsandhasPromptStatsare true,
limitsstays empty, andtierLabel/usageStatusTextstay emptyso
heroMeta()renders the neutral "Subscription" line rather than a warningcard. 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:
cachedReadTokensandcacheCreationTokensare both counted insideinputTokens(records satisfytotalTokens == 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-sessionreplays prior turnsinto the new session's log, so turns are deduped on
prompt_idand a forkdoesn't inflate prompt counts or token totals.
One prompt per turn. A
turn_completedserved by more than one modelcounts 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 survivemidnight, and it degrades to a direct scan on any cache failure. Without it
the widget rereads every session file on each refresh.
--forcebypasses 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_HOMEis honored, the way the Codex collector honorsCODEX_HOME.Not included: the icon
I deliberately did not add
shell/plugins/agents/assets/grok.svg— I wasn'tgoing 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 -ncunderTZ=UTCwithGROK_HOME/XDG_CACHE_HOMEredirected, so nothing touches the runner's real state.
./test/allshows no new failures.