Model ID
Use with the Workers AI binding, the AI Gateway REST API, or the Unified API.
Calling the provider directly? Use the provider's model name instead, including with provider-native endpoints or the provider's own SDK. For example, Anthropic's name for anthropic/claude-opus-4.8 is claude-opus-4-8.
- Third-party
- Zero data retention
Google's most capable Gemini 4 reasoning model, built for complex software engineering, math and science problems, autonomous multi-step agents, and long outputs up to 1M tokens.
| Model Info | |
|---|---|
| Context Window ↗ | 1,048,576 tokens |
| Terms and License | link ↗ |
| More information | link ↗ |
| Zero data retention | Yes |
| Request formats | Chat Completions |
| Pricing |
|
const response = await env.AI.run(
'google/gemini-4-carbon',
{
contents: [
{
parts: [
{
text: 'What are the key differences between optimistic and pessimistic concurrency control in distributed systems?',
},
],
role: 'user',
},
],
},
)
console.log(response)curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "google/gemini-4-carbon",
"input": {
"contents": [
{
"parts": [
{
"text": "What are the key differences between optimistic and pessimistic concurrency control in distributed systems?"
}
],
"role": "user"
}
]
}
}'In distributed systems, concurrency control ensures that multiple nodes or clients can access and modify shared data simultaneously without corrupting it or violating consistency guarantees.
The fundamental difference between **Pessimistic Concurrency Control (PCC)** and **Optimistic Concurrency Control (OCC)** lies in how they anticipate and handle data conflicts—a challenge that is heavily amplified in distributed environments due to network latency, partial failures, and lack of a global clock.
Here are the key differences between the two approaches in distributed systems:
---
### 1. Core Philosophy & Execution Flow
* **Pessimistic (PCC):** Assumes conflicts are **likely**. A transaction acquires exclusive or shared locks on the data across the relevant nodes *before* reading or modifying it. Other transactions trying to access the same data are blocked until the lock is released. *(“Ask for permission.”)*
* **Optimistic (OCC):** Assumes conflicts are **rare**. Transactions execute locally without acquiring locks. They read data (often noting a version number or timestamp), compute changes, and validate at **commit time** whether another node modified the data in the interim. If a conflict is detected, the transaction aborts and retries. *(“Ask for forgiveness.”)*
### 2. Network Overhead and Latency
* **Pessimistic:** High upfront network overhead. Acquiring and releasing distributed locks requires multiple network round-trips (often using protocols like Two-Phase Locking combined with Two-Phase Commit, or external coordinators like ZooKeeper/etcd) before work can safely finish. Locks are held across network boundaries, extending lock duration due to network latency.
* **Optimistic:** Low upfront latency. Reads and local computations require minimal coordination. Network coordination only happens during the final validation/commit phase (e.g., via an atomic Compare-And-Swap operation or distributed consensus). However, if a conflict occurs, the entire network cost is wasted and must be repeated.
### 3. Handling Partial Failures and Node Crashes
In a distributed system, nodes can crash or experience network partitions at any time.
* **Pessimistic:** Vulnerable to **blocking on failure**. If a client or coordinator node crashes while holding a distributed lock, that data becomes unavailable to the rest of the cluster until a lease/timeout expires and recovery protocols run.
* **Optimistic:** Highly resilient to slow or crashing clients. Because no locks are held during the read and compute phases, a crashed client does not block any other nodes in the system from making progress.
### 4. Deadlocks vs. Livelocks (Starvation)
* **Pessimistic:** Prone to **distributed deadlocks** (e.g., Node A locks Record 1 and waits for Record 2 on Node B, while Node B locks Record 2 and waits for Record 1). Detecting and resolving deadlocks across a network is complex and usually relies on aggressive timeouts.
* **Optimistic:** **Deadlock-free** because transactions do not wait on locks. However, under high contention, OCC suffers from **starvation or livelocks**, where competing nodes repeatedly force each other to abort and retry, causing throughput to collapse (thrashing).
### 5. Throughput and Contention Profile
* **Pessimistic:** Provides predictable, stable throughput under **high contention**. Even though transactions have to wait in line, work that starts is rarely wasted on rollbacks.
* **Optimistic:** Provides vastly superior throughput and scalability under **low contention** or read-heavy workloads, because nodes operate independently without waiting on a centralized lock manager.
---
### Summary Comparison
| Feature | Pessimistic Concurrency (PCC) | Optimistic Concurrency (OCC) |
| :--- | :--- | :--- |
| **Conflict Detection** | Before data access (Acquire Locks) | At commit time (Validation / Versioning) |
| **Conflict Resolution** | Block / Wait (or Timeout) | Abort, Rollback, and Retry |
| **Failure Impact** | Crashed lock-holders block other nodes | Crashed nodes do not block others |
| **Primary Hazard** | Distributed Deadlocks | Retry storms / Starvation |
| **Cost of Contention** | High latency (waiting for locks) | Wasted compute & network bandwidth (retries) |
| **Common Mechanisms** | Distributed 2PL, Leases, Fencing Tokens | Version vectors, MVCC, Compare-And-Swap, ETags |
---
### Real-World Distributed Examples & When to Choose
**Use Pessimistic Concurrency when:**
* Write contention on specific records ("hotspots") is high.
* The cost of rolling back and retrying a transaction is expensive (e.g., complex multi-step workflows or external side effects).
* *Examples:* Google Cloud Spanner (uses Two-Phase Locking for read-write transactions), distributed lock services (Redis Redlock, ZooKeeper, etcd) guarding critical sections.
**Use Optimistic Concurrency when:**
* Workloads are read-heavy and writes rarely collide.
* Transactions span user "think time" or unreliable wide-area networks (where holding locks would paralyze the system).
* You need maximum horizontal scalability and availability.
* *Examples:* Microservice APIs using HTTP `ETags` / `If-Match` headers, DynamoDB Conditional Writes, Apache Cassandra Lightweight Transactions (Paxos-based CAS), and conflict-free workflows in Git.{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"text": "In distributed systems, concurrency control ensures that multiple nodes or clients can access and modify shared data simultaneously without corrupting it or violating consistency guarantees. \n\nThe fundamental difference between **Pessimistic Concurrency Control (PCC)** and **Optimistic Concurrency Control (OCC)** lies in how they anticipate and handle data conflicts—a challenge that is heavily amplified in distributed environments due to network latency, partial failures, and lack of a global clock.\n\nHere are the key differences between the two approaches in distributed systems:\n\n---\n\n### 1. Core Philosophy & Execution Flow\n* **Pessimistic (PCC):** Assumes conflicts are **likely**. A transaction acquires exclusive or shared locks on the data across the relevant nodes *before* reading or modifying it. Other transactions trying to access the same data are blocked until the lock is released. *(“Ask for permission.”)*\n* **Optimistic (OCC):** Assumes conflicts are **rare**. Transactions execute locally without acquiring locks. They read data (often noting a version number or timestamp), compute changes, and validate at **commit time** whether another node modified the data in the interim. If a conflict is detected, the transaction aborts and retries. *(“Ask for forgiveness.”)*\n\n### 2. Network Overhead and Latency\n* **Pessimistic:** High upfront network overhead. Acquiring and releasing distributed locks requires multiple network round-trips (often using protocols like Two-Phase Locking combined with Two-Phase Commit, or external coordinators like ZooKeeper/etcd) before work can safely finish. Locks are held across network boundaries, extending lock duration due to network latency.\n* **Optimistic:** Low upfront latency. Reads and local computations require minimal coordination. Network coordination only happens during the final validation/commit phase (e.g., via an atomic Compare-And-Swap operation or distributed consensus). However, if a conflict occurs, the entire network cost is wasted and must be repeated.\n\n### 3. Handling Partial Failures and Node Crashes\nIn a distributed system, nodes can crash or experience network partitions at any time.\n* **Pessimistic:** Vulnerable to **blocking on failure**. If a client or coordinator node crashes while holding a distributed lock, that data becomes unavailable to the rest of the cluster until a lease/timeout expires and recovery protocols run.\n* **Optimistic:** Highly resilient to slow or crashing clients. Because no locks are held during the read and compute phases, a crashed client does not block any other nodes in the system from making progress.\n\n### 4. Deadlocks vs. Livelocks (Starvation)\n* **Pessimistic:** Prone to **distributed deadlocks** (e.g., Node A locks Record 1 and waits for Record 2 on Node B, while Node B locks Record 2 and waits for Record 1). Detecting and resolving deadlocks across a network is complex and usually relies on aggressive timeouts.\n* **Optimistic:** **Deadlock-free** because transactions do not wait on locks. However, under high contention, OCC suffers from **starvation or livelocks**, where competing nodes repeatedly force each other to abort and retry, causing throughput to collapse (thrashing).\n\n### 5. Throughput and Contention Profile\n* **Pessimistic:** Provides predictable, stable throughput under **high contention**. Even though transactions have to wait in line, work that starts is rarely wasted on rollbacks.\n* **Optimistic:** Provides vastly superior throughput and scalability under **low contention** or read-heavy workloads, because nodes operate independently without waiting on a centralized lock manager.\n\n---\n\n### Summary Comparison\n\n| Feature | Pessimistic Concurrency (PCC) | Optimistic Concurrency (OCC) |\n| :--- | :--- | :--- |\n| **Conflict Detection** | Before data access (Acquire Locks) | At commit time (Validation / Versioning) |\n| **Conflict Resolution** | Block / Wait (or Timeout) | Abort, Rollback, and Retry |\n| **Failure Impact** | Crashed lock-holders block other nodes | Crashed nodes do not block others |\n| **Primary Hazard** | Distributed Deadlocks | Retry storms / Starvation |\n| **Cost of Contention** | High latency (waiting for locks) | Wasted compute & network bandwidth (retries) |\n| **Common Mechanisms** | Distributed 2PL, Leases, Fencing Tokens | Version vectors, MVCC, Compare-And-Swap, ETags |\n\n---\n\n### Real-World Distributed Examples & When to Choose\n\n**Use Pessimistic Concurrency when:**\n* Write contention on specific records (\"hotspots\") is high.\n* The cost of rolling back and retrying a transaction is expensive (e.g., complex multi-step workflows or external side effects).\n* *Examples:* Google Cloud Spanner (uses Two-Phase Locking for read-write transactions), distributed lock services (Redis Redlock, ZooKeeper, etcd) guarding critical sections.\n\n**Use Optimistic Concurrency when:**\n* Workloads are read-heavy and writes rarely collide.\n* Transactions span user \"think time\" or unreliable wide-area networks (where holding locks would paralyze the system).\n* You need maximum horizontal scalability and availability.\n* *Examples:* Microservice APIs using HTTP `ETags` / `If-Match` headers, DynamoDB Conditional Writes, Apache Cassandra Lightweight Transactions (Paxos-based CAS), and conflict-free workflows in Git.",
"thoughtSignature": "AY89a1/7+mSAQqey81jFRefMaQ6AvrZVCY4ca/OurWm/h/1+06QvID7BNzGmb2jXzBAhKl682X8zaGjFvO9QHFoPpdoZHrJkSnSMRQsNeiiKb8L68SCvZ9YHy9yIDMzLLXlIy08htFW9XbwFld2Oyf8/TiRJuOhxdLuLTqW2v9e826fGeWN91THLDPVbT8RzCSIjviADVYfyImWw8NgHZsLZ/8CG1wQFWvf3nQ4dpimrRyHzsnaYm0vnRuVC8fgoqbpvNlpdQiYTEJRl6rB5enwL1SGgxNEJrHGQYMXtpVAogAoemH4YsE0kicxwPRHbzaAlMwOkDAyUkkqxe3tsjbwg372jAnGeX0iHD9sJZ8SZVv92+VJOtnqaSI19cj9mjClTul+lBhjHAvILDdu1bJka271FbjFJFTa/Xx53vy7Xi1adlFT9uM/P9Xm06H/PBXOkDYSVJN23ltVl5KSc19o5nYRYB0mlU1b1onaZST7d+aXRS1uGkmn5wRv+L66iCQcpmwAqpuctJTsYK+ZqLpixT8zc1lTL7yby6w5y6J5f1xq7LNE9dTDjcMI3Ga2/sYKkiCAjrhU2e9VGHay0+d2Q6T6TJVTtGlV4embLrItd4gl2yH67j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jiAggCbU/waJ1jb+9Wf40mHKwIjbs+G+xEB71epHLstcYHGQXAFRBfgltam2AfJf32yrcd6rlO90YI6YM+HqGXBLEtmjtcfTsoq24lUebnxkTrSupYdxNT4Akugq4vTUWG8FedhK6x01KyzrE0dYBpFvIEc4gBh/ZE6m8F7FEBgsUZn/+TMsPYczp00rKyeFEV17O6xZ30xQ7owyNcRGeTGwJOLC3ot75YfWlbJ5Mshfn8LxoiQHmbZpuTww0VGM8wHwX41GzWTGOR/q/JRDb7UblF6nfRP1abWErTqrkp24wuTPSEHRnC2m86lAGbQbvkytU7StkbVKirHfsrYsRMnaQyw7+ZAeUgN7Wklo9zgUE2kASLQIvCrwyjbvYfWxZgXtc4nlZMcaAVNSRE0mt8kLpA0lDYXZe/SFQNQmOJ2iEHUmxzvawhPQo+A31lAVs+0yCFmFxQa8qZPptuDaRAds/hNOY5z+zBHSunMAWKJ+6qkePQxjrDKjig70Rd7krB+zVLqNyEo1DPKYD1NdD5ZVeaEPqxTCToCKI7dpj1v6vkrFP4vLDrYrZqW5drGuQtUebWNQ0oKaV0HMTSQy04GXHwLtpfyO1HLru0Oecgh6agSATPkM8vOhKwFIBNWHkdgpwSMjIGXqb5HEX8qTHASOiu3MVJvfH3DVffJP4E1Us4+hahsOS28Q/KJEiOdRnUZCSbx4zv8o0N7xDxL/wDnU7t78GOHom1TgUGILreg
... response truncated ...Agentic Coding — Coding task with a system instruction
const response = await env.AI.run(
'google/gemini-4-carbon',
{
contents: [
{
parts: [
{ text: 'Write a Python function that merges two sorted lists into one sorted list.' },
],
role: 'user',
},
],
systemInstruction: {
parts: [
{
text: 'You are an expert software engineering agent. Write concise, correct, well-documented code.',
},
],
},
},
)
console.log(response)curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "google/gemini-4-carbon",
"input": {
"contents": [
{
"parts": [
{
"text": "Write a Python function that merges two sorted lists into one sorted list."
}
],
"role": "user"
}
],
"systemInstruction": {
"parts": [
{
"text": "You are an expert software engineering agent. Write concise, correct, well-documented code."
}
]
}
}
}'Here is a concise, efficient implementation using the classic two-pointer approach, which runs in $O(n + m)$ time and $O(n + m)$ space:
```python
from typing import TypeVar
T = TypeVar("T")
def merge_sorted_lists(list1: list[T], list2: list[T]) -> list[T]:
"""Merges two sorted lists into a single sorted list.
Args:
list1: The first sorted list.
list2: The second sorted list.
Returns:
A new sorted list containing all elements from both input lists.
"""
merged: list[T] = []
i, j = 0, 0
len1, len2 = len(list1), len(list2)
# Compare elements from both lists and append the smaller one
while i < len1 and j < len2:
if list1[i] <= list2[j]: # type: ignore[operator]
merged.append(list1[i])
i += 1
else:
merged.append(list2[j])
j += 1
# Append any remaining elements (only one of these slices will be non-empty)
merged.extend(list1[i:])
merged.extend(list2[j:])
return merged
```
### Example Usage
```python
a = [1, 3, 5, 7]
b = [2, 3, 6, 8, 9]
print(merge_sorted_lists(a, b))
# Output: [1, 2, 3, 3, 5, 6, 7, 8, 9]
```
### Pythonic Alternative (`heapq.merge`)
In production code, you can also use Python's standard library `heapq.merge`, which is implemented in C, works on any sorted iterable, and evaluates lazily:
```python
import heapq
merged = list(heapq.merge(list1, list2))
```{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"text": "Here is a concise, efficient implementation using the classic two-pointer approach, which runs in $O(n + m)$ time and $O(n + m)$ space:\n\n```python\nfrom typing import TypeVar\n\nT = TypeVar(\"T\")\n\n\ndef merge_sorted_lists(list1: list[T], list2: list[T]) -> list[T]:\n \"\"\"Merges two sorted lists into a single sorted list.\n\n Args:\n list1: The first sorted list.\n list2: The second sorted list.\n\n Returns:\n A new sorted list containing all elements from both input lists.\n \"\"\"\n merged: list[T] = []\n i, j = 0, 0\n len1, len2 = len(list1), len(list2)\n\n # Compare elements from both lists and append the smaller one\n while i < len1 and j < len2:\n if list1[i] <= list2[j]: # type: ignore[operator]\n merged.append(list1[i])\n i += 1\n else:\n merged.append(list2[j])\n j += 1\n\n # Append any remaining elements (only one of these slices will be non-empty)\n merged.extend(list1[i:])\n merged.extend(list2[j:])\n\n return merged\n```\n\n### Example Usage\n\n```python\na = [1, 3, 5, 7]\nb = [2, 3, 6, 8, 9]\n\nprint(merge_sorted_lists(a, b))\n# Output: [1, 2, 3, 3, 5, 6, 7, 8, 9]\n```\n\n### Pythonic Alternative (`heapq.merge`)\nIn production code, you can also use Python's standard library `heapq.merge`, which is implemented in C, works on any sorted iterable, and evaluates lazily:\n\n```python\nimport heapq\n\nmerged = list(heapq.merge(list1, list2))\n```",
"thoughtSignature": "AY89a19L81xxk8WZs3fRP6B8Vv2bXwZTmLS7bcqOzt2sFi46/NZNaY0wQLL6UYyf21SGZ7P+h2Z6pbXI/bplAAGnQ4NABreVFt0aO9F9mH3ZtdGJvQfqlX2eZQBB9Kx1S/E98ChMqvNU522YHmuWjrRJW+7LkUacUMD9LciaXVqdOvE4DzTQYMql5QG1KgHqx/svGO8r4EufCu2hXyc0fQAxlxIGxHg3zvMOHLGV71uM/gX0zs5yx3luFgE9T6JXLSs4K2TqhBpdY9Xtazvv5jh9F++lvv4IieJSPHlTQ3AIthRGJA9rZ9JsZvF9Yw3mMLtoMVfMgTNkPQiyKr2k314RonfhbAVmwcIqEovN/ulmsNF1005NnIBIJowjuLLz9bYGk63v/xBuKsit8OSWNvVo1AztsGJdHQdW+wXo655rVurWiePEFjLR6irN/nK/paDYyLr8rGfFFqFY/whidjbpzatZdZj+fNxXiMt4gDN3btYqTrxdA8Q8S2MkxwzQZfhi2LN0YAA2jSmf78a9aar9Qf3YDE4K8v5DOojSKfN+R+Zu0CNwVP86SDnxesVNz8iG85KAKJUXPHObH31robbmy4TjFEVu5OZx7hd1vT/kW3lmK8Gc1Et9XqYXZtKJz/NA7el1EaX5G4wH9gXmBlpeH2V9EvcOXbJl4WnZ5M6FzExNFXK+Rq7GedopGvufkeP+/7Y83vNyQWZtpDGyrQcffuDdXfqkw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PAhd3F17RSnm/OOs01cJ7KQdB5VbuzwCr1wl++qTxDOdZ3EF3zJ2krCU4duHXNmHkxTaulJsfzNjwwQMglC9P9qVAj/MXyl6QEfZlTTkdQXwoX+kr8PilWGtyqEUJz1c9F5YWY5El8Bj0icZEugQg2ry8itIkd39CzYJZ98Iea0FwjGbtbEBofOMIhR4xZYlcseF4wPoT32TrFSZVqlHJZ8yThaM="
}
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 68,
"candidatesTokenCount": 472,
"totalTokenCount": 956,
"trafficType": "ON_DEMAND",
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 68
}
],
"candidatesTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 472
}
],
"thoughtsTokenCount": 416
},
"modelVersion": "gemini-4-carbon",
"createTime": "2026-11-10T15:53:24.940732Z",
"responseId": "9D3zary1OZe4u-gPsOO98AY"
}Multi-turn Conversation — Continuing a multi-step coding conversation, typical of an agentic loop
const response = await env.AI.run(
'google/gemini-4-carbon',
{
contents: [
{
parts: [{ text: 'I have a bug where my recursive fibonacci function is too slow for n=40.' }],
role: 'user',
},
{
parts: [
{
text: "That's expected with naive recursion since it recomputes overlapping subproblems exponentially. You can fix it with memoization or an iterative approach. Which would you like to see?",
},
],
role: 'model',
},
{ parts: [{ text: 'Show me the memoized version.' }], role: 'user' },
],
},
)
console.log(response)curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "google/gemini-4-carbon",
"input": {
"contents": [
{
"parts": [
{
"text": "I have a bug where my recursive fibonacci function is too slow for n=40."
}
],
"role": "user"
},
{
"parts": [
{
"text": "That'\''s expected with naive recursion since it recomputes overlapping subproblems exponentially. You can fix it with memoization or an iterative approach. Which would you like to see?"
}
],
"role": "model"
},
{
"parts": [
{
"text": "Show me the memoized version."
}
],
"role": "user"
}
]
}
}'Since you didn't mention a specific language, here is how to do it in **Python** (the logic translates easily to any language).
Memoization works by storing the results of expensive function calls in a dictionary/hash map so you only calculate each Fibonacci number once. This drops the time complexity from $O(2^n)$ down to $O(n)$, making `n=40` run instantaneously.
### 1. Manual Memoization (Works in any language)
You pass a `memo` dictionary down through the recursive calls to keep track of values you've already solved:
```python
def fib(n, memo=None):
if memo is None:
memo = {}
# 1. Check if we already calculated this value
if n in memo:
return memo[n]
# 2. Base cases
if n <= 1:
return n
# 3. Calculate, store in memo, and return
memo[n] = fib(n - 1, memo) + fib(n - 2, memo)
return memo[n]
print(fib(40)) # Outputs: 102334155 instantly
```
*(Note: We use `memo=None` as the default argument instead of `memo={}` to avoid Python's mutable default argument quirk, though `memo={}` would technically work as a persistent cache here.)*
### 2. Using Built-in Caching (Python-specific)
If you happen to be using Python 3.9+, the standard library has a decorator that does this exact thing automatically:
```python
from functools import cache
@cache
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
print(fib(40))
```
*(For Python 3.2–3.8, you can use `@lru_cache(maxsize=None)` from `functools` instead).*
---
If you are using JavaScript, Java, C++, or another language, let me know and I'll write out the syntax for that one!{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"text": "Since you didn't mention a specific language, here is how to do it in **Python** (the logic translates easily to any language).\n\nMemoization works by storing the results of expensive function calls in a dictionary/hash map so you only calculate each Fibonacci number once. This drops the time complexity from $O(2^n)$ down to $O(n)$, making `n=40` run instantaneously.\n\n### 1. Manual Memoization (Works in any language)\nYou pass a `memo` dictionary down through the recursive calls to keep track of values you've already solved:\n\n```python\ndef fib(n, memo=None):\n if memo is None:\n memo = {}\n\n # 1. Check if we already calculated this value\n if n in memo:\n return memo[n]\n\n # 2. Base cases\n if n <= 1:\n return n\n\n # 3. Calculate, store in memo, and return\n memo[n] = fib(n - 1, memo) + fib(n - 2, memo)\n return memo[n]\n\n\nprint(fib(40)) # Outputs: 102334155 instantly\n```\n\n*(Note: We use `memo=None` as the default argument instead of `memo={}` to avoid Python's mutable default argument quirk, though `memo={}` would technically work as a persistent cache here.)*\n\n### 2. Using Built-in Caching (Python-specific)\nIf you happen to be using Python 3.9+, the standard library has a decorator that does this exact thing automatically:\n\n```python\nfrom functools import cache\n\n\n@cache\ndef fib(n):\n if n <= 1:\n return n\n return fib(n - 1) + fib(n - 2)\n\n\nprint(fib(40))\n```\n*(For Python 3.2–3.8, you can use `@lru_cache(maxsize=None)` from `functools` instead).*\n\n---\n\nIf you are using JavaScript, Java, C++, or another language, let me know and I'll write out the syntax for that one!",
"thoughtSignature": "AY89a1/czByhhcPnon5/MC4FSJQ8GHuoTWKShjS9Em2sttH6EB8BXix7r1Xp2k4Owso5MLW2N8QKgwdIc9VpWhFW/fKC/kZ3248r+DCQazHh0/BGYC6OorTbdrkaqc4k4RNK7TmeBN+bb++TB5BNP5Z68NHmjP4yosWEXWgAxpgLLYxJilodMjHFsuGobF+IHd3HdK6W9rXsiPELo2FeX+ovsl9wqMkpUIaoZUanym8UuT5HqYY9TNgcbu+AeCGSD69epkpv0pi6xTA2vPdDNuq3Etc9j3aRp1uU17yn/xOT9XL1fH+zHo7jL+siWVMiGVbcOFL7B6K5HApfnD+7OJ3yK0cF4SKSLFWWrFoGeneXsNk9p2/s2B6qQgtR1DHGZ0Bueol/iuM3WbfTbLDbJyZokqUQrEXz7uNIL+gkNBEAVmTqsTkQ32uxiLGQXJiEAKiTN5TPsMJyP+rzVdk/61W4d9EB4cTtTfZP0VzAHDsDutv2ZiHAW2Q+nKK4pJ/mjMp7xsDIFp4seDdXJBXbAqZD/vtTMsSFWRoeYTvwAXjZGFbkjpV/lBoqB1rAEH3FkmSpvNz5i2Wi3IbwLsZrNc5rZ/DNmnpGc/75gVnjlqs1MSGIMBKSr3ITVwM+w8xBE5lJcXYCw7BzMXg7bG4KOYzCkBpl0w5nwuUIpqfRY85uYjMM1VjHsQpz6OqRNzYPhqYliVvDGNJ26e8lcltuIUofUN9cZCBNKjm+9kxu+fuFqGuoU6Q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alTokenCount": 1090,
"trafficType": "ON_DEMAND",
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 101
}
],
"candidatesTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 481
}
],
"thoughtsTokenCount": 508
},
"modelVersion": "gemini-4-carbon",
"createTime": "2026-11-10T15:53:39.705154Z",
"responseId": "Az7zaoKFK4jIu-gPuKPXmQo"
}Spatial Reasoning — A reasoning-heavy puzzle with the thinking level set to HIGH
const response = await env.AI.run(
'google/gemini-4-carbon',
{
contents: [
{
parts: [
{
text: 'A cube is painted red on all faces, then cut into 27 equal smaller cubes. How many of the smaller cubes have exactly two red faces?',
},
],
role: 'user',
},
],
generationConfig: { thinkingConfig: { thinkingLevel: 'HIGH' } },
},
)
console.log(response)curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "google/gemini-4-carbon",
"input": {
"contents": [
{
"parts": [
{
"text": "A cube is painted red on all faces, then cut into 27 equal smaller cubes. How many of the smaller cubes have exactly two red faces?"
}
],
"role": "user"
}
],
"generationConfig": {
"thinkingConfig": {
"thinkingLevel": "HIGH"
}
}
}
}'There are **12** smaller cubes that have exactly two red faces.
**Here is why:**
Since the large cube is cut into 27 equal smaller cubes, it is divided into a $3 \times 3 \times 3$ grid ($\sqrt[3]{27} = 3$). You can figure out how many faces are painted based on where each smaller cube was located in the original large cube:
* **3 red faces (Corners):** A cube has 8 corners, so there are **8** of these cubes.
* **2 red faces (Edges):** The cubes with two painted faces are located along the edges, *between* the corners. A cube has 12 edges, and in a $3 \times 3 \times 3$ grid, there is 1 middle cube per edge ($12 \times 1 = \mathbf{12}$).
* **1 red face (Centers of each face):** A cube has 6 faces, with 1 cube right in the center of each face, giving **6** of these cubes.
* **0 red faces (Core):** There is **1** cube completely hidden in the very center of the $3 \times 3 \times 3$ grid with no paint at all.
*(Check: $8 + 12 + 6 + 1 = 27$ total cubes)*{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"text": "There are **12** smaller cubes that have exactly two red faces.\n\n**Here is why:**\nSince the large cube is cut into 27 equal smaller cubes, it is divided into a $3 \\times 3 \\times 3$ grid ($\\sqrt[3]{27} = 3$). You can figure out how many faces are painted based on where each smaller cube was located in the original large cube:\n\n* **3 red faces (Corners):** A cube has 8 corners, so there are **8** of these cubes.\n* **2 red faces (Edges):** The cubes with two painted faces are located along the edges, *between* the corners. A cube has 12 edges, and in a $3 \\times 3 \\times 3$ grid, there is 1 middle cube per edge ($12 \\times 1 = \\mathbf{12}$). \n* **1 red face (Centers of each face):** A cube has 6 faces, with 1 cube right in the center of each face, giving **6** of these cubes.\n* **0 red faces (Core):** There is **1** cube completely hidden in the very center of the $3 \\times 3 \\times 3$ grid with no paint at all.\n\n*(Check: $8 + 12 + 6 + 1 = 27$ total cubes)*",
"thoughtSignature": "AY89a1/ZMXQ0mbL4K6MLfpWo1nrnnkqoqmTrYN5o5YlCMNGHOFaDq+1U7ziC/4D0QJwWdRSIjkhn05UZLedtMl7UlQNe3Zkb9vGpmgHkpMBosWrLKfMJaD4wqSv1+elVVnQ3KVCgzhFa8dWDl0t9mVwzqpRkrgwP1SRpJlOY+ENWDUbxYNwXuYMed8bexwGRppr1ayhsESrhXe0S8gqyKufp3fon1OYhvG6E9XTFJ8Z4hiakFKBlCWEcQ9Okk0JzDpbkyyl+OwAAK2JHVx9VYZPm1zulNW6UNGD5f8ZN0ELsGNULq748irMw8J4d9x6fU1YUD9jlD/bzqrcn4/9HOiTSl3OPlfF4AV46M5B1+2YvhNLAU+wjaBup6Oj3cEVWeQa5cJE+K0qQ/0EgoL5aFAP0spXXehB1gzorbKd4cCDXL3Hwtkj5TZi/TsCFyfmOJJ/IjDfucdaAckgq5wobsgmb2KbeSVPRuqu7p5xuFPm8pMWUaCTwSnn9L+pVsGS1NADjTGIvNhyk5G0yz6QyHe0kDRPvPtH6Hod8B5RHIRMxU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nwzmiGf/zmKvlT//4kZxj5/O2rXHSzl+y/3QN278MY9XrZxa2qf7tMpj4/mOPqJe1m3Ijyf43l30UuMCl+hYwqHiAU6Reg870gCbC6LiSQ4W/4M7XsQ9hW5ICJ6Y0xHeGNpWxvsAk0ytFcPbClyIdwnQ3Ce9gQpFzu++6gtFWIH10Mgs9IWbPOci0Sg3cyCSJv1bUabn1O8FUD6YJsc3hUvfPd7Eyo+yutyh8da/pJaEaGwHbVj+79rJmVe2I4dy5SVvg9ne/laJiEPceZBopFCyIwOjobFRgZ9ksF8pAtTdl5qG1InzHMZNIerW2NpfkcUJz3RKMbtkj2mlUAcb6/HyyXBzwqeSvAQAVoSuGaZ2k+7lz6cH4AfipfdCsIhNzpsWTD6Z6FDqI="
}
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 62,
"candidatesTokenCount": 297,
"totalTokenCount": 1077,
"trafficType": "ON_DEMAND",
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 62
}
],
"candidatesTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 297
}
],
"thoughtsTokenCount": 718
},
"modelVersion": "gemini-4-carbon",
"createTime": "2026-11-10T15:53:55.521592Z",
"responseId": "Ez7zavjqH7_Xu-gPx6arkQM"
}▶contents[]
arrayrequired▶systemInstruction{}
object▶generationConfig{}
object▶safetySettings[]
array▶tools[]
arraytoolConfig
▶messages[]
arrayrequiredtemperature
numberminimum: 0maximum: 2max_tokens
numberexclusiveMinimum: 0max_completion_tokens
numberexclusiveMinimum: 0top_p
numberminimum: 0maximum: 1frequency_penalty
numberminimum: -2maximum: 2presence_penalty
numberminimum: -2maximum: 2stream
boolean▶stream_options{}
object▶tools[]
arraytool_choice
response_format
▶modalities[]
array▶audio{}
objectreasoning_effort
stringOptional reasoning control; availability and accepted values are model-dependent.▶candidates[]
array▶usageMetadata{}
objectmodelVersion
stringid
stringobject
stringcreated
numbermodel
string▶choices[]
array▶usage{}
object