Skip to main content

How to create custom Tools

Prerequisites

This guide assumes familiarity with the following concepts:

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โ€‹

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

The tool 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 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โ€‹

You can also use the DynamicStructuredTool 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โ€‹

For older agents that require tools which accept only a single input, you can pass the relevant parameters to the DynamicTool 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โ€‹

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.


Was this page helpful?


You can leave detailed feedback on GitHub.