Skip to content

xAI Streaming - Token Usage Not Returned Without stream_options #2

@xinpehr

Description

@xinpehr

Hi Vusal,

I found a bug in the xAI MessageService that causes token costs to always be calculated as zero.

Root Cause:
In src/Ai/Infrastructure/Services/xAi/MessageService.php, the API request body does not include stream_options: { include_usage: true }. Without this parameter, the xAI API does not return token usage data (prompt_tokens / completion_tokens) in the streaming response, causing $inTokens and $outTokens to always remain 0.

Impact:

  • Token costs for all xAI models (grok-*) are never deducted from user credits
  • Only tool call costs (e.g. Google Search) are correctly calculated, since they are independent of usage data

Fix:
Add stream_options to the request body in generateMessage():

$body = [
'messages' => $messages,
'model' => $model->value,
'stream' => true,
'stream_options' => [
'include_usage' => true
]
];

Thanks for your attention to this matter!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions