You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/agents/tools.md
+42-17
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,9 @@
2
2
3
3
[Tools](https://python.langchain.com/docs/concepts/tools/) are a way to encapsulate a function and its input schema in a way that can be passed to a chat model that supports tool calling. This allows the model to request the execution of this function with specific inputs.
4
4
5
-
## Create tools
5
+
You can either [define your own tools](#define-simple-tools) or use [prebuilt integrations](#prebuilt-tools) that LangChain provides.
6
+
7
+
## Define simple tools
6
8
7
9
You can pass a vanilla function to `create_react_agent` to use as a tool:
8
10
@@ -21,7 +23,7 @@ create_react_agent(
21
23
22
24
## Customize tools
23
25
24
-
If you want more control over how the tool is created, you can use `@tool` decorator:
26
+
For more control over tool behavior, use the`@tool` decorator:
For even more control and creating custom LangChain tools, see [this guide](https://python.langchain.com/docs/how_to/custom_tools/).
60
+
For additional customization, refer to the [custom tools guide](https://python.langchain.com/docs/how_to/custom_tools/).
59
61
60
62
## Hide arguments from the model
61
63
62
-
There are cases where certain arguments need to be passed to a tool at runtime but should not be generated by the model itself. In LangGraph's `create_react_agent`, you might want to pass agent [state](./context.md#via-state-tools) or [config](./context.md#via-config-tools) to the tools.
64
+
Some tools require runtime-only arguments (e.g., user ID or session context) that should not be controllable by the model.
65
+
66
+
You can put these arguments in the `state` or `config` of the agent, and access
67
+
this information inside the tool:
63
68
64
69
```python
65
70
from langgraph.prebuilt import InjectedState
@@ -82,9 +87,12 @@ def my_tool(
82
87
...
83
88
```
84
89
85
-
## Parallel tool calling
90
+
## Disable parallel tool calling
91
+
92
+
Some model providers support executing multiple tools in parallel, but
93
+
allow users to disable this feature.
86
94
87
-
Some model providers allow their models to request execution of several tools at once and allow users to explicitly enable or disable parallel tool calling. You can control this in `create_react_agent` via passing a `ChatModel` instance with bound tools:
95
+
For supported providers, you can disable parallel tool calling by setting `parallel_tool_calls=False` via the `model.bind_tools()` method:
88
96
89
97
```python
90
98
from langchain.chat_models import init_chat_model
@@ -111,7 +119,7 @@ agent.invoke({"messages": "what's 3 + 5 and 4 * 7? make both calculations in par
111
119
112
120
## Return tool results directly
113
121
114
-
`create_react_agent` allows you to return tool response directly and end the tool-calling loop early. To do so, you must specify `return_direct=True` when creating your tool:
122
+
Use `return_direct=True`to return tool results immediately and stop the agent loop:
Similar to parallel tool calling, certain providers allow users to specify `tool_choice`, indicating if the agent is always required to call a certain tools (or any tool). You can similarly control this via `model.bind_tools()`.
136
-
137
-
!!! Warning
138
-
139
-
If you set `tool_choice` to always call a particular tool, you will end up with an infinite tool-calling loop, as the stopping condition for `create_react_agent` is the absence of tool calls requested by an LLM. To address this you have a few options:
140
-
141
-
- mark your tool as `return_direct=True`. This will stop the agent loop after the the tool is executed for the first time (used in example below).
142
-
- specify `recursion_limit` when invoking the agent. This will limit the maximum number of graph steps that the agent can take before hitting an GraphRecursionError.
143
+
To force the agent to use specific tools, you can set the `tool_choice` option in `model.bind_tools()`:
143
144
144
145
```python
145
146
from langchain_core.tools import tool
@@ -159,4 +160,28 @@ agent = create_react_agent(
159
160
)
160
161
161
162
agent.invoke({"messages": "Hi, I am Bob"})
162
-
```
163
+
```
164
+
165
+
!!! Warning "Avoid infinite loops"
166
+
167
+
Forcing tool usage without stopping conditions can create infinite loops. Use one of the following safeguards:
168
+
169
+
- Mark the tool with [`return_direct=True`](#return-tool-results-directly) to end the loop after execution.
170
+
- Set [`recursion_limit`](../concepts/low_level.md#recursion-limit) to restrict the number of execution steps.
171
+
172
+
## Prebuilt tools
173
+
174
+
LangChain supports a wide range of prebuilt tool integrations for interacting with APIs, databases, file systems, web data, and more. These tools extend the functionality of agents and enable rapid development.
175
+
176
+
You can browse the full list of available integrations in the [LangChain integrations directory](https://python.langchain.com/docs/integrations/tools/).
177
+
178
+
Some commonly used tool categories include:
179
+
180
+
-**Search**: Bing, SerpAPI, Tavily
181
+
-**Code interpreters**: Python REPL, Node.js REPL
182
+
-**Databases**: SQL, MongoDB, Redis
183
+
-**Web data**: Web scraping and browsing
184
+
-**APIs**: OpenWeatherMap, NewsAPI, and others
185
+
186
+
These integrations can be configured and added to your agents using the same `tools` parameter shown in the examples above.
0 commit comments