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: src/oss/langchain/tools.mdx
+64-16Lines changed: 64 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Some chat models (e.g., [OpenAI](/oss/integrations/chat/openai), [Anthropic](/os
21
21
:::python
22
22
The simplest way to create a tool is with the @[`@tool`] decorator. By default, the function's docstring becomes the tool's description that helps the model understand when to use it:
23
23
24
-
```python wrap
24
+
```python
25
25
from langchain.tools import tool
26
26
27
27
@tool
@@ -66,7 +66,7 @@ const searchDatabase = tool(
66
66
67
67
By default, the tool name comes from the function name. Override it when you need something more descriptive:
68
68
69
-
```python wrap
69
+
```python
70
70
@tool("web_search") # Custom name
71
71
defsearch(query: str) -> str:
72
72
"""Search the web for information."""
@@ -79,7 +79,7 @@ print(search.name) # web_search
79
79
80
80
Override the auto-generated tool description for clearer model guidance:
81
81
82
-
```python wrap
82
+
```python
83
83
@tool("calculator", description="Performs arithmetic calculations. Use this for any math problems.")
Use `ToolRuntime` to access all runtime information in a single parameter. Simply add `runtime: ToolRuntime` to your tool signature, and it will be automatically injected without being exposed to the LLM.
162
210
@@ -168,7 +216,7 @@ Use `ToolRuntime` to access all runtime information in a single parameter. Simpl
168
216
169
217
Tools can access the current graph state using `ToolRuntime`:
170
218
171
-
```python wrap
219
+
```python
172
220
from langchain.tools import tool, ToolRuntime
173
221
174
222
# Access the current conversation state
@@ -204,7 +252,7 @@ The `tool_runtime` parameter is hidden from the model. For the example above, th
204
252
205
253
Use @[`Command`] to update the agent's state or control the graph's execution flow:
206
254
207
-
```python wrap
255
+
```python
208
256
from langgraph.types import Command
209
257
from langchain.messages import RemoveMessage
210
258
from langgraph.graph.message importREMOVE_ALL_MESSAGES
@@ -239,7 +287,7 @@ Access immutable configuration and contextual data like user IDs, session detail
239
287
240
288
Tools can access runtime context through `ToolRuntime`:
241
289
242
-
```python wrap
290
+
```python
243
291
from dataclasses import dataclass
244
292
from langchain_openai import ChatOpenAI
245
293
from langchain.agents import create_agent
@@ -293,7 +341,7 @@ result = agent.invoke(
293
341
:::js
294
342
Tools can access an agent's runtime context through the `config` parameter:
295
343
296
-
```ts wrap
344
+
```ts
297
345
import*aszfrom"zod"
298
346
import { ChatOpenAI } from"@langchain/openai"
299
347
import { createAgent } from"langchain"
@@ -337,7 +385,7 @@ Access persistent data across conversations using the store. The store is access
337
385
338
386
Tools can access and update the store through `ToolRuntime`:
339
387
340
-
```pythonwrap expandable
388
+
```python expandable
341
389
from typing import Any
342
390
from langgraph.store.memory import InMemoryStore
343
391
from langchain.agents import create_agent
@@ -386,7 +434,7 @@ agent.invoke({
386
434
:::js
387
435
Access persistent data across conversations using the store. The store is accessed via `config.store` and allows you to save and retrieve user-specific or application-specific data.
Stream custom updates from tools as they execute using `runtime.stream_writer`. This is useful for providing real-time feedback to users about what a tool is doing.
467
515
468
-
```python wrap
516
+
```python
469
517
from langchain.tools import tool, ToolRuntime
470
518
471
519
@tool
@@ -488,7 +536,7 @@ If you use `runtime.stream_writer` inside your tool, the tool must be invoked wi
488
536
:::js
489
537
Stream custom updates from tools as they execute using `config.streamWriter`. This is useful for providing real-time feedback to users about what a tool is doing.
0 commit comments