Skip to content
Google logo

Gemini 3.7 Flash

Text Generation • Google

View as MarkdownAgent setup

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

Gemini 3.7 Flash is a highly capable, natively multimodal reasoning model optimized for agentic workflows and real-world tasks.

Model Info
Context Window ↗1,048,576 tokens
Terms and Licenselink ↗
More informationlink ↗
Request formatsChat Completions
Pricing
  • Input (per 1M tokens)$0.75
  • Output (per 1M tokens)$3.75
  • Cached input (per 1M tokens)$0.075

Usage

const response = await env.AI.run(
  'google/gemini-3.7-flash',
  { contents: [{ parts: [{ text: 'What are the three laws of thermodynamics?' }], 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-3.7-flash",
  "input": {
    "contents": [
      {
        "parts": [
          {
            "text": "What are the three laws of thermodynamics?"
          }
        ],
        "role": "user"
      }
    ]
  }
}'
The **laws of thermodynamics** define fundamental physical quantities (like temperature, energy, and entropy) and describe how they behave under various circumstances. 

While there are historically **three** main laws, scientists later added a foundational law called the **Zeroth Law**. Here are all four, clearly explained:

---

### **1. The First Law (Law of Conservation of Energy)**
> **"Energy cannot be created or destroyed; it can only change forms."**

* **What it means:** The total amount of energy in an isolated system (like the universe) remains constant. When heat is added to a system, it either increases the system's internal energy or does work on the surroundings.
* **Equation:** $\Delta U = Q - W$ 
  *(Change in internal energy = Heat added to the system $-$ Work done by the system)*
* **Everyday Example:** A car engine converts the *chemical energy* stored in gasoline into *thermal energy* (heat) and *mechanical energy* (motion). No new energy is created.

---

### **2. The Second Law (Law of Entropy)**
> **"The entropy of an isolated system always increases over time."**

* **What it means:** Natural processes are irreversible. Heat will spontaneously flow from hotter objects to colder objects, but never the reverse on its own. **Entropy** is a measure of disorder or unusable energy; because some energy is always lost as heat in any transfer, the universe becomes increasingly disordered.
* **Consequence:** It is impossible to build a machine that is 100% efficient (a perpetual motion machine).
* **Everyday Example:** If you drop an ice cube into a cup of hot coffee, the heat moves from the coffee to the ice, melting it. You will never see a cup of warm coffee spontaneously separate into boiling coffee and an ice cube.

---

### **3. The Third Law (Absolute Zero)**
> **"The entropy of a pure crystalline substance approaches zero as the temperature approaches absolute zero."**

* **What it means:** **Absolute zero** (0 Kelvin, or $-273.15^\circ\text{C} / -459.67^\circ\text{F}$) is the point where all atomic motion stops. At this temperature, a perfect crystal would have zero disorder (zero entropy). 
* **Consequence:** It is physically impossible to cool any object down to absolute zero in a finite number of steps, though scientists can get within billionths of a degree close.

---

### *Bonus: The Zeroth Law (Thermal Equilibrium)*
*(Formulated after the first two, but deemed so fundamental it was placed before them.)*

> **"If System A is in thermal equilibrium with System B, and System B is in thermal equilibrium with System C, then System A is also in thermal equilibrium with System C."**

* **What it means:** "Thermal equilibrium" simply means no heat flows between two objects because they are the same temperature. This law defines the concept of **temperature** and allows us to use **thermometers**.

---

### **A Common Memory Trick (Ginsberg’s Analogy)**
A humorous way physicists summarize the laws of thermodynamics:
1. **First Law:** You can't win; you can only break even (energy is conserved).
2. **Second Law:** You can't break even (entropy always increases, so energy is lost as unusable heat).
3. **Third Law:** You can't get out of the game (you can never reach absolute zero).
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "The **laws of thermodynamics** define fundamental physical quantities (like temperature, energy, and entropy) and describe how they behave under various circumstances. \n\nWhile there are historically **three** main laws, scientists later added a foundational law called the **Zeroth Law**. Here are all four, clearly explained:\n\n---\n\n### **1. The First Law (Law of Conservation of Energy)**\n> **\"Energy cannot be created or destroyed; it can only change forms.\"**\n\n* **What it means:** The total amount of energy in an isolated system (like the universe) remains constant. When heat is added to a system, it either increases the system's internal energy or does work on the surroundings.\n* **Equation:** $\\Delta U = Q - W$ \n  *(Change in internal energy = Heat added to the system $-$ Work done by the system)*\n* **Everyday Example:** A car engine converts the *chemical energy* stored in gasoline into *thermal energy* (heat) and *mechanical energy* (motion). No new energy is created.\n\n---\n\n### **2. The Second Law (Law of Entropy)**\n> **\"The entropy of an isolated system always increases over time.\"**\n\n* **What it means:** Natural processes are irreversible. Heat will spontaneously flow from hotter objects to colder objects, but never the reverse on its own. **Entropy** is a measure of disorder or unusable energy; because some energy is always lost as heat in any transfer, the universe becomes increasingly disordered.\n* **Consequence:** It is impossible to build a machine that is 100% efficient (a perpetual motion machine).\n* **Everyday Example:** If you drop an ice cube into a cup of hot coffee, the heat moves from the coffee to the ice, melting it. You will never see a cup of warm coffee spontaneously separate into boiling coffee and an ice cube.\n\n---\n\n### **3. The Third Law (Absolute Zero)**\n> **\"The entropy of a pure crystalline substance approaches zero as the temperature approaches absolute zero.\"**\n\n* **What it means:** **Absolute zero** (0 Kelvin, or $-273.15^\\circ\\text{C} / -459.67^\\circ\\text{F}$) is the point where all atomic motion stops. At this temperature, a perfect crystal would have zero disorder (zero entropy). \n* **Consequence:** It is physically impossible to cool any object down to absolute zero in a finite number of steps, though scientists can get within billionths of a degree close.\n\n---\n\n### *Bonus: The Zeroth Law (Thermal Equilibrium)*\n*(Formulated after the first two, but deemed so fundamental it was placed before them.)*\n\n> **\"If System A is in thermal equilibrium with System B, and System B is in thermal equilibrium with System C, then System A is also in thermal equilibrium with System C.\"**\n\n* **What it means:** \"Thermal equilibrium\" simply means no heat flows between two objects because they are the same temperature. This law defines the concept of **temperature** and allows us to use **thermometers**.\n\n---\n\n### **A Common Memory Trick (Ginsberg’s Analogy)**\nA humorous way physicists summarize the laws of thermodynamics:\n1. **First Law:** You can't win; you can only break even (energy is conserved).\n2. **Second Law:** You can't break even (entropy always increases, so energy is lost as unusable heat).\n3. **Third Law:** You can't get out of the game (you can never reach absolute zero).",
            "thoughtSignature": "AY89a19XdC4x/f+U8hCl13sukl2kjv7cr4AAfyhSwG6PrM0gq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Jz+COF+5G/eoiN3xBCkOY8joRT9s4f0/5XiH7zpIggw2XS9L3jqzMdUnkNLV+zwtUSWC3PlS6JRq1NbZ3hdihlTj2uF7XWEeL29M+QYvrNjhjAcKHk1+Z6mu3on0ONd087rTyjl+TXjW/dGZEL5MqWh8F8TeCvb+NsRFnKou+malXbUHjTtft8Du/M/0XcmH+7rM0VeUTvEiu9Xr5xzuichY0zO7w6eYBbcPrNVKBCxzamwBKCPNoaK3bQTzFhecucJDbPZuUvmp9qYZss8iPvyCNiA36xV49tvnycbKTI8KVTeTccYxqouN5js/fZvHbzQfoj5C7eWtlZlQqgrTGxNxcJ6eRuAwdQ1sytW9BTAahQjEZlSN5yOSTlg9iRoAx8LWyI="
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 8,
    "candidatesTokenCount": 752,
    "totalTokenCount": 1433,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 8
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 752
      }
    ],
    "thoughtsTokenCount": 673
  },
  "modelVersion": "gemini-3.7-flash",
  "createTime": "2026-08-13T18:07:02.084997Z",
  "responseId": "xgd-aoWYBeumrb8Pht-PuQw"
}

Examples

With System Instruction — Using a system instruction to set an agentic coding persona
const response = await env.AI.run(
  'google/gemini-3.7-flash',
  {
    contents: [
      {
        parts: [
          { text: 'Write a Python function that merges two sorted lists into one sorted list.' },
        ],
        role: 'user',
      },
    ],
    generationConfig: { temperature: 0.2 },
    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-3.7-flash",
  "input": {
    "contents": [
      {
        "parts": [
          {
            "text": "Write a Python function that merges two sorted lists into one sorted list."
          }
        ],
        "role": "user"
      }
    ],
    "generationConfig": {
      "temperature": 0.2
    },
    "systemInstruction": {
      "parts": [
        {
          "text": "You are an expert software engineering agent. Write concise, correct, well-documented code."
        }
      ]
    }
  }
}'
Here is a clean, efficient Python function using the two-pointer approach, running in $O(n + m)$ time and space complexity:

```python
from typing import TypeVar

T = TypeVar("T")


def merge_sorted_lists(list1: list[T], list2: list[T]) -> list[T]:
    """Merges two pre-sorted lists into a single sorted list.

    Args:
        list1: A sorted list of elements.
        list2: Another sorted list of elements (same comparable type).

    Returns:
        A new sorted list containing all elements from both lists.

    Complexity:
        Time: O(n + m) where n and m are lengths of list1 and list2.
        Space: O(n + m) for the resulting merged list.
    """
    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]:
            merged.append(list1[i])
            i += 1
        else:
            merged.append(list2[j])
            j += 1

    # Append any remaining elements
    merged.extend(list1[i:])
    merged.extend(list2[j:])

    return merged


# Example usage:
if __name__ == "__main__":
    a = [1, 3, 5, 8]
    b = [2, 4, 6, 7, 9, 10]
    print(merge_sorted_lists(a, b))
    # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

### Alternative: Standard Library (`heapq.merge`)
If you prefer a built-in generator approach that handles large streams without loading everything into memory upfront:

```python
import heapq


def merge_sorted_lists_lazy(list1: list, list2: list) -> list:
    return list(heapq.merge(list1, list2))
```
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "Here is a clean, efficient Python function using the two-pointer approach, running in $O(n + m)$ time and space complexity:\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 pre-sorted lists into a single sorted list.\n\n    Args:\n        list1: A sorted list of elements.\n        list2: Another sorted list of elements (same comparable type).\n\n    Returns:\n        A new sorted list containing all elements from both lists.\n\n    Complexity:\n        Time: O(n + m) where n and m are lengths of list1 and list2.\n        Space: O(n + m) for the resulting merged list.\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]:\n            merged.append(list1[i])\n            i += 1\n        else:\n            merged.append(list2[j])\n            j += 1\n\n    # Append any remaining elements\n    merged.extend(list1[i:])\n    merged.extend(list2[j:])\n\n    return merged\n\n\n# Example usage:\nif __name__ == \"__main__\":\n    a = [1, 3, 5, 8]\n    b = [2, 4, 6, 7, 9, 10]\n    print(merge_sorted_lists(a, b))\n    # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n```\n\n### Alternative: Standard Library (`heapq.merge`)\nIf you prefer a built-in generator approach that handles large streams without loading everything into memory upfront:\n\n```python\nimport heapq\n\n\ndef merge_sorted_lists_lazy(list1: list, list2: list) -> list:\n    return list(heapq.merge(list1, list2))\n```",
            "thoughtSignature": "AY89a1/7DiGkUrOuttOAy7k+sp7leERYwo5R3cg8S3yxDA+Tnd3ssAiNYC4uxqEr3xMAISGixJES6CVe74Cy/DiRvl2HSM0SiU4TmHHyAd3eUcNt3BKORH5fSqzgLyyUtHkzhDnFjO1VQT9///WfxmS8BLlIDp9dXaF44o4sr67Kw8Qi+3GMlh61cUPhmoYWYdeSI7d6CdDBw5Dw6kPR1NuFDbvo3serTTbyTOPptFmW2L5VgqS+gYz0RPHbLF3kp9fGVMfkmBIZpT3Mk4iJcGDXcMPS27T0HiBAVbZMTUjaaaYimCA1V8GQ1XrIMwbWz2T+S2/EeqTTtRY4uwWGd9sknn0v4SF33qjOykzkNucZSxEzEf4G6sXK21aVN/hoD4GDwE/WxLmc3Vr+stIjP54jr5KR9qaMyNro7PFS1S4VRvYbiygNq/E8y9qUqw3JAp+2er2LF4rVrdEsltxCxOW7LwRbM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4zltdXAuEwSrLMJI5gKarpfGyeprJNwM23lzyJ8nYU8o8ROnBboniHLMv+LgtxIcomIPTOzsz"
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 32,
    "candidatesTokenCount": 517,
    "totalTokenCount": 1114,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 32
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 517
      }
    ],
    "thoughtsTokenCount": 565
  },
  "modelVersion": "gemini-3.7-flash",
  "createTime": "2026-08-13T19:07:35.820040Z",
  "responseId": "9xV-asiGMqyo3dAP3oHS-Qw"
}
Multi-turn Conversation — Continuing a multi-step coding conversation, typical of an agentic loop
const response = await env.AI.run(
  'google/gemini-3.7-flash',
  {
    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' },
    ],
    generationConfig: { maxOutputTokens: 2048 },
  },
)
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-3.7-flash",
  "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"
      }
    ],
    "generationConfig": {
      "maxOutputTokens": 2048
    }
  }
}'
Here is how to memoize the recursive Fibonacci function.

### 1. Using a Dictionary / Cache (Explicit)

You store results of previous calculations in a dictionary and look them up before computing:

```python
def fib(n, memo=None):
    if memo is None:
        memo = {}

    # Check if already computed
    if n in memo:
        return memo[n]

    # Base cases
    if n <= 0:
        return 0
    elif n == 1:
        return 1

    # Store result in memo dictionary
    memo[n] = fib(n - 1, memo) + fib(n - 2, memo)
    return memo[n]


# Test
print(fib(40))  # Returns 102334155 instantly
```

---

### 2. Using Python's Built-in `@lru_cache` (Idiomatic Python)

Python has a built-in decorator that handles memoization automatically:

```python
from functools import lru_cache


@lru_cache(maxsize=None)
def fib(n):
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    return fib(n - 1) + fib(n - 2)


# Test
print(fib(40))  # Returns 102334155 instantly
```

---

### Why this fixes the issue:
* **Naive recursion:** Time complexity is $O(2^n)$ because it recomputes identical values millions of times.
* **Memoized recursion:** Time complexity drops to $O(n)$ because each Fibonacci number from $0$ to $n$ is calculated only once. Space complexity is $O(n)$ to store the cache and recursion stack.
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "Here is how to memoize the recursive Fibonacci function.\n\n### 1. Using a Dictionary / Cache (Explicit)\n\nYou store results of previous calculations in a dictionary and look them up before computing:\n\n```python\ndef fib(n, memo=None):\n    if memo is None:\n        memo = {}\n\n    # Check if already computed\n    if n in memo:\n        return memo[n]\n\n    # Base cases\n    if n <= 0:\n        return 0\n    elif n == 1:\n        return 1\n\n    # Store result in memo dictionary\n    memo[n] = fib(n - 1, memo) + fib(n - 2, memo)\n    return memo[n]\n\n\n# Test\nprint(fib(40))  # Returns 102334155 instantly\n```\n\n---\n\n### 2. Using Python's Built-in `@lru_cache` (Idiomatic Python)\n\nPython has a built-in decorator that handles memoization automatically:\n\n```python\nfrom functools import lru_cache\n\n\n@lru_cache(maxsize=None)\ndef fib(n):\n    if n <= 0:\n        return 0\n    elif n == 1:\n        return 1\n    return fib(n - 1) + fib(n - 2)\n\n\n# Test\nprint(fib(40))  # Returns 102334155 instantly\n```\n\n---\n\n### Why this fixes the issue:\n* **Naive recursion:** Time complexity is $O(2^n)$ because it recomputes identical values millions of times.\n* **Memoized recursion:** Time complexity drops to $O(n)$ because each Fibonacci number from $0$ to $n$ is calculated only once. Space complexity is $O(n)$ to store the cache and recursion stack.",
            "thoughtSignature": "AY89a1+Wv/04SFz0MKQY8n+tFyT0cfnQ+6x5+vRQu2MbyVYQqMErpr6OvgnpAYufyK3yG/BXeedOilTz9V1KgVD1QWF0fywd6r7Yw6Hbfwl+RaCf+zGlEOeM7ZuqPAIjIrQEsfaYbR+THgipFCCvNuP//K5ObnatV7hRrDMt7pqltYwg5NWm/O+0H3YvLp55UZlztXfz8t91zLg4pJKMzutMMaupMh5AAHu/pe40KJg2Id/ZH9QCn0ZqrvwJA/vYoQjUNLmbHRQirXIF5W0o5w0yCF6HBkDHBRnu2OWUi7Ne4DIqAmJJYKv0ePurLSaDJLE09M5XS4WOyd7cSH3sBzVTfxYYRN5OeT5UUGZ16JjPcSc3eaT64u1Ng/erd2fT0NfSa5/P2cWGw9KvxOSsU/19OUoonpBQvhoMhioMg1aL2RyVI9ikxm8cl54JZJbmJpmR60vHsdXPFnBSGdG3DQ3MNv+HKZEr6kXQ1FliGJU8qXGcNOAMkgykbgunAecujeN/vpaC3SBNRdc7yMa6cT1T9FVL1l1Cuxx9OuWSQowkhoniTPsV22uOj6drYlKPU6AJJuhNjv/epFySrCJbB9F1rISGL4wYkAD6mDEmaT0rG15OxjEb97C/8OufUirZpcDDC1jR0sWj7FlS4KCQUsNzdhc5AJhLQPtA3IGgAZzDH3PHvQYhUo29VP6jMhOE5S3fmLa2TSiuKtoYwMaAIzkGdp0xKohPrVFtn9Zajb+7Y6I6TsA0WL7Pg/DTIiooTZBG+dGk49Tt6eFfW/ypKXDy9YErzQF6FTK/V9rgPHEpZdSVvFnOunGZ3I+o02B9esYSqSNQlaBI7Y0/5hpCfgf0Ap9G1sh1WMkuCL40d0jRevBbEZAKqrdG1Gr5whRbjNtWbD/Cx17clPpGT5K3dNq5NJY16xqzPwJyNgj7H9xgNFiJGh5xYzx4PBTN0NsgE1bK6t3BrPkWxG0vB7B4Flyj3HRzGHj43qkT2cRKEoNjsiTGIvvyBaa6QQOYZ5yu2hC6h7mpNntCeU5iaDtQA/NC010PqvELJpqmiGyYM9D9uazoHrelc5STEOyUH6Eei7cfhezFCB0ibUm795StAgZt4WvWpYXif4PMRc/gdsNoWZr9Wo0WhlCm4ivwwoiG71oPbzam8IRp+2PP8evgPm4AogOToIHUQMZFz2OnGXXX8E0I25wMe5D+q9zpLO+st7kOFLl1vITCPvDkjw=="
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 62,
    "candidatesTokenCount": 412,
    "totalTokenCount": 698,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 62
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 412
      }
    ],
    "thoughtsTokenCount": 224
  },
  "modelVersion": "gemini-3.7-flash",
  "createTime": "2026-08-13T19:07:41.574572Z",
  "responseId": "_RV-auyII6ip3dAP16iw4Qw"
}
Spatial Reasoning — Higher thinking budget for a spatial reasoning task
const response = await env.AI.run(
  'google/gemini-3.7-flash',
  {
    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: { maxOutputTokens: 4096 },
  },
)
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-3.7-flash",
  "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": {
      "maxOutputTokens": 4096
    }
  }
}'
To find the number of smaller cubes with **exactly two red faces**, we can analyze how the $3 \times 3 \times 3$ cube is structured:

1. **Corner cubes (3 painted faces):** There are **8** corners in a cube.
2. **Edge cubes (excluding corners, 2 painted faces):** A cube has 12 edges. With a $3 \times 3 \times 3$ cut, each edge has 1 middle cube with 2 painted faces:
   $$\text{Number of cubes} = 12 \times (3 - 2) = 12$$
3. **Face-center cubes (1 painted face):** There is 1 in the center of each of the 6 faces = **6**.
4. **Interior cube (0 painted faces):** There is **1** cube completely inside.

*(Check: $8 + 12 + 6 + 1 = 27$ total cubes)*

Therefore, there are **12** smaller cubes with exactly two red faces.
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "To find the number of smaller cubes with **exactly two red faces**, we can analyze how the $3 \\times 3 \\times 3$ cube is structured:\n\n1. **Corner cubes (3 painted faces):** There are **8** corners in a cube.\n2. **Edge cubes (excluding corners, 2 painted faces):** A cube has 12 edges. With a $3 \\times 3 \\times 3$ cut, each edge has 1 middle cube with 2 painted faces:\n   $$\\text{Number of cubes} = 12 \\times (3 - 2) = 12$$\n3. **Face-center cubes (1 painted face):** There is 1 in the center of each of the 6 faces = **6**.\n4. **Interior cube (0 painted faces):** There is **1** cube completely inside.\n\n*(Check: $8 + 12 + 6 + 1 = 27$ total cubes)*\n\nTherefore, there are **12** smaller cubes with exactly two red faces.",
            "thoughtSignature": "AY89a18GhPrAzwD4+OG6wmLOGj2YZE0gv9PY76IqT4vccoHQerNJubk8VCyX0atC0qHPy/28tlQG5WlMvbqfovvGqrSJeNHSVC6RoyKD7SMGVnSOw83d5XDYxirhMa5XgVbtEF6O5t03QYpIyB/ice00yWw9US/osp4NOYRDYTPA6uekMJkCjFH8lNuYP4RnEFmEXHyxBtnlrcXvB2fy9bWxZEw3ck69faKWfJF/Ia0oy27SOdIaQTzcczJum9F+AnjliMkpY4EdIm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fKisu9hF2HOp/vC03Bx0Weqw6XqYMnNvDSjIKEWTImfuGJyx0MK0VS4bvQm/T7T3OmbGnSsqHPx83n10xw=="
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 31,
    "candidatesTokenCount": 228,
    "totalTokenCount": 602,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 31
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 228
      }
    ],
    "thoughtsTokenCount": 343
  },
  "modelVersion": "gemini-3.7-flash",
  "createTime": "2026-08-13T19:07:45.456897Z",
  "responseId": "ARZ-asHxG6mp3dAP9fHcoQ0"
}

Parameters

Schema variant
toolConfig
modelVersion
string

API Schemas (Raw)

Input
Output

Was this helpful?