Installation
Environment
To use xAI, you’ll need to create an API key. The API key can be passed in as an init paramxai_api_key or set as environment variable XAI_API_KEY.
We've raised a $125M Series B to build the platform for agent engineering. Read more.
pip install -U langchain-xai
xai_api_key or set as environment variable XAI_API_KEY.
# Querying chat models with xAI
from langchain_xai import ChatXAI
chat = ChatXAI(
# xai_api_key="YOUR_API_KEY",
model="grok-4",
)
# stream the response back from the model
for m in chat.stream("Tell me fun things to do in NYC"):
print(m.content, end="", flush=True)
# if you don't want to do streaming, you can use the invoke method
# chat.invoke("Tell me fun things to do in NYC")
Was this page helpful?