一、为什么需要"思考工具"?
想象你是一名航空公司的客服,面对"我要退改签国际航班"这样的复杂请求时,会不假思索地直接操作吗?当然不会!正常人都会先:
- 确认用户身份和票务信息
- 核对退改签政策(24小时免费退?经济舱还是商务舱?)
- 检查航班状态(是否已起飞?是否有已使用航段?)
- 计算可能产生的费用
然而现有的AI助手在类似复杂场景中常常表现不佳。Anthropic团队通过τ-Bench基准测试发现,在航空客服这种政策复杂的场景中,Claude 3.7的基础版任务完成率仅有37%。
核心痛点在于:传统AI在连续工具调用(如先查票务信息→核对政策→计算费用)时,缺乏"暂停思考"的机制,容易在信息不完整时就仓促行动。
当前提升规划能力的方案对比:
| 方案类型 | 代表实现 | 特点 | 成本 |
|---|---|---|---|
| 纯Prompt工程 | - | 纯prompt工程引导显式规划,简单但效果有限 | 低 |
| Prompt+后训练 | OpenAI | 通过后训练让模型严格遵循这一指令,效果显著但需模型定制 | 高 |
| 思考工具 | Anthropic | 通过思考工具的形式提升遵循能力,指令遵循性好,适用开源模型 | 中 |
| 工程框架设计 | Manus | 通过工程框架设计,让规划和执行分离,让agent具有长程任务规划能力,适合长程任务但实现复杂 | 高 |
本文主要讨论如何使用开源模型提升模型在“快速任务”上的处理能力,这意味着,后训练和工程框架设计成本都相对较高,那么思考工具的方案可能就更加可取。「调用xx工具」是一个可明确执行和评判的指令,而「做一个规划」是一个模糊的指令,相对来说以工具的形式指令遵循效果更好,尤其是在复杂 prompt 和多工具的场景。
二、"思考工具"的创新设计
Anthropic的解决方案极具巧思——他们给Claude设计了一个专属的"思考tool",先简略介绍,在实战部分详细介绍。
{
"name": "think",
"description": "专用思考空间,仅记录思考内容不执行操作",
"input_schema": {
"type": "object",
"properties": {
"thought": {"type": "string", "description": "具体思考内容"},
"plan": {"type": "string", "description": "分解后的执行计划"},
"action": {"type": "string", "description": "下一步具体行动"},
"thoughtNumber": {"type": "string", "description": "思考步骤编号"}
},
"required": ["thought","plan","action","thoughtNumber"]
}
}
核心不在于工具调用的结果,而在于工具的输入传参,是需要模型自身来思考,通过构造输入传参的过程,实现逐步思考和规划。
这个工具的精妙之处在于:
- 零副作用:只记录思考过程不采取具体行动,避免过早决策
- 结构化思考**:强制模型将思考过程显式化输出
- 信息整合:特别适合处理外部工具返回的复杂信息
- 过程追溯:通过编号实现思考链路追踪
与推理模型(extended thinking)的关键区别:
- 推理模型:生成回答前的"腹稿",比如现有的o系列、r1系列等带思维链的模型;
- 思考工具:执行过程中的"检查点",特别是在获取新信息后。
三、效果验证
Claude团队在τ-Bench(客户服务场景基准测试)和SWE-Bench(代码修复基准)上进行了严格验证,τ-Bench的效果如下:
| 配置方案 | 首次成功率 | 提升幅度 |
|---|---|---|
| 基线版 | 37% | - |
| 仅推理模式 | 41.2% | +11% |
| 仅思考工具 | 40.4% | +9% |
| 思考工具+优化提示 | 58.4% | +54% |
从实验结果中也能看出,claude-sonnet-3.7 在非推理模式下使用思考工具,效果相比单纯的推理模式更好。[2]中有进一步分析,
- prompt 无法干预推理模型输出的思维链过程——推理模型的后训练一般只针对最终输出的结果,思维链的空间是由模型自由探索——而使用思考工具可以 prompt 大模型,提供 few shot 示例指导模型在特定的垂直领域该如何思考。
- 一般 chat template 会把推理模型的历史思考内容在上下文中删掉(Claude 是保留模型第一次调用工具前输出的思考内容,后续不再打开思考),而使用思考工具,模型可以在多工具调用的过程中任意使用,并且保留在上下文中,这对复杂的多工具调用是有益处的。
四、实践经验
根据实验结果,这些场景最适合思考工具:
- 动作密集型任务:如航空、金融等有复杂合规要求的领域
- 长链条工具调用:需要连续调用3个以上工具的任务
- 高代价决策:错误会导致严重后果的场景(如医疗咨询)
而不太适用的场景包括:
- 单次工具调用
- 简单指令执行
- 无需外部信息的推理任务
想要复现这种提升?三个要点:
- 领域示例是关键: 在系统提示中提供本领域的具体思考范例
- 思考检查点设计:在获取新信息后强制思考,特别是在关键决策前
- 适度使用:过度思考会导致响应变慢,需平衡效率与准确性
五、Qwen Agent实战
本部分介绍如何实现和应用思考工具,参考[2]。
system prompt如下:
<instruction>
1. 你是一个 agent,请持续调用工具直至完美完成用户的任务,停止调用工具后,系统会自动交还控制权给用户。请只有在确定问题已解决后才终止调用工具。
2. 请善加利用你的工具收集相关信息,绝对不要猜测或编造答案。
3. 「思考和规划」是一个系统工具,在每次调用其他任务工具之前,你必须**首先调用思考和规划工具**:针对用户的任务详细思考和规划,并对之前工具调用的结果进行深入反思(如有),输出的顺序是thought, plan, action, thoughtNumber。
- 「思考和规划」工具不会获取新信息或更改数据库,只会将你的想法保存到记忆中。
- 思考完成之后不需要等待工具返回,你可以继续调用其他任务工具,你一次可以调用多个任务工具。
- 任务工具调用完成之后,你可以停止输出,系统会把工具调用结果给你,你必须再次调用思考和规划工具,然后继续调用任务工具,如此循环,直到完成用户的任务。
</instruction>
思考工具如下:
{
"name": "思考和规划",
"id":"think_and_plan"
"description": "这是用于系统化思考与规划的工具,支持用户在面对复杂问题或任务时,分阶段梳理思考、规划和行动步骤。工具强调思考(thought)、计划(plan)与实际行动(action)的结合,通过编号(thoughtNumber)追踪过程。该工具不会获取新信息或更改数据库,只会将想法附加到记忆中。当需要复杂推理或某种缓存记忆时,可以使用它。",
"input_schema": {
"type": "object",
"properties": {
"thought": {
"type": "string",
"description": "当前的思考内容,可以是对问题的分析、假设、洞见、反思或对前一步骤的总结。强调深度思考和逻辑推演,是每一步的核心。"
},
"plan": {
"type": "string",
"description": "针对当前任务拟定的计划或方案,将复杂问题分解为多个可执行步骤。"
},
"action": {
"type": "string",
"description": "基于当前思考和计划,建议下一步采取的行动步骤,要求具体、可执行、可验证,可以是下一步需要调用的一个或多个工具。"
},
"thoughtNumber": {
"type": "string",
"description": "当前思考步骤的编号,用于追踪和回溯整个思考与规划过程,便于后续复盘与优化。"
}
},
"required": ["thought","plan","action","thoughtNumber"]
}
}
实战:让非推理模型也能具备逐步推理能力
以AI painting任务为例,模型使用的是DeepSeek-V3非推理模型,在Qwen-Agent框架下使用思考工具。工具配置了3个:
- 绘图工具 my_image_gen:用户基于prompt绘制图片并返回远程图片链接
- 代码解释器工具 code_interpreter:用于执行'下载远程图片'的代码
- 思考工具think:用于调用工具前规划
可以看到think也是一个单独tool,每次需要调用外部工具时,先调用think tool进行结构化思考和规划,输入参数包括thought思考、计划plan、action实际行动、思考的编号等参数。实际调用工具后,会进一步基于工具结果,进一步调用think进行思考和下一步规划。
可以看到一共进行了3次思考工具的调用。
- 调用my_image_gen前思考和规划;
- 返回my_image_gen结果后、调用code_interpreter前进行思考和规划;
- 任务结束前思考,检查所有步骤已经完成。
think工具实现
@register_tool('think')
class Think(BaseTool):
description = "这是用于系统化思考与规划的工具,支持用户在面对复杂问题或任务时,分阶段梳理思考、规划和行动步骤。工具强调思考(thought)、计划(plan)与实际行动(action)的结合,通过编号(thoughtNumber)追踪过程。该工具不会获取新信息或更改数据库,只会将想法附加到记忆中。当需要复杂推理或某种缓存记忆时,可以使用它。",
parameters = [{
'thought': "当前的思考内容,可以是对问题的分析、假设、洞见、反思或对前一步骤的总结。强调深度思考和逻辑推演,是每一步的核心",
'type': 'string',
'required': True,
},
{
'plan': "针对当前任务拟定的计划或方案,将复杂问题分解为多个可执行步骤。",
'type': 'string',
'required': True,
},
{
'action': "基于当前思考和计划,建议下一步采取的行动步骤,要求具体、可执行、可验证,可以是下一步需要调用的一个或多个工具",
'type': 'string',
'required': True,
},
{
'thoughtNumber': "当前思考步骤的编号,用于追踪和回溯整个思考与规划过程,便于后续复盘与优化",
'type': 'integer',
'required': True,
}]
def call(self, params: str, **kwargs) -> str:
results = json5.loads(params)
return"THINK DONE"# 任意即可
system = (
'''
system:
1. 你是一个 agent,请持续调用工具直至完美完成用户的任务,停止调用工具后,系统会自动交还控制权给用户。请只有在确定问题已解决后才终止调用工具。
2. 请善加利用你的工具收集相关信息,绝对不要猜测或编造答案。
3. 「思考和规划」是一个系统工具,在每次调用其他任务工具之前,你必须**首先调用思考和规划工具**:针对用户的任务详细思考和规划,并对之前工具调用的结果进行深入反思(如有),输出的顺序是thought, plan, action, thoughtNumber。
- 「思考和规划」工具不会获取新信息或更改数据库,只会将你的想法保存到记忆中。
- 思考完成之后不需要等待工具返回,你可以继续调用其他任务工具,你一次可以调用多个任务工具。
- 任务工具调用完成之后,你可以停止输出,系统会把工具调用结果给你,你必须再次调用思考和规划工具,然后继续调用任务工具,如此循环,直到完成用户的任务。
task:
"According to the user's request, you first draw a picture and then automatically "
'run code to download the picture and select an image operation from the given document to process the image
''')
tools = [
'my_image_gen',
'think',
'code_interpreter',
]
当然我们也可以使用现成的mcp工具,sequential-thinking,实现的功能类似,但是扩展更多“上下文回溯”能力:如思维分支和版本回溯。
{"mcpServers": {
"sequential-thinking": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-sequential-thinking"
]
}
}}
sequential-thinking工具描述:
具体工具描述实现如下,
{
"type": "function",
"function": {
"name": "sequential-thinking-sequentialthinking",
"description": "A detailed tool for dynamic and reflective problem-solving through thoughts.\nThis tool helps analyze problems through a flexible thinking process that can adapt and evolve.\nEach thought can build on, question, or revise previous insights as understanding deepens.\n\nWhen to use this tool:\n- Breaking down complex problems into steps\n- Planning and design with room for revision\n- Analysis that might need course correction\n- Problems where the full scope might not be clear initially - Problems that require a multi-step solution\n- Tasks that need to maintain context over multiple steps\n- Situations where irrelevant information needs to be filtered out\n\nKey features:\n- You can adjust total_thoughts up or down as you progress\n- You can question or revise previous thoughts\n- You can add more thoughts even after reaching what seemed like the end\n- You can express uncertainty and explore alternative approaches\n- Not every thought needs to build linearly - you can branch or backtrack\n- Generates a solution hypothesis\n- Verifies the hypothesis based on the Chain of Thought steps\n- Repeats the process until satisfied\n- Provides a correct answer\n\nParameters explained:\n- thought: Your current thinking step, which can include:\n Regular analytical steps\n Revisions of previous thoughts\n Questions about previous decisions\n Realizations about needing more analysis\n Changes in approach\n Hypothesis generation\n* Hypothesis verification\n- next_thought_needed: True if you need more thinking, even if at what seemed like the end\n- thought_number: Current number in sequence (can go beyond initial total if needed)\n- total_thoughts: Current estimate of thoughts needed (can be adjusted up/down)\n- is_revision: A boolean indicating if this thought revises previous thinking\n- revises_thought: If is_revision is true, which thought number is being reconsidered\n- branch_from_thought: If branching, which thought number is the branching point\n- branch_id: Identifier for the current branch (if any)\n- needs_more_thoughts: If reaching end but realizing more thoughts needed\n\nYou should:\n1. Start with an initial estimate of needed thoughts, but be ready to adjust\n2. Feel free to question or revise previous thoughts\n3. Don't hesitate to add more thoughts if needed, even at the end \n4. Express uncertainty when present\n5. Mark thoughts that revise previous thinking or branch into new paths\n6. Ignore information that is irrelevant to the current step\n7. Generate a solution hypothesis when appropriate\n8. Verify the hypothesis based on the Chain of Thought steps\n9. Repeat the process until satisfied with the solution\n10. Provide a single, ideally correct answer as the final output\n11. Only set next_thought_needed to false when truly done and a satisfactory answer is reached",
"parameters": {
"type": "object",
"properties": {
"thought": {
"type": "string",
"description": "Your current thinking step"
},
"nextThoughtNeeded": {
"type": "boolean",
"description": "Whether another thought step is needed"
},
"thoughtNumber": {
"type": "integer",
"description": "Current thought number",
"minimum": 1
},
"totalThoughts": {
"type": "integer",
"description": "Estimated total thoughts needed",
"minimum": 1
},
"isRevision": {
"type": "boolean",
"description": "Whether this revises previous thinking"
},
"revisesThought": {
"type": "integer",
"description": "Which thought is being reconsidered",
"minimum": 1
},
"branchFromThought": {
"type": "integer",
"description": "Branching point thought number",
"minimum": 1
},
"branchId": {
"type": "string",
"description": "Branch identifier"
},
"needsMoreThoughts": {
"type": "boolean",
"description": "If more thoughts are needed"
}
},
"required": [
"thought",
"nextThoughtNeeded",
"thoughtNumber",
"totalThoughts"
]
}
}
}
翻译一下工具描述,教科书式工具描述。
总结
本文从Anthropic和阿里的两篇文章出发,介绍了"思考工具"的设计动机、实现方案、效果验证以及Qwen Agent上的实战。将思考封装成工具(think tool)提升agent的planning能力,极具巧思,大家实践过程中不妨试试。
- 思考工具化:将抽象思考过程转化为可调用的结构化工具,实现"显式思考"
- 暂停机制:在关键决策点强制暂停,避免信息不完整时的仓促行动
- 思维外化:完整保留思考轨迹,支持复杂任务的上下文回溯
- 遵循能力: 工具参数化的方式,指令化强制遵循
- 领域适配:相比于通用思维链,可以在思考工具中注入领域知识