Automating approval loops is one of the most impactful ways to accelerate business processes, reduce manual errors, and ensure compliance at scale. In 2026, the rise of custom AI agents has made it possible to design highly tailored, intelligent approval workflows that adapt to your organization’s unique rules and requirements.
In this deep-dive tutorial, you'll learn how to build a custom AI agent that automates a multi-step approval loop — from request intake through AI-driven decision-making, escalation, and audit logging. We’ll use open-source tools and modern LLM APIs, with hands-on code and configuration you can adapt to your stack.
As we covered in our Ultimate Guide to Automating Approval Workflows with AI in 2026, the possibilities for workflow automation are expanding rapidly. Here, we’ll zoom in on the technical “how” — giving you everything you need to launch your own custom AI approval agent.
Prerequisites
- Python 3.11+ (for agent scripting and orchestration)
- Node.js 18+ (for optional workflow integrations, e.g., Slack bots)
- LangChain 0.1.0+ (for LLM orchestration)
- OpenAI (or compatible) API key (for LLM access)
- Basic knowledge of REST APIs and JSON
- Familiarity with workflow tools (e.g., Zapier, n8n, or custom webhooks)
- Git (for version control)
Note: This guide assumes you have pip and npm installed, and access to a terminal or command prompt.
1. Define Your Approval Workflow Logic
-
Map out your approval stages. For this tutorial, we’ll automate a simple three-stage workflow:
- Request Intake (e.g., employee submits a purchase request)
- AI Agent Review (auto-approve or escalate based on policy)
- Manager Approval (if escalated)
-
Specify your approval criteria. For example:
- Requests under $500 auto-approved by AI
- Requests $500+ require manager sign-off
-
List required integrations. For this guide:
- Slack for notifications
- Google Sheets for logging approvals
Tip: For more complex, multi-step approval chains, see our guide on Prompt Engineering for Dynamic Approval Chains.
2. Set Up Your Project Environment
-
Create a new project folder and initialize Git.
mkdir ai-approval-workflow && cd ai-approval-workflow git init
-
Create and activate a Python virtual environment.
python3 -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies.
pip install langchain openai flask python-dotenv requests
-
Set up your
.envfile with your API keys.OPENAI_API_KEY=sk-... SLACK_WEBHOOK_URL=https://hooks.slack.com/services/... GOOGLE_SHEETS_WEBHOOK_URL=https://your-webhook-url...
3. Build the AI Approval Agent Core
-
Create
approval_agent.py.This script will:
- Accept incoming approval requests (as JSON)
- Use an LLM to apply your policy
- Trigger escalation if needed
- Log results
import os import json from flask import Flask, request, jsonify from dotenv import load_dotenv from langchain.llms import OpenAI load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") app = Flask(__name__) def ai_decision(request_data): # Compose prompt for the LLM prompt = ( f"Review the following purchase request:\n" f"Employee: {request_data['employee']}\n" f"Amount: ${request_data['amount']}\n" f"Description: {request_data['description']}\n" "If the amount is less than $500, respond with 'APPROVE'. " "If $500 or more, respond with 'ESCALATE'." ) llm = OpenAI(openai_api_key=OPENAI_API_KEY, temperature=0) response = llm(prompt) return response.strip().upper() @app.route('/approve', methods=['POST']) def approve(): data = request.json decision = ai_decision(data) result = { "employee": data['employee'], "amount": data['amount'], "decision": decision } # (Integration hooks will be called here) return jsonify(result), 200 if __name__ == "__main__": app.run(port=5001)Screenshot description: Terminal running
python approval_agent.py, Flask server started on port 5001. -
Test your endpoint locally.
curl -X POST http://localhost:5001/approve -H "Content-Type: application/json" -d '{"employee":"Alice","amount":450,"description":"Conference registration"}'Expected output:
{"employee":"Alice","amount":450,"decision":"APPROVE"}
For more on LLM prompt design, see Prompt Engineering for Approval Workflows: Templates & Real-World Examples.
4. Integrate Escalation & Notification Logic
-
Add Slack notifications for escalations.
import requests def notify_manager(request_data): slack_url = os.getenv("SLACK_WEBHOOK_URL") message = { "text": f"Approval needed for {request_data['employee']} (${request_data['amount']}): {request_data['description']}" } response = requests.post(slack_url, json=message) return response.status_code == 200 -
Update your
/approveroute inapproval_agent.py:@app.route('/approve', methods=['POST']) def approve(): data = request.json decision = ai_decision(data) result = { "employee": data['employee'], "amount": data['amount'], "decision": decision } if decision == "ESCALATE": notify_manager(data) # (Add logging in the next step) return jsonify(result), 200 -
Test escalation:
curl -X POST http://localhost:5001/approve -H "Content-Type: application/json" -d '{"employee":"Bob","amount":650,"description":"Laptop purchase"}'Expected output:
{"employee":"Bob","amount":650,"decision":"ESCALATE"}and a Slack notification to your configured channel.
For remote team best practices, see Building Approval Workflows for Remote-First Teams: AI-Driven Best Practices in 2026.
5. Add Audit Logging (Google Sheets Example)
-
Set up a Google Sheets webhook (e.g., via Zapier or n8n).
- Trigger: Webhook receives POST
- Action: Append row to your sheet (columns: timestamp, employee, amount, decision)
-
Add logging to your agent:
import datetime def log_approval(request_data, decision): webhook_url = os.getenv("GOOGLE_SHEETS_WEBHOOK_URL") payload = { "timestamp": datetime.datetime.utcnow().isoformat(), "employee": request_data['employee'], "amount": request_data['amount'], "decision": decision } response = requests.post(webhook_url, json=payload) return response.status_code == 200 -
Call
log_approval()in your/approveroute:@app.route('/approve', methods=['POST']) def approve(): data = request.json decision = ai_decision(data) result = { "employee": data['employee'], "amount": data['amount'], "decision": decision } if decision == "ESCALATE": notify_manager(data) log_approval(data, decision) return jsonify(result), 200 -
Verify logging:
- Submit a test request
- Check your Google Sheet for a new row
For more on document-centric workflows, see Document AI Workflows: Automating Contract Review and Approval at Scale.
6. Orchestrate the Full Approval Loop
-
Integrate with your workflow tool (optional).
- Set up a Zapier or n8n workflow to POST approval requests to your agent’s
/approveendpoint. - Use Slack, Teams, or email for manager approvals if escalated.
- Set up a Zapier or n8n workflow to POST approval requests to your agent’s
-
Handle manager responses.
- Option 1: Manager replies in Slack; your bot listens and updates the log.
- Option 2: Manager clicks an “Approve” link that triggers a webhook to your agent.
-
Example: Add a simple manager approval endpoint:
@app.route('/manager_approve', methods=['POST']) def manager_approve(): data = request.json # Update Google Sheets with manager approval log_approval(data, "MANAGER_APPROVED") return jsonify({"status": "Manager approved"}), 200 -
Test end-to-end:
- Submit a high-value request (triggers escalation)
- Manager approves via Slack or direct API call
- Check logs for both AI and manager approvals
Common Issues & Troubleshooting
-
LLM not responding or slow: Check your OpenAI API key and network connectivity. Try lowering model
temperaturefor more deterministic results. -
Slack notifications not received: Double-check your
SLACK_WEBHOOK_URL. Ensure your bot is invited to the right channel. - Google Sheets not updating: Verify your webhook URL, Zapier/n8n setup, and payload format.
- Flask server errors: Review logs for stack traces. Ensure all environment variables are set.
- Approval logic not matching policy: Adjust your LLM prompt and test with various amounts/descriptions.
Next Steps
- Expand policies: Add more nuanced rules, e.g., department-specific thresholds, vendor checks, or context-aware escalation.
- Integrate with HR or procurement systems: See How LLMs Are Streamlining Procurement Approvals: Practical Use Cases for 2026.
- Harden for security & compliance: Review Security & Compliance Risks in Automated Approval Workflows: How to Mitigate in 2026.
- Explore agent-driven SOC workflows: See Building Custom AI Agents for Automated SOC Workflows.
Automating approval loops with custom AI agents unlocks major efficiency and compliance gains. By following this tutorial, you’ve built a robust foundation for intelligent, adaptable workflow automation. For a broader strategy overview and advanced patterns, check out our Ultimate Guide to Automating Approval Workflows with AI in 2026.