Grok 4: Testing Grok 4 via xAI API: Code Interpreter, Tools, and Python Examples
Large Language Models

Grok 4: Testing Grok 4 via xAI API: Code Interpreter, Tools, and Python Examples

Learn to test Grok 4 using the xAI API with Python. Follow this detailed tutorial for setup, API calls, DeepSearch, code execution, and troubleshooting best practices.

Grok 4 Code Interpreter and xAI API: A Python Developer’s Guide – Grok 4 is xAI’s current flagship model, trained on the Colossus supercomputer with 200,000 NVIDIA H100 GPUs, sporting a 256,000-token context window, and built with reinforcement learning baked in at pretraining scale.

All of which sounds impressive until you actually try to use it and realize you spent twenty minutes hunting for the code interpreter documentation.

This guide covers how to use Grok 4 via the xAI API in Python, including the code execution tool, built-in web search, function calling, and the bits the official docs make you dig for.


Understanding Grok 4’s Core Architecture

Grok xAI API

The architecture story behind Grok 4 is worth knowing, not because you’ll need it in your code, but because it explains why the model behaves the way it does.

Grok 4 builds on Grok 3’s foundation with one key addition: reinforcement learning applied at pretraining scale, not just fine-tuning.

The xAI team found promising scaling trends during their Grok 3 Reasoning work and went further, running RL training on the full Colossus cluster with over an order of magnitude more compute than previous runs.

The result is a model that doesn’t just predict the next token.

It reasons, backtracks, and uses tools as part of its thinking process rather than as bolted-on afterthoughts.

The 6x compute efficiency improvement over previous implementations is also what makes Grok 4 Heavy feasible.

That variant runs multiple reasoning agents in parallel, typically requiring around 10 minutes of computation, which lets it approach problems from different angles simultaneously before settling on an answer.

Reinforcement Learning and Why It Changes Tool Use

Traditional LLMs call tools because they’re prompted to. Grok 4 was trained with RL to integrate tools into its reasoning loop at a fundamental level.

That means when you give it access to a code interpreter or web search, it doesn’t just fetch a result and paste it into the response.

It can use the tool output to inform further reasoning, call tools iteratively, and return citations for everything it looked up.

This is relevant to understand before you start writing API calls, because tool integration in the xAI API is more capable than the old tools=["code_interpreter"] string syntax the original post documented.

The API has moved to a proper Responses API with typed tool objects, and the difference matters.

Python Exercises for Beginners With 20+ Real Projects

Summarize with: ChatGPT Grok Perplexity Claude Python Exercises for Beginners With Real Projects ... Read More


Does Grok 4 Have a Code Interpreter?

Yes, and it’s more capable than the name implies. The code execution tool runs actual Python in a sandboxed environment on xAI’s servers.

You don’t ship Python files anywhere. You send a prompt, tell Grok to use the code execution tool, and it writes and runs the code itself, returning both the result and, optionally, citations if it pulled in any external context.

The sandboxed environment comes pre-loaded with the packages you’d expect for data work: NumPy, Pandas, Matplotlib, SciPy, and other standard scientific computing libraries.

It does not have access to external networks or persistent file storage, so don’t expect it to fetch URLs mid-execution or remember state between separate requests.

Also worth noting: the tool naming differs depending on which SDK you use.

SDK / API Tool Name
xAI SDK (xai_sdk) code_execution
OpenAI-compatible Responses API code_interpreter
Vercel AI SDK xai.tools.codeExecution()

What Python Libraries Are Available in Grok’s Code Interpreter?

The xAI sandbox includes the most common Python packages for numerical and scientific computing. Confirmed available packages include NumPy, Pandas, Matplotlib, and SciPy.

Most popular data science packages are accessible, but the environment has no file system write access and no outbound network calls from within executed code.

The model itself determines what code to write and run. You describe what you want computed, Grok writes the Python, executes it server-side, and returns the answer.

Basic Code Execution via the xAI SDK

Here’s how to use the code execution tool with the native xAI SDK:

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import code_execution

client = Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(
    model="grok-4.20-reasoning",
    tools=[code_execution()],
    include=["verbose_streaming"],
)

chat.append(user("Calculate the compound interest for $10,000 at 5% annually for 10 years"))

is_thinking = True
for response, chunk in chat.stream():
    for tool_call in chunk.tool_calls:
        print(f"\nCalling tool: {tool_call.function.name} with args: {tool_call.function.arguments}")
    if response.usage.reasoning_tokens and is_thinking:
        print(f"\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)
    if chunk.content and is_thinking:
        print("\n\nResult:")
        is_thinking = False
    if chunk.content and not is_thinking:
        print(chunk.content, end="", flush=True)

print("\n\nUsage:", response.usage)
print("Server tool calls:", response.server_side_tool_usage)

The verbose_streaming flag in include is optional but useful during development. It lets you watch the tool calls happen in real time as Grok reasons through the problem.

Code Execution with the OpenAI-Compatible SDK

If you’re already using the OpenAI Python client elsewhere, you can point it at the xAI Responses API with minimal changes. Note the tool name here is code_interpreter, not code_execution:

import os
from openai import OpenAI

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

response = client.responses.create(
    model="grok-4.20-reasoning",
    input=[
        {
            "role": "user",
            "content": "Calculate the compound interest for $10,000 at 5% annually for 10 years",
        },
    ],
    tools=[
        {"type": "code_interpreter"},
    ],
)

print(response)

Multi-Turn Data Analysis with Code Execution

The real power shows up in multi-turn conversations where each step builds on the previous result. Here’s a pattern for iterative data analysis:

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import code_execution

client = Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(
    model="grok-4.20-reasoning",
    tools=[code_execution()],
    include=["verbose_streaming"],
)

# Step 1: Analyze the data
chat.append(user("""
I have sales data for Q1-Q4: [120000, 135000, 98000, 156000].
Analyze this and show: quarterly trends, growth rates, and a statistical summary.
"""))

print("=== Step 1: Analysis ===\n")
is_thinking = True
for response, chunk in chat.stream():
    for tool_call in chunk.tool_calls:
        print(f"\nTool called: {tool_call.function.name}")
    if chunk.content and is_thinking:
        print("\nResults:")
        is_thinking = False
    if chunk.content:
        print(chunk.content, end="", flush=True)

chat.append(response)

# Step 2: Follow-up prediction
chat.append(user("Now predict Q1 next year using linear regression on this data."))

print("\n\n=== Step 2: Prediction ===\n")
is_thinking = True
for response, chunk in chat.stream():
    if chunk.content and is_thinking:
        print("\nPrediction:")
        is_thinking = False
    if chunk.content:
        print(chunk.content, end="", flush=True)

Getting Started with the xAI API in Python

Grok 4 via xai api

If you’re setting up from scratch, the steps are quick. If you already have the SDK installed from a previous Grok project, skip ahead to the API calls section.

You’ll need Python 3.8+, an API key from console.x.ai, and the xAI SDK:

pip install xai-sdk

Creating a Python virtual environment first is recommended, especially if you’re testing multiple AI SDKs in the same environment. They have a tendency to argue over dependency versions.

Configuring the API Key

Store the key in an environment variable. Hard-coding credentials in scripts is a classic move that never ends well.

On Unix/Linux/macOS:

export XAI_API_KEY=xai_123_your_api_key_here

On Windows:

set XAI_API_KEY=xai_123_your_api_key_here

Or use a .env file with python-dotenv for larger projects:

import os
from dotenv import load_dotenv
load_dotenv()
print(os.getenv("XAI_API_KEY"))  # Verify it loaded

Making Basic API Calls to Grok 4

The simplest call uses the xAI SDK’s Client class. This is just text generation with no tools attached:

import os
from xai_sdk import Client

client = Client(api_key=os.getenv("XAI_API_KEY"))

response = client.sampler.sample(
    model="grok-4-0709",
    prompt="Explain the Pythagorean theorem in simple terms.",
    temperature=0.4,
    max_tokens=200
)
print(response.content)

Use grok-4-0709 for the specific stable release or grok-4 for the latest version. For anything involving tools or reasoning, use grok-4.20-reasoning which is the model that actually supports the full tool-calling API.


Built-in Tools in Grok 4: Web Search, X Search, and Function Calling

The xAI API supports two categories of tools. Built-in tools (web search, X search, code execution) run server-side on xAI’s infrastructure.

Function calling lets you define custom tools that the model can invoke, with your code handling execution locally.

Both categories can be mixed in a single request.

Built-in Tool: Web Search

Web search gives Grok real-time access to the internet. The Responses API returns citations automatically, so you can trace exactly where the information came from:

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search

client = Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(
    model="grok-4.20-reasoning",
    tools=[web_search()],
)

chat.append(user("What are the latest updates from xAI?"))

for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)

