Skip to content

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

Closed
jaredpohl wants to merge 1 commit into
omacom:quattrofrom
jaredpohl:feat/agents-grok-usage-collector
Closed

jaredpohl wants to merge 1 commit into
omacom:quattrofrom
jaredpohl:feat/agents-grok-usage-collector

Conversation

@jaredpohl

Copy link
Copy Markdown

Why

Grok is already a first-party default coding agent (omarchy default agent grok). The agents bar widget is not “every installed agent”: it only shows subscriptions that have an omarchy-agent-usage-* collector writing JSON into ~/.local/state/omarchy/agents/usage/.

omarchy-agent-usage-update discovers collectors by globbing $OMARCHY_PATH/bin/omarchy-agent-usage-*. Claude, Codex, and Fireworks ship one. Grok did not. On a machine whose default agent is Grok, Claude/Codex/Fireworks still appear (they have collectors) and Grok does not, even with weeks of local CLI history and a live SuperGrok allowance.

This is the designed extension point from the widget README:

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

A marketplace plugin cannot register a collector. Discovery is $OMARCHY_PATH/bin/omarchy-agent-usage-* only.

Tracked as suggestion #7198. Related collector PRs: #6485, #7200, #6902, #8280, #8649, #9840, #9924, #10074, #11539. This one is from a live diagnosis on Omarchy 4.0.4 + Grok 1.0.34 (SuperGrok Plus): the panel already had Claude, Codex, and Fireworks tabs, and Grok was the missing fourth.

What the panel shows

Once omarchy-agent-usage-grok has written grok.json, the existing panel grows a Grok tab with:

  • Plan label (SuperGrok Plus, X Premium+, etc.)
  • Weekly credit-pool meter and reset time
  • Grok Build slice of that same week (coding-agent product only; Chat/Imagine are ignored)
  • Tokens by day for the last week, and tokens by model

No QML changes. The panel already watches whatever records land in the usage directory. Optional assets/grok.svg / grok-light.svg from the official Grok mark; the bar glyph still stands in without them.

How the numbers are made

Local stats

From $GROK_HOME/sessions (default ~/.grok/sessions/<cwd>/<id>/):

  • Preferred source: updates.jsonl rows with method _x.ai/session/update / session/update and sessionUpdate: turn_completed. That is the same billed usage grok usage <session-id> prints, including inputTokens, outputTokens, cachedReadTokens, cacheCreationTokens, and per-model buckets.
  • Each distinct prompt_id is one prompt; a retried turn with the same id replaces the previous count rather than doubling it.
  • Nested subagents/ directories are not walked, so parent totals are not counted twice.
  • Fallback: usage.json when a session has no turn_completed rows (older sessions).
  • GROK_HOME is honored the way CODEX_HOME is.

Token charts are this machine's Grok CLI sessions only. Work on grok.com, the Grok app, or another computer does not appear. That is the same local-vs-account split Claude's transcripts have.

Limits and plan

From the same billing document Grok's own /usage modal uses, authenticated with the OAuth session in ~/.grok/auth.json:

  • GET https://cli-chat-proxy.grok.com/v1/billing?format=credits → creditUsagePercent (weekly pool), currentPeriod.end (reset), productUsage[GrokBuild].usagePercent
  • GET https://cli-chat-proxy.grok.com/v1/user?include=subscription → subscriptionTier (SuperGrokPlus → SuperGrok Plus, XPremiumPlus → X Premium+)

creditUsagePercent is returned on a 0–100 scale (10 means 10%). The panel meters are 0–1, so values greater than 1 are divided by 100. Values already in 0–1 are left alone.

A failed or unreachable probe sets retryAdvised and still shows local token stats. Without a signed-in CLI the collector still prints valid JSON (ready: false, Waiting for auth) so the update runner has something to write.

Deliberate omissions

  • No prepaid balance. The billing payload includes prepaidBalance: { val: 0 } on SuperGrok. Publishing that as balance draws Fireworks' credit ledger and reads as “Prepaid credits $0.00” for a subscription that is neither prepaid nor empty. Zero prepaid is omitted.
  • No Chat/Imagine meters. Only the GrokBuild product slice is added, so the coding-agent tab does not mix in grok.com chat.
  • The collector does not write auth.json. An expired access token is a retry, not a rewrite of the user's login files.

Files

File Change
bin/omarchy-agent-usage-grok Hidden collector (omarchy agent usage-update already globs omarchy-agent-usage-*)
shell/plugins/agents/assets/grok.svg (+ -light twin) Optional mark
shell/plugins/agents/manifest.json Enable grok by default; mention it in the widget description
shell/plugins/agents/README.md Document the collector, GROK_HOME, billing source, and providers example
manual/17-ai.md List Grok next to Claude, Codex, and Fireworks
test/shell.d/agent-usage-grok-scanner-test.sh Scanner + billing-shape tests

