Skip to content

fix(grok): record native provider token usage - #252

Merged
adampeterhiggins merged 1 commit into
mainfrom
fix/grok-provider-turn-usage
Oct 9, 2026
Merged

adampeterhiggins merged 1 commit into
mainfrom
fix/grok-provider-turn-usage

Conversation

@adampeterhiggins

Copy link
Copy Markdown
Owner

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:

  • Focused Grok usage, completion runtime, ACP runtime model, Grok adapter, and subagent projection tests — 149 passed.
  • Grok replay integration tests — 16 passed, including recorded simple, multi-turn, tool-heavy, and error scenarios.
  • node_modules/.bin/vp run --filter t3 typecheck — passed.
  • Targeted lint — no errors; four existing warnings.

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.
  • Replay integration tests filtered to 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).

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.

1 participant