Open History to see the last commit
1import { ZAI_THINKING_MODE_URL } from '../provider-replay.ts'
2import { GLM_REPLAY_FIXTURE } from '../fixtures/provider-replay.ts'
3import { afterEach, describe, expect, it } from 'vitest'
4
5import {
6 namedBy,
7 parseZaiContextWindows,
8 parseZaiOutputCaps,
9 parseZaiReasoning,
10 parseZaiThinkingSwitch,
11 parseZaiTokenPrices,
12 provider,
13 ZAI_OPENAPI_URL,
14 ZAI_OVERVIEW_URL,
15 ZAI_PRICING_URL,
16 ZAI_THINKING_URL,
17 zaiSpecFacts,
18 zaiSupportedBy,
19} from './zai.ts'
20
21/** `max_tokens` descriptions of https://docs.z.ai/openapi.json (2026-10-06). */
22const TEXT_MAX_TOKENS =
23 'The maximum number of tokens for model output, the GLM-5.3, GLM-5.2, GLM-5.1, GLM-5, GLM-4.7, GLM-4.6 series supports 128K maximum output, the GLM-4.5 series supports 96K maximum output, the GLM-4.6v series supports 32K maximum output, the GLM-4.5v series supports 16K maximum output, GLM-4-32B-0414-128K supports 16K maximum output.'
24const VISION_MAX_TOKENS =
25 'The maximum number of tokens for model output. `GLM-5.3-Flash` series supports a maximum output length of 128K, the GLM-4.6V series supports 32K, the GLM-4.5V series supports 16K, and autoglm-phone-multilingual supports 4K. It is recommended to set it to no less than 1024.'
26
27/** `ChatThinking.type` of https://docs.z.ai/openapi.json (2026-10-07). */
28const THINKING_TYPE = {
29 type: 'string',
30 description:
31 'Whether to enable the chain of thought(`GLM-5.3` `GLM-5.3-FLASH` series can only be enabled, and the thinking depth is controlled by `reasoning_effort`; for other models, when enabled, GLM-5.2 GLM-5.1 GLM-5 GLM-4.6 GLM-4.5 and others will automatically determine whether to think, while GLM-4.7 and GLM-4.5V will think compulsorily), default: enabled',
32 default: 'enabled',
33 enum: ['enabled', 'disabled'],
34}
35
36const part = (type: string) => ({
37 type: 'object',
38 properties: { type: { type: 'string', enum: [type] } },
39})
40
41const message = (role: string, content: Array<unknown>) => ({
42 type: 'object',
43 properties: {
44 role: { type: 'string', enum: [role] },
45 content: { oneOf: content },
46 },
47})
48
49/** Excerpt of https://docs.z.ai/openapi.json (2026-10-06), same nesting. */
50function spec(
51 overrides: {
52 textMaxTokens?: string
53 visionMaxTokens?: string
54 parts?: Array<string>
55 thinking?: string
56 thinkingType?: unknown
57 visionTools?: string
58 } = {},
59) {
60 const shared = {
61 temperature: { type: 'number' },
62 thinking: { $ref: '#/components/schemas/ChatThinking' },
63 tool_choice: { oneOf: [{ type: 'string', enum: ['auto'] }] },
64 }
65 return {
66 openapi: '3.0.1',
67 info: { title: 'Z.AI API', version: '1' },
68 paths: {
69 '/paas/v4/chat/completions': {
70 post: {
71 requestBody: {
72 content: {
73 'application/json': {
74 schema: {
75 oneOf: [
76 { $ref: '#/components/schemas/ChatCompletionTextRequest' },
77 {
78 $ref: '#/components/schemas/ChatCompletionVisionRequest',
79 },
80 ],
81 },
82 },
83 },
84 },
85 responses: { '200': {} },
86 },
87 },
88 '/paas/v4/images/generations': {
89 post: {
90 requestBody: {
91 content: {
92 'application/json': {
93 schema: { $ref: '#/components/schemas/CreateImageRequest' },
94 },
95 },
96 },
97 },
98 },
99 '/paas/v4/tokenizer': {
100 post: {
101 requestBody: {
102 content: {
103 'application/json': {
104 schema: { $ref: '#/components/schemas/TokenizerRequest' },
105 },
106 },
107 },
108 },
109 },
110 },
111 components: {
112 schemas: {
113 ChatCompletionTextRequest: {
114 type: 'object',
115 properties: {
116 model: {
117 type: 'string',
118 enum: [
119 'glm-5.3',
120 'glm-5.2',
121 'glm-5',
122 'glm-4.7-flash',
123 'glm-4.5-air',
124 'glm-4-32b-0414-128k',
125 ],
126 },
127 messages: {
128 type: 'array',
129 items: {
130 oneOf: [
131 message('user', [{ type: 'string' }]),
132 message('system', [{ type: 'string' }]),
133 ],
134 },
135 },
136 max_tokens: {
137 type: 'integer',
138 description: overrides.textMaxTokens ?? TEXT_MAX_TOKENS,
139 maximum: 131072,
140 },
141 reasoning_effort: {
142 type: 'string',
143 description:
144 "Controls the model's reasoning effort level, takes effect when `thinking` is enabled. Default is `max`, supported by `GLM-5.2` and above. For the `GLM-5.3` `GLM-5.3-FLASH` model, only the `low` / `high` / `max` levels are supported.",
145 enum: [
146 'max',
147 'xhigh',
148 'high',
149 'medium',
150 'low',
151 'minimal',
152 'none',
153 ],
154 },
155 response_format: {
156 type: 'object',
157 description:
158 'Specifies the response format of the model. Defaults to text. Only text models support this field.',
159 properties: { type: { type: 'string' } },
160 },
161 tool_stream: { type: 'boolean' },
162 tools: {
163 type: 'array',
164 description:
165 'A list of tools the model may call. Currently, only functions are supported as a tool. A max of 128 functions are supported.\n',
166 },
167 ...shared,
168 },
169 },
170 ChatThinking: {
171 type: 'object',
172 description:
173 overrides.thinking ??
174 'Only supported by GLM-4.5 series and higher models. This parameter is used to control whether the model enable the chain of thought.',
175 properties: { type: overrides.thinkingType ?? THINKING_TYPE },
176 },
177 ChatCompletionVisionRequest: {
178 type: 'object',
179 properties: {
180 model: {
181 type: 'string',
182 enum: [
183 'glm-5.3-flashx',
184 'glm-5.3-flash',
185 'glm-4.6v',
186 'glm-4.5v',
187 'autoglm-phone-multilingual',
188 ],
189 },
190 messages: {
191 type: 'array',
192 items: {
193 oneOf: [
194 message('user', [
195 {
196 type: 'array',
197 items: {
198 $ref: '#/components/schemas/VisionMultimodalContentItem',
199 },
200 },
201 { type: 'string' },
202 ]),
203 message('assistant', [{ type: 'string' }]),
204 ],
205 },
206 },
207 max_tokens: {
208 type: 'integer',
209 description: overrides.visionMaxTokens ?? VISION_MAX_TOKENS,
210 maximum: 131072,
211 },
212 reasoning_effort: {
213 type: 'string',
214 description:
215 "Controls the model's reasoning effort level, takes effect when `thinking` is enabled. Default is `max`.",
216 enum: ['max', 'high', 'low'],
217 },
218 tools: {
219 type: 'array',
220 description:
221 overrides.visionTools ??
222 'A list of tools the model may call. Only supported by `GLM-5.3-Flash` series, the GLM-4.6V series, and autoglm-phone-multilingual. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.\n',
223 },
224 ...shared,
225 },
226 },
227 VisionMultimodalContentItem: {
228 oneOf: (
229 overrides.parts ?? ['text', 'image_url', 'video_url', 'file']
230 ).map(part),
231 },
232 CreateImageRequest: {
233 type: 'object',
234 properties: { model: { type: 'string', enum: ['glm-image'] } },
235 },
236 TokenizerRequest: {
237 type: 'object',
238 properties: {
239 model: { type: 'string', enum: ['glm-5', 'glm-ocr'] },
240 },
241 },
242 },
243 },
244 }
245}
246
247/** Excerpt of https://docs.z.ai/guides/overview/pricing.md (2026-10-06). */
248const PRICING = `
249Prices per 1M tokens.
250
251| Model | Input | Cached Input | Cached Input Storage | Output |
252| :- | :- | :- | :- | :- |
253| GLM-5.3 | \\$1.4 | \\$0.26 | Limited-time Free | \\$4.4 |
254| GLM-5.3-Flash | \\$0.15 | \\$0.03 | Limited-time Free | \\$0.50 |
255| GLM-4-32B-0414-128K | \\$0.1 | - | - | \\$0.1 |
256| GLM-4.7-Flash | Free | Free | Free | Free |
257| Not-A-Model | \\$9 | \\$1 | - | \\$9 |
258
259### Image Generation Models
260
261Prices per image.
262
263| Model | Price |
264| GLM-Image | \\$0.015 |
265`
266
267/** Excerpt of https://docs.z.ai/guides/overview/overview.md (2026-10-06). */
268const OVERVIEW = `
269## Featured Models
270
271| Model | Strength | Language | Context | Resource |
272| :- | :- | :- | :- | :- |
273| GLM-5.3-Flash | Delivering frontier intelligence at radically lower cost | English & Chinese | 1M | [Guide](/guides/vlm/glm-5.3-flash) |
274| GLM-5.3-FlashX | Delivering inference speeds of 200 tokens/s | English & Chinese | 1M | [Guide](/guides/vlm/glm-5.3-flash) |
275| GLM-5.3 | Stronger in long-horizon, complex tasks | English & Chinese | 1M | [Guide](/guides/llm/glm-5.3) |
276| GLM-5.2 | Open-source SOTA coding | English & Chinese | 1M | [Guide](/guides/llm/glm-5.2) |
277| GLM-OCR | Document Parsing<br />Information Extraction | Multiple | / | [Guide](/guides/vlm/glm-ocr) |
278
279<AccordionGroup>
280 <Accordion title="Other Models, Agents and Tools">
281 ### Text Models
282
283 | Model | Strength | Language | Context | Resource |
284 | :- | :- | :- | :- | :- |
285 | GLM-5 | Agentic Long-Term Planning and Execution | English & Chinese | 200K | [Guide](/guides/llm/glm-5) |
286 | GLM-4.5-Air | Cost-Effective<br />High Performance | English & Chinese | 128K | [Guide](/guides/llm/glm-4.5) |
287 | GLM-4-32B-0414-128K | High intelligence | English & Chinese | 128K | [Guide](/guides/llm/glm-4-32b-0414-128k) |
288 | GLM-4.7-Flash | Free, Lightweight | English & Chinese | 200K | [Guide](/guides/llm/glm-4.7) |
289
290 ### Vision Models
291
292 | Model | Strength | Language | Context | Resource |
293 | :- | :- | :- | :- | :- |
294 | GLM-4.6V | Native Function Call Support | English & Chinese | 128K | [Guide](/guides/vlm/glm-4.6v) |
295
296 ### Built-in Tools
297
298 | Tool | Capability |
299 | :- | :- |
300 | Web Search | Provide real-time, concise, direct answers |
301
302 ### Image Generation Models
303
304 | Model | Strength | Language | Resolution | Resource |
305 | :- | :- | :- | :- | :- |
306 | GLM-Image | Open-source SOTA in text rendering | English & Chinese | multiple resolutions | [Guide](/guides/image/glm-image) |
307 </Accordion>
308</AccordionGroup>
309`
310
311/** Excerpt of https://docs.z.ai/guides/capabilities/thinking.md (2026-10-06). */
312const THINKING = `
313### Core Parameters
314
315* **\`reasoning_effort\`**: Controls the degree of reasoning within the thought chain, and is only supported by \`GLM-5.2\` and above.
316 * Available values: \`max\` (default and recommended, deep inference), \`high\` (enhanced inference), \`low\` (mild inference, only supported by GLM-5.3 and GLM-5.3-FLASH)
317 * In the API request:
318 * * For GLM-5.3 and GLM-5.3-FLASH, only \`max\`, \`high\` and \`low\` are supported. Any other input will result in an error.
319 * * For GLM-5.2, the supported options are \`max\` (default and recommended, for deep inference), \`xhigh\`, \`high\` (enhanced inference), \`medium\`, \`low\`, \`minimal\`, and \`none\`. Among them, \`none\` or \`minimal\` indicate that the model stops thinking; \`low\`/\`medium\` are mapped to \`high\`; \`xhigh\` is mapped to \`max\`.
320 * In the Coding Plan request:
321 * * For GLM-5.3 and GLM-5.3-FLASH, \`none\`, \`minimal\`, and \`low\` are mapped to \`low\`; \`medium\`, \`high\` are mapped to \`high\`; \`xhigh\` and \`max\` are mapped to \`max\`.
322* **\`model\`**: A model that enables deep thinking, supported by \`GLM-4.5\` and above versions.
323`
324
325const originalFetch = globalThis.fetch
326
327afterEach(() => {
328 globalThis.fetch = originalFetch
329})
330
331function serve(docs: Record<string, string>): Array<string> {
332 const urls: Array<string> = []
333 globalThis.fetch = ((url: string) => {
334 urls.push(String(url))
335 const body = docs[String(url)]
336 return body === undefined
337 ? Promise.reject(new Error(`unexpected fetch: ${String(url)}`))
338 : Promise.resolve(new Response(body))
339 }) as typeof fetch
340 return urls
341}
342
343const DOCS = {
344 [ZAI_THINKING_MODE_URL]: GLM_REPLAY_FIXTURE,
345 [ZAI_OPENAPI_URL]: JSON.stringify(spec()),
346 [ZAI_PRICING_URL]: PRICING,
347 [ZAI_OVERVIEW_URL]: OVERVIEW,
348 [ZAI_THINKING_URL]: THINKING,
349}
350
351describe('zai', () => {
352 it('lists OpenAPI model ids and per-1M prices that match those ids', async () => {
353 const urls = serve(DOCS)
354
355 const listed = await provider.listModels({})
356 const fetched = await provider.fetchSpec({})
357 const byId = new Map(listed.models.map((model) => [model.rawId, model]))
358
359 expect(listed.models.map((model) => model.rawId)).toEqual([
360 'glm-4-32b-0414-128k',
361 'glm-4.5-air',
362 'glm-4.5v',
363 'glm-4.6v',
364 'glm-4.7-flash',
365 'glm-5',
366 'glm-5.2',
367 'glm-5.3',
368 'glm-5.3-flash',
369 'glm-5.3-flashx',
370 'glm-image',
371 'glm-ocr',
372 ])
373 expect(byId.get('glm-5.3')?.pricing).toMatchObject({
374 tables: {
375 rate: {
376 base: {
377 input_tokens: 1.4 / 1_000_000,
378 output_tokens: 4.4 / 1_000_000,
379 cache_read_tokens: 0.26 / 1_000_000,
380 },
381 },
382 },
383 })
384 expect(byId.get('glm-5.3-flash')?.pricing).toMatchObject({
385 tables: {
386 rate: {
387 base: {
388 input_tokens: 0.15 / 1_000_000,
389 output_tokens: 0.5 / 1_000_000,
390 },
391 },
392 },
393 })
394 // "Free" and "-" are not rates.
395 expect(byId.get('glm-4.7-flash')?.pricing).toBeNull()
396 expect(byId.get('glm-4-32b-0414-128k')?.pricing).not.toHaveProperty(
397 'tables.rate.base.cache_read_tokens',
398 )
399 expect(byId.get('glm-image')?.pricing).toBeNull()
400 expect(listed.models.some((model) => model.rawId === 'not-a-model')).toBe(
401 false,
402 )
403 expect(fetched.specs).toHaveLength(1)
404 expect(fetched.sources[0]?.url).toBe(ZAI_OPENAPI_URL)
405 expect(provider.classify('/paas/v4/chat/completions', {})).toBe('chat')
406 expect(provider.classify('/paas/v4/images/generations', {})).toBe('image')
407 expect(provider.classify('/paas/v4/tokenizer', {})).toBeNull()
408 expect(urls).toEqual([
409 ZAI_OPENAPI_URL,
410 ZAI_PRICING_URL,
411 ZAI_OVERVIEW_URL,
412 ZAI_THINKING_URL,
413 ZAI_THINKING_MODE_URL,
414 ZAI_OPENAPI_URL,
415 ])
416 expect(provider.classify('/paas/v4/videos/generations', {})).toBe('video')
417 expect(provider.classify('/paas/v4/audio/speech', {})).toBe('audio')
418 })
419
420 it('binds each id to the route whose request lists it', async () => {
421 serve(DOCS)
422 const { models } = await provider.listModels({})
423 const byId = new Map(models.map((model) => [model.rawId, model]))
424
425 for (const id of ['glm-5.3', 'glm-4.5-air', 'glm-4.6v', 'glm-5.3-flashx']) {
426 expect(byId.get(id)?.activity).toBe('chat')
427 expect(byId.get(id)?.schemaEndpointId).toBe('paas/v4/chat/completions')
428 }
429 // glm-5 is also a tokenizer model; the chat route binds it.
430 expect(byId.get('glm-5')?.activity).toBe('chat')
431 expect(byId.get('glm-image')?.activity).toBe('image')
432 expect(byId.get('glm-image')?.schemaEndpointId).toBe(
433 'paas/v4/images/generations',
434 )
435 // The tokenizer is not a generation route.
436 expect(byId.get('glm-ocr')?.activity).toBeUndefined()
437 expect(byId.get('glm-ocr')?.schemaEndpointId).toBeUndefined()
438 })
439
440 it('fills chat facts from the spec, the overview, and the thinking page', async () => {
441 serve(DOCS)
442 const { models } = await provider.listModels({})
443 const byId = new Map(models.map((model) => [model.rawId, model]))
444
445 expect(byId.get('glm-5.3')).toMatchObject({
446 contextWindow: 1_000_000,
447 // "128K" is the label of the spec's exact `maximum`.
448 maxOutput: 131_072,
449 modalities: { input: ['text'], output: ['text'] },
450 reasoning: {
451 mode: 'effort',
452 mandatory: true,
453 efforts: ['max', 'high', 'low'],
454 },
455 })
456 expect(byId.get('glm-5.2')?.reasoning).toEqual({
457 mode: 'effort',
458 mandatory: false,
459 efforts: ['max', 'xhigh', 'high', 'medium', 'low', 'minimal', 'none'],
460 })
461 // The vision request has its own cap list and content parts.
462 expect(byId.get('glm-5.3-flashx')).toMatchObject({
463 contextWindow: 1_000_000,
464 maxOutput: 131_072,
465 modalities: { input: ['text', 'image', 'video', 'file'] },
466 reasoning: { mandatory: true, efforts: ['max', 'high', 'low'] },
467 })
468 expect(byId.get('glm-4.6v')).toMatchObject({
469 contextWindow: 128_000,
470 maxOutput: 32_000,
471 modalities: { input: ['text', 'image', 'video', 'file'] },
472 })
473 // A series cap covers its variants; glm-5 is not glm-5.x.
474 expect(byId.get('glm-4.5-air')?.maxOutput).toBe(96_000)
475 expect(byId.get('glm-4.7-flash')?.maxOutput).toBe(131_072)
476 expect(byId.get('glm-4-32b-0414-128k')?.maxOutput).toBe(16_000)
477 expect(byId.get('glm-5')).toMatchObject({
478 contextWindow: 200_000,
479 maxOutput: 131_072,
480 })
481 // No effort list names these: `thinking.type` is their whole control,
482 // and only the GLM-5.3 series "can only be enabled".
483 for (const id of ['glm-5', 'glm-4.5-air', 'glm-4.7-flash', 'glm-4.6v']) {
484 expect(byId.get(id)?.reasoning).toEqual({
485 mode: 'toggle',
486 mandatory: false,
487 })
488 expect(byId.get(id)?.factSources?.reasoning).toMatchObject({
489 derivation: 'upstream-spec',
490 sourceUrl: ZAI_OPENAPI_URL,
491 path: 'thinking.type',
492 })
493 }
494 // `thinking` is "GLM-4.5 series and higher": no switch, no object.
495 expect(byId.get('glm-4-32b-0414-128k')?.reasoning).toBeUndefined()
496 expect(
497 byId.get('glm-4-32b-0414-128k')?.factSources?.reasoning,
498 ).toBeUndefined()
499 expect(byId.get('glm-image')?.reasoning).toBeUndefined()
500 // "/" and a Resolution column are not context windows.
501 expect(byId.get('glm-ocr')?.contextWindow).toBeUndefined()
502 expect(byId.get('glm-image')?.contextWindow).toBeUndefined()
503 })
504
505 it('names the document each fact came from', async () => {
506 serve(DOCS)
507 const { models } = await provider.listModels({})
508 const sources = models.find(
509 (model) => model.rawId === 'glm-5.3',
510 )?.factSources
511
512 expect(sources?.pricing).toMatchObject({
513 derivation: 'docs-derived',
514 sourceUrl: ZAI_PRICING_URL,
515 })
516 expect(sources?.contextWindow).toMatchObject({
517 derivation: 'docs-derived',
518 sourceUrl: ZAI_OVERVIEW_URL,
519 })
520 expect(sources?.reasoning).toMatchObject({
521 derivation: 'docs-derived',
522 sourceUrl: ZAI_THINKING_URL,
523 })
524 expect(sources?.maxOutput).toMatchObject({
525 derivation: 'upstream-spec',
526 sourceUrl: ZAI_OPENAPI_URL,
527 path: 'max_tokens',
528 })
529 expect(sources?.modalities).toMatchObject({
530 derivation: 'upstream-spec',
531 sourceUrl: ZAI_OPENAPI_URL,
532 })
533 expect(sources?.maxOutput?.sourceHash).toMatch(/^[0-9a-f]{64}$/)
534 expect(sources?.capabilities?.tools).toMatchObject({
535 derivation: 'upstream-spec',
536 sourceUrl: ZAI_OPENAPI_URL,
537 endpointId: 'paas/v4/chat/completions',
538 path: '/properties/tools',
539 })
540 // The thinking page does not name FlashX; its request variant's own
541 // `reasoning_effort` enum is the same list.
542 expect(
543 models.find((model) => model.rawId === 'glm-5.3-flashx')?.factSources
544 ?.reasoning,
545 ).toMatchObject({
546 derivation: 'upstream-spec',
547 sourceUrl: ZAI_OPENAPI_URL,
548 path: 'reasoning_effort',
549 })
550 expect(
551 models.find((model) => model.rawId === 'glm-ocr')?.factSources,
552 ).toBeUndefined()
553 })
554
555 it('lists only the flags the spec gives each model', async () => {
556 serve(DOCS)
557 const { models } = await provider.listModels({})
558 const flags = (id: string) => {
559 const model = models.find((row) => row.rawId === id)
560 expect(model?.exactCapabilities).toBe(true)
561 return [...(model!.capabilities as Array<string>)].sort()
562 }
563 const base = ['max_tokens', 'temperature']
564
565 expect(flags('glm-5.3')).toEqual(
566 [
567 ...base,
568 'reasoning',
569 'reasoning_effort',
570 'response_format',
571 'tool_choice',
572 'tools',
573 ].sort(),
574 )
575 // `reasoning_effort` is "supported by GLM-5.2 and above".
576 expect(flags('glm-5')).toEqual(
577 [...base, 'reasoning', 'response_format', 'tool_choice', 'tools'].sort(),
578 )
579 // `thinking` is "GLM-4.5 series and higher".
580 expect(flags('glm-4-32b-0414-128k')).toEqual(
581 [...base, 'response_format', 'tool_choice', 'tools'].sort(),
582 )
583 // The vision request has no `response_format`.
584 expect(flags('glm-5.3-flashx')).toEqual(
585 [...base, 'reasoning', 'reasoning_effort', 'tool_choice', 'tools'].sort(),
586 )
587 // No effort list names glm-4.6v, whatever enum its variant carries.
588 expect(flags('glm-4.6v')).toEqual(
589 [...base, 'reasoning', 'tool_choice', 'tools'].sort(),
590 )
591 // Vision `tools` names the series it is supported by; not glm-4.5v.
592 expect(flags('glm-4.5v')).toEqual([...base, 'reasoning'].sort())
593 expect(
594 models.find((row) => row.rawId === 'glm-image')?.capabilities,
595 ).toBeUndefined()
596 })
597
598 it('throws on a 200 that is a web page', async () => {
599 serve({
600 ...DOCS,
601 [ZAI_OVERVIEW_URL]: '<!DOCTYPE html><html><body>Not found</body></html>',
602 })
603 await expect(provider.listModels({})).rejects.toThrow(/returned HTML/)
604 })
605})
606
607describe('zai parsers fail closed', () => {
608 it('reads per-1M prices from the indented "Other Pricing" accordion', () => {
609 // Excerpt of https://docs.z.ai/guides/overview/pricing.md (2026-10-12).
610 const prices = parseZaiTokenPrices(`
611<AccordionGroup>
612 <Accordion title="Other Pricing">
613 ### Vision Models
614
615 Prices per 1M tokens.
616
617 | Model | Input | Cached Input | Cached Input Storage | Output |
618 | :- | :- | :- | :- | :- |
619 | GLM-OCR | \\$0.03 | \\\\ | \\\\ | \\$0.03 |
620
621 ### Batch Models
622
623 | Model | Input | Cached Input | Cached Input Storage | Output |
624 | :- | :- | :- | :- | :- |
625 | Not-Per-1M | \\$9 | \\$1 | - | \\$9 |
626 </Accordion>
627</AccordionGroup>
628`)
629 expect([...prices]).toEqual([
630 ['glm-ocr', { input: 0.03, output: 0.03, cache: null }],
631 ])
632 })
633
634 it('reads prices and context windows from blockquoted accordions', () => {
635 // Excerpts of the pricing and overview pages (2026-10-20).
636 const prices = parseZaiTokenPrices(`
637> **Other Pricing**
638>
639> ### Vision Models
640>
641> Prices per 1M tokens.
642>
643> | Model | Input | Cached Input | Cached Input Storage | Output |
644> | :- | :- | :- | :- | :- |
645> | GLM-OCR | \\$0.03 | \\\\ | \\\\ | \\$0.03 |
646`)
647 expect([...prices]).toEqual([
648 ['glm-ocr', { input: 0.03, output: 0.03, cache: null }],
649 ])
650 const windows = parseZaiContextWindows(`
651> **Other Models, Agents and Tools**
652>
653> ### Text Models
654>
655> | Model | Strength | Language | Context | Resource |
656> | :- | :- | :- | :- | :- |
657> | GLM-5.1 | Coding proficiency | English & Chinese | 200K | [Guide](/guides/llm/glm-5.1) |
658`)
659 expect([...windows]).toEqual([['glm-5.1', 200_000]])
660 })
661
662 it('throws when the pricing page lists two prices for one model', () => {
663 expect(() =>
664 parseZaiTokenPrices(`
665Prices per 1M tokens.
666
667| Model | Input | Cached Input | Cached Input Storage | Output |
668| GLM-4.5 | \\$0.6 | \\$0.11 | Limited-time Free | \\$2.2 |
669
670 ### Text Models
671
672 Prices per 1M tokens.
673
674 | Model | Input | Cached Input | Cached Input Storage | Output |
675 | GLM-4.5 | \\$0.5 | \\$0.11 | Limited-time Free | \\$2.2 |
676`),
677 ).toThrow('two prices for GLM-4.5')
678 expect(
679 parseZaiTokenPrices(`
680Prices per 1M tokens.
681
682| Model | Input | Cached Input | Cached Input Storage | Output |
683| GLM-4.5 | \\$0.6 | \\$0.11 | Limited-time Free | \\$2.2 |
684| GLM-4.5 | \\$0.6 | \\$0.11 | Limited-time Free | \\$2.2 |
685`).get('glm-4.5'),
686 ).toEqual({ input: 0.6, output: 2.2, cache: 0.11 })
687 })
688
689 it('reads each output cap with its own models', () => {
690 expect(parseZaiOutputCaps(VISION_MAX_TOKENS)).toEqual([
691 { names: ['glm-5.3-flash'], series: true, tokens: 128_000 },
692 { names: ['glm-4.6v'], series: true, tokens: 32_000 },
693 { names: ['glm-4.5v'], series: true, tokens: 16_000 },
694 { names: ['autoglm-phone-multilingual'], series: false, tokens: 4_000 },
695 ])
696 })
697
698 it('throws on a reworded output cap', () => {
699 // Without the throw, glm-4.5 would take the next clause's 32K.
700 expect(() =>
701 parseZaiOutputCaps(
702 TEXT_MAX_TOKENS.replace(
703 'supports 96K maximum output',
704 'supports up to 96K of output',
705 ),
706 ),
707 ).toThrow(/unreadable output cap/)
708 // A clause with another verb must not take the next clause's cap:
709 // unguarded, the 128K series would be stored as 96K ...
710 expect(() =>
711 parseZaiOutputCaps(
712 TEXT_MAX_TOKENS.replace(
713 'GLM-4.6 series supports 128K maximum output',
714 'GLM-4.6 series allows up to 128K of output',
715 ),
716 ),
717 ).toThrow(/unreadable output cap/)
718 // ... and glm-4.6v as 16K.
719 expect(() =>
720 zaiSpecFacts(
721 spec({
722 visionMaxTokens: VISION_MAX_TOKENS.replace(
723 'the GLM-4.6V series supports 32K',
724 'the GLM-4.6V series can output 32K',
725 ),
726 }),
727 ),
728 ).toThrow(/unreadable output cap/)
729 // The last clause has no next one to fall into; it is left over.
730 expect(() =>
731 parseZaiOutputCaps(
732 VISION_MAX_TOKENS.replace(
733 'autoglm-phone-multilingual supports 4K',
734 'autoglm-phone-multilingual is capped at 4K',
735 ),
736 ),
737 ).toThrow(/model with no output cap/)
738 expect(() =>
739 parseZaiOutputCaps('The maximum number of tokens for model output.'),
740 ).toThrow(/states no output cap/)
741 expect(() =>
742 zaiSpecFacts(spec({ textMaxTokens: 'Up to 128K for every model.' })),
743 ).toThrow(/states no output cap/)
744 })
745
746 it('drops a flag whose "supported by" clause it cannot read', () => {
747 expect(zaiSupportedBy('A list of tools. A max of 128.', 'glm-5')).toBeNull()
748 expect(
749 zaiSupportedBy('Only supported by GLM-4.5 series and higher.', 'glm-5'),
750 ).toBe(true)
751 expect(
752 zaiSupportedBy('Only supported by GLM-4.5 series and higher.', 'glm-4'),
753 ).toBe(false)
754
755 const reasoning = (thinking: string, id: string) =>
756 Object.keys(
757 zaiSpecFacts(spec({ thinking })).get(id)?.capabilities ?? {},
758 ).includes('reasoning')
759 // Reworded floor: nobody keeps the flag.
760 expect(
761 reasoning('Only supported by models newer than GLM-4.5.', 'glm-5.3'),
762 ).toBe(false)
763 expect(
764 reasoning('Only supported by GLM-4.5 or GLM-5 on request.', 'glm-5'),
765 ).toBe(false)
766
767 const tools = Object.keys(
768 zaiSpecFacts(
769 spec({ visionTools: 'Only supported by most GLM-4.6V models.' }),
770 ).get('glm-4.6v')?.capabilities ?? {},
771 )
772 expect(tools).not.toContain('tools')
773 expect(tools).not.toContain('tool_choice')
774 })
775
776 it('throws on a content part it does not know', () => {
777 expect(() =>
778 zaiSpecFacts(spec({ parts: ['text', 'image_url', 'hologram_url'] })),
779 ).toThrow(/unknown message content part: hologram_url/)
780 })
781
782 it('throws when the chat request lists no model ids', () => {
783 const document = spec()
784 document.components.schemas.ChatCompletionTextRequest.properties.model.enum =
785 []
786 document.components.schemas.ChatCompletionVisionRequest.properties.model.enum =
787 []
788 expect(() => zaiSpecFacts(document)).toThrow(/lists no model ids/)
789 })
790
791 it('throws on a context cell that is not a token count', () => {
792 expect(() =>
793 parseZaiContextWindows(OVERVIEW.replace('| 200K |', '| up to 200K |')),
794 ).toThrow(/unreadable context window for GLM-5/)
795 expect(() =>
796 parseZaiContextWindows(OVERVIEW.replaceAll('Context', 'Window')),
797 ).toThrow(/no context windows/)
798 })
799
800 it('reads who cannot turn thinking off from the switch itself', () => {
801 expect(parseZaiThinkingSwitch(THINKING_TYPE)).toEqual({
802 names: ['glm-5.3', 'glm-5.3-flash'],
803 series: true,
804 })
805 })
806
807 it('stores a toggle that cannot be turned off as mandatory', async () => {
808 // A model the description locks, with no effort list of its own.
809 serve({
810 ...DOCS,
811 [ZAI_OPENAPI_URL]: JSON.stringify(
812 spec({
813 thinkingType: {
814 ...THINKING_TYPE,
815 description: THINKING_TYPE.description.replace(
816 '`GLM-5.3` `GLM-5.3-FLASH` series',
817 '`GLM-5.3` `GLM-5.3-FLASH` `GLM-4.7` series',
818 ),
819 },
820 }),
821 ),
822 })
823 const { models } = await provider.listModels({})
824 const reasoning = (id: string) =>
825 models.find((model) => model.rawId === id)?.reasoning
826 expect(reasoning('glm-4.7-flash')).toEqual({
827 mode: 'toggle',
828 mandatory: true,
829 })
830 expect(reasoning('glm-5')).toEqual({ mode: 'toggle', mandatory: false })
831 // An effort row keeps its own reading.
832 expect(reasoning('glm-5.3')?.mode).toBe('effort')
833 })
834
835 it('states mandatory only from the switch or an off value in the list', async () => {
836 const effort = (id: string) =>
837 namedBy(id, parseZaiReasoning(THINKING))?.reasoning
838 // "only `max`, `high` and `low`" does not say thinking cannot turn off.
839 expect(effort('glm-5.3')).toEqual({
840 mode: 'effort',
841 mandatory: null,
842 efforts: ['max', 'high', 'low'],
843 })
844 expect(effort('glm-5.2')?.mandatory).toBe(false)
845
846 // The switch no longer locks the GLM-5.3 series: nothing states it.
847 const unlocked = THINKING_TYPE.description.replace(
848 '`GLM-5.3` `GLM-5.3-FLASH` series',
849 '`GLM-4.7` series',
850 )
851 serve({
852 ...DOCS,
853 [ZAI_OPENAPI_URL]: JSON.stringify(
854 spec({ thinkingType: { ...THINKING_TYPE, description: unlocked } }),
855 ),
856 })
857 const { models } = await provider.listModels({})
858 for (const id of ['glm-5.3', 'glm-5.3-flash', 'glm-5.3-flashx']) {
859 expect(models.find((model) => model.rawId === id)?.reasoning).toEqual({
860 mode: 'effort',
861 mandatory: null,
862 efforts: ['max', 'high', 'low'],
863 })
864 }
865 })
866
867 it('throws when a locked model lists a none effort', async () => {
868 serve({
869 ...DOCS,
870 [ZAI_OPENAPI_URL]: JSON.stringify(
871 spec({
872 thinkingType: {
873 ...THINKING_TYPE,
874 description: THINKING_TYPE.description.replace(
875 '`GLM-5.3` `GLM-5.3-FLASH` series',
876 '`GLM-5.3` `GLM-5.3-FLASH` `GLM-5.2` series',
877 ),
878 },
879 }),
880 ),
881 })
882 await expect(provider.listModels({})).rejects.toThrow(
883 /glm-5.2 lists a none effort but cannot disable thinking/,
884 )
885 })
886
887 it('throws on a thinking.type switch it cannot read', async () => {
888 const reworded = [
889 // The sentence that says who may send `disabled` is gone.
890 {
891 ...THINKING_TYPE,
892 description: 'Whether to enable the chain of thought',
893 },
894 {
895 ...THINKING_TYPE,
896 description: THINKING_TYPE.description.replace(
897 'series can only be enabled',
898 'series always think',
899 ),
900 },
901 {
902 ...THINKING_TYPE,
903 description: THINKING_TYPE.description.replace(
904 'for other models, when enabled',
905 'other models cannot be disabled either',
906 ),
907 },
908 // The locked list is not a plain list of models.
909 {
910 ...THINKING_TYPE,
911 description: THINKING_TYPE.description.replace(
912 '`GLM-5.3` `GLM-5.3-FLASH` series',
913 'every model except `GLM-4.5` series',
914 ),
915 },
916 // A third position is not an on/off switch.
917 { ...THINKING_TYPE, enum: ['enabled', 'disabled', 'auto'] },
918 { ...THINKING_TYPE, enum: ['enabled'] },
919 { type: 'string' },
920 ]
921 for (const thinkingType of reworded) {
922 expect(() => parseZaiThinkingSwitch(thinkingType)).toThrow(
923 /unreadable thinking.type switch/,
924 )
925 serve({
926 ...DOCS,
927 [ZAI_OPENAPI_URL]: JSON.stringify(spec({ thinkingType })),
928 })
929 await expect(provider.listModels({})).rejects.toThrow(
930 /unreadable thinking.type switch/,
931 )
932 }
933 })
934
935 it('throws on a reworded reasoning_effort list', () => {
936 expect(() =>
937 parseZaiReasoning(
938 THINKING.replace(
939 'For GLM-5.2, the supported options are',
940 'GLM-5.2 takes',
941 ),
942 ),
943 ).toThrow(/unreadable reasoning_effort line/)
944 expect(() =>
945 parseZaiReasoning(THINKING.replace('In the API request:', 'API:')),
946 ).toThrow(/no API reasoning_effort list/)
947 })
948})
949
950it('sources GLM replay from the native thinking-mode guide and rejects malformed guides', async () => {
951 serve(DOCS)
952 const model = (await provider.listModels({})).models.find(
953 (row) => row.rawId === 'glm-5.2',
954 )
955 expect(model?.requestMap?.replayReasoningContent).toBe(true)
956 expect(model?.factSources?.requestMap?.sourceUrl).toBe(ZAI_THINKING_MODE_URL)
957 serve({
958 ...DOCS,
959 [ZAI_THINKING_MODE_URL]:
960 '# Thinking Mode\nThe response contains reasoning_content.',
961 })
962 await expect(provider.listModels({})).rejects.toThrow(/no verified replay/)
963})