LangChain

Wrap a LangChain or LangGraph tool so it runs only after a person approves, using the HTTP API.

A LangChain agent acts through its tools, so that is where the check belongs: inside the tool, before the call that sends, pays or deletes. The model can then never reach the action without a person’s yes, whatever its prompt says.

1. A small helper

Save this as approvals.py. It asks, waits for the answer, and returns the request once nobody is deciding any more. It needs requests and the agent key in VARIANTGATE_KEY.

import os
import requests

API_URL = os.environ.get("VARIANTGATE_API_URL", "https://api.variantgate.com")
HEADERS = {"Authorization": f"Bearer {os.environ['VARIANTGATE_KEY']}"}


def ask_person(action: str, summary: str, **fields) -> dict:
    """Ask for approval and wait until the request is no longer pending."""
    body = {"agent": "langchain-agent", "action": action, "summary": summary,
            "wait_seconds": 30, **fields}
    response = requests.post(f"{API_URL}/v1/requests", headers=HEADERS,
                             json=body, timeout=40)
    response.raise_for_status()
    request = response.json()
    while request["status"] == "pending":
        response = requests.get(f"{API_URL}/v1/requests/{request['id']}",
                                headers=HEADERS, params={"wait_seconds": 30},
                                timeout=40)
        response.raise_for_status()
        request = response.json()
    return request


def report(request_id: str, outcome: str, note: str = "") -> None:
    """Say how the approved action went: "succeeded" or "failed"."""
    response = requests.post(f"{API_URL}/v1/requests/{request_id}/outcome",
                             headers=HEADERS, json={"outcome": outcome, "note": note},
                             timeout=10)
    response.raise_for_status()

2. Ask inside the tool

from langchain_core.tools import tool

from approvals import ask_person, report


@tool
def send_invoice(customer_email: str, amount_eur: str) -> str:
    """Email an invoice to a customer."""
    summary = f"Email an invoice for €{amount_eur} to {customer_email}"
    answer = ask_person(
        "invoice.send",
        summary,
        amount=amount_eur,
        currency="EUR",
        details={"to": customer_email},
        ttl_seconds=900,
    )
    if answer["allowed"] is not True:
        return f"Not sent: it was not approved ({answer['status']})."
    if answer["approved_summary"] != summary:
        # A person reworded it, perhaps to another address. This tool can
        # only do what it asked for, so it does nothing.
        report(answer["id"], "failed", "not done: approved with other wording")
        return f"Not sent. Approved instead: {answer['approved_summary']}"
    amount = answer["approved_amount"]  # a person may have lowered it
    invoice_id = billing.send_invoice(customer_email, amount)
    report(answer["id"], "succeeded", f"invoice {invoice_id}")
    return f"Sent invoice {invoice_id} for €{amount}."
  • Check allowed, never status: an approved request answers allowed: false while the workspace is paused.
  • Return a sentence on a no instead of raising, so the model can tell the user what happened and does not retry another way.
  • State the amount, and send approved_amount to the billing call, so what was approved and what was done cannot differ: a person may approve a lower amount than asked. Rules with amount limits go by it.
  • An error from the API raises, so the tool fails and nothing is sent.

The tool waits while a person decides, up to the request’s time limit. Pick ttl_seconds to suit the task: minutes for a chat, hours for a batch job.

With LangGraph

LangGraph’s interrupt pauses a graph to wait for input from whoever is running it. The approval here is a different thing: the question goes to an inbox that others can answer, rules can answer the routine ones, and the answer is kept on a record. Calling ask_person inside a tool node works the same as above, and the two can be combined.

The same helper works in any Python agent. For the reasoning behind each choice, read how to add human approval to an AI agent.