How to Intermediate · 3 min read

How to add human approval to AI agent actions

Quick answer
Add human approval to AI agent actions by intercepting the agent's proposed actions and prompting a human reviewer to confirm or reject them before execution. Use a control loop in your code that pauses for human input after the AI suggests an action, ensuring oversight and safety.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable for secure access.

bash
pip install openai

Step by step

This example demonstrates a simple AI agent that proposes actions, then waits for human approval before proceeding.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Function to get AI agent's proposed action

def get_agent_action(prompt):
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content.strip()

# Main loop with human approval

def run_agent_with_human_approval():
    prompt = "Suggest the next action for the agent in plain text."
    while True:
        action = get_agent_action(prompt)
        print(f"AI proposes action: {action}")
        approval = input("Approve this action? (yes/no/exit): ").strip().lower()
        if approval == "yes":
            print(f"Action '{action}' executed.")
            # Here you would add code to actually perform the action
            break
        elif approval == "no":
            print("Action rejected. Asking AI for a new action...")
            prompt = "Suggest a different action for the agent."
        elif approval == "exit":
            print("Exiting agent loop.")
            break
        else:
            print("Invalid input. Please type 'yes', 'no', or 'exit'.")

if __name__ == "__main__":
    run_agent_with_human_approval()
output
AI proposes action: Send an email to the client.
Approve this action? (yes/no/exit): no
Action rejected. Asking AI for a new action...
AI proposes action: Schedule a meeting with the client.
Approve this action? (yes/no/exit): yes
Action 'Schedule a meeting with the client.' executed.

Common variations

You can implement asynchronous approval workflows using async Python and webhooks, or integrate approval UIs in web apps. Different models like claude-3-5-sonnet-20241022 can be used for improved reasoning. Streaming responses allow partial action previews before approval.

Troubleshooting

  • If the AI repeatedly suggests undesired actions, refine the prompt or add constraints.
  • If input hangs, ensure your terminal supports input() or switch to a GUI/web interface.
  • For API errors, verify your API key and network connectivity.

Key Takeaways

  • Intercept AI agent actions and require explicit human approval before execution.
  • Use a simple input loop to confirm, reject, or exit agent actions interactively.
  • Customize prompts and models to improve action relevance and safety.
  • Consider async or UI-based approval for scalable human-in-the-loop workflows.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