|
| 1 | +# Agent Tools |
| 2 | + |
| 3 | +Tools are Python functions that the LangGraph agent can call. They can read and update agent state, fetch data, and perform any server-side logic. |
| 4 | + |
| 5 | +## Creating a Tool |
| 6 | + |
| 7 | +Use the `@tool` decorator from LangChain: |
| 8 | + |
| 9 | +```python |
| 10 | +from langchain.tools import tool, ToolRuntime |
| 11 | + |
| 12 | +@tool |
| 13 | +def my_tool(arg1: str, arg2: int, runtime: ToolRuntime): |
| 14 | + """Description of what this tool does. The agent reads this to decide when to call it.""" |
| 15 | + return f"Result: {arg1} x {arg2}" |
| 16 | +``` |
| 17 | + |
| 18 | +- The docstring tells the agent when and how to use the tool |
| 19 | +- `runtime: ToolRuntime` gives access to agent state (optional parameter) |
| 20 | +- Return value is sent back to the agent as the tool result |
| 21 | + |
| 22 | +## Reading State |
| 23 | + |
| 24 | +Access current agent state via `runtime.state`: |
| 25 | + |
| 26 | +```python |
| 27 | +# apps/agent/src/todos.py |
| 28 | + |
| 29 | +@tool |
| 30 | +def get_todos(runtime: ToolRuntime): |
| 31 | + """Get the current todos.""" |
| 32 | + return runtime.state.get("todos", []) |
| 33 | +``` |
| 34 | + |
| 35 | +## Updating State |
| 36 | + |
| 37 | +Return a `Command` with an `update` dict to modify agent state: |
| 38 | + |
| 39 | +```python |
| 40 | +from langgraph.types import Command |
| 41 | +from langchain.messages import ToolMessage |
| 42 | + |
| 43 | +@tool |
| 44 | +def manage_todos(todos: list[Todo], runtime: ToolRuntime) -> Command: |
| 45 | + """Manage the current todos.""" |
| 46 | + # Ensure all todos have unique IDs |
| 47 | + for todo in todos: |
| 48 | + if "id" not in todo or not todo["id"]: |
| 49 | + todo["id"] = str(uuid.uuid4()) |
| 50 | + |
| 51 | + return Command(update={ |
| 52 | + "todos": todos, |
| 53 | + "messages": [ |
| 54 | + ToolMessage( |
| 55 | + content="Successfully updated todos", |
| 56 | + tool_call_id=runtime.tool_call_id |
| 57 | + ) |
| 58 | + ] |
| 59 | + }) |
| 60 | +``` |
| 61 | + |
| 62 | +Key points: |
| 63 | +- `Command(update={...})` merges the update into agent state |
| 64 | +- Include a `ToolMessage` in the `messages` list to acknowledge the tool call |
| 65 | +- Use `runtime.tool_call_id` for the message's `tool_call_id` |
| 66 | + |
| 67 | +## Returning Data (No State Update) |
| 68 | + |
| 69 | +For tools that just return data without modifying state, return the value directly: |
| 70 | + |
| 71 | +```python |
| 72 | +# apps/agent/src/query.py |
| 73 | + |
| 74 | +import csv |
| 75 | +from pathlib import Path |
| 76 | +from langchain.tools import tool |
| 77 | + |
| 78 | +# Load data at module init |
| 79 | +_data = [] |
| 80 | +with open(Path(__file__).parent / "db.csv") as f: |
| 81 | + _data = list(csv.DictReader(f)) |
| 82 | + |
| 83 | +@tool |
| 84 | +def query_data(query: str): |
| 85 | + """Query the financial transactions database. Call this before creating charts.""" |
| 86 | + return _data |
| 87 | +``` |
| 88 | + |
| 89 | +## Registering Tools with the Agent |
| 90 | + |
| 91 | +Add tools to the agent's `tools` list in `apps/agent/main.py`: |
| 92 | + |
| 93 | +```python |
| 94 | +from src.todos import todo_tools # [manage_todos, get_todos] |
| 95 | +from src.query import query_data |
| 96 | +from src.plan import plan_visualization |
| 97 | + |
| 98 | +agent = create_deep_agent( |
| 99 | + model=ChatOpenAI(model="gpt-5.4-2026-03-05"), |
| 100 | + tools=[query_data, plan_visualization, *todo_tools], |
| 101 | + middleware=[CopilotKitMiddleware()], |
| 102 | + context_schema=AgentState, |
| 103 | + ... |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +You can pass individual tools or spread a list of tools. |
| 108 | + |
| 109 | +## Example: Adding a New Tool |
| 110 | + |
| 111 | +Say you want to add a tool that fetches weather data: |
| 112 | + |
| 113 | +**1. Create the tool** (`apps/agent/src/weather.py`): |
| 114 | + |
| 115 | +```python |
| 116 | +from langchain.tools import tool |
| 117 | + |
| 118 | +@tool |
| 119 | +def get_weather(city: str): |
| 120 | + """Get the current weather for a city.""" |
| 121 | + # Your implementation here |
| 122 | + return {"city": city, "temp": 72, "condition": "sunny"} |
| 123 | +``` |
| 124 | + |
| 125 | +**2. Register it** in `apps/agent/main.py`: |
| 126 | + |
| 127 | +```python |
| 128 | +from src.weather import get_weather |
| 129 | + |
| 130 | +agent = create_deep_agent( |
| 131 | + tools=[query_data, plan_visualization, *todo_tools, get_weather], |
| 132 | + ... |
| 133 | +) |
| 134 | +``` |
| 135 | + |
| 136 | +The agent can now call `get_weather` when a user asks about weather. If you want a custom UI for the result, register a `useRenderTool` on the frontend (see [Generative UI](generative-ui.md)). |
| 137 | + |
| 138 | +## Next Steps |
| 139 | + |
| 140 | +- [Agent State](agent-state.md) — How state sync works between tools and the frontend |
| 141 | +- [Generative UI](generative-ui.md) — Render custom UI for tool results |
0 commit comments