# xAI with OpenAI Python SDK

> Use the OpenAI Python SDK to integrate with xAI via Helicone to log your xAI usage.

import { strings } from "/snippets/strings.mdx";
import LegacyWarning from "/snippets/legacy-provider-warning.mdx";

<LegacyWarning />

This integration is used to log usage with the [xAI](https://x.ai) API.

## {strings.howToIntegrate}

<Steps>
  <Step title={strings.generateKey}>
    <div dangerouslySetInnerHTML={{ __html: strings.generateKeyInstructions }} />
  </Step>

  <Step title={strings.setApiKey}>
    ```bash
    HELICONE_API_KEY=<your-helicone-api-key>
    XAI_API_KEY=<your-xai-api-key>
    ```
  </Step>

  <Step title={strings.modifyBasePath}>

    ```python OpenAI SDK
    from openai import OpenAI
    from dotenv import load_dotenv
    import os

    load_dotenv()

    helicone_api_key = os.getenv("HELICONE_API_KEY")
    XAI_API_KEY = os.getenv("XAI_API_KEY")

    client = OpenAI(
      api_key=XAI_API_KEY,
      base_url="https://x.helicone.ai/v1",
      default_headers={
        "Helicone-Auth": f"Bearer {helicone_api_key}"
      }
    )

    chat_completion = client.chat.completions.create(
      model="grok-4-latest",
      messages=[{"role": "user", "content": "Hello, how are you?"}],
      max_tokens=1024,
      temperature=0.7
    )

    print(chat_completion)
    ```

  </Step>

  <Step title={strings.verifyInHelicone}>
    <div dangerouslySetInnerHTML={{ __html: strings.verifyInHeliconeDesciption("xAI") }} />
  </Step>
</Steps>

## {strings.relatedGuides}

<CardGroup cols={2}>
  <Card
    title="Building a chatbot with structured outputs"
    icon="lightbulb"
    href="/guides/cookbooks/openai-structured-outputs"
    iconType="light"
    vertical
  >
    {strings.chatbotCookbookDescription}
  </Card>
  <Card
    title="How to Prompt Thinking Models Effectively"
    icon="arrows-rotate"
    href="/guides/cookbooks/prompt-thinking-models"
    iconType="light"
    vertical
  >
    {strings.howToPromptThinkingModelsCookbookDescription}
  </Card>
</CardGroup>
