@ai-sdk/xai 4.0.36 → 4.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +59 -26
- package/dist/index.js.map +1 -1
- package/docs/01-xai.mdx +45 -5
- package/package.json +3 -3
- package/src/responses/xai-responses-api.ts +2 -0
- package/src/responses/xai-responses-language-model-options.ts +7 -2
- package/src/responses/xai-responses-language-model.ts +18 -5
- package/src/xai-chat-language-model-options.ts +7 -1
- package/src/xai-chat-language-model.ts +20 -1
package/docs/01-xai.mdx
CHANGED
|
@@ -121,19 +121,54 @@ The AI SDK option accepts these values, but each xAI model supports a subset:
|
|
|
121
121
|
- `'high'` — More reasoning tokens for deeper thinking. Suited for very
|
|
122
122
|
challenging problems, complex math, multi-step logic, and competition-level
|
|
123
123
|
tasks.
|
|
124
|
+
- `'xhigh'` — Uses the most reasoning tokens for the most challenging tasks.
|
|
125
|
+
This level is only supported by `grok-4.6`.
|
|
124
126
|
|
|
125
127
|
<Note>
|
|
126
128
|
Support and defaults are model-specific. `grok-4.3` supports `'none'`,
|
|
127
129
|
`'low'`, `'medium'`, and `'high'`. `grok-4.5` supports `'low'`, `'medium'`,
|
|
128
|
-
and `'high'`, defaults to `'high'`, and cannot disable reasoning.
|
|
129
|
-
`
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
and `'high'`, defaults to `'high'`, and cannot disable reasoning. `grok-4.6`
|
|
131
|
+
supports `'low'`, `'medium'`, `'high'`, and `'xhigh'`, and defaults to
|
|
132
|
+
`'high'`. The `grok-4.20-reasoning` and `grok-4.20-non-reasoning` variants do
|
|
133
|
+
not accept this option. For `grok-4.20-multi-agent`, `'low'`, `'medium'`, and
|
|
134
|
+
`'high'` control the number of agents instead of reasoning depth. See xAI's
|
|
135
|
+
[reasoning
|
|
132
136
|
docs](https://docs.x.ai/developers/model-capabilities/text/reasoning) and
|
|
133
|
-
[Grok 4.
|
|
137
|
+
[Grok 4.6 model page](https://docs.x.ai/developers/models/grok-4.6) for
|
|
134
138
|
current details.
|
|
135
139
|
</Note>
|
|
136
140
|
|
|
141
|
+
### Priority Processing
|
|
142
|
+
|
|
143
|
+
`providerOptions.xai.serviceTier` requests higher scheduling priority, which
|
|
144
|
+
typically lowers time-to-first-token and speeds up inter-token latency. This
|
|
145
|
+
works for both the Responses API (default) and the Chat Completions API
|
|
146
|
+
(`xai.chat()`).
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { xai } from '@ai-sdk/xai';
|
|
150
|
+
import { generateText } from 'ai';
|
|
151
|
+
|
|
152
|
+
const { providerMetadata } = await generateText({
|
|
153
|
+
model: xai('grok-4.6'),
|
|
154
|
+
prompt: 'Explain quantum entanglement.',
|
|
155
|
+
providerOptions: {
|
|
156
|
+
xai: { serviceTier: 'priority' },
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// 'priority' when the request was served at the priority tier,
|
|
161
|
+
// 'default' when priority capacity was unavailable.
|
|
162
|
+
console.log(providerMetadata?.xai?.serviceTier);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Priority requests are billed at a premium per-token rate, and xAI only charges
|
|
166
|
+
that rate when the response confirms the priority tier — so read the applied
|
|
167
|
+
tier back from `providerMetadata.xai.serviceTier` rather than assuming the
|
|
168
|
+
request you sent is the tier you got. Omitting the option is equivalent to
|
|
169
|
+
`'default'`. See xAI's [priority processing
|
|
170
|
+
docs](https://docs.x.ai/developers/advanced-api-usage/priority-processing).
|
|
171
|
+
|
|
137
172
|
## Realtime Models
|
|
138
173
|
|
|
139
174
|
<Note type="warning">Realtime is an experimental feature.</Note>
|
|
@@ -581,6 +616,10 @@ The following provider options are available:
|
|
|
581
616
|
|
|
582
617
|
The ID of the previous response from the model. You can use it to continue a conversation.
|
|
583
618
|
|
|
619
|
+
- **serviceTier** _'default' | 'priority'_
|
|
620
|
+
|
|
621
|
+
Scheduling priority for the request. `'priority'` buys lower time-to-first-token and faster inter-token latency at a premium per-token price. The tier xAI actually applied comes back on `providerMetadata.xai.serviceTier`, and is `'default'` when priority capacity was unavailable. See [Priority Processing](https://docs.x.ai/developers/advanced-api-usage/priority-processing).
|
|
622
|
+
|
|
584
623
|
<Note>
|
|
585
624
|
The Responses API only supports server-side tools. You cannot mix server-side
|
|
586
625
|
tools with client-side function tools in the same request.
|
|
@@ -590,6 +629,7 @@ The following provider options are available:
|
|
|
590
629
|
|
|
591
630
|
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming | Reasoning |
|
|
592
631
|
| ----------------------------- | ----------- | ----------------- | ---------- | -------------- | --------- |
|
|
632
|
+
| `grok-4.6` | <Check /> | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
593
633
|
| `grok-4.5` | <Check /> | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
594
634
|
| `grok-4.20-reasoning` | <Check /> | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
595
635
|
| `grok-4.20-non-reasoning` | <Check /> | <Check /> | <Check /> | <Check /> | <Cross /> |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/xai",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.38",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ai-sdk/provider": "
|
|
33
|
-
"@ai-sdk/provider
|
|
32
|
+
"@ai-sdk/provider-utils": "5.0.27",
|
|
33
|
+
"@ai-sdk/provider": "4.0.7"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "22.19.19",
|
|
@@ -287,6 +287,7 @@ export const xaiResponsesResponseSchema = z.object({
|
|
|
287
287
|
output: z.array(outputItemSchema),
|
|
288
288
|
usage: xaiResponsesUsageSchema.nullish(),
|
|
289
289
|
status: z.string(),
|
|
290
|
+
service_tier: z.string().nullish(),
|
|
290
291
|
});
|
|
291
292
|
|
|
292
293
|
export const xaiResponsesChunkSchema = z.union([
|
|
@@ -571,6 +572,7 @@ export const xaiResponsesChunkSchema = z.union([
|
|
|
571
572
|
response: z.object({
|
|
572
573
|
incomplete_details: z.object({ reason: z.string() }).nullish(),
|
|
573
574
|
usage: xaiResponsesUsageSchema.nullish(),
|
|
575
|
+
service_tier: z.string().nullish(),
|
|
574
576
|
}),
|
|
575
577
|
}),
|
|
576
578
|
z.object({
|
|
@@ -5,6 +5,7 @@ export type XaiResponsesModelId =
|
|
|
5
5
|
| 'grok-4.20-reasoning'
|
|
6
6
|
| 'grok-4.3'
|
|
7
7
|
| 'grok-4.5'
|
|
8
|
+
| 'grok-4.6'
|
|
8
9
|
| 'grok-latest'
|
|
9
10
|
| (string & {});
|
|
10
11
|
|
|
@@ -16,14 +17,18 @@ export const xaiLanguageModelResponsesOptions = z.object({
|
|
|
16
17
|
* Constrains how hard a reasoning model thinks before responding.
|
|
17
18
|
* Possible values are `none` (disables reasoning entirely; supported by
|
|
18
19
|
* `grok-4.3` and newer reasoning models), `low` (uses fewer reasoning
|
|
19
|
-
* tokens), `medium`,
|
|
20
|
+
* tokens), `medium`, `high` (uses more reasoning tokens), and `xhigh`
|
|
21
|
+
* (supported by `grok-4.6`).
|
|
20
22
|
*
|
|
21
23
|
* @see https://docs.x.ai/docs/guides/reasoning
|
|
22
24
|
*/
|
|
23
|
-
reasoningEffort: z
|
|
25
|
+
reasoningEffort: z
|
|
26
|
+
.enum(['none', 'low', 'medium', 'high', 'xhigh'])
|
|
27
|
+
.optional(),
|
|
24
28
|
reasoningSummary: z.enum(['auto', 'concise', 'detailed']).optional(),
|
|
25
29
|
logprobs: z.boolean().optional(),
|
|
26
30
|
topLogprobs: z.number().int().min(0).max(8).optional(),
|
|
31
|
+
serviceTier: z.enum(['default', 'priority']).optional(),
|
|
27
32
|
/**
|
|
28
33
|
* Whether to store the input message(s) and model response for later retrieval.
|
|
29
34
|
* Must be set to `false` for teams with Zero Data Retention (ZDR) enabled,
|
|
@@ -202,7 +202,7 @@ export class XaiResponsesLanguageModel implements LanguageModelV4 {
|
|
|
202
202
|
low: 'low',
|
|
203
203
|
medium: 'medium',
|
|
204
204
|
high: 'high',
|
|
205
|
-
xhigh: 'high',
|
|
205
|
+
xhigh: this.modelId === 'grok-4.6' ? 'xhigh' : 'high',
|
|
206
206
|
},
|
|
207
207
|
warnings,
|
|
208
208
|
});
|
|
@@ -255,6 +255,9 @@ export class XaiResponsesLanguageModel implements LanguageModelV4 {
|
|
|
255
255
|
...(options.previousResponseId != null && {
|
|
256
256
|
previous_response_id: options.previousResponseId,
|
|
257
257
|
}),
|
|
258
|
+
...(options.serviceTier != null && {
|
|
259
|
+
service_tier: options.serviceTier,
|
|
260
|
+
}),
|
|
258
261
|
};
|
|
259
262
|
|
|
260
263
|
if (xaiTools && xaiTools.length > 0) {
|
|
@@ -526,10 +529,16 @@ export class XaiResponsesLanguageModel implements LanguageModelV4 {
|
|
|
526
529
|
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
527
530
|
outputTokens: { total: 0, text: 0, reasoning: 0 },
|
|
528
531
|
},
|
|
529
|
-
...(response.usage?.cost_in_usd_ticks != null
|
|
532
|
+
...((response.usage?.cost_in_usd_ticks != null ||
|
|
533
|
+
response.service_tier != null) && {
|
|
530
534
|
providerMetadata: {
|
|
531
535
|
xai: {
|
|
532
|
-
|
|
536
|
+
...(response.usage?.cost_in_usd_ticks != null && {
|
|
537
|
+
costInUsdTicks: response.usage.cost_in_usd_ticks,
|
|
538
|
+
}),
|
|
539
|
+
...(response.service_tier != null && {
|
|
540
|
+
serviceTier: response.service_tier,
|
|
541
|
+
}),
|
|
533
542
|
},
|
|
534
543
|
},
|
|
535
544
|
}),
|
|
@@ -580,6 +589,7 @@ export class XaiResponsesLanguageModel implements LanguageModelV4 {
|
|
|
580
589
|
let hasFunctionCall = false;
|
|
581
590
|
let usage: LanguageModelV4Usage | undefined = undefined;
|
|
582
591
|
let costInUsdTicks: number | undefined = undefined;
|
|
592
|
+
let serviceTier: string | undefined = undefined;
|
|
583
593
|
let isFirstChunk = true;
|
|
584
594
|
const contentBlocks: Record<string, { type: 'text' }> = {};
|
|
585
595
|
const seenToolCalls = new Set<string>();
|
|
@@ -774,6 +784,8 @@ export class XaiResponsesLanguageModel implements LanguageModelV4 {
|
|
|
774
784
|
costInUsdTicks = response.usage.cost_in_usd_ticks ?? undefined;
|
|
775
785
|
}
|
|
776
786
|
|
|
787
|
+
serviceTier = response.service_tier ?? undefined;
|
|
788
|
+
|
|
777
789
|
if (event.type === 'response.incomplete') {
|
|
778
790
|
const reason =
|
|
779
791
|
'incomplete_details' in response
|
|
@@ -1222,10 +1234,11 @@ export class XaiResponsesLanguageModel implements LanguageModelV4 {
|
|
|
1222
1234
|
},
|
|
1223
1235
|
outputTokens: { total: 0, text: 0, reasoning: 0 },
|
|
1224
1236
|
},
|
|
1225
|
-
...(costInUsdTicks != null && {
|
|
1237
|
+
...((costInUsdTicks != null || serviceTier != null) && {
|
|
1226
1238
|
providerMetadata: {
|
|
1227
1239
|
xai: {
|
|
1228
|
-
costInUsdTicks,
|
|
1240
|
+
...(costInUsdTicks != null && { costInUsdTicks }),
|
|
1241
|
+
...(serviceTier != null && { serviceTier }),
|
|
1229
1242
|
},
|
|
1230
1243
|
},
|
|
1231
1244
|
}),
|
|
@@ -6,6 +6,7 @@ export type XaiChatModelId =
|
|
|
6
6
|
| 'grok-4.20-reasoning'
|
|
7
7
|
| 'grok-4.3'
|
|
8
8
|
| 'grok-4.5'
|
|
9
|
+
| 'grok-4.6'
|
|
9
10
|
| 'grok-latest'
|
|
10
11
|
| (string & {});
|
|
11
12
|
|
|
@@ -59,16 +60,21 @@ export const xaiLanguageModelChatOptions = z.object({
|
|
|
59
60
|
* - `low` (default): Uses some reasoning tokens, but still fast.
|
|
60
61
|
* - `medium`: More thinking for less-latency-sensitive applications.
|
|
61
62
|
* - `high`: Uses more reasoning tokens for deeper thinking.
|
|
63
|
+
* - `xhigh`: Uses the most reasoning tokens (supported by `grok-4.6`).
|
|
62
64
|
*
|
|
63
65
|
* Note: Not every Grok model accepts every value. Refer to xAI's docs for
|
|
64
66
|
* the values supported by your selected model.
|
|
65
67
|
*
|
|
66
68
|
* @see https://docs.x.ai/docs/guides/reasoning
|
|
67
69
|
*/
|
|
68
|
-
reasoningEffort: z
|
|
70
|
+
reasoningEffort: z
|
|
71
|
+
.enum(['none', 'low', 'medium', 'high', 'xhigh'])
|
|
72
|
+
.optional(),
|
|
69
73
|
logprobs: z.boolean().optional(),
|
|
70
74
|
topLogprobs: z.number().int().min(0).max(8).optional(),
|
|
71
75
|
|
|
76
|
+
serviceTier: z.enum(['default', 'priority']).optional(),
|
|
77
|
+
|
|
72
78
|
/**
|
|
73
79
|
* Whether to enable parallel function calling during tool use.
|
|
74
80
|
* When true, the model can call multiple functions in parallel.
|
|
@@ -158,7 +158,7 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
|
158
158
|
low: 'low',
|
|
159
159
|
medium: 'medium',
|
|
160
160
|
high: 'high',
|
|
161
|
-
xhigh: 'high',
|
|
161
|
+
xhigh: this.modelId === 'grok-4.6' ? 'xhigh' : 'high',
|
|
162
162
|
},
|
|
163
163
|
warnings,
|
|
164
164
|
});
|
|
@@ -181,6 +181,9 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
|
181
181
|
seed,
|
|
182
182
|
reasoning_effort: reasoningEffort,
|
|
183
183
|
|
|
184
|
+
// scheduling priority
|
|
185
|
+
service_tier: options.serviceTier,
|
|
186
|
+
|
|
184
187
|
// parallel function calling
|
|
185
188
|
parallel_function_calling: options.parallel_function_calling,
|
|
186
189
|
|
|
@@ -347,6 +350,11 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
|
347
350
|
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
348
351
|
outputTokens: { total: 0, text: 0, reasoning: 0 },
|
|
349
352
|
},
|
|
353
|
+
...(response.service_tier != null && {
|
|
354
|
+
providerMetadata: {
|
|
355
|
+
xai: { serviceTier: response.service_tier },
|
|
356
|
+
},
|
|
357
|
+
}),
|
|
350
358
|
request: { body },
|
|
351
359
|
response: {
|
|
352
360
|
...getResponseMetadata(response),
|
|
@@ -426,6 +434,7 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
|
426
434
|
raw: undefined,
|
|
427
435
|
};
|
|
428
436
|
let usage: LanguageModelV4Usage | undefined = undefined;
|
|
437
|
+
let serviceTier: string | undefined = undefined;
|
|
429
438
|
let isFirstChunk = true;
|
|
430
439
|
const contentBlocks: Record<
|
|
431
440
|
string,
|
|
@@ -485,6 +494,11 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
|
485
494
|
usage = convertXaiChatUsage(value.usage);
|
|
486
495
|
}
|
|
487
496
|
|
|
497
|
+
// the applied tier is repeated on every chunk; keep the latest
|
|
498
|
+
if (value.service_tier != null) {
|
|
499
|
+
serviceTier = value.service_tier;
|
|
500
|
+
}
|
|
501
|
+
|
|
488
502
|
const choice = value.choices[0];
|
|
489
503
|
|
|
490
504
|
// update finish reason if present
|
|
@@ -644,6 +658,9 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
|
644
658
|
},
|
|
645
659
|
outputTokens: { total: 0, text: 0, reasoning: 0 },
|
|
646
660
|
},
|
|
661
|
+
...(serviceTier != null && {
|
|
662
|
+
providerMetadata: { xai: { serviceTier } },
|
|
663
|
+
}),
|
|
647
664
|
});
|
|
648
665
|
},
|
|
649
666
|
}),
|
|
@@ -711,6 +728,7 @@ const xaiChatResponseSchema = z.object({
|
|
|
711
728
|
object: z.literal('chat.completion').nullish(),
|
|
712
729
|
usage: xaiUsageSchema.nullish(),
|
|
713
730
|
citations: z.array(z.string().url()).nullish(),
|
|
731
|
+
service_tier: z.string().nullish(),
|
|
714
732
|
code: z.string().nullish(),
|
|
715
733
|
error: z.string().nullish(),
|
|
716
734
|
});
|
|
@@ -744,6 +762,7 @@ const xaiChatChunkSchema = z.object({
|
|
|
744
762
|
),
|
|
745
763
|
usage: xaiUsageSchema.nullish(),
|
|
746
764
|
citations: z.array(z.string().url()).nullish(),
|
|
765
|
+
service_tier: z.string().nullish(),
|
|
747
766
|
});
|
|
748
767
|
|
|
749
768
|
const xaiStreamErrorSchema = z.object({
|