A practical guide to LangChain tool-calling workflows
A LangChain learning note explains how Tool exposes Python functions to models through a name, description and argument JSON Schema. It walks through the complete tool-calling loop: binding tools, receiving tool_calls, executing the selected tools in application code and returning the results as ToolMessage objects. The note also covers ToolRuntime injection, artifacts, parallel calls and common implementation errors.
The guide provides developers with a concrete implementation sequence for LangChain tool calls while clarifying that the model only proposes structured calls and application code performs the execution.
1. Tool 到底是什么
Tool 是 LangChain 中把 Python 函数包装成模型可理解、可请求调用的标准化接口。核心逻辑很简单:模型本身不执行任何代码,它只负责"说"要调什么、传什么参数;真正干活的是你的程序。
每个 Tool 必须携带三个关键信息,模型靠它们做决策:
- name:工具名,模型用它来指代要调用的函数
- description:自然语言描述,模型靠它判断"什么时候该用这个工具"
- args:参数的 JSON Schema,模型靠它知道该传什么字段、什么类型
BaseTool 是所有工具的基类,它本身继承自 Runnable,所以工具天然支持 invoke / ainvoke 调用。
2. 创建 Tool 的三种方式
方式一:@tool 装饰器(首选)
最常用、最推荐的方式。给普通 Python 函数加上装饰器,LangChain 会自动从函数签名和 docstring 推断出 name、description 和参数 schema。