Skip to content

streamText() returns NaN for usage with @ai-sdk/xai #5736

@liangfung

Description

@liangfung

Description

Bug description

I encountered an issue while integrating the xAI Grok provider. When calling streamText(), the returned usage object has promptTokens, completionTokens, and totalTokens all set to NaN. Here is my code:

import { xai } from '@ai-sdk/xai';
import { streamText } from 'ai';

streamText({
    model: xai('grok-3-mini-beta'),
    messages: [{
        role: 'user',
        content: 'who are you'
    }],
    onFinish: async ({ usage }) => {
        console.log('Usage:', usage);
    },
}

Returns

Usage: {promptTokens: NaN, completionTokens: NaN, totalTokens: NaN}

Info about my ai sdk version

"ai": "^4.3.5",
"@ai-sdk/xai": "^1.2.11",

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelprovider/xaiIssues related to the @ai-sdk/xai providersupport

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions