Create tools
Basic tool definition
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:
Server-side tool use: Some chat models feature built-in tools (web search, code interpreters) that are executed server-side. See Server-side tool use for details.
Customize tool properties
Custom tool name
By default, the tool name comes from the function name. Override it when you need something more descriptive:Custom tool description
Override the auto-generated tool description for clearer model guidance:Advanced schema definition
Define complex inputs with Pydantic models or JSON schemas:Reserved argument names
The following parameter names are reserved and cannot be used as tool arguments. Using these names will cause runtime errors.
To access runtime information, use the
ToolRuntime parameter instead of naming your own arguments config or runtime.
Access context
Tools are most powerful when they can access runtime information like conversation history, user data, and persistent memory. This section covers how to access and update this information from within your tools. Tools can access runtime information through theToolRuntime parameter, which provides:
Short-term memory (State)
State represents short-term memory that exists for the duration of a conversation. It includes the message history and any custom fields you define in your graph state.Add
runtime: ToolRuntime to your tool signature to access state. This parameter is automatically injected and hidden from the LLM - it won’t appear in the tool’s schema.Access state
Tools can access the current conversation state usingruntime.state:
Update state
UseCommand to update the agent’s state. This is useful for tools that need to update custom state fields:
Context
Context provides immutable configuration data that is passed at invocation time. Use it for user IDs, session details, or application-specific settings that shouldn’t change during a conversation. Access context throughruntime.context:
Long-term memory (Store)
TheBaseStore provides persistent storage that survives across conversations. Unlike state (short-term memory), data saved to the store remains available in future sessions.
Access the store through runtime.store. The store uses a namespace/key pattern to organize data:
Stream writer
Stream real-time updates from tools during execution. This is useful for providing progress feedback to users during long-running operations. Useruntime.stream_writer to emit custom updates:
If you use
runtime.stream_writer inside your tool, the tool must be invoked within a LangGraph execution context. See Streaming for more details.ToolNode
ToolNode is a prebuilt node that executes tools in LangGraph workflows. It handles parallel tool execution, error handling, and state injection automatically.
For custom workflows where you need fine-grained control over tool execution patterns, use
ToolNode instead of create_agent. It’s the building block that powers agent tool execution.Basic usage
Error handling
Configure how tool errors are handled. See theToolNode API reference for all options.
Route with tools_condition
Usetools_condition for conditional routing based on whether the LLM made tool calls:
State injection
Tools can access the current graph state throughToolRuntime: