Skip to content
xAI logo

Grok 4.5

Text Generation • xAI

View as MarkdownAgent setup
  • Third-party
  • Zero data retention

xAI's Grok 4.5, a frontier model built for coding, agentic tasks, and knowledge work. Accepts text and image inputs, and supports function calling, structured outputs, and configurable reasoning effort (low, medium, high).

Model Info
Context Window ↗500,000 tokens
Terms and Licenselink ↗
More informationlink ↗
Zero data retentionYes
Request formatsChat Completions
Pricing
  • Input <200k (per 1M)$2.00
  • Cached input <200k (per 1M)$0.30
  • Output <200k (per 1M)$6.00
  • Input >=200k (per 1M)$4.00
  • Cached input >=200k (per 1M)$0.60
  • Output >=200k (per 1M)$12.00

Usage

const response = await env.AI.run(
  'xai/grok-4.5',
  { messages: [{ content: 'What are the three laws of thermodynamics?', role: 'user' }] },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "messages": [
    {
      "content": "What are the three laws of thermodynamics?",
      "role": "user"
    }
  ]
}'
**First Law (Conservation of Energy):**  
The total energy of an isolated system is constant. Energy can be transformed from one form to another (e.g., heat into work) but cannot be created or destroyed. For a closed thermodynamic system this is commonly expressed as  
\[
\Delta U = Q - W
\]  
(where \(\Delta U\) is the change in internal energy, \(Q\) is heat added to the system, and \(W\) is work done by the system; sign conventions can vary).

**Second Law (Entropy / Direction of Processes):**  
The entropy of an isolated system never decreases; spontaneous processes increase the total entropy of the universe. Equivalent classical statements include:  
- Heat does not spontaneously flow from a colder body to a hotter body (Clausius).  
- It is impossible to convert heat completely into work in a cyclic process without other effects (Kelvin–Planck).  

This law introduces the thermodynamic arrow of time and the concept of irreversible processes.

**Third Law (Nernst Heat Theorem / Absolute Zero):**  
As the temperature of a system approaches absolute zero (0 K), the entropy approaches a minimum value (commonly taken as zero for a perfect crystalline substance). A practical consequence is that absolute zero is unattainable in a finite number of thermodynamic processes.

(Note: A “zeroth” law is also recognized—systems in thermal equilibrium with a third system are in equilibrium with each other, defining temperature—but it is not counted among the classical three laws.)

These laws form the foundation of classical thermodynamics and constrain all macroscopic energy-conversion processes.
{
  "id": "02cf6d3a-3354-9195-bdb5-199a2c5a88b2",
  "object": "chat.completion",
  "created": 1784118519,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "**First Law (Conservation of Energy):**  \nThe total energy of an isolated system is constant. Energy can be transformed from one form to another (e.g., heat into work) but cannot be created or destroyed. For a closed thermodynamic system this is commonly expressed as  \n\\[\n\\Delta U = Q - W\n\\]  \n(where \\(\\Delta U\\) is the change in internal energy, \\(Q\\) is heat added to the system, and \\(W\\) is work done by the system; sign conventions can vary).\n\n**Second Law (Entropy / Direction of Processes):**  \nThe entropy of an isolated system never decreases; spontaneous processes increase the total entropy of the universe. Equivalent classical statements include:  \n- Heat does not spontaneously flow from a colder body to a hotter body (Clausius).  \n- It is impossible to convert heat completely into work in a cyclic process without other effects (Kelvin–Planck).  \n\nThis law introduces the thermodynamic arrow of time and the concept of irreversible processes.\n\n**Third Law (Nernst Heat Theorem / Absolute Zero):**  \nAs the temperature of a system approaches absolute zero (0 K), the entropy approaches a minimum value (commonly taken as zero for a perfect crystalline substance). A practical consequence is that absolute zero is unattainable in a finite number of thermodynamic processes.\n\n(Note: A “zeroth” law is also recognized—systems in thermal equilibrium with a third system are in equilibrium with each other, defining temperature—but it is not counted among the classical three laws.)\n\nThese laws form the foundation of classical thermodynamics and constrain all macroscopic energy-conversion processes.",
        "reasoning_content": "The question is: \"What are the three laws of thermodynamics?\"\nThe three laws of thermodynamics are:\n\n1. **First Law**: Energy is conserved. In any process, the total energy of an isolated system remains constant; it can only change form (e.g., heat to work).",
        "refusal": null
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 214,
    "completion_tokens": 323,
    "total_tokens": 924,
    "prompt_tokens_details": {
      "text_tokens": 214,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 387,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 44960000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}

Examples

With System Message — Using a system message to set context
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    messages: [
      { content: 'You are a helpful coding assistant specializing in Python.', role: 'system' },
      { content: 'How do I read a JSON file in Python?', role: 'user' },
    ],
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "messages": [
    {
      "content": "You are a helpful coding assistant specializing in Python.",
      "role": "system"
    },
    {
      "content": "How do I read a JSON file in Python?",
      "role": "user"
    }
  ]
}'
To read a JSON file in Python, use the built-in `json` module:

```python
import json

with open("data.json", "r", encoding="utf-8") as f:
    data = json.load(f)

print(data)
```

### Notes
- `json.load(f)` reads from a file object and returns a Python object (`dict`, `list`, etc.).
- Prefer `with open(...)` so the file is closed automatically.
- Use `encoding="utf-8"` for reliable handling of non-ASCII text.

### If you already have a JSON string
```python
import json

json_string = '{"name": "Alice", "age": 30}'
data = json.loads(json_string)
```

### Basic error handling
```python
import json

try:
    with open("data.json", "r", encoding="utf-8") as f:
        data = json.load(f)
except FileNotFoundError:
    print("File not found")
except json.JSONDecodeError as e:
    print(f"Invalid JSON: {e}")
```
{
  "id": "e5c0b732-b377-9257-a707-558da370c135",
  "object": "chat.completion",
  "created": 1784118525,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "To read a JSON file in Python, use the built-in `json` module:\n\n```python\nimport json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as f:\n    data = json.load(f)\n\nprint(data)\n```\n\n### Notes\n- `json.load(f)` reads from a file object and returns a Python object (`dict`, `list`, etc.).\n- Prefer `with open(...)` so the file is closed automatically.\n- Use `encoding=\"utf-8\"` for reliable handling of non-ASCII text.\n\n### If you already have a JSON string\n```python\nimport json\n\njson_string = '{\"name\": \"Alice\", \"age\": 30}'\ndata = json.loads(json_string)\n```\n\n### Basic error handling\n```python\nimport json\n\ntry:\n    with open(\"data.json\", \"r\", encoding=\"utf-8\") as f:\n        data = json.load(f)\nexcept FileNotFoundError:\n    print(\"File not found\")\nexcept json.JSONDecodeError as e:\n    print(f\"Invalid JSON: {e}\")\n```",
        "reasoning_content": "The user is asking how to read a JSON file in Python. This is a straightforward coding question. I should provide a clear, helpful answer with code examples.\n",
        "refusal": null
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 229,
    "completion_tokens": 237,
    "total_tokens": 570,
    "prompt_tokens_details": {
      "text_tokens": 229,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 104,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 23120000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}
Multi-turn Conversation — Continuing a conversation with context
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    max_completion_tokens: 8192,
    messages: [
      {
        content: 'I need help planning a road trip from San Francisco to Los Angeles.',
        role: 'user',
      },
      {
        content:
          "I'd be happy to help! The drive is about 380 miles and takes roughly 5-6 hours. Would you like suggestions for scenic routes or interesting stops along the way?",
        role: 'assistant',
      },
      { content: 'Yes, what are some good places to stop?', role: 'user' },
    ],
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "max_completion_tokens": 8192,
  "messages": [
    {
      "content": "I need help planning a road trip from San Francisco to Los Angeles.",
      "role": "user"
    },
    {
      "content": "I'\''d be happy to help! The drive is about 380 miles and takes roughly 5-6 hours. Would you like suggestions for scenic routes or interesting stops along the way?",
      "role": "assistant"
    },
    {
      "content": "Yes, what are some good places to stop?",
      "role": "user"
    }
  ]
}'
**Great choice—there are tons of excellent stops depending on whether you take the faster inland route (I-5, ~5–6 hours) or the scenic coastal route (Highway 1 / Pacific Coast Highway, ~8–10+ hours with stops).**

Most people planning a fun road trip prefer the **coastal route** for the views and variety. Here are some of the best places to stop (roughly north to south):

### Top Recommended Stops (Coastal Route via Hwy 1)
1. **Monterey / Carmel-by-the-Sea** (about 2 hours from SF)  
   - Walk Cannery Row, visit the Monterey Bay Aquarium, or stroll the charming village of Carmel. Great for a coffee, lunch, or short hike.

2. **Big Sur** (highlight of the drive)  
   - Iconic stops: Bixby Creek Bridge viewpoint, McWay Falls (waterfall onto the beach), and pullouts for dramatic ocean cliffs. Allow extra time—roads can be winding and parking limited. Pfeiffer Beach or Julia Pfeiffer Burns State Park are worth it if you have time.

3. **Hearst Castle (San Simeon)**  
   - Tour the opulent hilltop estate of William Randolph Hearst. Book tickets ahead if possible. Nearby elephant seal viewing at Piedras Blancas is free and fascinating.

4. **San Luis Obispo / Pismo Beach / Avila Beach**  
   - SLO has a fun downtown, bubblegum alley, and good food. Pismo is classic California beach vibes (dunes, pier). Great mid-trip stretch and meal stop.

5. **Santa Barbara**  
   - Beautiful Spanish-style downtown, State Street, waterfront, and nearby wine tasting in the Santa Ynez Valley if you detour a bit. Perfect last major stop before LA (about 1.5–2 hours remaining).

### Other Solid Options
- **Santa Cruz** (early stop) – Boardwalk, surfing vibe, or nearby redwoods.
- **Paso Robles** (if you cut inland a bit via Hwy 46) – Excellent wine country for tastings.
- **Solvang** (near Santa Barbara) – Quaint Danish-themed town with bakeries and windmills.
- Quick nature hits: Point Lobos State Natural Reserve (near Carmel) or Elephant Seal Rookery.

### Practical Tips
- **Timing**: Start early from SF to enjoy daylight in Big Sur. The full coastal drive with a few stops easily fills a full day (or make it overnight in SLO/Pismo/Santa Barbara).
- **Food**: Fresh seafood in Monterey, burgers or farm-to-table in SLO, tacos or wine-country bites farther south.
- **Gas & rest**: Fill up before Big Sur (limited services). Traffic can build near LA, especially weekends.
- **Alternatives**: Pure I-5 is faster but boring (mostly Central Valley farmland). A hybrid (I-5 partway then cut over to the coast) works if you’re short on time.
- Check road conditions—Hwy 1 in Big Sur occasionally has closures due to slides.

**What kind of trip are you aiming for?** (e.g., scenic/nature-focused, food & wine, beaches, family-friendly, one long day vs. overnight, any must-sees or avoidances like crowds?) Also, roughly how many days/hours do you have, and any interests (hiking, photography, history, kids, etc.)? I can refine a sample itinerary or map out timing from there.
{
  "id": "84bb1a71-ed73-9245-9e02-987a9126e53b",
  "object": "chat.completion",
  "created": 1784118527,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "**Great choice—there are tons of excellent stops depending on whether you take the faster inland route (I-5, ~5–6 hours) or the scenic coastal route (Highway 1 / Pacific Coast Highway, ~8–10+ hours with stops).**\n\nMost people planning a fun road trip prefer the **coastal route** for the views and variety. Here are some of the best places to stop (roughly north to south):\n\n### Top Recommended Stops (Coastal Route via Hwy 1)\n1. **Monterey / Carmel-by-the-Sea** (about 2 hours from SF)  \n   - Walk Cannery Row, visit the Monterey Bay Aquarium, or stroll the charming village of Carmel. Great for a coffee, lunch, or short hike.\n\n2. **Big Sur** (highlight of the drive)  \n   - Iconic stops: Bixby Creek Bridge viewpoint, McWay Falls (waterfall onto the beach), and pullouts for dramatic ocean cliffs. Allow extra time—roads can be winding and parking limited. Pfeiffer Beach or Julia Pfeiffer Burns State Park are worth it if you have time.\n\n3. **Hearst Castle (San Simeon)**  \n   - Tour the opulent hilltop estate of William Randolph Hearst. Book tickets ahead if possible. Nearby elephant seal viewing at Piedras Blancas is free and fascinating.\n\n4. **San Luis Obispo / Pismo Beach / Avila Beach**  \n   - SLO has a fun downtown, bubblegum alley, and good food. Pismo is classic California beach vibes (dunes, pier). Great mid-trip stretch and meal stop.\n\n5. **Santa Barbara**  \n   - Beautiful Spanish-style downtown, State Street, waterfront, and nearby wine tasting in the Santa Ynez Valley if you detour a bit. Perfect last major stop before LA (about 1.5–2 hours remaining).\n\n### Other Solid Options\n- **Santa Cruz** (early stop) – Boardwalk, surfing vibe, or nearby redwoods.\n- **Paso Robles** (if you cut inland a bit via Hwy 46) – Excellent wine country for tastings.\n- **Solvang** (near Santa Barbara) – Quaint Danish-themed town with bakeries and windmills.\n- Quick nature hits: Point Lobos State Natural Reserve (near Carmel) or Elephant Seal Rookery.\n\n### Practical Tips\n- **Timing**: Start early from SF to enjoy daylight in Big Sur. The full coastal drive with a few stops easily fills a full day (or make it overnight in SLO/Pismo/Santa Barbara).\n- **Food**: Fresh seafood in Monterey, burgers or farm-to-table in SLO, tacos or wine-country bites farther south.\n- **Gas & rest**: Fill up before Big Sur (limited services). Traffic can build near LA, especially weekends.\n- **Alternatives**: Pure I-5 is faster but boring (mostly Central Valley farmland). A hybrid (I-5 partway then cut over to the coast) works if you’re short on time.\n- Check road conditions—Hwy 1 in Big Sur occasionally has closures due to slides.\n\n**What kind of trip are you aiming for?** (e.g., scenic/nature-focused, food & wine, beaches, family-friendly, one long day vs. overnight, any must-sees or avoidances like crowds?) Also, roughly how many days/hours do you have, and any interests (hiking, photography, history, kids, etc.)? I can refine a sample itinerary or map out timing from there.",
        "reasoning_content": "The user is asking for good places to stop on a road trip from San Francisco to Los Angeles. This is a continuation of planning a road trip.\n",
        "refusal": null
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 274,
    "completion_tokens": 742,
    "total_tokens": 1207,
    "prompt_tokens_details": {
      "text_tokens": 274,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 191,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 59540000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}
Coding with Low Reasoning Effort — Setting a lower reasoning effort for a fast, low-latency response
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    max_completion_tokens: 8192,
    messages: [
      {
        content:
          'Find and fix the bug, then explain it: function median(a){a.sort();return a[a.length/2]}',
        role: 'user',
      },
    ],
    reasoning_effort: 'low',
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "max_completion_tokens": 8192,
  "messages": [
    {
      "content": "Find and fix the bug, then explain it: function median(a){a.sort();return a[a.length/2]}",
      "role": "user"
    }
  ],
  "reasoning_effort": "low"
}'
```js
function median(a) {
  if (!Array.isArray(a) || a.length === 0) return undefined;
  const sorted = a.slice().sort((x, y) => x - y); // copy + numeric sort
  const mid = Math.floor(sorted.length / 2);
  return sorted.length % 2
    ? sorted[mid]
    : (sorted[mid - 1] + sorted[mid]) / 2;
}
```

**Bugs fixed**
1. **Default `sort()` is lexicographic** (e.g. `[10, 2, 3]` → `["10","2","3"]`). A numeric comparator is required.
2. **`a[a.length/2]` is wrong**:
   - For odd length (e.g. 5) you get a float index `2.5` → `undefined`.
   - For even length the conventional median is the average of the two central elements, not a single one.