print("\nCitations:", response.citations)

You can restrict or exclude specific domains:

# Only search within specific domains
tools=[web_search(allowed_domains=["techcrunch.com", "arxiv.org"])]

# Exclude specific domains
tools=[web_search(excluded_domains=["reddit.com"])]

Note: allowed_domains and excluded_domains cannot be used together in the same request.

Combining Multiple Built-in Tools

Here’s the quick-start pattern for using web search, X search, and code execution together. This is the same pattern from the official xAI docs, which is worth knowing verbatim:

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search, x_search, code_execution

client = Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(
    model="grok-4.20-reasoning",
    tools=[
        web_search(),
        x_search(),
        code_execution(),
    ],
)

chat.append(user("What are the latest updates from xAI?"))

for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)

print("\nCitations:", response.citations)

Grok decides which tools to use and in what order. You don’t need to specify.

Function Calling: Defining Custom Tools

Function calling is the other half of the tools story. You define a tool with a name, description, and JSON schema for parameters.

Grok calls it when needed, you execute your function locally and return the result, then Grok continues.

import os
import json
from xai_sdk import Client
from xai_sdk.chat import user, tool, tool_result

client = Client(api_key=os.getenv("XAI_API_KEY"))

# Define the tool schema
tools = [
    tool(
        name="get_temperature",
        description="Get current temperature for a location",
        parameters={
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "City name"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "default": "fahrenheit"}
            },
            "required": ["location"]
        },
    ),
]

chat = client.chat.create(
    model="grok-4.20-reasoning",
    tools=tools,
)
chat.append(user("What is the temperature in San Francisco?"))
response = chat.sample()

# Handle the tool call
if response.tool_calls:
    chat.append(response)
    for tc in response.tool_calls:
        args = json.loads(tc.function.arguments)
        # Your actual function logic goes here
        result = {"location": args["location"], "temperature": 59, "unit": args.get("unit", "fahrenheit")}
        chat.append(tool_result(json.dumps(result)))

    response = chat.sample()

print(response.content)

The model may request multiple tool calls in a single response (parallel function calling is on by default). Process all of them before continuing the conversation.

Using Pydantic for Type-Safe Tool Schemas

For cleaner code with type checking, define tool parameters using Pydantic models:

from typing import Literal
from pydantic import BaseModel, Field
from xai_sdk.chat import tool

class TemperatureRequest(BaseModel):
    location: str = Field(description="City and state, e.g. San Francisco, CA")
    unit: Literal["celsius", "fahrenheit"] = Field("fahrenheit", description="Temperature unit")

tools = [
    tool(
        name="get_temperature",
        description="Get current temperature for a location",
        parameters=TemperatureRequest.model_json_schema(),
    ),
]

This is considerably cleaner than writing raw JSON schema dicts by hand, especially when your tools have more than three parameters.


Grok 4: xAI API’s Tool Pricing and Token Usage

Tool requests are priced on two components: token usage and tool invocations. Each tool call adds cost on top of the tokens consumed, and because Grok may call multiple tools to answer a single query, costs scale with the complexity of the request.

Check the xAI pricing page for current rates. The usage data comes back with every response:

print(response.usage)
print(response.server_side_tool_usage)

For monitoring token consumption programmatically:

import os
import time
from xai_sdk import Client

client = Client(api_key=os.getenv("XAI_API_KEY"))

start = time.time()
response = client.sampler.sample(
    model="grok-4-0709",
    prompt="Describe machine learning in 100 words.",
    temperature=0.4,
    max_tokens=150
)
elapsed = time.time() - start

