Alpha Notice: These docs cover the v1-alpha release. Content is incomplete and subject to change.For the latest stable version, see the v0 LangChain Python or LangChain JavaScript docs.
Create tools
Basic tool definition
The simplest way to create a tool is by importing thetool
function from the langchain
package. You can use zod to define the tool’s input schema:
schema
property as a JSON schema object:
Use tools with agents
Agents go beyond simple tool binding by adding reasoning loops, state management, and multi-step execution.To see examples of how to use tools with agents, see Agents.
Advanced tool patterns
ToolNode
ToolNode is a prebuilt LangGraph component that handles tool calls within an agent’s workflow. It works seamlessly withcreateAgent()
, offering advanced tool execution control, built in parallelism, and error handling.
Configuration options
ToolNode
accepts the following parameters:
tools
A list of LangChain
tool
objects.handleToolErrors
Controls how tool execution failures are handled.
Can be:
boolean
((error: unknown, toolCall: ToolCall) => ToolMessage | undefined)
true
Error handling strategies
ToolNode
provides built-in error handling for tool execution through its handleToolErrors
property.
To customize the error handling behavior, you can configure handleToolErrors
to either be a boolean
or a custom error handler function:
true
: Catch all errors and return aToolMessage
with the default error template containing the exception details. (default)false
: Disable error handling entirely, allowing exceptions to propagate.((error: unknown, toolCall: ToolCall) => ToolMessage | undefined)
: Catch all errors and return aToolMessage
with the result of calling the function with the exception.
Agent creation
Pass a configuredToolNode
directly to createAgent()
:
ToolNode
to createAgent()
, the agent uses your exact configuration including error handling, custom names, and tags. This is useful when you need fine-grained control over tool execution behavior.
State, context, and memory
Accessing runtime context inside a tool
Accessing runtime context inside a tool
runtime
: The execution environment of your agent, containing immutable configuration and contextual data that persists throughout the agent’s execution (e.g., user IDs, session details, or application-specific configuration).config
parameter:Accessing long-term memory inside a tool
Accessing long-term memory inside a tool
store
: LangChain’s persistence layer. An agent’s long-term memory store, e.g. user-specific or application-specific data stored across conversations.InMemoryStore
to store long-term memory:Updating long-term memory inside a tool
Updating long-term memory inside a tool
To update long-term memory, you can use the
.put()
method of InMemoryStore
. A complete example of persistent memory across sessions: