中文

Guides

Chat

Text in, text out. Chat is the most popular feature on the xAI API, and can be used for anything from summarizing articles, generating creative writing, answering questions, providing customer support, to assisting with coding tasks.grok.cadn.net.cn


  • xAI Account: You need an xAI account to access the API.
  • API Key: Ensure that your API key has access to the chat endpoint and the chat model is enabled.

If you don't have these and are unsure of how to create one, follow the Hitchhiker's Guide to Grok.grok.cadn.net.cn

You can create an API key on the xAI Console API Keys Page.grok.cadn.net.cn

Set your API key in your environment:grok.cadn.net.cn

export XAI_API_KEY="your_api_key"

You can also stream the response, which is covered in Streaming Response.grok.cadn.net.cn

The user sends a request to the xAI API endpoint. The API processes this and returns a complete response.grok.cadn.net.cn

import os
from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_XAI_API_KEY_HERE>",
    base_url="https://api.x.ai/v1",
)

completion = client.chat.completions.create(
    model="grok-2-latest",
    messages=[
        {"role": "system", "content": "You are a PhD-level mathematician."},
        {"role": "user", "content": "What is 2 + 2?"},
    ],
)

print(completion.choices[0].message)

Response:grok.cadn.net.cn

ChatCompletionMessage(
    content='2 + 2 equals 4.',
    refusal=None,
    role='assistant',
    audio=None,
    function_call=None,
    tool_calls=None
)

The xAI API is stateless and does not process new request with the context of your previous request history.grok.cadn.net.cn

However, you can provide previous chat generation prompts and results to a new chat generation request to let the model process your new request with the context in mind.grok.cadn.net.cn

An example message:grok.cadn.net.cn

{
  "role": "user",
  "content": [{ "type": "text", "text": "Why don't eggs tell jokes?" }]
},
{
  "role": "assistant",
  "content": [{ "type": "text", "text": "They'd crack up!" }]
},
{
  "role": "user",
  "content": [{"type": "text", "text": "Can you explain the joke?"}],
}

This strategy can be used within function calling, in which the model response will invoke a tool call, the user's program responds to the tool call and continues the conversation by appending tool call result to the message. For more details, check out our guide on Function Calling.grok.cadn.net.cn


Unlike some models from other providers, one of the unique aspects of xAI API is its flexibility with message roles:grok.cadn.net.cn

  • No Order Limitation: You can mix system, user, or assistant roles in any sequence for your conversation context.

Example 1 - Multiple System Messages:grok.cadn.net.cn

[
{"role": "system", "content": "..."},
{"role": "system", "content": "..."},
{"role": "user", "content": "..."},
{"role": "user", "content": "..."}
]

Example 2 - User Messages First:grok.cadn.net.cn

{"role": "user", "content": "..."},
{"role": "user", "content": "..."},
{"role": "system", "content": "..."}

You can customize the following parameters in the request to achieve different generation results.grok.cadn.net.cn

Request body

A list of messages that make up the the chat conversation. Different models support different message types, such as image and text.grok.cadn.net.cn

Model name for the model to use.grok.cadn.net.cn