Integrations
LangChain
Add Lelu authorization to any LangChain agent or chain. Works with LangChain.js and LangChain Python via a tool wrapper that intercepts calls before execution.
Installation
terminal
# JavaScript / TypeScript npm install lelu-agent-auth langchain @langchain/openai # Python pip install lelu-agent-auth langchain langchain-openai
TypeScript — wrap a tool
tools/delete_record.ts
import { DynamicTool } from "@langchain/core/tools";
import { createClient } from "lelu-agent-auth";
const lelu = createClient({ apiKey: process.env.LELU_API_KEY! });
export const deleteRecord = new DynamicTool({
name: "delete_record",
description: "Permanently deletes a record from the database",
func: async (input: string) => {
const decision = await lelu.authorize({ tool: "delete_record" });
if (decision.decision === "deny") {
return `Blocked: ${decision.reason}`;
}
if (decision.decision === "human_review") {
return `Awaiting approval (id: ${decision.requestId})`;
}
return await actualDeleteRecord(input);
},
});Python — BaseTool subclass
tools/delete_record.py
import asyncio, os
from langchain.tools import BaseTool
from lelu import LeluClient, AuthorizeRequest
lelu = LeluClient(api_key=os.environ["LELU_API_KEY"])
class DeleteRecordTool(BaseTool):
name = "delete_record"
description = "Permanently deletes a record"
def _run(self, input: str) -> str:
# The SDK is async; bridge it from LangChain's sync _run.
decision = asyncio.run(lelu.authorize(AuthorizeRequest(tool="delete_record")))
if decision.decision == "deny":
return f"Blocked: {decision.reason}"
if decision.decision == "human_review":
return f"Pending approval: {decision.request_id}"
return actual_delete(input)Using in an agent
agent.ts
import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
import { ChatOpenAI } from "@langchain/openai";
import { deleteRecord } from "./tools/delete_record";
const llm = new ChatOpenAI({ model: "gpt-4o" });
const agent = await createOpenAIFunctionsAgent({ llm, tools: [deleteRecord], prompt });
const executor = new AgentExecutor({ agent, tools: [deleteRecord] });
await executor.invoke({ input: "Delete the user record for ID 42" });