Repository navigation
fix(grok): record native provider token usage - #252
Merged
Merged
Conversation
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.
Problem
Native Grok provider turns do not record their prompt-wide token usage, so app-owned delegated tasks cannot show usage totals. Grok can also emit an empty completion notification before the usage-bearing terminal payload, causing the completion fallback to discard reported counts.
Change
Preserve usage from Grok completion notifications and map prompt-wide input, output, cache, and reasoning counts onto
turnTokenUsage. Keep context occupancy and last-call metadata separate from billable usage. A Grok-specific ACP flavor hook leaves other ACP providers unchanged.When an empty completion arrives first, allow up to 250 ms for a usage-bearing terminal notification or the prompt RPC response, preserving the existing fallback for hung RPCs. Missing counts remain unavailable; interrupted or incomplete reports remain partial. Existing delegated-task aggregation consumes the recorded counts. Historical turns are not backfilled. Update the fork differences guide.
Scope and approval
Requested by the fork maintainer in this thread. This PR addresses the native Grok provider; Grok models run through Cursor use the separate Cursor integration.
Verification
Implementation agent reported:
node_modules/.bin/vp run --filter t3 typecheck— passed.Parent independently reran:
node_modules/.bin/vp test run apps/server/src/provider/acp/GrokTurnTokenUsage.test.ts apps/server/src/provider/acp/XAiAcpExtension.test.ts— 66 passed.runs (simple|multi_turn|todo_list|grok_prompt_error)/grok— 4 passed, checking persisted token counts and aggregation against recorded payloads.git diff --check— passed.No live provider, browser, or dev-server verification was performed. No UI components were changed.
Model: GPT-6.1-Sol. Harness: Codex through T3 Code (delegated implementation, parent review).