Skip to main content

Documentation Index

Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-naomid-1779801766-572e290.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

This page documents Grok models from xAI. Do not confuse xAI with Groq, a separate inference provider. See the Groq integration.
xAI develops Grok chat models. See the xAI model documentation for available model IDs. This guide will help you getting started with ChatXAI chat models. For detailed documentation of all ChatXAI features and configurations head to the API reference.

Overview

Integration details

ClassPackageSerializablePY supportDownloadsVersion
ChatXAI@langchain/xaiNPM - DownloadsNPM - Version

Model features

See the links in the table headers below for guides on how to use specific features.

Setup

To access ChatXAI models, create an xAI account, get an API key, and install the @langchain/xai integration package.

Credentials

Head to the xAI website to sign up and generate an API key. Set the XAI_API_KEY environment variable:
export XAI_API_KEY="your-api-key"
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

Installation

The LangChain ChatXAI integration lives in the @langchain/xai package:
npm install @langchain/xai @langchain/core

Instantiation

Now we can instantiate our model object and generate chat completions:
import { ChatXAI } from "@langchain/xai";

const llm = new ChatXAI({
    model: "grok-3-fast",
    temperature: 0,
    maxTokens: undefined,
    maxRetries: 2,
    // other params...
})

Invocation

const aiMsg = await llm.invoke([
    [
      "system",
      "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ],
    ["human", "I love programming."],
])
console.log(aiMsg)
AIMessage {
  "id": "71d7e3d8-30dd-472c-8038-b6b283dcee63",
  "content": "J'adore programmer.",
  "additional_kwargs": {},
  "response_metadata": {
    "tokenUsage": {
      "promptTokens": 30,
      "completionTokens": 6,
      "totalTokens": 36
    },
    "finish_reason": "stop",
    "usage": {
      "prompt_tokens": 30,
      "completion_tokens": 6,
      "total_tokens": 36
    },
    "system_fingerprint": "fp_3e3898d4ce"
  },
  "tool_calls": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "output_tokens": 6,
    "input_tokens": 30,
    "total_tokens": 36,
    "input_token_details": {},
    "output_token_details": {}
  }
}
console.log(aiMsg.content)
J'adore programmer.

API reference

For detailed documentation of all ChatXAI features and configurations head to the API reference.