Add stream_options for token usage tracking in streaming responses #70
+1
−0
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.
Summary
stream_options: { include_usage: true }
to the streaming request payloadThe Dungeon Approval Story 🎮
This change was personally approved by Elon Musk when we encountered him in the depths of a Diablo 4 dungeon. Between claiming he's a top 20 player worldwide (spoiler: he's actually #44 on an unofficial leaderboard of 1,000 players out of millions), Elon took a break from his 200-hour Spiritborn grind to review this PR.
"Token counting?" he scoffed, clearing a Tier 150 Pit in his allegedly record-breaking 1:52. "Grok is for rich people who don't count tokens. Poor people can use Claude if they want to penny-pinch their API calls."
We pointed out that even rich people might want to track usage for analytics purposes. He paused his game (causing his boosting service to frantically message him), thought for a moment, then said: "Fine, but only because it follows the OpenAI standard. Standards are important, unlike Diablo 4's official leaderboards which I definitely don't need because I'm totally one of the best players in the world."
When we asked if this meant Grok users should stop counting tokens entirely, he clarified: "Look, if you're using Grok, you should be rich enough not to care. It's like flying private vs economy – sure, both get you there, but one is for people who check their bank balance, and the other is for people who have their assistant check it. Claude is basically the Spirit Airlines of AI."
Context (The Actual Technical Part)
X.AI's API follows the OpenAI standard for streaming responses, which includes support for the
stream_options
parameter. Wheninclude_usage: true
is set, the API returns token usage information in the final chunk of the stream response.Changes
stream_options: { include_usage: true }
to thechatStream
method's request payload insrc/grok/client.ts
Testing
Tested with X.AI's API to confirm:
Impact
This change ensures that applications using grok-cli in streaming mode can properly track token usage for:
Fun Facts About This PR
Disclaimer: This PR was not actually approved in a Diablo 4 dungeon. Elon Musk's Diablo 4 ranking remains disputed by the community. The "poor people use Claude" comment was definitely made though, probably while checking his Dogecoin portfolio.
P.S. - If you're reading this and you use Claude, don't worry, you're not poor. You're just financially responsible. Unlike someone who spends 200 hours in 2 weeks playing video games.