gRPC API
Chat
xai_api.Chat
An API that exposes our language models via a Chat interface.
Methods of xai_api.Chat
GetCompletion
unary
Samples a response from the model and blocks until the response has been fully generated.
GetCompletionChunk
server streaming
Samples a response from the model and streams out the model tokens as they are being generated.
StartDeferredCompletion
unary
Starts sampling of the model and immediately returns a response containing a request id. The request id may be used to poll the `GetDeferredCompletion` RPC.
GetDeferredCompletion
unary
Gets the result of a deferred completion started by calling `StartDeferredCompletion`.
GetStoredCompletion
unary
Retrieve a stored response using the response ID.
DeleteStoredCompletion
unary
Delete a stored response using the response ID.
Send a chat completion
import os
import xai_sdk
from xai_sdk.chat import user
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(model="grok-4.6")
chat.append(user("What is the meaning of life?"))
response = chat.sample()
print(response.content)
Streaming chat
import os
import xai_sdk
from xai_sdk.chat import user
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(model="grok-4.6")
chat.append(user("Tell me a short joke"))
for response, chunk in chat.stream():
print(chunk.content, end="", flush=True)
print()
Compact a long conversation
Shrink a long conversation into a single encrypted compaction blob that can be appended to a follow-up chat. See Context Compaction for the full guide.
import os
import xai_sdk
from xai_sdk.chat import assistant, system, user
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
# Option A: compact a Chat in place. Prior messages are replaced with the
# encrypted compaction blob; chat.sample() continues to work transparently.
chat = client.chat.create(model="grok-4.6", use_encrypted_content=True)
chat.append(system("You are a concise and knowledgeable science tutor."))
chat.append(user("What is the Higgs boson and why is it important?"))
chat.append(chat.sample())
# ... many more turns ...
compact = chat.compact()
print(f"Dropped {compact.dropped_message_count} messages, "
f"summary used {compact.usage.total_tokens} tokens")
# Option B: compact a standalone message list with client.chat.compact_context().
messages = [
system("You are a concise and knowledgeable science tutor."),
user("What is the Higgs boson and why is it important?"),
assistant("The Higgs boson is an elementary particle..."),
]
compact = client.chat.compact_context(model="grok-4.6", messages=messages)
# Hand the compaction to a fresh chat; appending replaces existing messages
# with the encrypted blob.
new_chat = client.chat.create(model="grok-4.6", use_encrypted_content=True)
new_chat.append(compact)
new_chat.append(user("What gives particles their mass?"))
print(new_chat.sample().content)
Last updated: September 2, 2026