print(f"Response: {response.content}")
print(f"Response time: {elapsed:.2f}s")
print(f"Prompt tokens: {response.usage.prompt_tokens}")
print(f"Completion tokens: {response.usage.completion_tokens}")
print(f"Total tokens: {response.usage.total_tokens}")

Troubleshooting Common API Issues in xAI Grok 4+

Here’s some of the common API issues and how to troubleshoot them:

Authentication Errors (401)

The API key isn’t valid or the environment variable isn’t set. Check the value:

import os
print(os.getenv("XAI_API_KEY"))  # Should print your key, not None

With error handling wrapped around the API call:

import os
from xai_sdk import Client

try:
    client = Client(api_key=os.getenv("XAI_API_KEY"))
    response = client.sampler.sample(
        model="grok-4-0709",
        prompt="Test.",
        max_tokens=10
    )
    print(response.content)
except Exception as e:
    if "401" in str(e):
        print("Auth error: check your XAI_API_KEY in the xAI Console.")
    else:
        print(f"Error: {e}")

Rate Limit Errors (429)

Exponential backoff handles this gracefully:

import os
import time
from xai_sdk import Client

client = Client(api_key=os.getenv("XAI_API_KEY"))
retries = 3

for attempt in range(retries):
    try:
        response = client.sampler.sample(
            model="grok-4-0709",
            prompt="Summarize AI trends.",
            max_tokens=100
        )
        print(response.content)
        break
    except Exception as e:
        if "429" in str(e) and attempt < retries - 1:
            time.sleep(2 ** attempt)
        else:
            print(f"Error: {e}")

Benchmark Performance at a Glance

If you’re evaluating whether Grok 4 is worth using for your specific use case, the benchmark numbers are worth knowing.

On ARC-AGI V2, Grok 4 hit 15.9% accuracy, roughly double Claude Opus at around 8.6%. On Humanity’s Last Exam (text-only subset), it reached 50.7%. On USAMO’25, it scored 61.9%.

Grok 4 Heavy, the parallel compute variant, hit saturation on most academic benchmarks and was the first model to score 50% on Humanity’s Last Exam overall.

Heavy requires around 10 minutes per query given the multi-agent parallel approach.

For practical agentic tasks, the Vending-Bench results show Grok 4 reaching $4,694 net worth across five runs versus Claude Opus 4 at $2,077.

These benchmarks aren’t everything, but they do give a reasonable sense of reasoning capability at the frontier level.

If you’re comparing AI providers more broadly, the ChatGPT alternatives guide covers how Grok 4 stacks up against other frontier models from different providers.


Quick Reference: Grok 4 API Essentials

Item Value
Base model (stable) grok-4-0709
Reasoning model grok-4.20-reasoning
Context window 256,000 tokens
Code execution tool (xAI SDK) code_execution
Code execution tool (OpenAI SDK) code_interpreter
Responses API endpoint https://api.x.ai/v1/responses
Console console.x.ai

Wrapping Up

Throughout this tutorial, I have guided you through testing Grok 4 via the xAI API, demonstrating its capabilities in text generation, real-time data retrieval, and code execution, which showcase its technical versatility.

By following the structured steps, from setting up your environment to optimizing prompts and handling errors, you can effectively explore Grok 4’s potential, ensuring reliable and efficient interactions.

This process, which I find rewarding for its precision, equips you with the tools to integrate advanced AI into your projects.

For further exploration, consult the xAI documentation at https://x.ai/api to stay updated on API enhancements.


Frequently Asked Questions (FAQs)

Does Grok 4's code interpreter support Python packages like NumPy and Pandas?

Yes. The sandboxed execution environment includes NumPy, Pandas, Matplotlib, SciPy, and most popular scientific computing packages.

It doesn’t support external network calls from within executed code or persistent file storage between requests.

What's the difference between grok-4-0709 and grok-4.20-reasoning?

grok-4-0709 is the stable base model suitable for text generation and standard API calls using the sampler interface. grok-4.20-reasoning is the reasoning variant required for the Responses API and full tool-calling support, including code execution, web search, and function calling. Use the reasoning model any time you’re working with tools.

Passionate about SEO, WordPress, Python, and AI, I love blending creativity and code to craft innovative digital solutions and share insights with fellow enthusiasts.