Skip to content

Commit 0ebf962

Browse files
authored
feat(tools): runtime context access diagram (#1223)
also remove `wrap` because it looks like improperly formatted snippets
1 parent 2cddfb8 commit 0ebf962

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

src/oss/langchain/tools.mdx

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Some chat models (e.g., [OpenAI](/oss/integrations/chat/openai), [Anthropic](/os
2121
:::python
2222
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:
2323

24-
```python wrap
24+
```python
2525
from langchain.tools import tool
2626

2727
@tool
@@ -66,7 +66,7 @@ const searchDatabase = tool(
6666

6767
By default, the tool name comes from the function name. Override it when you need something more descriptive:
6868

69-
```python wrap
69+
```python
7070
@tool("web_search") # Custom name
7171
def search(query: str) -> str:
7272
"""Search the web for information."""
@@ -79,7 +79,7 @@ print(search.name) # web_search
7979

8080
Override the auto-generated tool description for clearer model guidance:
8181

82-
```python wrap
82+
```python
8383
@tool("calculator", description="Performs arithmetic calculations. Use this for any math problems.")
8484
def calc(expression: str) -> str:
8585
"""Evaluate mathematical expressions."""
@@ -91,7 +91,7 @@ def calc(expression: str) -> str:
9191
Define complex inputs with Pydantic models or JSON schemas:
9292

9393
<CodeGroup>
94-
```python wrap Pydantic model
94+
```python Pydantic model
9595
from pydantic import BaseModel, Field
9696
from typing import Literal
9797

@@ -117,7 +117,7 @@ Define complex inputs with Pydantic models or JSON schemas:
117117
return result
118118
```
119119

120-
```python wrap JSON Schema
120+
```python JSON Schema
121121
weather_schema = {
122122
"type": "object",
123123
"properties": {
@@ -149,14 +149,62 @@ Define complex inputs with Pydantic models or JSON schemas:
149149
:::python
150150
Tools can access runtime information through the `ToolRuntime` parameter, which provides:
151151

152-
- **State** - Mutable data that flows through execution (messages, counters, custom fields)
152+
- **State** - Mutable data that flows through execution (e.g., messages, counters, custom fields)
153153
- **Context** - Immutable configuration like user IDs, session details, or application-specific configuration
154154
- **Store** - Persistent long-term memory across conversations
155155
- **Stream Writer** - Stream custom updates as tools execute
156-
- **Config** - RunnableConfig for the execution
156+
- **Config** - `RunnableConfig` for the execution
157157
- **Tool Call ID** - ID of the current tool call
158158

159-
### ToolRuntime
159+
```mermaid
160+
graph LR
161+
%% Runtime Context
162+
subgraph "🔧 Tool Runtime Context"
163+
A[Tool Call] --> B[ToolRuntime]
164+
B --> C[State Access]
165+
B --> D[Context Access]
166+
B --> E[Store Access]
167+
B --> F[Stream Writer]
168+
end
169+
170+
%% Available Resources
171+
subgraph "📊 Available Resources"
172+
C --> G[Messages]
173+
C --> H[Custom State]
174+
D --> I[User ID]
175+
D --> J[Session Info]
176+
E --> K[Long-term Memory]
177+
E --> L[User Preferences]
178+
end
179+
180+
%% Tool Capabilities
181+
subgraph "⚡ Enhanced Tool Capabilities"
182+
M[Context-Aware Tools]
183+
N[Stateful Tools]
184+
O[Memory-Enabled Tools]
185+
P[Streaming Tools]
186+
end
187+
188+
%% Connections
189+
G --> M
190+
H --> N
191+
I --> M
192+
J --> M
193+
K --> O
194+
L --> O
195+
F --> P
196+
197+
%% Styling
198+
classDef runtimeStyle fill:#e3f2fd,stroke:#1976d2
199+
classDef resourceStyle fill:#e8f5e8,stroke:#388e3c
200+
classDef capabilityStyle fill:#fff3e0,stroke:#f57c00
201+
202+
class A,B,C,D,E,F runtimeStyle
203+
class G,H,I,J,K,L resourceStyle
204+
class M,N,O,P capabilityStyle
205+
```
206+
207+
### `ToolRuntime`
160208

161209
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.
162210

@@ -168,7 +216,7 @@ Use `ToolRuntime` to access all runtime information in a single parameter. Simpl
168216

169217
Tools can access the current graph state using `ToolRuntime`:
170218

171-
```python wrap
219+
```python
172220
from langchain.tools import tool, ToolRuntime
173221

174222
# Access the current conversation state
@@ -204,7 +252,7 @@ The `tool_runtime` parameter is hidden from the model. For the example above, th
204252

205253
Use @[`Command`] to update the agent's state or control the graph's execution flow:
206254

207-
```python wrap
255+
```python
208256
from langgraph.types import Command
209257
from langchain.messages import RemoveMessage
210258
from langgraph.graph.message import REMOVE_ALL_MESSAGES
@@ -239,7 +287,7 @@ Access immutable configuration and contextual data like user IDs, session detail
239287

240288
Tools can access runtime context through `ToolRuntime`:
241289

242-
```python wrap
290+
```python
243291
from dataclasses import dataclass
244292
from langchain_openai import ChatOpenAI
245293
from langchain.agents import create_agent
@@ -293,7 +341,7 @@ result = agent.invoke(
293341
:::js
294342
Tools can access an agent's runtime context through the `config` parameter:
295343

296-
```ts wrap
344+
```ts
297345
import * as z from "zod"
298346
import { ChatOpenAI } from "@langchain/openai"
299347
import { createAgent } from "langchain"
@@ -337,7 +385,7 @@ Access persistent data across conversations using the store. The store is access
337385

338386
Tools can access and update the store through `ToolRuntime`:
339387

340-
```python wrap expandable
388+
```python expandable
341389
from typing import Any
342390
from langgraph.store.memory import InMemoryStore
343391
from langchain.agents import create_agent
@@ -386,7 +434,7 @@ agent.invoke({
386434
:::js
387435
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.
388436

389-
```ts wrap expandable
437+
```ts expandable
390438
import * as z from "zod";
391439
import { createAgent, tool } from "langchain";
392440
import { InMemoryStore } from "@langchain/langgraph";
@@ -465,7 +513,7 @@ console.log(result);
465513
:::python
466514
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.
467515

468-
```python wrap
516+
```python
469517
from langchain.tools import tool, ToolRuntime
470518

471519
@tool
@@ -488,7 +536,7 @@ If you use `runtime.stream_writer` inside your tool, the tool must be invoked wi
488536
:::js
489537
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.
490538

491-
```ts wrap
539+
```ts
492540
import * as z from "zod";
493541
import { tool } from "langchain";
494542

0 commit comments

Comments
 (0)