-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
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!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels