*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to create custom Tools

On this page

How to create custom Tools
==========================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [LangChain tools](/v0.2/docs/concepts#tools)
*   [Agents](/v0.2/docs/concepts/#agents)

When constructing your own agent, you will need to provide it with a list of Tools that it can use. While LangChain includes some prebuilt tools, it can often be more useful to use tools that use custom logic. This guide will walk you through some ways you can create custom tools.

The biggest difference here is that the first function requires an object with multiple input fields, while the second one only accepts an object with a single field. Some older agents only work with functions that require single inputs, so it’s important to understand the distinction.

`tool` function[​](#tool-function "Direct link to tool-function")
-----------------------------------------------------------------

Only available in `@langchain/core` version 0.2.7 and above.

The [`tool`](https://api.js.langchain.com/classes/langchain_core_tools.tool.html) wrapper function is a convenience method for turning a JavaScript function into a tool. It requires the function itself along with some additional arguments that define your tool. The most important are:

*   The tool’s `name`, which the LLM will use as context as well as to reference the tool
*   An optional, but recommended `description`, which the LLM will use as context to know when to use the tool
*   A `schema`, which defines the shape of the tool’s input

The `tool` function will return an instance of the [`StructuredTool`](https://api.js.langchain.com/classes/langchain_core_tools.StructuredTool.html) class, so it is compatible with all the existing tool calling infrastructure in the LangChain library.

    import { z } from "zod";import { tool } from "@langchain/core/tools";const adderSchema = z.object({  a: z.number(),  b: z.number(),});const adderTool = tool(  async (input): Promise<string> => {    const sum = input.a + input.b;    return `The sum of ${input.a} and ${input.b} is ${sum}`;  },  {    name: "adder",    description: "Adds two numbers together",    schema: adderSchema,  });await adderTool.invoke({ a: 1, b: 2 });

    The sum of 1 and 2 is 3

`DynamicStructuredTool`[​](#dynamicstructuredtool "Direct link to dynamicstructuredtool")
-----------------------------------------------------------------------------------------

You can also use the [`DynamicStructuredTool`](https://api.js.langchain.com/classes/langchain_core_tools.DynamicStructuredTool.html) class to declare tools. Here’s an example - note that tools must always return strings!

    import { DynamicStructuredTool } from "@langchain/core/tools";import { z } from "zod";const multiplyTool = new DynamicStructuredTool({  name: "multiply",  description: "multiply two numbers together",  schema: z.object({    a: z.number().describe("the first number to multiply"),    b: z.number().describe("the second number to multiply"),  }),  func: async ({ a, b }: { a: number; b: number }) => {    return (a * b).toString();  },});await multiplyTool.invoke({ a: 8, b: 9 });

    "72"

`DynamicTool`[​](#dynamictool "Direct link to dynamictool")
-----------------------------------------------------------

For older agents that require tools which accept only a single input, you can pass the relevant parameters to the [`DynamicTool`](https://v02.api.js.langchain.com/classes/langchain_core_tools.DynamicTool.html) class. This is useful when working with older agents that only support tools that accept a single input. In this case, no schema is required:

    import { DynamicTool } from "@langchain/core/tools";const searchTool = new DynamicTool({  name: "search",  description: "look things up online",  func: async (_input: string) => {    return "LangChain";  },});await searchTool.invoke("foo");

    "LangChain"

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You’ve now seen a few ways to create custom tools in LangChain.

Next, you might be interested in learning [how to use a chat model to call tools](/v0.2/docs/how_to/tool_calling/).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to write a custom retriever class

](/v0.2/docs/how_to/custom_retriever)[

Next

How to debug your LLM apps

](/v0.2/docs/how_to/debugging)

*   [`tool` function](#tool-function)
*   [`DynamicStructuredTool`](#dynamicstructuredtool)
*   [`DynamicTool`](#dynamictool)
*   [Next steps](#next-steps)