!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
        *   [Quickstart](/v0.1/docs/use_cases/tool_use/quickstart/)
        *   [Agents](/v0.1/docs/use_cases/tool_use/agents/)
        *   [Choosing between multiple tools](/v0.1/docs/use_cases/tool_use/multiple_tools/)
        *   [Parallel tool use](/v0.1/docs/use_cases/tool_use/parallel/)
        *   [Tool error handling](/v0.1/docs/use_cases/tool_use/tool_error_handling/)
        *   [Human-in-the-loop](/v0.1/docs/use_cases/tool_use/human_in_the_loop/)
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   [Tool use](/v0.1/docs/use_cases/tool_use/)
*   Agents

On this page

Agents
======

Chains are great when we know the specific sequence of tool usage needed for any user input. But for certain use cases, how many times we use tools depends on the input. In these cases, we want to let the model itself decide how many times to use tools and in what order. That's where [Agents](/v0.1/docs/modules/agents/) come in!

LangChain comes with a number of built-in agents that are optimized for different use cases. Read about [all the available agent types](/v0.1/docs/modules/agents/agent_types/) here.

For this example, let’s try out the OpenAI tools agent, which makes use of the new OpenAI tool-calling API (this is only available in the latest OpenAI models, and differs from function-calling in that the model can return multiple function invocations at once).

Keep in mind that some agents only support single-argument tools - for these agents, you will need to use a `DynamicTool` instead and parse the input string yourself.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

Because we're using OpenAI for this guide, we'll need to install its partner package:

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

You'll need to sign up for an OpenAI key and set it as an environment variable named `OPENAI_API_KEY`.

We'll also use the popular validation library [Zod](https://zod.dev) to define our tool schemas. It's already a dependency of `langchain`, but you can install it explicitly like this too:

*   npm
*   Yarn
*   pnpm

    npm install zod

    yarn add zod

    pnpm add zod

Create tools[​](#create-tools "Direct link to Create tools")
------------------------------------------------------------

First, we need to create some tool to call. For this example, we will create custom tools from functions. For more information on creating custom tools, please [see this guide](/v0.1/docs/modules/agents/tools/).

    import { z } from "zod";import { DynamicStructuredTool } from "@langchain/core/tools";const addTool = new DynamicStructuredTool({  name: "add",  description: "Add two integers together.",  schema: z.object({    firstInt: z.number(),    secondInt: z.number(),  }),  func: async ({ firstInt, secondInt }) => {    return (firstInt + secondInt).toString();  },});const multiplyTool = new DynamicStructuredTool({  name: "multiply",  description: "Multiply two integers together.",  schema: z.object({    firstInt: z.number(),    secondInt: z.number(),  }),  func: async ({ firstInt, secondInt }) => {    return (firstInt * secondInt).toString();  },});const exponentiateTool = new DynamicStructuredTool({  name: "exponentiate",  description: "Exponentiate the base to the exponent power.",  schema: z.object({    base: z.number(),    exponent: z.number(),  }),  func: async ({ base, exponent }) => {    return (base ** exponent).toString();  },});const tools = [addTool, multiplyTool, exponentiateTool];

Create prompt[​](#create-prompt "Direct link to Create prompt")
---------------------------------------------------------------

    import { pull } from "langchain/hub";import type { ChatPromptTemplate } from "@langchain/core/prompts";// Get the prompt to use - you can modify this!// You can also see the full prompt at:// https://smith.langchain.com/hub/hwchase17/openai-tools-agentconst prompt = await pull<ChatPromptTemplate>("hwchase17/openai-tools-agent");

Create agent[​](#create-agent "Direct link to Create agent")
------------------------------------------------------------

    import { ChatOpenAI } from "@langchain/openai";import { AgentExecutor, createOpenAIToolsAgent } from "langchain/agents";const model = new ChatOpenAI({  model: "gpt-3.5-turbo-1106",  temperature: 0,});const agent = await createOpenAIToolsAgent({  llm: model,  tools,  prompt,});const agentExecutor = new AgentExecutor({  agent,  tools,  verbose: true,});

Invoke agent[​](#invoke-agent "Direct link to Invoke agent")
------------------------------------------------------------

info

You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/fc11cacc-f467-4c39-a46e-8bfefa37b1f9/r)

    await agentExecutor.invoke({  input:    "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result",});

    > Entering new AgentExecutor chain...Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`243Invoking: `add` with `{'first_int': 12, 'second_int': 3}`15Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`3645Invoking: `exponentiate` with `{'base': 3645, 'exponent': 2}`13286025The result of taking 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.> Finished chain.

    {  input: 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',  output: 'The result of taking 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.'}

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Quickstart

](/v0.1/docs/use_cases/tool_use/quickstart/)[

Next

Choosing between multiple tools

](/v0.1/docs/use_cases/tool_use/multiple_tools/)

*   [Setup](#setup)
*   [Create tools](#create-tools)
*   [Create prompt](#create-prompt)
*   [Create agent](#create-agent)
*   [Invoke agent](#invoke-agent)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.