Add approvals and limits to an agent

DevOps, Hands On25 minutesFreeSteps checked 23 November 2026By Cloud Hands On

In this task you'll let an agent read freely but make it ask a person before it changes anything.

Sort every tool into read (status, logs, search) or write (restart, scale, delete). Reads run on their own. Writes stop and ask a person, showing exactly what will happen, and run only on a clear yes. A refusal goes back to the model as the tool result, so it explains instead of retrying.

Add limits around the whole loop: a step limit, a list of the services the agent may touch, and identity limits outside the code (the agent's account should only have the rights its tools need). Guardrails in your loop work whatever the model decides, which is why they matter more than instructions in the prompt.

Example. SS Kaiser's agent can read logs on every service but may only restart shop-api and stock-worker, and only after the on-call engineer types yes. Payments services aren't in its list at all, because the payments team never agreed to it.

Before you start

You need: Python 3.10 or newer (on Windows, use py where the steps say python). No AI account: the practice model runs on your computer. The agents folder from the earlier tasks. Time: 25 minutes.

Where to look

  1. agent.py
  2. WRITE_TOOLS
  3. approve()

Steps

  1. Add the restart_service tool and its function from Commands.
  2. Add WRITE_TOOLS and approve(), and call approve after validate in run().
  3. Run python agent.py "Restart shop-api". The agent stops and asks; type no. The step shows refused and nothing restarts.
  4. Run it again and type yes: the practice restart runs.For scripts and tests, AGENT_APPROVE=no (PowerShell: $env:AGENT_APPROVE="no") answers for you.
  5. Ask python agent.py "Check shop-api status" and see no question asked: reads don't need approval.

Commands

def restart_service(service): return f"restarted {service} (practice: nothing really restarted)"
# Add to TOOLS:
{"name": "restart_service", "description": "Restart a service. Changes production: needs approval.", "input_schema": S}

WRITE_TOOLS = {"restart_service"}
def approve(tool, args):
    if tool not in WRITE_TOOLS: return True
    if os.environ.get("AGENT_APPROVE") in ("yes", "no"):          # for tests and pipelines
        return os.environ["AGENT_APPROVE"] == "yes"
    return input(f"Approve {tool}({args})? Type yes to allow: ").strip().lower() == "yes"

# In run(), between the if error and else lines:
elif not approve(tool, args): result = "error: a person refused this action. Do not try it again."

python agent.py "Restart shop-api"

Watch out for

Cost
Free.
Security
Approval prompts must show the real tool and arguments, never the model's summary of them. Give the agent its own identity with read-only rights, and a separate, narrow one for the few write tools.
Common mistakes
Asking for approval so often that people say yes without reading, and putting rules only in the prompt ('never restart payments'), which a model can ignore.

Clean up

Keep the agents folder for the next tasks in this section; delete it at the end.

Words to know

Guardrail
A rule in your code that limits what the agent can do, whatever the model says.
Human in the loop
A person approves an action before it runs.
Write tool
A tool that changes something.
Least privilege
Only the rights a job needs.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going