Add Grok local token cost tracking and main-menu usage chart - #2407
Add Grok local token cost tracking and main-menu usage chart#2407chenow9 wants to merge 37 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0fd5e7c6a8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @Test | ||
| func `parses turn_completed matching headless usage fields`() throws { | ||
| let line = #""" | ||
| {"timestamp":1784626073,"method":"_x.ai/session/update","params":{"sessionId":"019f8400-c020-7941-bf9f-3b424b755104","update":{"sessionUpdate":"turn_completed","prompt_id":"c0c89ae6-c50e-41a6-8169-a2f3124eb4d8","stop_reason":"end_turn","usage":{"inputTokens":12845,"outputTokens":32,"totalTokens":12877,"cachedReadTokens":10752,"reasoningTokens":27,"modelCalls":1,"apiDurationMs":1772,"costUsdTicks":76036000,"modelUsage":{"grok-4.5-build":{"inputTokens":12845,"outputTokens":32,"totalTokens":12877,"cachedReadTokens":10752,"reasoningTokens":27,"modelCalls":1,"apiDurationMs":1772,"costUsdTicks":76036000}},"numTurns":1}},"_meta":{"eventId":"019f8400-c020-7941-bf9f-3b424b755104-29","agentTimestampMs":1784626073119}}} |
There was a problem hiding this comment.
Use a public or fake Grok model fixture
This test fixture embeds grok-4.5-build; that is a plausible provider model identifier with an internal-looking -build suffix rather than a clearly fictitious test value, so copying real headless logs here can expose a non-public model name. Please use the released public ID or an obviously fake sentinel consistently in the fixture and expectation.
Useful? React with 👍 / 👎.
| if let modelUsage = usage["modelUsage"] as? [String: Any] { | ||
| models = modelUsage.keys.sorted() |
There was a problem hiding this comment.
Preserve per-model Grok usage splits
When a turn_completed event reports more than one entry in modelUsage, this keeps only the model names and drops the nested token/cost totals; the daily/session/project aggregators then assign the whole turn to turn.models.first. That makes top-model and model-breakdown output wrong for multi-model Grok turns, so the scanner should carry the nested per-model usage into TurnRecord and aggregate each model separately.
Useful? React with 👍 / 👎.
|
Codex review: needs maintainer review before merge. Reviewed August 12, 2026, 5:10 AM ET / 09:10 UTC. ClawSweeper reviewWhat this changesAdds opt-in local Grok session-log token and reported-cost tracking to CodexBar’s CLI and menu cost views, backed by a bounded parse cache. Merge readinessKeep open: current main and v0.49.2 do not provide Grok cost tracking, while the PR has credible runtime proof. A maintainer must approve the new local metadata-retention boundary and the branch must be rebased from its current conflict state. Priority: P2 Review scores
Verification
How this fits togetherCodexBar turns provider usage sources into shared cost snapshots consumed by menu cards, charts, and flowchart LR
A[Grok session logs] --> B[Local usage scanner]
B --> C[Bounded parse cache]
C --> D[Shared cost snapshot]
D --> E[Menu cost card]
D --> F[Usage history chart]
D --> G[Cost CLI]
Decision needed
Why: The implementation can bound, expire, and delete the cache, but only maintainers can set the product’s acceptable local-retention contract. Before merge
Findings
Agent review detailsSecurityNeeds attention: No dependency or workflow concern was found, but the new persisted local activity-metadata boundary requires explicit approval. Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Approve the bounded local-only retention contract if acceptable, then rebase onto current main while preserving explicit Cost opt-in and cache deletion on disable. Do we have a high-confidence way to reproduce the issue? Not applicable as a bug report; redacted CLI and native-menu captures show the proposed feature operating against real Grok session data. Is this the best way to solve the issue? Unclear: the parsing and UI approach is coherent and proof-backed, but durable local retention needs owner approval before it is the best product solution. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against a90dfed5c264. LabelsLabel justifications:
EvidenceSecurity concerns:
What I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (39 earlier review cycles; latest 8 shown)
|
0fd5e7c to
a60cb02
Compare
Review feedback addressed (rebased onto latest main)Fixes
Also fixed rebase fallout ( Verification
Real behavior proof (redacted)Project paths/names redacted. Command: codexbar cost --provider grokNotes:
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Follow-up: palette scoped to Grok onlyCode fix ([P2])
(Menu screenshot comment withdrawn.) @clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
f05d8c5 to
0858042
Compare
|
🦞👀 Command router queued. I will update this comment with the next step. |
Menu proof (redacted)Grok Cost card + history chart. The original linked image was removed because it exposed an internal-looking model identifier (
@clawsweeper note: historical unredacted proof reference sanitized. |
0858042 to
8838488
Compare
CI / review follow-upRebased onto latest
Upstream CI currently shows @clawsweeper re-review |
Single proof commit for project-grouped CLI with no sole same-path self-source rows; banner references the fix lineage without a tip race.
Final-head CLI proof (
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Refuse oversized artifacts before decode, prune oldest session files by mtime under entry/byte budgets, and never persist a cache over the save cap—mirroring CostUsageCacheIO safety policy.
Address ClawSweeper P1: bound Grok parse cacheFinding: GrokTurnUsageCache loaded with Data(contentsOf:) and saved whole JSON with no size/entry/retention bounds. Fix on head
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Replace project names/paths with generic placeholders across CLI proof assets, and add current-head GrokTurnUsageScannerTests output covering oversized cache refuse and entry/byte prune.
Addressed ClawSweeper P1 + current-head proof (head
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Append the shared history-incomplete note on --group-by project output, cover it with a CLICostTests regression, and fully redact Top project names in menu proof assets.
Addressed ClawSweeper P1 + P2 (head
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Capture live project-grouped cost output showing the incomplete-history warning, fully redact project/workspace identifiers, and drop stale head stamps from menu proof banners.
Final-head real behavior proof (head
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
…token-cost # Conflicts: # Sources/CodexBarCore/Generated/CodexParserHash.generated.swift
Resolve gatekeeper line pins and Codex parser hash after main advanced; document Grok local Cost parse-cache retention contract.
Apply format cleanups and re-pin the CostHistoryChart Grok projects gatekeeper anchor after blank-line removal.
Rebase/merge hygiene + cache retention docs (head
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Expire session-file cache entries after 90 days, delete empty/expired artifacts, and purge the Grok parse cache when Cost tracking is disabled.
Narrower Grok cache retention (head
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Capture a write-generation token at scan start, invalidate it when Cost is disabled, and reject stale saves so in-flight scans cannot recreate the parse cache after opt-out.
P1 race fence + current-head retention proof (head
|
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Point terminal/screenshot proof at the current head and document the path-keyed write-token fence plus 90-day expiry/delete-on-disable contract.
P1 race fence + current-head retention proof (head
|


Summary
Grok currently has no token usage or cost breakdown in the menu (billing/credits only, when available). This PR adds local session-based usage stats for Grok, following the same Cost UX style as Codex.
~/.grok/sessions/**/updates.jsonl→turn_completedusage) viaGrokTurnUsageScannercodexbar cost --provider grok, Cost card, history chartcostUsdTicksare missing)Cost mapping
inputTokens - cachedReadTokenscostUsdTicks / 1e10when present; missing ticks are not estimatedsummary.json→info.cwddocs/grok.mdas data source Just tried it but doesn't show an icon in the menu bar / doesn't work #4Test plan
GrokTurnUsageScannerTests(meta parse, EOF flush, oversized refuse, entry/byte prune)Exact-head real behavior proof
CLI project grouping + incomplete-history (final head; fully redacted)
Live
codexbar cost --provider grok --group-by projecton the final head. Shows:project-NN,workspace-session-NN,/Users/REDACTED/...)Note: history incomplete — some session logs were only partially scanned (size/budget limits).Banner says final head (no soon-stale short hash).
Cache bounds
GrokTurnUsageScannerTests: oversized-load refuse + entry/byte prune.Menu (email + top project redacted; final-head banner)
Email, top-model id, and Top project name redacted. Banner: final head (no embedded short hash).
Opt-in upgrade default
Preference stayed absent (Cost OFF) until user enables Cost explicitly.
Retention + Cost-off race fence (final head)
Current-head evidence for:
invalidateAndDelete(generation bump + delete)stale write token does not recreate cache after Cost-off invalidationBanner: final head. Paths redacted (
/Users/REDACTED/...).Exact-head retention proof (final head)
Race fence + 90-day expiry + delete-on-disable
Current-head evidence for:
invalidateAndDeletebumps write generation + deletes cache)writeTokencannot recreate the cache after opt-out)Redacted paths only (
/Users/REDACTED/...). Banner: final head.