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:
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.
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.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. Tools can access an agent’s runtime context through theconfig parameter:
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 config.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. Useconfig.writer to emit custom updates:
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
Use @[tools_condition] for conditional routing based on whether the LLM made tool calls:
State injection
Tools can access the current graph state through @[ToolRuntime]:
For more details on accessing state, context, and long-term memory from tools, see Access context.
Prebuilt tools
LangChain provides a large collection of prebuilt tools and toolkits for common tasks like web search, code interpretation, database access, and more. These ready-to-use tools can be directly integrated into your agents without writing custom code. See the tools and toolkits integration page for a complete list of available tools organized by category.Server-side tool use
Some chat models feature built-in tools that are executed server-side by the model provider. These include capabilities like web search and code interpreters that don’t require you to define or host the tool logic. Refer to the individual chat model integration pages and the tool calling documentation for details on enabling and using these built-in tools.Connect these docs to Claude, VSCode, and more via MCP for real-time answers.