Skip to content

Commit 82c9c92

Browse files
eyurtsevvbarda
andauthored
docs(agents): tools page (#4287)
Co-authored-by: Vadym Barda <vadym@langchain.dev>
1 parent 49b7380 commit 82c9c92

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

docs/docs/agents/tools.md

+42-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
[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.
44

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
68

79
You can pass a vanilla function to `create_react_agent` to use as a tool:
810

@@ -21,7 +23,7 @@ create_react_agent(
2123

2224
## Customize tools
2325

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:
2527

2628
```python
2729
# highlight-next-line
@@ -39,7 +41,7 @@ def multiply(a: int, b: int) -> int:
3941
return a * b
4042
```
4143

42-
A common pattern is defining a custom tool input schema as a Pydantic model:
44+
You can also define a custom input schema using Pydantic:
4345

4446
```python
4547
from pydantic import BaseModel, Field
@@ -55,11 +57,14 @@ def multiply(a: int, b: int) -> int:
5557
return a * b
5658
```
5759

58-
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/).
5961

6062
## Hide arguments from the model
6163

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:
6368

6469
```python
6570
from langgraph.prebuilt import InjectedState
@@ -82,9 +87,12 @@ def my_tool(
8287
...
8388
```
8489

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.
8694

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:
8896

8997
```python
9098
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
111119

112120
## Return tool results directly
113121

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:
115123

116124
```python
117125
from langchain_core.tools import tool
@@ -132,14 +140,7 @@ agent.invoke({"messages": "what's 3 + 5?"})
132140

133141
## Force tool use
134142

135-
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()`:
143144

144145
```python
145146
from langchain_core.tools import tool
@@ -159,4 +160,28 @@ agent = create_react_agent(
159160
)
160161

161162
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.
187+

0 commit comments

Comments
 (0)