01 / AI · Analysis
Z.AI details its GLM-5.2 API with 1M-token context
The developer guide covers project-wide code refactoring, Python SDK installation and streaming API calls.
Z.AI has published an API guide for GLM-5.2, a text model with a context window of one million tokens, covering tasks involving entire software projects. Tokens are units of text processed by AI models. The guide describes how developers can supply multiple code files and design documents in a single task.
The previous model, GLM-5.1, had a context limit of 200,000 tokens. Despite the larger context window, the maximum output length remains 128K tokens. Both models accept and generate text.
The official Python examples use a Z.AI API key and zai-sdk, a software development kit for connecting applications to the model. The documentation covers standard API calls and streaming, which delivers responses as the model generates them.
Project-wide code refactoring
Z.AI said it trained GLM-5.2 to retain earlier design decisions throughout extended coding tasks involving multiple files. One example is refactoring, the process of restructuring existing code without changing how the software behaves.
The developer guide recommends supplying source code, test files, configuration files and engineering rules together. Developers first ask the model to identify the project structure and each component's role, then specify the scope of modifications and the constraints it must preserve. The task concludes with a request for test results.
On Terminal-Bench 2.1, which evaluates performance on terminal-based tasks, GLM-5.2 scored 81.0 against GLM-5.1's 62.0, according to figures published by Z.AI.
Calling the model from Python
The Python SDK provides access to the model through the ZaiClient class and its client.chat.completions.create() method. The official examples follow these steps:
- Run
pip install zai-sdkto install the official Python SDK. - Import the client with
from zai import ZaiClientand supply the issued API key throughapi_key. - Set the model identifier to
glm-5.2and include task instructions and user requests inmessages. - Enable reasoning by setting
typewithinthinkingtoenabled, and setreasoning_efforttomax. - Use
max_tokensto limit response length andtemperatureto control output variability. - Retrieve the completed response through
response.choices[0].message, or addstream=Trueto receive it as it is generated.
Streaming separates the model's reasoning output, available through reasoning_content, from the main response in content. Applications can use these fields to display responses as they are generated during lengthy tasks.
Output limits and free model options
The official API examples set max_tokens to 4096. This is the output limit for the example request, rather than the model's maximum output capacity of 128K tokens.
Z.AI also lists GLM-4.7-Flash as a free model with a context window of 200K tokens. The GLM-5.2 developer overview does not specify API pricing.
The SDK installation command and complete Python examples for standard and streaming API calls are available in the official GLM-5.2 developer documentation.