3. **In-place mutation** – `sort()` changes the caller’s array; a shallow copy avoids that.
4. Empty / non-array input is now handled safely.
{
  "id": "bb92a233-a9ac-9a94-bdf1-6e5e6a14c060",
  "object": "chat.completion",
  "created": 1784118539,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "```js\nfunction median(a) {\n  if (!Array.isArray(a) || a.length === 0) return undefined;\n  const sorted = a.slice().sort((x, y) => x - y); // copy + numeric sort\n  const mid = Math.floor(sorted.length / 2);\n  return sorted.length % 2\n    ? sorted[mid]\n    : (sorted[mid - 1] + sorted[mid]) / 2;\n}\n```\n\n**Bugs fixed**\n1. **Default `sort()` is lexicographic** (e.g. `[10, 2, 3]` → `[\"10\",\"2\",\"3\"]`). A numeric comparator is required.\n2. **`a[a.length/2]` is wrong**:\n   - For odd length (e.g. 5) you get a float index `2.5` → `undefined`.\n   - For even length the conventional median is the average of the two central elements, not a single one.\n3. **In-place mutation** – `sort()` changes the caller’s array; a shallow copy avoids that.\n4. Empty / non-array input is now handled safely.",
        "reasoning_content": "The user wants me to find and fix the bug in this function: function median(a){a.sort();return a[a.length/2]}\nThe bug is that `a.length/2` coerces to an integer index (so for odd-length arrays it actually returns the element at `floor(length/2)`, not the true middle), and the default string sort makes numeric data come out wrong. It also mutates the caller’s array.",
        "refusal": null
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 233,
    "completion_tokens": 250,
    "total_tokens": 1115,
    "prompt_tokens_details": {
      "text_tokens": 233,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 632,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 55660000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}
Streaming Response — Enable streaming for real-time output
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    messages: [{ content: 'Explain the concept of recursion with a simple example.', role: 'user' }],
    stream: true,
    stream_options: { include_usage: true },
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "messages": [
    {
      "content": "Explain the concept of recursion with a simple example.",
      "role": "user"
    }
  ],
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}'
**Recursion** is a programming technique where a function calls itself to solve a smaller instance of the same problem, until it reaches a simple stopping condition.

### Core Idea
- Break a problem into smaller subproblems of the same type.
- Each recursive call works on a reduced version of the input.
- Eventually hit a **base case** that returns a value without further calls (this prevents infinite recursion).
- Combine the results as the calls unwind.

Without a proper base case, recursion continues forever (or until the call stack overflows).

### Simple Example: Factorial
The factorial of a non-negative integer \( n \) (written \( n! \)) is:

- \( 0! = 1 \) and \( 1! = 1 \)
- \( n! = n \times (n-1)! \) for \( n > 1 \)

**Python implementation:**

```python
def factorial(n):
    # Base case: stops the recursion
    if n == 0 or n == 1:
        return 1
    # Recursive case: calls itself with a smaller argument
    else:
        return n * factorial(n - 1)
```

**How it works for `factorial(5)`:**

1. `factorial(5)` → \( 5 \times \) `factorial(4)`
2. `factorial(4)` → \( 4 \times \) `factorial(3)`
3. `factorial(3)` → \( 3 \times \) `factorial(2)`
4. `factorial(2)` → \( 2 \times \) `factorial(1)`
5. `factorial(1)` → returns `1` (base case)

Now the calls return and multiply:

- \( 2 \times 1 = 2 \)
- \( 3 \times 2 = 6 \)
- \( 4 \times 6 = 24 \)
- \( 5 \times 24 = 120 \)

Result: `120`

### Why This Works
Each call reduces \( n \) by 1, guaranteed to eventually hit the base case. The multiplications happen on the way back up the call stack.

### Quick Tips
- Always define a clear base case first.
- Ensure every recursive path moves toward that base case.
- Recursion is elegant for problems with natural recursive structure (trees, divide-and-conquer, factorial, Fibonacci, etc.), but deep recursion can cause stack overflows—iteration or tail-call optimization can be alternatives when needed.

