messages and read the reply from choices[0].message.content.
Optional parameters and tool support vary by model. If your application is built on Anthropic Messages or Gemini
generateContent request structures, use the Claude protocol or Gemini protocol page instead. The Text generation API guide compares the three formats.
Make a request
Create an API key in Token management in thedefault group, with its billing mode set to Usage first (按量优先, recommended) or Usage-based (按量计费), and export it in your terminal:
Authorization: Bearer authentication and a JSON body containing model and messages. The examples use gpt-5.4-mini.
- cURL
- Python
connected. Replace the prompt with your application’s message once the client is configured.
Choose a model
Pick the model in the model catalog. Before you change themodel value, check three things in the model’s row:
- Endpoints includes OpenAI-compatible. A model that lists only Responses, such as the
o3-proseries, needs/v1/responsesinstead; see the Responses API examples. - Token groups includes the group of your API key. Use a
default-group key for this endpoint:Claude Code-group keys are for the Claude protocol only, and features such as tool calls are not guaranteed to work when they send Chat Completions requests. - Input, Output, and Cache read prices per 1M tokens fit your workload.
gpt-5.4-miniandgpt-5.4gemini-3.8-flash,gemini-3.6-flash, andgemini-3.5-flash-lite
o3-pro.
A general ranking will not tell you which model suits your application. Run a sample of your own prompts through two or three candidates and compare answer quality, instruction following, structured output, tool-call success if you use tools, latency, and the token usage recorded for each request.
Pin the chosen model ID in your configuration so that a model change is a deliberate update.
Request fields
Start with
model and messages. Optional fields and accepted values depend on the model; a request rejected for an unsupported parameter needs a field change before you retry it. See the official Chat Completions reference for upstream field definitions.
Messages and conversation history
Each message has arole and content. Use user for user input and assistant for previous model replies. Send the history your application needs with each request:
system or developer, tool messages, and multimodal content depend on the selected model. To send images to a vision model, see the Image understanding API.
Read the response
A text response contains achoices array. This shortened example shows the fields to read:
message.content for the answer and finish_reason to see why generation ended. A length finish reason means the reply reached an output or context limit; shorten the input or adjust the model’s supported output limit if you need a complete answer.
For a model that supports tool calls, the reply may contain message.tool_calls instead of ordinary text. Preserve those calls and their matching results when continuing the conversation. Token counts may appear in usage; use call logs to review account charges.
Stream a response
Add"stream": true to receive the reply in pieces as the model writes it. The response is a stream of server-sent events:
- Each
data:line carries achat.completion.chunkobject. - The new text is in
choices[0].delta.content. - The stream ends with a
data: [DONE]line.
- cURL
- Python
-N turns off cURL’s output buffering so that each chunk prints as soon as it arrives.id and model:
- Append each
delta.contentin order to rebuild the full reply. - The first chunk may carry only the role, and some chunks, such as a final usage chunk, can have an empty
choicesarray, so check before you read. - The last content chunk sets
finish_reason;data: [DONE]then closes the stream, and the Python SDK ends the loop at that point. - If the connection drops before
[DONE], treat the reply as incomplete.
Handle errors
For a client timeout, check whether the request appears in call logs before sending it again. The Python examples disable automatic retries; add retry handling deliberately in your application.
When contacting support, include the request time, endpoint, model, token group, and error message with credentials removed. Never send your API key.