Gemini 思考

The Gemini 3 and 2.5 series models use an internal "thinking process" that significantly improves their reasoning and multi-step planning abilities, making them highly effective for complex tasks such as coding, advanced mathematics, and data analysis.

本指南介绍了如何使用 Gemini API 来使用 Gemini 的思考功能。

使用思考功能生成内容

使用思考模型发起请求与其他任何内容生成请求类似。主要区别在于在 支持思考的模型model 字段中指定一个:

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="Explain the concept of Occam's Razor and provide a simple, everyday example."
)
print(interaction.steps[-1].content[0].text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Explain the concept of Occam's Razor and provide a simple, everyday example."
});
console.log(interaction.steps.at(-1).content[0].text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "Explain the concept of Occam'\''s Razor and provide a simple example."
  }'

思考总结

思考总结可让您深入了解模型的内部推理过程。 默认情况下,系统只会返回最终输出。您可以使用 thinking_summaries 启用思考总结:

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="What is the sum of the first 50 prime numbers?",
    generation_config={
        "thinking_summaries": "auto"
    }
)

for step in interaction.steps:
    if step.type == "thought":
        print("Thought summary:")
        for content_block in step.summary:
            if content_block.type == "text":
                print(content_block.text)
        print()
    elif step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print("Answer:")
                print(content_block.text)
                print()

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "What is the sum of the first 50 prime numbers?",
    generationConfig: {
        thinkingSummaries: "auto"
    }
});

for (const step of interaction.steps) {
    if (step.type === "thought") {
        console.log("Thought summary:");
        for (const contentBlock of step.summary) {
            if (contentBlock.type === "text") console.log(contentBlock.text);
        }
    } else if (step.type === "model_output") {
        for (const contentBlock of step.content) {
            if (contentBlock.type === "text") {
                console.log("Answer:");
                console.log(contentBlock.text);
            }
        }
    }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "What is the sum of the first 50 prime numbers?",
    "generationConfig": {
      "thinkingSummaries": "auto"
    }
  }'

使用思考功能进行流式传输

您可以使用流式传输在生成期间接收增量思考总结。 这将返回滚动增量总结,因为它们是生成的:

Python

from google import genai

client = genai.Client()

prompt = """
Alice, Bob, and Carol each live in a different house on the same street: red, green, and blue.
Alice does not live in the red house.
Bob does not live in the green house.
Carol does not live in the red or green house.
Which house does each person live in?
"""

thoughts = ""
answer = ""

stream = client.interactions.create(
    model="gemini-3-flash-preview",
    input=prompt,
    generation_config={
        "thinking_summaries": "auto"
    },
    stream=True
)

thoughts = ""
answer = ""

for event in stream:
    if event.event_type == "step.delta":
        if event.delta.type == "thought":
            if not thoughts:
                print("Thinking...")
            summary_text = getattr(event.delta, 'text', '')
            print(f"[Thought] {summary_text}", end="")
            thoughts += summary_text
        elif event.delta.type == "text" and event.delta.text:
            if not answer:
                print("\nAnswer:")
            print(event.delta.text, end="")
            answer += event.delta.text

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const prompt = `Alice, Bob, and Carol each live in a different house on the same
street: red, green, and blue. Alice does not live in the red house.
Bob does not live in the green house.
Carol does not live in the red or green house.
Which house does each person live in?`;

let thoughts = "";
let answer = "";

const stream = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: prompt,
    generationConfig: {
        thinkingSummaries: "auto"
    },
    stream: true
});

for await (const event of stream) {
    if (event.type === "step.delta") {
        if (event.delta.type === "thought") {
            if (!thoughts) console.log("Thinking...");
            process.stdout.write(`[Thought] ${event.delta.text || ""}`);
            thoughts += event.delta.text || "";
        } else if (event.delta.type === "text" && event.delta.text) {
            if (!answer) console.log("\nAnswer:");
            process.stdout.write(event.delta.text);
            answer += event.delta.text;
        }
    }
}

控制思考

Gemini 模型默认采用动态思考,根据请求的复杂程度自动调整推理工作量。不过,您可以使用配置参数来控制此行为。

思考等级 (Gemini 3)

对于 Gemini 3 模型及更高版本,我们建议使用 thinking_level 参数,该参数可让您控制推理行为。

思考等级 Gemini 3.1 Pro Gemini 3 Pro(已废弃 Gemini 3 Flash 说明
minimal 不支持 不支持 支持 与大多数查询的“无思考”设置匹配。对于复杂的编码任务,模型可能会进行非常少的思考。最大限度地减少聊天或高吞吐量应用的延迟。请注意,minimal 并不能保证思考功能处于关闭状态。
low 支持 支持 支持 最大限度地减少延迟和费用。最适合指令遵循、聊天或高吞吐量应用。
medium 支持 不支持 支持 针对大多数任务进行平衡思考。
high 支持(默认、动态) 支持(默认、动态) 支持(默认、动态) 最大限度地提高推理深度。模型可能需要更长的时间才能 生成第一个(非思考)输出 token,但输出将经过更仔细的推理。

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="Provide a list of 3 famous physicists and their key contributions",
    generation_config={
        "thinking_level": "low"
    }
)
print(interaction.steps[-1].content[0].text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Provide a list of 3 famous physicists and their key contributions",
    generationConfig: {
        thinkingLevel: "low"
    }
});
console.log(interaction.steps.at(-1).content[0].text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "Provide a list of 3 famous physicists and their key contributions",
    "generation_config": {
      "thinking_level": "low"
    }
  }'

您无法为 Gemini 3 Pro 停用思考功能。Gemini 3 Flash 也不支持完全停用思考功能,但 minimal 设置意味着模型可能不会思考(尽管它仍然有可能思考)。

思考特征

Gemini API 是无状态的,因此模型会独立处理每个 API 请求,并且无法访问多轮交互中之前轮次的思考上下文。

为了能够在多轮交互中保持思考上下文,Gemini 会返回思考特征,这些特征是模型内部思考过程的加密表示形式。

  • Gemini 2.5 模型 会在启用思考功能且 请求包含 函数调用(具体来说是 函数声明)时返回思考特征。
  • Gemini 3 模型 可能会针对所有类型的 部件返回思考特征。 我们建议您始终按收到的方式传递所有特征,但函数调用特征是 必需的 。如需了解详情,请参阅 思考特征页面。
  • 特征由模型在回答的其他部分(例如函数调用或文本部分)中返回。在后续对话轮次中,将包含所有部分的完整回答 返回给模型。
  • 请勿将包含特征的部分串联在一起。
  • 请勿将包含签名的部分与不包含签名的部分合并。

价格

启用思考功能后,回答价格是输出 token 和思考 token 的总和。您可以从 total_thought_tokens 字段获取生成的思考 token 总数。

Python

# ...
print("Thoughts tokens:", interaction.usage.total_thought_tokens)
print("Output tokens:", interaction.usage.total_output_tokens)

JavaScript

// ...
console.log(`Thoughts tokens: ${interaction.usage.totalThoughtTokens}`);
console.log(`Output tokens: ${interaction.usage.totalOutputTokens}`);

思考模型会生成完整的思考内容,以提高最终回答的质量,然后输出 总结,以便深入了解思考过程。价格基于模型需要生成的完整思考 token,尽管 API 仅输出总结。

如需详细了解 token,请参阅 token 计数 指南。

最佳实践

本部分提供了有关高效使用思考模型的指南。

调试和引导

  • 查看推理:如果您没有从 思考模型中获得预期的回答,仔细分析 Gemini 的思考总结可能会有所帮助。 您可以了解它是如何分解任务并得出结论的,并使用该信息来纠正,以获得正确的结果。

  • 在推理中提供指导:如果您希望获得特别长的 输出,不妨在提示中提供指导,以限制模型使用的 思考量。这样,您就可以为回答预留更多 token 输出。

任务复杂程度

  • 简单任务(思考功能可以关闭): 对于不需要复杂推理的简单请求,不需要思考功能。示例:
    • “DeepMind 是在哪里成立的?”
    • 这封电子邮件是要求开会还是仅提供信息?
  • 中等任务(默认/部分思考): 许多常见请求都受益于一定程度的逐步处理或更深入的理解。示例:
    • 将光合作用比作成长。
    • 比较和对比电动汽车和混合动力汽车。
  • 困难任务(最大思考能力): 对于真正复杂的挑战,我们建议设置较高的思考预算。这些类型的任务需要模型充分发挥其推理和规划能力。示例:
    • 解决 AIME 2025 中的问题 1:求所有整数基数 b > 9 的和...
    • 为可视化实时股票市场数据的 Web 应用编写 Python 代码,包括用户身份验证。尽可能提高效率。

支持的模型

模型 默认思考 支持的等级
gemini-3.1-pro-preview 开启(高) 低、中、高
gemini-3-flash-preview 开启(高) 极低、低、中、高
gemini-3-pro-preview 开启(高) 低、高
gemini-2.5-pro 开启 预算:128-32768
gemini-2.5-flash 开启 预算:0-24576
gemini-2.5-flash-lite 关闭 预算:0-24576

思考模型适用于 Gemini 的所有工具和功能。这使模型能够与外部系统互动、执行代码或访问实时信息,并将结果纳入其推理中。

后续步骤