!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/)
*   Human-in-the-loop

On this page

Human-in-the-loop
=================

There are certain tools that we don’t trust a model to execute on its own. One thing we can do in such situations is require human approval before the tool is invoked.

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

We’ll need to install the following packages:

    npm install langchain @langchain/core @langchain/openai readline zod

We’ll use `readline` to handle accepting input from the user.

### LangSmith[​](#langsmith "Direct link to LangSmith")

Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls. As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent. The best way to do this is with [LangSmith](https://smith.langchain.com/).

Note that LangSmith is not needed, but it is helpful. If you do want to use LangSmith, after you sign up at the link above, make sure to set your environment variables to start logging traces:

    export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=YOUR_KEY

Chain[​](#chain "Direct link to Chain")
---------------------------------------

Suppose we have the following (dummy) tools and tool-calling chain:

    import { ChatOpenAI } from "@langchain/openai";import {  Runnable,  RunnableLambda,  RunnablePassthrough,} from "@langchain/core/runnables";import { StructuredTool } from "@langchain/core/tools";import { JsonOutputToolsParser } from "langchain/output_parsers";import { z } from "zod";class CountEmails extends StructuredTool {  schema = z.object({    lastNDays: z.number(),  });  name = "count_emails";  description = "Count the number of emails sent in the last N days.";  async _call(input: z.infer<typeof this.schema>): Promise<string> {    return (input.lastNDays * 2).toString();  }}class SendEmail extends StructuredTool {  schema = z.object({    message: z.string(),    recipient: z.string(),  });  name = "send_email";  description = "Send an email.";  async _call(input: z.infer<typeof this.schema>): Promise<string> {    return `Successfully sent email to ${input.recipient}`;  }}const tools = [new CountEmails(), new SendEmail()];

    const model = new ChatOpenAI({  model: "gpt-3.5-turbo",  temperature: 0,}).bind({  tools,});/** * Function for dynamically constructing the end of the chain based on the model-selected tool. */const callTool = (toolInvocation: Record<string, any>): Runnable => {  const toolMap: Record<string, StructuredTool> = tools.reduce((acc, tool) => {    acc[tool.name] = tool;    return acc;  }, {});  const tool = toolMap[toolInvocation.type];  return RunnablePassthrough.assign({    output: (input, config) => tool.invoke(input.args, config),  });};

    // .map() allows us to apply a function to a list of inputs.const callToolList = new RunnableLambda({ func: callTool }).map();const chain = model.pipe(new JsonOutputToolsParser()).pipe(callToolList);

    await chain.invoke("How many emails did I get in the last 5 days?");

    [ { type: "count_emails", args: { lastNDays: 5 }, output: "10" } ]

Adding human approval[​](#adding-human-approval "Direct link to Adding human approval")
---------------------------------------------------------------------------------------

We can add a simple human approval step to our `toolChain` function:

    import * as readline from "readline";import { JsonOutputToolsParser } from "langchain/output_parsers";import { callToolList, model } from "./helpers.js";// Use readline to ask the user for approvalfunction askQuestion(question: string): Promise<string> {  const rl = readline.createInterface({    input: process.stdin,    output: process.stdout,  });  return new Promise((resolve) => {    rl.question(question, (answer) => {      rl.close();      resolve(answer);    });  });}async function humanApproval(toolInvocations: any[]): Promise<any[]> {  const toolStrs = toolInvocations    .map((toolCall) => JSON.stringify(toolCall, null, 2))    .join("\n\n");  const msg = `Do you approve of the following tool invocations\n\n${toolStrs}Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no.\n`;  // Ask the user for approval  const resp = await askQuestion(msg);  if (!["yes", "y"].includes(resp.toLowerCase())) {    throw new Error(`Tool invocations not approved:\n\n${toolStrs}`);  }  return toolInvocations;}const chain = model  .pipe(new JsonOutputToolsParser())  .pipe(humanApproval)  .pipe(callToolList);const response = await chain.invoke(  "how many emails did i get in the last 5 days?");console.log(response);/**Do you approve of the following tool invocations{  "type": "count_emails",  "args": {    "lastNDays": 5  }}Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no.y[ { type: 'count_emails', args: { lastNDays: 5 }, output: '10' } ] */const response2 = await chain.invoke(  "Send sally@gmail.com an email saying 'What's up homie'");console.log(response2);/**Do you approve of the following tool invocations{  "type": "send_email",  "args": {    "message": "What's up homie",    "recipient": "sally@gmail.com"  }}Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no.y[  {    type: 'send_email',    args: { message: "What's up homie", recipient: 'sally@gmail.com' },    output: 'Successfully sent email to sally@gmail.com'  }] */

#### API Reference:

*   [JsonOutputToolsParser](https://api.js.langchain.com/classes/langchain_output_parsers.JsonOutputToolsParser.html) from `langchain/output_parsers`

> #### Examine the LangSmith traces from the code above [here](https://smith.langchain.com/public/aac711ff-b1a1-4fd7-a298-0f20909259b6/r) and [here](https://smith.langchain.com/public/7b35ee77-b369-4b95-af4f-b83510f9a93b/r).[​](#examine-the-langsmith-traces-from-the-code-above-here-and-here. "Direct link to examine-the-langsmith-traces-from-the-code-above-here-and-here.")

* * *

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

[

Previous

Tool error handling

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

Next

Interacting with APIs

](/v0.1/docs/use_cases/api/)

*   [Setup](#setup)
    *   [LangSmith](#langsmith)
*   [Chain](#chain)
*   [Adding human approval](#adding-human-approval)

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.