Autonomous AI agents are great until they drop a database table or send an unintended email to thousands of clients. Letting an LLM execute arbitrary actions without human validation is a fast track to production incidents. To keep your systems safe, you need an approval gate that pauses high-risk tool calls and waits for explicit human confirmation before executing sensitive operations.
- Python 3.9 or higher installed on your machine.
- Basic familiarity with Python functions and dictionaries.
- No external libraries required, standard Python libraries like
functoolsandtypingwork fine.
Step 1: Flag Risky Tools with Metadata
Start by defining custom tool decorators or metadata to tag functions as high risk. You do not need complex frameworks to do this. A simple decorator that attaches a requires_approval attribute to sensitive functions keeps your code clean.
import functools
def ai_tool(requires_approval: bool = False):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper.requires_approval = requires_approval
wrapper.is_ai_tool = True
return wrapper
return decorator
# Example tools
@ai_tool(requires_approval=False)
def search_knowledge_base(query: str) -> str:
return f"Results for: {query}"
@ai_tool(requires_approval=True)
def delete_user_account(user_id: str) -> str:
return f"User {user_id} successfully deleted."
Step 2: Build the Interceptor Routine
Next, write an execution layer that inspects the target tool before running it. If the function has requires_approval=True, the wrapper suspends execution and requests user consent via console input, API callback, or Slack notification.
Here is a lightweight CLI implementation:
def execute_tool(tool_func, *args, **kwargs):
tool_name = tool_func.__name__
needs_review = getattr(tool_func, "requires_approval", False)
if needs_review:
print(f"\n[APPROVAL REQUIRED] Agent requested to call: {tool_name}")
print(f"Arguments: args={args}, kwargs={kwargs}")
user_input = input("Approve execution? (y/N): ").strip().lower()
if user_input != 'y':
print(f"[BLOCKED] Execution of {tool_name} was aborted by user.")
return {"status": "rejected", "reason": "User denied approval"}
print(f"[EXECUTING] Running {tool_name}...")
result = tool_func(*args, **kwargs)
return {"status": "success", "result": result}
This pattern works well whether you are building custom AI agent development pipelines or setting up complex enterprise rules. For instance, in sensitive fields like AI for Accounting & Finance, automated fund transfers or invoice modifications should always hit an explicit guardrail before moving real money.
Step 3: Handle Agent Responses and Execution
Now wire the tool executor directly into your agent call loop. When the model selects a tool, route the call through your execute_tool router rather than calling the tool directly.
# Simulated model tool calls coming from an agent framework
tool_calls_queue = [
{"func": search_knowledge_base, "args": ("password reset steps",)},
{"func": delete_user_account, "kwargs": {"user_id": "usr_9981"}}
]
for call in tool_calls_queue:
func = call["func"]
args = call.get("args", ())
kwargs = call.get("kwargs", {})
response = execute_tool(func, *args, **kwargs)
print("Execution output:", response)
Integrating human approval gates like this forms the backbone of reliable workflow automation, giving teams confidence to deploy autonomous systems without giving up control over dangerous actions.
Expected Results
When running the full script, standard read operations execute immediately, while high-risk functions prompt for explicit approval:
[EXECUTING] Running search_knowledge_base...
Execution output: {'status': 'success', 'result': 'Results for: password reset steps'}
[APPROVAL REQUIRED] Agent requested to call: delete_user_account
Arguments: args=(), kwargs={'user_id': 'usr_9981'}
Approve execution? (y/N): n
[BLOCKED] Execution of delete_user_account was aborted by user.
Execution output: {'status': 'rejected', 'reason': 'User denied approval'}
If you answer y, the function executes and returns the success output. If you enter n or anything else, the process halts safely and returns a clear refusal message to the agent so it can handle the failure gracefully.
If you need help scaling agent architectures or embedding custom governance controls into your production stack, the team at Gaper can connect you with experienced engineers who build secure AI software.
Top comments (0)