grok-4

This documentation is valid for the following list of our models:

  • x-ai/grok-4-07-09

Model Overview

Grok 4 is boldly described by its developers as the most intelligent model in the world (as of July 2025).

How to Make a Call

Step-by-Step Instructions

1️ Setup You Can’t Skip

▪️ Create an Account: Visit the AI/ML API website and create an account (if you don’t have one yet). ▪️ Generate an API Key: After logging in, navigate to your account dashboard and generate your API key. Ensure that key is enabled on UI.

2️ Copy the code example

At the bottom of this page, you'll find a code example that shows how to structure the request. Choose the code snippet in your preferred programming language and copy it into your development environment.

3️ Modify the code example

▪️ Replace <YOUR_AIMLAPI_KEY> with your actual AI/ML API key from your account. ▪️ Insert your question or request into the content field—this is what the model will respond to.

4️ (Optional) Adjust other optional parameters if needed

Only model and messages are required parameters for this model (and we’ve already filled them in for you in the example), but you can include optional parameters if needed to adjust the model’s behavior. Below, you can find the corresponding API schema, which lists all available parameters along with notes on how to use them.

5️ Run your modified code

Run your modified code in your development environment. Response time depends on various factors, but for simple prompts it rarely exceeds a few seconds.

API Schema

Coming soon

Code Example

import requests
import json   # for getting a structured output with indentation

response = requests.post(
    "https://api.aimlapi.com/v1/chat/completions",
    headers={
        "Content-Type":"application/json", 

        # Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>:
        "Authorization":"Bearer <YOUR_AIMLAPI_KEY>",
        "Content-Type":"application/json"
    },
    json={
        "model":"x-ai/grok-4-07-09",
        "messages":[
            {
                "role":"user",

                # Insert your question for the model here, instead of Hello:
                "content":"Hello"
            }
        ]
        
    }
)

data = response.json()
print(json.dumps(data, indent=2, ensure_ascii=False))
Response
{
  "id": "gen-1752837143-rG1L7RPFpBi9pJdCHTzm",
  "system_fingerprint": "fp_ff08cddfd3",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "logprobs": null,
      "message": {
        "role": "assistant",
        "content": "Hello! I'm Grok, built by xAI to help with answers, ideas, and a bit of cosmic wit. What can I do for you today? 🚀",
        "reasoning_content": "Thinking... Thinking... ",
        "refusal": null
      }
    }
  ],
  "created": 1752837143,
  "model": "x-ai/grok-4",
  "usage": {
    "prompt_tokens": 53,
    "completion_tokens": 5689,
    "total_tokens": 5742,
    "prompt_tokens_details": {
      "cached_tokens": 2
    },
    "completion_tokens_details": {
      "reasoning_tokens": 138
    }
  }
}

Last updated

Was this helpful?