This pattern (base case + recursive case that shrinks the problem) is the essence of recursion.
[
  {
    "id": "b2ed7fb2-3e57-96e3-9a6c-464dd11745eb",
    "object": "chat.completion.chunk",
    "created": 1784118545,
    "model": "grok-4.5",
    "choices": [
      {
        "index": 0,
        "delta": {
          "reasoning_content": "The",
          "role": "assistant"
        }
      }
    ],
    "system_fingerprint": "fp_a39489019fa99b6e",
    "service_tier": "default"
  },
  {
    "id": "b2ed7fb2-3e57-96e3-9a6c-464dd11745eb",
    "object": "chat.completion.chunk",
    "created": 1784118545,
    "model": "grok-4.5",
    "choices": [
      {
        "index": 0,
        "delta": {
          "reasoning_content": " user"
        }
      }
    ],
    "system_fingerprint": "fp_a39489019fa99b6e",
    "service_tier": "default"
  },
  "... 559 more chunks omitted ...",
  {
    "id": "b2ed7fb2-3e57-96e3-9a6c-464dd11745eb",
    "object": "chat.completion.chunk",
    "created": 1784118551,
    "model": "grok-4.5",
    "choices": [],
    "usage": {
      "prompt_tokens": 216,
      "completion_tokens": 544,
      "total_tokens": 1040,
      "prompt_tokens_details": {
        "text_tokens": 216,
        "audio_tokens": 0,
        "image_tokens": 0,
        "cached_tokens": 128
      },
      "completion_tokens_details": {
        "reasoning_tokens": 280,
        "audio_tokens": 0,
        "accepted_prediction_tokens": 0,
        "rejected_prediction_tokens": 0
      },
      "num_sources_used": 0,
      "cost_in_usd_ticks": 51840000
    },
    "system_fingerprint": "fp_a39489019fa99b6e",
    "service_tier": "default"
  }
]
Image Understanding — Analyze an image supplied alongside a text prompt
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    messages: [
      {
        content: [
          {
            image_url: { url: 'https://v3.fal.media/files/koala/NLVPfOI4XL1cWT2PmmqT3_Hope.png' },
            type: 'image_url',
          },
          {
            text: 'Describe the person in this image and their surroundings in one sentence.',
            type: 'text',
          },
        ],
        role: 'user',
      },
    ],
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "messages": [
    {
      "content": [
        {
          "image_url": {
            "url": "https://v3.fal.media/files/koala/NLVPfOI4XL1cWT2PmmqT3_Hope.png"
          },
          "type": "image_url"
        },
        {
          "text": "Describe the person in this image and their surroundings in one sentence.",
          "type": "text"
        }
      ],
      "role": "user"
    }
  ]
}'
A smiling young woman with dark hair pulled back, wearing a blue knit sweater and multiple rings, holds a small fuzzy microphone while posing in a cozy indoor room with warm lighting, a framed photo collage on the wall, and decorative elements like a plant and artwork.
{
  "id": "f864bc43-a81c-995b-bb6c-253294c05423",
  "object": "chat.completion",
  "created": 1784491003,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "A smiling young woman with dark hair pulled back, wearing a blue knit sweater and multiple rings, holds a small fuzzy microphone while posing in a cozy indoor room with warm lighting, a framed photo collage on the wall, and decorative elements like a plant and artwork.",
        "reasoning_content": "The user wants me to describe the person in the image and their surroundings in one sentence.\n",
        "refusal": null
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 2628,
    "completion_tokens": 52,
    "total_tokens": 2827,
    "prompt_tokens_details": {
      "text_tokens": 221,
      "audio_tokens": 0,
      "image_tokens": 2407,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 147,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 62324000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}
Function Calling — Force the model to return a typed function call
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    messages: [{ content: 'What is the current temperature in San Francisco?', role: 'user' }],
    tool_choice: 'required',
    tools: [
      {
        function: {
          description: 'Get the current temperature for a city',
          name: 'get_temperature',
          parameters: {
            additionalProperties: false,
            properties: {
              city: { type: 'string' },
              unit: { enum: ['celsius', 'fahrenheit'], type: 'string' },
            },
            required: ['city', 'unit'],
            type: 'object',
          },
          strict: true,
        },
        type: 'function',
      },
    ],
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "messages": [
    {
      "content": "What is the current temperature in San Francisco?",
      "role": "user"
    }
  ],
  "tool_choice": "required",
  "tools": [
    {
      "function": {
        "description": "Get the current temperature for a city",
        "name": "get_temperature",
        "parameters": {
          "additionalProperties": false,
          "properties": {
            "city": {
              "type": "string"
            },
            "unit": {
              "enum": [
                "celsius",
                "fahrenheit"
              ],
              "type": "string"
            }
          },
          "required": [
            "city",
            "unit"
          ],
          "type": "object"
        },
        "strict": true
      },
      "type": "function"
    }
  ]
}'
I'll check the current temperature in San Francisco for you.
{
  "id": "5e8b5367-6685-95f6-8a15-fd3d5e785de5",
  "object": "chat.completion",
  "created": 1784491007,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "tool_calls",
      "message": {
        "role": "assistant",
        "content": "I'll check the current temperature in San Francisco for you.",
        "reasoning_content": "The user wants the current temperature in San Francisco. I have a tool for that: get_temperature. I need to specify the city and the unit. The city is San Francisco, but what unit? It doesn't specify,...",
        "refusal": null,
        "tool_calls": [
          {
            "id": "call-206bc4d0-666a-48ea-8b03-28467f98b9df-0",
            "type": "function",
            "function": {
              "name": "get_temperature",
              "arguments": "{\"city\":\"San Francisco\",\"unit\":\"fahrenheit\"}"
            }
          }
        ]
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 333,
    "completion_tokens": 31,
    "total_tokens": 445,
    "prompt_tokens_details": {
      "text_tokens": 333,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 81,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 11204000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}
Structured Output — Constrain the response to a JSON schema
const response = await env.AI.run(
  'xai/grok-4.5',
  {
    messages: [
      {
        content: 'Classify the sentiment of: The launch was smooth and customers loved it.',
        role: 'user',
      },
    ],
    response_format: {
      json_schema: {
        name: 'sentiment_result',
        schema: {
          additionalProperties: false,
          properties: {
            confidence: { maximum: 1, minimum: 0, type: 'number' },
            sentiment: { enum: ['positive', 'neutral', 'negative'], type: 'string' },
          },
          required: ['sentiment', 'confidence'],
          type: 'object',
        },
        strict: true,
      },
      type: 'json_schema',
    },
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "xai/grok-4.5",
  "messages": [
    {
      "content": "Classify the sentiment of: The launch was smooth and customers loved it.",
      "role": "user"
    }
  ],
  "response_format": {
    "json_schema": {
      "name": "sentiment_result",
      "schema": {
        "additionalProperties": false,
        "properties": {
          "confidence": {
            "maximum": 1,
            "minimum": 0,
            "type": "number"
          },
          "sentiment": {
            "enum": [
              "positive",
              "neutral",
              "negative"
            ],
            "type": "string"
          }
        },
        "required": [
          "sentiment",
          "confidence"
        ],
        "type": "object"
      },
      "strict": true
    },
    "type": "json_schema"
  }
}'
{"confidence":0.95,"sentiment":"positive"}
{
  "id": "da248276-7d35-9291-9645-7f495af55158",
  "object": "chat.completion",
  "created": 1784491010,
  "model": "grok-4.5",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "{\"confidence\":0.95,\"sentiment\":\"positive\"}",
        "reasoning_content": "The user wants me to classify the sentiment of: \"The launch was smooth and customers loved it.\"\n",
        "refusal": null
      }
    }
  ],
  "service_tier": "default",
  "system_fingerprint": "fp_a39489019fa99b6e",
  "usage": {
    "prompt_tokens": 300,
    "completion_tokens": 12,
    "total_tokens": 472,
    "prompt_tokens_details": {
      "text_tokens": 300,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 128
    },
    "completion_tokens_details": {
      "reasoning_tokens": 160,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "cost_in_usd_ticks": 14144000,
    "num_sources_used": 0
  },
  "gatewayMetadata": {
    "keySource": "Unified"
  }
}

Parameters

max_completion_tokens
integer | null
max_tokens
integer | null
n
integer | null
parallel_tool_calls
boolean
prompt_cache_key
string
reasoning_effort
string | nullenum: low, medium, high
seed
integer | null
service_tier
stringenum: default, priority
stream
boolean
temperature
number | null
tools
array | null
top_p
number | null
user
string
id
string
object
stringconst: chat.completion
created
number
model
string
citations
array | null
output_files
array | null
service_tier
stringenum: default, priority
system_fingerprint
string

API Schemas (Raw)

Input
Output

Was this helpful?