*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to use tools

On this page

How to use tools
================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Chatbots](/v0.2/docs/concepts/#messages)
*   [Agents](/v0.2/docs/tutorials/agents)
*   [Chat history](/v0.2/docs/concepts/#chat-history)

This section will cover how to create conversational agents: chatbots that can interact with other systems and APIs using tools.

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

For this guide, we’ll be using an [tool calling agent](/v0.2/docs/how_to/agent_executor) with a single tool for searching the web. The default will be powered by [Tavily](/v0.2/docs/integrations/tools/tavily_search), but you can switch it out for any similar tool. The rest of this section will assume you’re using Tavily.

You’ll need to [sign up for an account on the Tavily website](https://tavily.com), and install the following packages:

*   npm
*   yarn
*   pnpm

    npm i @langchain/core @langchain/openai langchain

    yarn add @langchain/core @langchain/openai langchain

    pnpm add @langchain/core @langchain/openai langchain

    import { TavilySearchResults } from "@langchain/community/tools/tavily_search";import { ChatOpenAI } from "@langchain/openai";const tools = [  new TavilySearchResults({    maxResults: 1,  }),];const llm = new ChatOpenAI({  model: "gpt-3.5-turbo-1106",  temperature: 0,});

To make our agent conversational, we must also choose a prompt with a placeholder for our chat history. Here’s an example:

    import { ChatPromptTemplate } from "@langchain/core/prompts";// Adapted from https://smith.langchain.com/hub/jacob/tool-calling-agentconst prompt = ChatPromptTemplate.fromMessages([  [    "system",    "You are a helpful assistant. You may not need to use tools for every query - the user may just want to chat!",  ],  ["placeholder", "{messages}"],  ["placeholder", "{agent_scratchpad}"],]);

Great! Now let’s assemble our agent:

    import { AgentExecutor, createToolCallingAgent } from "langchain/agents";const agent = await createToolCallingAgent({  llm,  tools,  prompt,});const agentExecutor = new AgentExecutor({ agent, tools });

Running the agent[​](#running-the-agent "Direct link to Running the agent")
---------------------------------------------------------------------------

Now that we’ve set up our agent, let’s try interacting with it! It can handle both trivial queries that require no lookup:

    import { HumanMessage } from "@langchain/core/messages";await agentExecutor.invoke({  messages: [new HumanMessage("I'm Nemo!")],});

    {  messages: [    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "I'm Nemo!",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "I'm Nemo!",      name: undefined,      additional_kwargs: {},      response_metadata: {}    }  ],  output: "Hi Nemo! It's great to meet you. How can I assist you today?"}

Or, it can use of the passed search tool to get up to date information if needed:

    await agentExecutor.invoke({  messages: [    new HumanMessage(      "What is the current conservation status of the Great Barrier Reef?"    ),  ],});

    {  messages: [    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "What is the current conservation status of the Great Barrier Reef?",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "What is the current conservation status of the Great Barrier Reef?",      name: undefined,      additional_kwargs: {},      response_metadata: {}    }  ],  output: "The Great Barrier Reef has recorded its highest amount of coral cover since the Australian Institute"... 688 more characters}

Conversational responses[​](#conversational-responses "Direct link to Conversational responses")
------------------------------------------------------------------------------------------------

Because our prompt contains a placeholder for chat history messages, our agent can also take previous interactions into account and respond conversationally like a standard chatbot:

    import { AIMessage } from "@langchain/core/messages";await agentExecutor.invoke({  messages: [    new HumanMessage("I'm Nemo!"),    new AIMessage("Hello Nemo! How can I assist you today?"),    new HumanMessage("What is my name?"),  ],});

    {  messages: [    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "I'm Nemo!",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "I'm Nemo!",      name: undefined,      additional_kwargs: {},      response_metadata: {}    },    AIMessage {      lc_serializable: true,      lc_kwargs: {        content: "Hello Nemo! How can I assist you today?",        tool_calls: [],        invalid_tool_calls: [],        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "Hello Nemo! How can I assist you today?",      name: undefined,      additional_kwargs: {},      response_metadata: {},      tool_calls: [],      invalid_tool_calls: [],      usage_metadata: undefined    },    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "What is my name?",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "What is my name?",      name: undefined,      additional_kwargs: {},      response_metadata: {}    }  ],  output: "Your name is Nemo!"}

If preferred, you can also wrap the agent executor in a [`RunnableWithMessageHistory`](/v0.2/docs/how_to/message_history/) class to internally manage history messages. Let’s redeclare it this way:

    const agent2 = await createToolCallingAgent({  llm,  tools,  prompt,});const agentExecutor2 = new AgentExecutor({ agent: agent2, tools });

Then, because our agent executor has multiple outputs, we also have to set the `outputMessagesKey` property when initializing the wrapper:

    import { ChatMessageHistory } from "langchain/stores/message/in_memory";import { RunnableWithMessageHistory } from "@langchain/core/runnables";const demoEphemeralChatMessageHistory = new ChatMessageHistory();const conversationalAgentExecutor = new RunnableWithMessageHistory({  runnable: agentExecutor2,  getMessageHistory: (_sessionId) => demoEphemeralChatMessageHistory,  inputMessagesKey: "messages",  outputMessagesKey: "output",});await conversationalAgentExecutor.invoke(  { messages: [new HumanMessage("I'm Nemo!")] },  { configurable: { sessionId: "unused" } });

    {  messages: [    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "I'm Nemo!",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "I'm Nemo!",      name: undefined,      additional_kwargs: {},      response_metadata: {}    }  ],  output: "Hi Nemo! It's great to meet you. How can I assist you today?"}

    await conversationalAgentExecutor.invoke(  { messages: [new HumanMessage("What is my name?")] },  { configurable: { sessionId: "unused" } });

    {  messages: [    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "I'm Nemo!",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "I'm Nemo!",      name: undefined,      additional_kwargs: {},      response_metadata: {}    },    AIMessage {      lc_serializable: true,      lc_kwargs: {        content: "Hi Nemo! It's great to meet you. How can I assist you today?",        tool_calls: [],        invalid_tool_calls: [],        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "Hi Nemo! It's great to meet you. How can I assist you today?",      name: undefined,      additional_kwargs: {},      response_metadata: {},      tool_calls: [],      invalid_tool_calls: [],      usage_metadata: undefined    },    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "What is my name?",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "What is my name?",      name: undefined,      additional_kwargs: {},      response_metadata: {}    }  ],  output: "Your name is Nemo!"}

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

You’ve now learned how to create chatbots with tool-use capabilities.

For more, check out the other guides in this section, including [how to add history to your chatbots](/v0.2/docs/how_to/chatbots_memory).

* * *

#### 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 do retrieval

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

Next

How to split code

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

*   [Setup](#setup)
*   [Running the agent](#running-the-agent)
*   [Conversational responses](#conversational-responses)
*   [Next steps](#next-steps)