Menu
CoddyTech

Human Approval

Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 13 of 22.

Some tools only read: a calendar lookup or a weather check changes nothing. Others act in the world: they send an email, charge a card or delete a file, and a wrong call cannot be taken back. An agent should run the first kind on its own and ask a person before the second kind.

if block.name in NEEDS_APPROVAL:
    print(f"Approve {block.name} {block.input}? (yes/no)")
    if input() != "yes":
        return {"type": "tool_result", "tool_use_id": block.id,
                "content": "The user declined this action.", "is_error": True}

Show the person the tool's name and its exact arguments, so they approve exactly what will happen. Ask before the tool runs, never after.

A refusal is still a tool_result: every tool_use block needs one in the next message, or the API refuses the request. Marking it with "is_error": True tells the model the action did not happen, so it reports that instead of claiming success. In a web app the question becomes a confirm button; the loop waits for the answer the same way.

challenge icon

Challenge

Medium

The office assistant sends emails without asking. Make it ask first. Only run_tool changes.

  1. When the tool's name is in NEEDS_APPROVAL, print Approve <name> <input>? (yes/no) with the tool's name and its input dict, then read one line with input().
  2. If the line is not yes, do not run the tool. Return a result whose content is The user declined this action., with "is_error": True.
  3. Run every other tool, and an approved one, as before.

A calendar question asks nothing and reads no extra line. For example, for the input Send an email to ana@example.com about the team lunch and no the program prints:

Approve send_email {'to': 'ana@example.com', 'subject': 'team lunch'}? (yes/no)
Answer: Sorry, I could not finish: The user declined this action.
Emails sent: 0

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

EVENTS = {"Friday": "team lunch at 12:30", "Monday": "budget review at 9:00"}
SENT = []

TOOLS = [
    {
        "name": "list_events",
        "description": "List the calendar events for one day. Use it to answer questions about the calendar.",
        "input_schema": {
            "type": "object",
            "properties": {"day": {"type": "string", "description": "A weekday, such as Friday"}},
            "required": ["day"],
        },
    },
    {
        "name": "send_email",
        "description": "Send an email to a recipient. Use it when the user asks to email someone.",
        "input_schema": {
            "type": "object",
            "properties": {
                "to": {"type": "string", "description": "The recipient's email address"},
                "subject": {"type": "string", "description": "The subject line"},
            },
            "required": ["to", "subject"],
        },
    },
]
NEEDS_APPROVAL = {"send_email"}


def list_events(day):
    return f"{day}: {EVENTS.get(day, 'nothing planned')}"


def send_email(to, subject):
    SENT.append(to)
    return f"Email sent to {to}."


FUNCTIONS = {"list_events": list_events, "send_email": send_email}


def run_tool(block):
    # Ask before a tool in NEEDS_APPROVAL runs
    return {"type": "tool_result", "tool_use_id": block.id, "content": FUNCTIONS[block.name](**block.input)}


request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    results = [run_tool(block) for block in response.content if block.type == "tool_use"]
    messages.append({"role": "user", "content": results})
    response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)

for block in response.content:
    if block.type == "text":
        print("Answer: " + block.text)
print(f"Emails sent: {len(SENT)}")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Tools and Agents

Practice on your own: Online Python compiler