LangChain

Add human approval gates to AI agent workflows. Require confirmation before sensitive actions.

When to use this

  • AI agents that can modify production data
  • Automated systems that send emails or messages
  • Agents with access to payment or billing systems
  • Any action that's difficult to reverse

Installation

terminal
pip install requests

Create Approval Tool

Create a custom LangChain tool that requests human approval via ottr:

approval_tool.py
import requests
import time
from langchain.tools import BaseTool
from typing import Optional

class HumanApprovalTool(BaseTool):
    name = "human_approval"
    description = "Request human approval before performing sensitive actions. Use this when the action could have significant consequences."

    api_key: str
    api_base: str = "https://api.ottr.run"

    def _run(self, action_description: str) -> str:
        # Create approval request
        response = requests.post(
            f"{self.api_base}/v1/approvals",
            headers={
                "Authorization": f"Bearer {self.api_key}",
                "Content-Type": "application/json"
            },
            json={
                "key_path": f"agent/action/{int(time.time())}",
                "value": "approved",
                "ttl_seconds": 600,
                "info": action_description,
                "actions": [
                    {"key": "approve", "value": "approved", "label": "Approve", "style": "primary"},
                    {"key": "reject", "value": "rejected", "label": "Reject", "style": "danger"}
                ]
            }
        )

        data = response.json()
        approval_url = data["url"]
        key_path = f"agent/action/{int(time.time())}"

        print(f"Approval required: {approval_url}")

        # Poll for approval
        while True:
            result = requests.get(
                f"{self.api_base}/v1/key/{key_path}",
                headers={"Authorization": f"Bearer {self.api_key}"}
            )
            value = result.text.strip('"')

            if value == "approved":
                return "Action approved by human. Proceed."
            elif value == "rejected":
                return "Action rejected by human. Do not proceed."

            time.sleep(5)

Use with Agent

Add the approval tool to your agent's toolkit:

agent.py
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI

# Initialize the tool
approval_tool = HumanApprovalTool(
    api_key="otw_your_api_key_here"
)

# Create agent with the approval tool
llm = ChatOpenAI(temperature=0)
agent = initialize_agent(
    tools=[approval_tool, ...other_tools],
    llm=llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# The agent will now request approval for sensitive actions
agent.run("Delete all inactive user accounts from the database")

How it works

  1. Agent decides to use the human_approval tool
  2. Tool creates an ottr approval URL and prints it
  3. Human clicks the link and approves or rejects
  4. Tool returns the decision to the agent
  5. Agent proceeds or aborts based on human decision

Tips

  • Use descriptive info messages so humans understand what they're approving
  • Set appropriate ttl_seconds based on your workflow timing
  • Configure Slack notifications to alert humans when approval is needed
  • Use unique key_path values to track different actions