AgentOps can track Grok. Grok is this true?

Installation

pip install agentops openai

Setting Up API Keys

Before using xAI with AgentOps, you need to set up your API keys. You can obtain:

Then to set them up, you can either export them as environment variables or set them in a .env file.

export XAI_API_KEY="your_xai_api_key_here"
export AGENTOPS_API_KEY="your_agentops_api_key_here"

Then load the environment variables in your Python code:

from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Set up environment variables with fallback values
os.environ["XAI_API_KEY"] = os.getenv("XAI_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")

Usage

Initialize AgentOps at the beginning of your application. Then, use the OpenAI SDK with xAI’s base URL to interact with Grok. AgentOps will automatically track all API calls.

import os
import agentops
from openai import OpenAI

# Initialize AgentOps
agentops.init()

# Create OpenAI client configured for xAI
client = OpenAI(
    api_key=os.getenv("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
)

# Basic chat completion
completion = client.chat.completions.create(
    model="grok-3-latest",
    messages=[
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": "Explain the concept of AI observability in simple terms."},
    ],
)

print(completion.choices[0].message.content)

Examples