Tools extend what agents can do—letting them fetch real-time data, execute code, query external databases, and take actions in the world.Under the hood, tools are callable functions with well-defined inputs and outputs that get passed to a chat model. The model decides when to invoke a tool based on the conversation context, and what input arguments to provide.
For details on how models handle tool calls, see Tool calling. Trace tool calls and debug errors with LangSmith. Follow the tracing quickstart to get set up.We recommend you also set up LangSmith Engine which monitors your traces, detects issues, and proposes fixes.
The simplest way to create a tool is by importing the tool function from the langchain package. You can use zod to define the tool’s input schema:
import * as z from "zod"import { tool } from "langchain"const searchDatabase = tool( ({ query, limit }) => `Found ${limit} results for '${query}'`, { name: "search_database", description: "Search the customer database for records matching the query.", schema: z.object({ query: z.string().describe("Search terms to look for"), limit: z.number().describe("Maximum number of results to return"), }), });
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.
Prefer snake_case for tool names (e.g., web_search instead of Web Search). Some model providers have issues with or reject names containing spaces or special characters with errors. Sticking to alphanumeric characters, underscores, and hyphens helps to improve compatibility across providers.
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 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.
While thread_id (passed via config={"configurable": {"thread_id": ...}}) scopes the conversation: message history and checkpoints, context carries per-run data your tools and middleware read at invocation time. In production you typically pass both together: a stable thread_id per conversation, and a context object on every invoke.
Tools can access an agent’s runtime context through the config parameter. Pass context alongside a thread_id so the conversation is persisted across turns:
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "google-genai:gemini-3.5-flash" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "openai:gpt-5.5" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "anthropic:claude-sonnet-4-6" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "openrouter:openrouter:z-ai/glm-5.2" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "fireworks:accounts/fireworks/models/glm-5p2" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "baseten:zai-org/GLM-5.2" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
import * as z from "zod";import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";const getUserName = tool( (_, config) => { return config.context.user_name; }, { name: "get_user_name", description: "Get the user's name.", schema: z.object({}), },);const contextSchema = z.object({ user_name: z.string(),});const agent = createAgent({ model: new ChatOpenAI({ model: "ollama:north-mini-code-1.0" }), tools: [getUserName], contextSchema,});const result = await agent.invoke( { messages: [{ role: "user", content: "What is my name?" }], }, { configurable: { thread_id: crypto.randomUUID() }, context: { user_name: "John Smith" }, },);
The BaseStore 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:
import * as z from "zod";import { createAgent, tool } from "langchain";import { InMemoryStore } from "@langchain/langgraph";import { ChatOpenAI } from "@langchain/openai";const store = new InMemoryStore();// Access memoryconst getUserInfo = tool( async ({ user_id }) => { const value = await store.get(["users"], user_id); console.log("get_user_info", user_id, value); return value; }, { name: "get_user_info", description: "Look up user info.", schema: z.object({ user_id: z.string(), }), });// Update memoryconst saveUserInfo = tool( async ({ user_id, name, age, email }) => { console.log("save_user_info", user_id, name, age, email); await store.put(["users"], user_id, { name, age, email }); return "Successfully saved user info."; }, { name: "save_user_info", description: "Save user info.", schema: z.object({ user_id: z.string(), name: z.string(), age: z.number(), email: z.string(), }), });const agent = createAgent({ model: new ChatOpenAI({ model: "gpt-5.5" }), tools: [getUserInfo, saveUserInfo], store,});// First session: save user infoawait agent.invoke({ messages: [ { role: "user", content: "Save the following user: userid: abc123, name: Foo, age: 25, email: foo@langchain.dev", }, ],});// Second session: get user infoconst result = await agent.invoke({ messages: [ { role: "user", content: "Get user info for user with id 'abc123'" }, ],});console.log(result);// Here is the user info for user with ID "abc123":// - Name: Foo// - Age: 25// - Email: foo@langchain.dev
Stream real-time updates from tools during execution. This is useful for providing progress feedback to users during long-running operations.Use config.writer to emit custom updates:
import * as z from "zod";import { tool, ToolRuntime } from "langchain";const getWeather = tool( ({ city }, config: ToolRuntime) => { const writer = config.writer; // Stream custom updates as the tool executes if (writer) { writer(`Looking up data for city: ${city}`); writer(`Acquired data for city: ${city}`); } return `It's always sunny in ${city}!`; }, { name: "get_weather", description: "Get weather for a given city.", schema: z.object({ city: z.string(), }), });
In LangChain, tools are used by agents (for example via create_agent) and tool error handling is configured through middleware.For LangGraph workflows, tool execution is handled by ToolNode. See ToolNode for Graph API usage, including how tools can access the current graph state and run-scoped context.
Return a string when the tool should provide plain text for the model to read and use in its next response.
import { tool } from "langchain";import * as z from "zod";const getWeather = tool(({ city }) => `It is currently sunny in ${city}.`, { name: "get_weather", description: "Get weather for a city.", schema: z.object({ city: z.string() }),});
Behavior:
The return value is converted to a ToolMessage.
The model sees that text and decides what to do next.
No agent state fields are changed unless the model or another tool does so later.
Use this when the result is naturally human-readable text.
Return an object (for example, a dict) when your tool produces structured data that the model should inspect.
import { tool } from "langchain";import * as z from "zod";const getWeatherData = tool( ({ city }) => ({ city, temperature_c: 22, conditions: "sunny", }), { name: "get_weather_data", description: "Get structured weather data for a city.", schema: z.object({ city: z.string() }), },);
Behavior:
The object is serialized and sent back as tool output.
The model can read specific fields and reason over them.
Like string returns, this does not directly update graph state.
Use this when downstream reasoning benefits from explicit fields instead of free-form text.
Tools are not limited to plain text. When the model supports multimodal tool results, the tool can return standard content blocks so the model receives text, images, and other media in one tool result.
import { tool } from "langchain";import { z } from "zod";const captureScreenshot = tool( async () => [ { type: "text", text: "Screenshot of the current page:" }, { type: "image", url: "https://example.com/page.png" }, ], { name: "capture_screenshot", description: "Capture a screenshot of the current page.", schema: z.object({}), });
Behavior:
The return value is converted to a ToolMessage with multimodal content.
Use message.content_blocks to read the normalized block list after the tool runs.
The model must support the modalities you return. Check your model’s capabilities before returning images, audio, or video.
For block types and provider-specific requirements, see Multimodal messages. MCP tools that return images or mixed content are converted the same way; see Multimodal tool content.
Return a Command when the tool needs to update graph state (for example, setting user preferences or app state).
You can return a Command with or without including a ToolMessage.
If the model needs to see that the tool succeeded (for example, to confirm a preference change), include a ToolMessage in the update, using runtime.tool_call_id for the tool_call_id parameter.
import { tool, ToolMessage, type ToolRuntime } from "langchain";import { Command } from "@langchain/langgraph";import * as z from "zod";const setLanguage = tool( async ({ language }, config: ToolRuntime) => { return new Command({ update: { preferredLanguage: language, messages: [ new ToolMessage({ content: `Language set to ${language}.`, tool_call_id: config.toolCallId, }), ], }, }); }, { name: "set_language", description: "Set the preferred response language.", schema: z.object({ language: z.string() }), },);
Behavior:
The command updates state using update.
Updated state is available to subsequent steps in the same run.
Use reducers for fields that may be updated by parallel tool calls.
Use this when the tool is not just returning data, but also mutating agent state.
Set return direct on a tool to short-circuit the agent loop: the agent returns the tool’s output to the caller immediately, without sending it back through the model for further processing.
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "google-genai:gemini-3.5-flash" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "openai:gpt-5.5" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "anthropic:claude-sonnet-4-6" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "openrouter:openrouter:z-ai/glm-5.2" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "fireworks:accounts/fireworks/models/glm-5p2" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "baseten:zai-org/GLM-5.2" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
import { ChatOpenAI } from "@langchain/openai";import { createAgent, tool } from "langchain";import * as z from "zod";const fetchOrderStatus = tool( ({ order_id }) => { return `Order ${order_id} is shipped and will arrive in 2 days.`; }, { name: "fetch_order_status", description: "Fetch the current status of a customer order.", schema: z.object({ order_id: z.string() }), returnDirect: true, },);const agent = createAgent({ model: new ChatOpenAI({ model: "ollama:north-mini-code-1.0" }), tools: [fetchOrderStatus],});const result = await agent.invoke({ messages: [ { role: "user", content: "What is the status of order #12345?" }, ],});// The agent returns the tool output directly without another LLM call:// "Order 12345 is shipped and will arrive in 2 days."
Behavior:
The tool executes normally and its output is wrapped in a ToolMessage.
The agent stops looping and returns the tool’s output as the final response, bypassing any additional model call.
If the model calls multiple tools in a single turn, return_direct takes effect only when all called tools have return_direct=True.
Use this when:
The tool’s output is the complete, user-ready answer (for example, a lookup that returns a ready-to-display result).
You want to avoid an extra model call when no additional reasoning is needed.
You need deterministic, unmodified output — the model cannot rephrase, summarize, or act on the tool result.
Because the model does not process the tool’s output, return_direct=True is not suitable for tools whose results require further reasoning, summarization, or chaining with other tool calls.
Tools access graph state through ToolRuntime. See Access context for state, context, store, and streaming APIs.For more details on accessing state, context, and long-term memory from tools, see Access context.
With dynamic tools, the set of tools available to the agent is modified at runtime rather than defined all upfront. Not every tool is appropriate for every situation. Too many tools may overwhelm the model (overload context) and increase errors; too few limit capabilities. Dynamic tool selection enables adapting the available toolset based on authentication state, user permissions, feature flags, or conversation stage.There are two approaches depending on whether tools are known ahead of time:
Filtering pre-registered tools
Runtime tool registration
When all possible tools are known at agent creation time, you can pre-register them and dynamically filter which ones are exposed to the model based on state, permissions, or context.
State
Store
Runtime Context
Enable advanced tools only after certain conversation milestones:
When tools are discovered or created at runtime (e.g., loaded from an MCP server, generated based on user data, or fetched from a remote registry), you need to both register the tools and handle their execution dynamically.This requires two middleware hooks:
wrap_model_call - Add the dynamic tools to the request
wrap_tool_call - Handle execution of the dynamically added tools
import { createAgent, createMiddleware, tool } from "langchain";import * as z from "zod";// A tool that will be added dynamically at runtimeconst calculateTip = tool( ({ billAmount, tipPercentage = 20 }) => { const tip = billAmount * (tipPercentage / 100); return `Tip: $${tip.toFixed(2)}, Total: $${(billAmount + tip).toFixed(2)}`; }, { name: "calculate_tip", description: "Calculate the tip amount for a bill", schema: z.object({ billAmount: z.number().describe("The bill amount"), tipPercentage: z.number().default(20).describe("Tip percentage"), }), });const dynamicToolMiddleware = createMiddleware({ name: "DynamicToolMiddleware", wrapModelCall: (request, handler) => { // Add dynamic tool to the request // This could be loaded from an MCP server, database, etc. return handler({ ...request, tools: [...request.tools, calculateTip], }); }, wrapToolCall: (request, handler) => { // Handle execution of the dynamic tool if (request.toolCall.name === "calculate_tip") { return handler({ ...request, tool: calculateTip }); } return handler(request); },});const agent = createAgent({ model: "gpt-4o", tools: [getWeather], // Only static tools registered here middleware: [dynamicToolMiddleware],});// The agent can now use both getWeather AND calculateTipconst result = await agent.invoke({ messages: [{ role: "user", content: "Calculate a 20% tip on $85" }],});
This approach is best when:
Tools are discovered at runtime (e.g., from an MCP server)
Tools are generated dynamically based on user data or configuration
You’re integrating with external tool registries
The wrap_tool_call hook is required for runtime-registered tools because the agent needs to know how to execute tools that weren’t in the original tool list. Without it, the agent won’t know how to invoke the dynamically added tool.
Some tools should run where your user’s app runs (typically the browser), not inside the process. Headless tools are tool definitions, which include the name, description, and argument schema, that you register on the server with your agent. The implementation is registered only on the client and executed after a short interrupt/resume handshake.This is different from ordinary tools whose function body runs on the server, and from server-side tool use where the model provider executes built-in tools remotely.
In both runtimes, the model sees a normal tool it can call, but the actual execution happens outside the server process.
Define the tool with tool({ name, description, schema }) from langchain, metadata and validation only, no server-side runner.
Attach the real behavior with .implement(async (args) => { ... }), which returns a headless tool implementation (definition + execute function).
Register the definition from step 1 with createAgent or your graph so the model sees the tool in its usual tool-calling loop.
Pass the implementation from step 2 to your streaming hook’s tools option.
Put tool definitions (tool({ name, description, schema })) and implementations (.implement(...)) in separate modules. Import the shared definition file from your server agent and from your frontend so names and schemas stay aligned; keep client-only execute logic in implementation modules the server never loads.
When the model issues a tool call for one of these tools, the run interrupts instead of executing the tool locally. Your app can inspect the payload, perform the action in the right environment (for example a browser, another service, or a human review step), then resume the graph with the tool result. When you use the supported JS SDK hooks, they can detect headless-tool interrupts, run the matching client implementation, and submit the resume command for you.Use the optional onTool callback to observe lifecycle events (start, success, error) for UI feedback such as spinners or toasts.
Headless tools frontend pattern
See an end-to-end example of schema-only tools executed in the client with useStream.
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.
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.