Open History to see the last commit
1/**
2 * Model metadata interface for documentation and type inference
3 */
4import type {
5 GrokBuildProviderOptions,
6 GrokTextProviderOptions,
7} from './text/text-provider-options'
8
9interface ModelMeta {
10 name: string
11 supports: {
12 input: Array<'text' | 'image' | 'audio' | 'video' | 'document'>
13 output: Array<'text' | 'image' | 'audio' | 'video'>
14 capabilities?: Array<'reasoning' | 'tool_calling' | 'structured_outputs'>
15 tools?: ReadonlyArray<GrokProviderToolKind>
16 }
17 max_input_tokens?: number
18 max_output_tokens?: number
19 context_window?: number
20 knowledge_cutoff?: string
21 pricing?: {
22 input: {
23 normal: number
24 cached?: number
25 }
26 output: {
27 normal: number
28 }
29 }
30}
31
32const GROK_4_5 = {
33 name: 'grok-4.5',
34 context_window: 500_000,
35 supports: {
36 input: ['text', 'image', 'document'],
37 output: ['text'],
38 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
39 tools: [],
40 },
41 pricing: {
42 input: {
43 normal: 2,
44 cached: 0.3,
45 },
46 output: {
47 normal: 6,
48 },
49 },
50} as const satisfies ModelMeta
51
52const GROK_4_6 = {
53 name: 'grok-4.6',
54 context_window: 500_000,
55 supports: {
56 input: ['text', 'image', 'document'],
57 output: ['text'],
58 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
59 tools: [],
60 },
61 pricing: {
62 input: {
63 normal: 2,
64 cached: 0.5,
65 },
66 output: {
67 normal: 6,
68 },
69 },
70} as const satisfies ModelMeta
71
72const GROK_4_7 = {
73 name: 'grok-4.7',
74 context_window: 500_000,
75 max_output_tokens: 450_000,
76 supports: {
77 input: ['text', 'image', 'document'],
78 output: ['text'],
79 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
80 tools: [],
81 },
82 pricing: {
83 input: {
84 normal: 1.6,
85 cached: 0.4,
86 },
87 output: {
88 normal: 4.8,
89 },
90 },
91} as const satisfies ModelMeta
92
93export type GrokProviderToolKind =
94 | 'web_search'
95 | 'x_search'
96 | 'file_search'
97 | 'mcp'
98
99const GROK_RESPONSES_TOOLS = [
100 'web_search',
101 'x_search',
102 'file_search',
103 'mcp',
104] as const satisfies ReadonlyArray<GrokProviderToolKind>
105
106// Imagine API image models. Pricing is per generated image (output only).
107const GROK_IMAGINE_IMAGE = {
108 name: 'grok-imagine-image',
109 supports: {
110 input: ['text', 'image'],
111 output: ['image'],
112 },
113 pricing: {
114 input: {
115 normal: 0,
116 },
117 output: {
118 normal: 0.02,
119 },
120 },
121} as const satisfies ModelMeta
122
123const GROK_IMAGINE_IMAGE_QUALITY = {
124 name: 'grok-imagine-image-quality',
125 supports: {
126 input: ['text', 'image'],
127 output: ['image'],
128 },
129 pricing: {
130 input: {
131 normal: 0,
132 },
133 output: {
134 normal: 0.05,
135 },
136 },
137} as const satisfies ModelMeta
138
139// xAI's recommended Imagine image model. Supports the 2.0-only `quality`
140// provider option ('low' | 'medium', default 'medium').
141const GROK_IMAGINE_IMAGE_2_0 = {
142 name: 'grok-imagine-image-2.0',
143 supports: {
144 input: ['text', 'image'],
145 output: ['image'],
146 },
147 pricing: {
148 input: {
149 normal: 0,
150 },
151 output: {
152 normal: 0.04,
153 },
154 },
155} as const satisfies ModelMeta
156
157// Imagine API video models. Pricing is per second of generated video
158// (output only); generated videos carry an audio track.
159//
160// Both models support text-to-video and image-to-video (a starting-frame
161// image is optional). grok-imagine-video-1.5 is the documented default: it
162// adds native 1080p for text-to-video / image-to-video plus
163// reference-to-video (`reference_images` / `reference_audios`; reference
164// output is capped at 720p). Source-video edit (`/v1/videos/edits`) and
165// extend (`/v1/videos/extensions`) are grok-imagine-video only — xAI's
166// 1.5 model page lists text+image input, not video.
167const GROK_IMAGINE_VIDEO = {
168 name: 'grok-imagine-video',
169 supports: {
170 input: ['text', 'image', 'video'],
171 output: ['video', 'audio'],
172 },
173 pricing: {
174 input: {
175 normal: 0,
176 },
177 output: {
178 // per second of video
179 normal: 0.05,
180 },
181 },
182} as const satisfies ModelMeta
183
184const GROK_IMAGINE_VIDEO_1_5 = {
185 name: 'grok-imagine-video-1.5',
186 supports: {
187 input: ['text', 'image'],
188 output: ['video', 'audio'],
189 },
190 pricing: {
191 input: {
192 normal: 0,
193 },
194 output: {
195 // per second of video
196 normal: 0.08,
197 },
198 },
199} as const satisfies ModelMeta
200
201const GROK_4_3 = {
202 name: 'grok-4.3',
203 context_window: 1_000_000,
204 supports: {
205 input: ['text', 'image'],
206 output: ['text'],
207 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
208 tools: GROK_RESPONSES_TOOLS,
209 },
210 pricing: {
211 input: {
212 normal: 1.25,
213 cached: 0.2,
214 },
215 output: {
216 normal: 2.5,
217 },
218 },
219} as const satisfies ModelMeta
220
221const GROK_BUILD_0_1 = {
222 name: 'grok-build-0.1',
223 context_window: 256_000,
224 supports: {
225 input: ['text', 'image'],
226 output: ['text'],
227 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
228 tools: GROK_RESPONSES_TOOLS,
229 },
230 pricing: {
231 input: {
232 normal: 1,
233 cached: 0.2,
234 },
235 output: {
236 normal: 2,
237 },
238 },
239} as const satisfies ModelMeta
240
241// Vertex Model Garden IDs. These are not the xAI API catalog.
242// Source: https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/grok
243const GROK_4_20_REASONING = {
244 name: 'grok-4.20-reasoning',
245 context_window: 1_000_000,
246 supports: {
247 input: ['text', 'image'],
248 output: ['text'],
249 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
250 tools: [] as const,
251 },
252} as const satisfies ModelMeta
253
254const GROK_4_20_NON_REASONING = {
255 name: 'grok-4.20-non-reasoning',
256 context_window: 1_000_000,
257 supports: {
258 input: ['text', 'image'],
259 output: ['text'],
260 capabilities: ['structured_outputs', 'tool_calling'],
261 tools: [] as const,
262 },
263} as const satisfies ModelMeta
264
265const GROK_4_1_FAST_REASONING = {
266 name: 'grok-4.1-fast-reasoning',
267 context_window: 2_000_000,
268 supports: {
269 input: ['text', 'image'],
270 output: ['text'],
271 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
272 tools: [] as const,
273 },
274} as const satisfies ModelMeta
275
276const GROK_4_1_FAST_NON_REASONING = {
277 name: 'grok-4.1-fast-non-reasoning',
278 context_window: 2_000_000,
279 supports: {
280 input: ['text', 'image'],
281 output: ['text'],
282 capabilities: ['structured_outputs', 'tool_calling'],
283 tools: [] as const,
284 },
285} as const satisfies ModelMeta
286
287/**
288 * Grok chat models supported by the xAI Responses adapter.
289 */
290export const GROK_CHAT_MODELS = [
291 GROK_4_7.name,
292 GROK_4_5.name,
293 GROK_4_6.name,
294 GROK_BUILD_0_1.name,
295 GROK_4_3.name,
296] as const
297
298/**
299 * Grok chat models on Vertex AI / Gemini Enterprise Agent Platform.
300 * This list is the Google partner catalog, not the xAI API catalog.
301 */
302export const GROK_VERTEX_CHAT_MODELS = [
303 GROK_4_3.name,
304 GROK_4_20_REASONING.name,
305 GROK_4_20_NON_REASONING.name,
306 GROK_4_1_FAST_REASONING.name,
307 GROK_4_1_FAST_NON_REASONING.name,
308] as const
309
310/**
311 * Grok Image Generation Models
312 */
313export const GROK_IMAGE_MODELS = [
314 GROK_IMAGINE_IMAGE.name,
315 GROK_IMAGINE_IMAGE_2_0.name,
316 GROK_IMAGINE_IMAGE_QUALITY.name,
317] as const
318
319/**
320 * Grok Video Generation Models (xAI Imagine API)
321 *
322 * @experimental Video generation is an experimental feature and may change.
323 */
324export const GROK_VIDEO_MODELS = [
325 GROK_IMAGINE_VIDEO.name,
326 GROK_IMAGINE_VIDEO_1_5.name,
327] as const
328
329// xAI's `/v1/tts` endpoint is endpoint-addressed and does not take a `model`
330// parameter. This synthetic identifier satisfies the SDK's `TTSOptions.model`
331// contract and provides a stable value for logging and fixture matching.
332const GROK_TTS = {
333 name: 'grok-tts',
334 supports: {
335 input: ['text'],
336 output: ['audio'],
337 },
338} as const satisfies ModelMeta
339
340// xAI's `/v1/stt` endpoint is endpoint-addressed and does not take a `model`
341// parameter. This synthetic identifier satisfies the SDK's
342// `TranscriptionOptions.model` contract.
343const GROK_STT = {
344 name: 'grok-stt',
345 supports: {
346 input: ['audio'],
347 output: ['text'],
348 },
349} as const satisfies ModelMeta
350
351const GROK_VOICE_FAST_1 = {
352 name: 'grok-voice-fast-1.0',
353 supports: {
354 input: ['audio', 'text'],
355 output: ['audio', 'text'],
356 capabilities: ['tool_calling'],
357 tools: [] as const,
358 },
359} as const satisfies ModelMeta
360
361/** @deprecated xAI has deprecated grok-voice-think-fast-1.0 — use grok-voice-think-fast-2.0. */
362const GROK_VOICE_THINK_FAST_1 = {
363 name: 'grok-voice-think-fast-1.0',
364 supports: {
365 input: ['audio', 'text'],
366 output: ['audio', 'text'],
367 capabilities: ['reasoning', 'tool_calling'],
368 tools: [] as const,
369 },
370} as const satisfies ModelMeta
371
372// xAI's current recommended speech-to-speech model.
373const GROK_VOICE_THINK_FAST_2 = {
374 name: 'grok-voice-think-fast-2.0',
375 supports: {
376 input: ['audio', 'text'],
377 output: ['audio', 'text'],
378 capabilities: ['reasoning', 'tool_calling'],
379 tools: [] as const,
380 },
381} as const satisfies ModelMeta
382
383// Rolling alias used by xAI's realtime docs examples; always points at the
384// latest speech-to-speech model.
385const GROK_VOICE_LATEST = {
386 name: 'grok-voice-latest',
387 supports: {
388 input: ['audio', 'text'],
389 output: ['audio', 'text'],
390 capabilities: ['reasoning', 'tool_calling'],
391 tools: [] as const,
392 },
393} as const satisfies ModelMeta
394
395export const GROK_TTS_MODELS = [GROK_TTS.name] as const
396
397export const GROK_TRANSCRIPTION_MODELS = [GROK_STT.name] as const
398
399export const GROK_REALTIME_MODELS = [
400 GROK_VOICE_THINK_FAST_2.name,
401 GROK_VOICE_LATEST.name,
402 GROK_VOICE_FAST_1.name,
403 GROK_VOICE_THINK_FAST_1.name,
404] as const
405
406/**
407 * Default speech-to-speech model used by the realtime token issuer and the
408 * realtime client adapter when no model is specified. Single source of truth
409 * so a future default bump cannot leave the two sides disagreeing.
410 */
411export const GROK_DEFAULT_REALTIME_MODEL: GrokRealtimeModel =
412 'grok-voice-think-fast-2.0'
413
414export type GrokChatModel = (typeof GROK_CHAT_MODELS)[number]
415export type GrokVertexChatModel = (typeof GROK_VERTEX_CHAT_MODELS)[number]
416export type GrokTextAdapterModel = GrokChatModel | GrokVertexChatModel
417export type GrokImageModel = (typeof GROK_IMAGE_MODELS)[number]
418export type GrokVideoModel = (typeof GROK_VIDEO_MODELS)[number]
419export type GrokTTSModel = (typeof GROK_TTS_MODELS)[number]
420export type GrokTranscriptionModel = (typeof GROK_TRANSCRIPTION_MODELS)[number]
421export type GrokRealtimeModel = (typeof GROK_REALTIME_MODELS)[number]
422
423/**
424 * Type-only map from Grok chat model name to its supported input modalities.
425 * Used for type inference when constructing multimodal messages.
426 */
427export type GrokModelInputModalitiesByName = {
428 [GROK_4_3.name]: typeof GROK_4_3.supports.input
429 [GROK_BUILD_0_1.name]: typeof GROK_BUILD_0_1.supports.input
430 [GROK_4_5.name]: typeof GROK_4_5.supports.input
431 [GROK_4_6.name]: typeof GROK_4_6.supports.input
432 [GROK_4_20_REASONING.name]: typeof GROK_4_20_REASONING.supports.input
433 [GROK_4_20_NON_REASONING.name]: typeof GROK_4_20_NON_REASONING.supports.input
434 [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.input
435 [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.input
436 [GROK_4_7.name]: typeof GROK_4_7.supports.input
437}
438
439/**
440 * Type-only map from Grok chat model name to its supported provider tools.
441 * Keeps Grok provider-tool factories type-checked against the models that
442 * advertise xAI Responses server-side tools.
443 */
444export type GrokChatModelToolCapabilitiesByName = {
445 [GROK_4_3.name]: typeof GROK_4_3.supports.tools
446 [GROK_BUILD_0_1.name]: typeof GROK_BUILD_0_1.supports.tools
447 [GROK_4_20_REASONING.name]: typeof GROK_4_20_REASONING.supports.tools
448 [GROK_4_20_NON_REASONING.name]: typeof GROK_4_20_NON_REASONING.supports.tools
449 [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.tools
450 [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.tools
451 [GROK_4_7.name]: typeof GROK_4_7.supports.tools
452}
453
454export type GrokProviderOptions = GrokTextProviderOptions
455
456/**
457 * Type-only map from Grok chat model name to its provider options type.
458 */
459export type GrokChatModelProviderOptionsByName = {
460 [GROK_4_3.name]: GrokProviderOptions
461 [GROK_BUILD_0_1.name]: GrokBuildProviderOptions
462 [GROK_4_20_REASONING.name]: GrokProviderOptions
463 [GROK_4_20_NON_REASONING.name]: GrokProviderOptions
464 [GROK_4_1_FAST_REASONING.name]: GrokProviderOptions
465 [GROK_4_1_FAST_NON_REASONING.name]: GrokProviderOptions
466}
467
468// ===========================
469// Type Resolution Helpers
470// ===========================
471
472/**
473 * Resolve provider options for a specific model.
474 * If the model has explicit options in the map, use those; otherwise use base options.
475 */
476export type ResolveProviderOptions<TModel extends string> =
477 TModel extends keyof GrokChatModelProviderOptionsByName
478 ? GrokChatModelProviderOptionsByName[TModel]
479 : GrokProviderOptions
480
481/**
482 * Resolve input modalities for a specific model.
483 * If the model has explicit modalities in the map, use those; otherwise use text only.
484 */
485export type ResolveInputModalities<TModel extends string> =
486 TModel extends keyof GrokModelInputModalitiesByName
487 ? GrokModelInputModalitiesByName[TModel]
488 : readonly ['text']