Test plan

  • bash test/shell.d/agent-usage-grok-scanner-test.sh — no credentials → hidden record; one count per prompt_id; nested subagents/ skipped; exclusive token buckets + usage.json fallback; SuperGrok Plus label; weekly 10 → 0.1; Grok Build slice only; zero prepaid omitted; XPremiumPlus → X Premium+
  • Live panel on Omarchy 4.0.4 with Grok 1.0.34 as the default agent: Grok chip appeared beside Claude / Codex / Fireworks; SuperGrok Plus; weekly 11% (reset 2026-09-21); Grok Build 11%; today 14.7M tokens on grok-4.6-build
  • python3 -m py_compile bin/omarchy-agent-usage-grok
  • ./test/all on CI

Happy to close this in favor of another collector PR if maintainers already have a preferred one in review; the billing shape and turn_completed contract above are the bits that were missing from a working Grok tab on this machine.

Grok is already a first-party default agent, but the panel only shows
subscriptions that have an omarchy-agent-usage-* collector. This adds
the missing Grok one: local turn_completed stats from GROK_HOME/sessions,
plus the weekly SuperGrok credit pool from the same billing endpoint as
Grok's own /usage modal.
@sprajs

sprajs commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

percent_from_api() misreads low usage under the 0–100 billing contract described above: it divides only values greater than 1. In an isolated check of the function, 0.5 returned 0.5 and 1 returned 1.0, so 0.5%/1% usage becomes 50%/100%; 1.01 then drops back to 1.01%.

Could these billing percentage fields always be divided by 100, with regression cases at 0, 0.5, 1, 1.01 and 100? If another payload format uses fractions, that needs an explicit schema distinction rather than guessing from the value.

@dhh

dhh commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Thanks, this was a valuable reference. Grok landed in the agents panel with #13992: its plan, the weekly credits meter from the same billing endpoint (showing 0% with the reset when creditUsagePercent is absent), multiple Grok accounts, and autoswitch. Reading tokens from turn_completed rows isn't in yet; closing this in favor of #13992.

@dhh dhh closed this Oct 2, 2026
dhh added a commit that referenced this pull request Oct 2, 2026
Each Grok session keeps a usage.json, the ledger `grok usage` prints, with
every finished turn's end time and tokens by model. The collector now
reads it for tokens today, by day for the last week, and by model, with
cached input kept apart, and counts today's prompts by the turns that
ended today. Found in #12352's research.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dhh added a commit that referenced this pull request Oct 2, 2026
…k through mise (#14004)

* Let the agents in the panel be put in any order

Drag an agent by its mark to move its section; the header it will land
on lights up, and the move happens on release. Each agent's header is
now a keyboard stop with its own highlight, and Ctrl+Up/Down moves the
agent the cursor is in. The order is kept in agents/order.json beside
the usage records. The key catcher turns Ctrl+Up/Down into a reorder
only for panels that opt in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Light only an agent's mark when the cursor or a drag is on it

The mark is the handle the agent moves by, so the keyboard cursor and
the drop spot while dragging box it alone rather than the header line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Keep a lit agent mark's box from being clipped at the panel's edge

The box overhangs the content's left edge, which the scrolling area
clipped. The scrolling area now reaches a little into the panel's
padding with the content shifted back, so nothing moves and the box
draws whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Reuse a Grok session scan only on the day it was made

A limits-only refresh just after midnight reused a scan from the evening
before, which counted yesterday's sessions as today's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Keep Ctrl+Up/Down from moving an agent when the cursor is outside one

The hero's buttons and the starter tiles carry indices too, and the
lookup read them as accounts, so with several accounts Ctrl+Up/Down on
one of them moved an unrelated agent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Report a failed mise update even after the Grok upkeep runs

The Grok block ran after `mise up` and its last command set the script's
status, so a failed tool update could read as a success to callers that
warn about it. The update's own status is now the script's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Unpack a Grok update into ~/.grok whatever GROK_HOME says

The upkeep replaces ~/.grok's link, but the npm launcher unpacks into
GROK_HOME when it's set, so with a custom home the link never came back
at the new release and the old one was restored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Hold off reordering agents while an account name is being edited

Ctrl+Up/Down reached the key catcher during an inline rename, and moving
the agent rebuilt its section, dropping the unfinished name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Install Grok through mise's first-party package

The npm launcher keeps an old ~/.grok/bin binary because mise skips its
postinstall. Use mise's grok tool, and drop the npm tool so its shim
does not stay ahead of the stub.

* Drop the npm Grok workarounds now that mise installs Grok itself

With Grok installed through mise's first-party package, the CLI is the
binary mise manages, so the update upkeep that repointed ~/.grok/bin and
the shared bin directory for added Grok accounts have nothing left to do.
omarchy-update-mise is back to running mise up and nothing else, and
adding a first Grok account installs the same package.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Count Grok's tokens and prompts from its usage ledger

Each Grok session keeps a usage.json, the ledger `grok usage` prints, with
every finished turn's end time and tokens by model. The collector now
reads it for tokens today, by day for the last week, and by model, with
cached input kept apart, and counts today's prompts by the turns that
ended today. Found in #12352's research.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Count cache writes in Grok's daily token totals

Grok's totalTokens leaves cache writes out while the per-model buckets
count them, so a turn with cache writes added less to its day than to
its model. The day's total is now the sum of those same buckets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Remove the npm launcher's old Grok binaries when moving to mise's Grok

Omarchy's npm wrapper ran the binary the launcher unpacked into
~/.grok/bin, where x.ai's installer also puts a copy with a PATH entry
ahead of mise. Once the wrapper is replaced, a binary left there would
keep shadowing the mise tool, so the migration removes it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jesse Miller <[email protected]>
sgruendel pushed a commit to sgruendel/omarchy that referenced this pull request Oct 3, 2026
…k through mise (omacom#14004)

* Let the agents in the panel be put in any order

Drag an agent by its mark to move its section; the header it will land
on lights up, and the move happens on release. Each agent's header is
now a keyboard stop with its own highlight, and Ctrl+Up/Down moves the
agent the cursor is in. The order is kept in agents/order.json beside
the usage records. The key catcher turns Ctrl+Up/Down into a reorder
only for panels that opt in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Light only an agent's mark when the cursor or a drag is on it

The mark is the handle the agent moves by, so the keyboard cursor and
the drop spot while dragging box it alone rather than the header line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Keep a lit agent mark's box from being clipped at the panel's edge

The box overhangs the content's left edge, which the scrolling area
clipped. The scrolling area now reaches a little into the panel's
padding with the content shifted back, so nothing moves and the box
draws whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Reuse a Grok session scan only on the day it was made

A limits-only refresh just after midnight reused a scan from the evening
before, which counted yesterday's sessions as today's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Keep Ctrl+Up/Down from moving an agent when the cursor is outside one

The hero's buttons and the starter tiles carry indices too, and the
lookup read them as accounts, so with several accounts Ctrl+Up/Down on
one of them moved an unrelated agent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Report a failed mise update even after the Grok upkeep runs

The Grok block ran after `mise up` and its last command set the script's
status, so a failed tool update could read as a success to callers that
warn about it. The update's own status is now the script's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Unpack a Grok update into ~/.grok whatever GROK_HOME says

The upkeep replaces ~/.grok's link, but the npm launcher unpacks into
GROK_HOME when it's set, so with a custom home the link never came back
at the new release and the old one was restored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Hold off reordering agents while an account name is being edited

Ctrl+Up/Down reached the key catcher during an inline rename, and moving
the agent rebuilt its section, dropping the unfinished name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Install Grok through mise's first-party package

The npm launcher keeps an old ~/.grok/bin binary because mise skips its
postinstall. Use mise's grok tool, and drop the npm tool so its shim
does not stay ahead of the stub.

* Drop the npm Grok workarounds now that mise installs Grok itself

With Grok installed through mise's first-party package, the CLI is the
binary mise manages, so the update upkeep that repointed ~/.grok/bin and
the shared bin directory for added Grok accounts have nothing left to do.
omarchy-update-mise is back to running mise up and nothing else, and
adding a first Grok account installs the same package.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Count Grok's tokens and prompts from its usage ledger

Each Grok session keeps a usage.json, the ledger `grok usage` prints, with
every finished turn's end time and tokens by model. The collector now
reads it for tokens today, by day for the last week, and by model, with
cached input kept apart, and counts today's prompts by the turns that
ended today. Found in omacom#12352's research.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Count cache writes in Grok's daily token totals

Grok's totalTokens leaves cache writes out while the per-model buckets
count them, so a turn with cache writes added less to its day than to
its model. The day's total is now the sum of those same buckets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Remove the npm launcher's old Grok binaries when moving to mise's Grok

Omarchy's npm wrapper ran the binary the launcher unpacked into
~/.grok/bin, where x.ai's installer also puts a copy with a PATH entry
ahead of mise. Once the wrapper is replaced, a binary left there would
keep shadowing the mise tool, so the migration removes it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jesse Miller <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants