Gemini 思考设置
本页说明 Gemini 模型的思考默认值与调节方式。模型 ID 与接口地址见 Gemini 系列概览。
要点除 Flash-Lite 外,Gemini 模型默认开启思考,复杂问题可能耗时 1 分钟以上。对速度敏感的场景请降低思考量。
Gemini 模型在回答前会先进行思考。思考消耗的 token 计入输出并按输出单价计费,数量见 usageMetadata.thoughtsTokenCount(OpenAI 兼容接口为 usage.completion_tokens_details.reasoning_tokens)。
实测在同一道数学题上,thinkingLevel: "low" 可将 Gemini 3.5 Flash 的思考 token 从约 6000 降至约 4600,耗时也相应缩短。thinkingBudget: 0 可关闭 3.5 Flash、3 Flash Preview 的思考;3.1 Pro Preview、3.8 Flash 传入后仍会思考,无法完全关闭。
Python(google-genai)
gemini_thinking.py
import os
from google import genai
from google.genai import types
client = genai.Client(
api_key=os.environ["OFOX_API_KEY"],
http_options={"api_version": "v1beta", "base_url": "https://api.ofox.ai/gemini"},
)
resp = client.models.generate_content(
model="google/gemini-3.8-flash",
contents="找出所有小于 50 的正整数 n,使 n² + n + 41 不是质数。",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="low"),
),
)
print(resp.text)
print("思考 token:", resp.usage_metadata.thoughts_token_count or 0)thinkingLevel: "low"时,模型对简单问题可能不进行思考,thoughtsTokenCount不出现。- 需要查看思考过程时,在
thinkingConfig中加入"includeThoughts": true,parts中带有"thought": true的条目为思考摘要,其余为回答。 - 思考 token 计入
maxOutputTokens。上限设置过小时,回答会在中途截断,见常见报错。 thinkingConfig位于generationConfig内。
最后更新于