Z.AI
Learn how to use Z.AI's GLM models with the AI SDK.
The Z.AI provider gives you access to GLM language and vision models through the Z.AI API.
API keys can be created in the Z.AI API key console.
Setup
The Z.AI provider is available through the @ai-sdk/zai package. Install it with:
$ pnpm add @ai-sdk/zai
$ npm install @ai-sdk/zai
$ yarn add @ai-sdk/zai
$ bun add @ai-sdk/zai
Set the ZAI_API_KEY environment variable:
Provider Instance
Import the default provider instance zai from @ai-sdk/zai:
For custom configuration, use createZai:
The provider accepts these optional settings:
-
apiKey string
API key sent in the
Authorizationheader. It defaults to theZAI_API_KEYenvironment variable. -
baseURL string
URL prefix for API requests. It defaults to
https://api.z.ai/api/paas/v4. -
headers Record<string, string>
Additional request headers.
-
fetch FetchFunction
Custom fetch implementation, for example to intercept requests in middleware.
Language Models
Create a language model by passing its model id to the provider:
Current model families include GLM 5, GLM 4.7, GLM 4.6, GLM 4.5, and the GLM vision models. Model availability changes over time; see the Z.AI model documentation for the current catalog.
The provider supports:
- text generation and streaming
- reasoning output and reasoning history
- function calling, including incremental tool-call streaming
- JSON object output
- URL-based image and video inputs on compatible vision models
Provider Options
Z.AI-specific options can be passed through providerOptions.zai:
The following options are available:
-
doSample boolean
Enables sampling. When disabled,
temperatureandtopPdo not take effect. -
thinking object
Controls thinking with
type: 'enabled' | 'disabled'. SetclearThinkingtofalseto retain reasoning from previous assistant messages. -
reasoningEffort 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max'
Controls reasoning effort on supported models.
-
toolStream boolean
Enables incremental function-call argument streaming on supported models.
-
requestId string
A caller-provided request id between 6 and 64 characters.
-
userId string
A non-sensitive end-user id between 6 and 128 characters.
Streaming Tool Calls
Enable toolStream when you want supported GLM models to stream function-call arguments incrementally: