!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

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
        *   [Prompt + LLM](/v0.1/docs/expression_language/cookbook/prompt_llm_parser/)
        *   [Multiple chains](/v0.1/docs/expression_language/cookbook/multiple_chains/)
        *   [Retrieval augmented generation (RAG)](/v0.1/docs/expression_language/cookbook/retrieval/)
        *   [Querying a SQL DB](/v0.1/docs/expression_language/cookbook/sql_db/)
        *   [Adding memory](/v0.1/docs/expression_language/cookbook/adding_memory/)
        *   [Using tools](/v0.1/docs/expression_language/cookbook/tools/)
        *   [Agents](/v0.1/docs/expression_language/cookbook/agents/)
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
*   [Cookbook](/v0.1/docs/expression_language/cookbook/)
*   Agents

Agents
======

You can pass a Runnable into an agent.

Building an agent from a runnable usually involves a few things:

1.  Data processing for the intermediate steps (`agent_scratchpad`). These need to represented in a way that the language model can recognize them. This should be pretty tightly coupled to the instructions in the prompt. For this reason, in the below example with an XML agent, we use the built in util `formatXml` to format the steps as XML.
    
2.  The prompt itself. Below, this is the default XML agent prompt, which includes variables for the tool list and user question. It also contains examples of inputs and outputs for the agent to learn from.
    
3.  The model, complete with stop tokens if needed (in our case, needed).
    
4.  The output parser - should be in sync with how the prompt specifies things to be formatted. In our case, we'll continue with the theme of XML and use the default `XMLAgentOutputParser`
    

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/anthropic

    yarn add @langchain/anthropic

    pnpm add @langchain/anthropic

    import { ChatAnthropic } from "@langchain/anthropic";import { AgentExecutor } from "langchain/agents";import { formatXml } from "langchain/agents/format_scratchpad/xml";import { XMLAgentOutputParser } from "langchain/agents/xml/output_parser";import { ChatPromptTemplate } from "langchain/prompts";import { AgentStep } from "langchain/schema";import { RunnableSequence } from "langchain/schema/runnable";import { Tool, ToolParams } from "langchain/tools";import { renderTextDescription } from "langchain/tools/render";

    // Define the model with stop tokens.const model = new ChatAnthropic({ temperature: 0 }).bind({  stop: ["</tool_input>", "</final_answer>"],});

For this example we'll define a custom tool for simplicity. You may use our built in tools, or define tools yourself, following the format you see below.

    class SearchTool extends Tool {  static lc_name() {    return "SearchTool";  }  name = "search-tool";  description = "This tool preforms a search about things and whatnot.";  constructor(config?: ToolParams) {    super(config);  }  async _call(_: string) {    return "32 degrees";  }}const tools = [new SearchTool()];

    const template = `You are a helpful assistant. Help the user answer any questions.You have access to the following tools:{tools}In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. \You will then get back a response in the form <observation></observation>For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:<tool>search</tool><tool_input>weather in SF</tool_input><observation>64 degrees</observation>When you are done, respond with a final answer between <final_answer></final_answer>. For example:<final_answer>The weather in SF is 64 degrees</final_answer>Begin!Question: {input}`;

    const prompt = ChatPromptTemplate.fromMessages([  ["human", template],  ["ai", "{agent_scratchpad}"],]);const outputParser = new XMLAgentOutputParser();

    const runnableAgent = RunnableSequence.from([  {    input: (i: { input: string; tools: Tool[]; steps: AgentStep[] }) => i.input,    tools: (i: { input: string; tools: Tool[]; steps: AgentStep[] }) =>      renderTextDescription(i.tools),    agent_scratchpad: (i: {      input: string;      tools: Tool[];      steps: AgentStep[];    }) => formatXml(i.steps),  },  prompt,  model,  outputParser,]);

    const executor = AgentExecutor.fromAgentAndTools({  agent: runnableAgent,  tools,});

    console.log("Loaded executor");const input = "What is the weather in SF?";console.log(`Calling executor with input: ${input}`);const response = await executor.invoke({ input, tools });console.log(response);

    Loaded executorCalling executor with input: What is the weather in SF?{ output: 'The weather in SF is 32 degrees' }

* * *

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

[

Previous

Using tools

](/v0.1/docs/expression_language/cookbook/tools/)[

Next

LangChain Expression Language (LCEL)

](/v0.1/docs/expression_language/)

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.