Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-berries-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/xai': patch
---

fix(provider/xai): correct usage token calculation for reasoning models
44 changes: 44 additions & 0 deletions examples/ai-functions/src/generate-text/xai-usage-full.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
import { run } from '../lib/run';

const models = [
'grok-4',
'grok-4-1-fast-reasoning',
'grok-4-1-fast-non-reasoning',
'grok-4-fast-reasoning',
'grok-4-fast-non-reasoning',
'grok-code-fast-1',
'grok-3',
'grok-3-fast',
'grok-3-mini',
'grok-3-mini-fast',
];

run(async () => {
for (const modelId of models) {
try {
const result = await generateText({
model: xai(modelId),
prompt: 'Say a single word.',
});

const body = result.response.body as Record<string, any>;
const raw = body.usage;
const sdk = result.usage;

console.log(`--- ${modelId} ---`);
console.log(
`raw: completion_tokens=${raw.completion_tokens}, reasoning_tokens=${raw.completion_tokens_details?.reasoning_tokens ?? 0}, total_tokens=${raw.total_tokens}`,
);
console.log(
`sdk: outputTokens=${sdk.outputTokens}, textTokens=${sdk.outputTokenDetails?.textTokens}, reasoningTokens=${sdk.outputTokenDetails?.reasoningTokens}, totalTokens=${sdk.totalTokens}`,
);
console.log();
} catch (e: any) {
console.log(`--- ${modelId} ---`);
console.log(`error: ${e.message?.slice(0, 80)}`);
console.log();
}
}
});
16 changes: 16 additions & 0 deletions examples/ai-functions/src/generate-text/xai-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
import { run } from '../lib/run';

run(async () => {
const result = await generateText({
model: xai('grok-3-mini'),
prompt: 'Say a single word.',
});

console.log('text:', result.text);
console.log();
console.log('raw usage:', JSON.stringify(result.response.body, null, 2));
console.log();
console.log('sdk usage:', JSON.stringify(result.usage, null, 2));
});
43 changes: 43 additions & 0 deletions examples/ai-functions/src/stream-text/xai-usage-full.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { xai } from '@ai-sdk/xai';
import { streamText } from 'ai';
import { run } from '../lib/run';

const models = [
'grok-4',
'grok-4-1-fast-reasoning',
'grok-4-1-fast-non-reasoning',
'grok-4-fast-reasoning',
'grok-4-fast-non-reasoning',
'grok-code-fast-1',
'grok-3',
'grok-3-fast',
'grok-3-mini',
'grok-3-mini-fast',
];

run(async () => {
for (const modelId of models) {
try {
const result = streamText({
model: xai(modelId),
prompt: 'Say a single word.',
});

for await (const textPart of result.textStream) {
void textPart;
}

const sdk = await result.usage;

console.log(`--- ${modelId} ---`);
console.log(
`sdk: outputTokens=${sdk.outputTokens}, textTokens=${sdk.outputTokenDetails?.textTokens}, reasoningTokens=${sdk.outputTokenDetails?.reasoningTokens}, totalTokens=${sdk.totalTokens}`,
);
console.log();
} catch (e: any) {
console.log(`--- ${modelId} ---`);
console.log(`error: ${e.message?.slice(0, 80)}`);
console.log();
}
}
});
18 changes: 18 additions & 0 deletions examples/ai-functions/src/stream-text/xai-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { xai } from '@ai-sdk/xai';
import { streamText } from 'ai';
import { run } from '../lib/run';

run(async () => {
const result = streamText({
model: xai('grok-3-mini'),
prompt: 'Say a single word.',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log();
console.log('sdk usage:', JSON.stringify(await result.usage, null, 2));
});
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Response: I'll go with "Hello" as it's a common greeting and keeps it simple.",
},
"outputTokens": {
"reasoning": 228,
"text": -227,
"total": 1,
"text": 1,
"total": 229,
},
"raw": {
"completion_tokens": 1,
Expand Down Expand Up @@ -235,8 +235,8 @@ I should call this function to advance the user's request.",
},
"outputTokens": {
"reasoning": 189,
"text": -163,
"total": 26,
"text": 26,
"total": 215,
},
"raw": {
"completion_tokens": 26,
Expand Down Expand Up @@ -333,8 +333,8 @@ exports[`XaiChatLanguageModel > doStream > text > should stream text content 1`]
},
"outputTokens": {
"reasoning": 290,
"text": -289,
"total": 1,
"text": 1,
"total": 291,
},
"raw": {
"completion_tokens": 1,
Expand Down Expand Up @@ -438,8 +438,8 @@ exports[`XaiChatLanguageModel > doStream > tool call > should stream tool call c
},
"outputTokens": {
"reasoning": 196,
"text": -170,
"total": 26,
"text": 26,
"total": 222,
},
"raw": {
"completion_tokens": 26,
Expand Down
Loading
Loading