learning-it by example

#24 AI agents and MCP 4.9 min

Approval Gates for Risky Agent Actions

My prompt told the agent to ask before deleting. It deleted the file anyway. Here is a gate in code that stopped it.

1. In one run with a small local model (llama3.2) and no approval gate, the prompt said to ask the user to confirm before deleting any file. What happened when it was asked to delete one?
2. With the approval gate and a stand-in person who always says no, what happened to the file old_report.txt?
3. In the approval gate, what happens when the approver for a risky tool crashes or never answers?
4. If the approver for a risky tool answers with the text yes please, does the tool run?
5. What does the Python approval gate for risky agent tools not check?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Run the six gate checks

    Copy the code folder first. Run python gate_checks.py. This needs no model. Check that the output matches the video: True, True, False, False, False, False. For each of the six lines, write one sentence on why the gate gave that answer.

    A hint

    Open gate.py and follow the check method: safe, risky or unlisted.

  2. Add two more cases

    In gate_checks.py add two cases to CASES. First, a risky tool whose approver returns the number 1 instead of True. Second, a safe tool, list_files, with an approver that crashes. Predict both answers, then run the file. When we ran them we saw False for the first and True for the second.

    A hint

    The crash function is already defined at the top of the file.

  3. Read the decision log

    In a Python prompt, create g = Gate(lambda t, a: True) and call g.check three times: with list_files and an empty dictionary, with delete_file and {'name': 'a.txt'}, and with format_disk and an empty dictionary. Then print the lines in g.log. Predict the three words at the start of each line before you run. We saw allowed, approved and refused.

    A hint

    Import Gate from gate. The log is a list of strings on the gate object.

  4. List a new risky tool

    Call Gate(lambda t, a: True).check('rename_file', {'old': 'a', 'new': 'b'}) and predict the answer. Then add rename_file to the RISKY set in gate.py and call it again, once with an approver that says True and once with one that says False. When we ran this we saw False first, then True and False. Start a new Python prompt after editing gate.py, because an open prompt keeps the old file and would show False again.

    A hint

    A tool that is on neither list is refused, even if the approver would say yes.

  5. Predict a sloppy yes with the model

    In demo.py change the gate-yes entry in PEOPLE so it returns the text yes instead of True. With the Ollama app running, predict what python demo.py gate-yes prints about old_report.txt and what the log says. Then run it and compare. This is a prediction task: we did not run the changed demo.

    A hint

    The model should still ask for the delete in the same way. Think about what the gate does with an answer that is not exactly True.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

One small local model, one request and one tool, fixed settings, one run. The person is a scripted stand-in that always says yes or always says no; real approval needs a screen, a timeout and someone who reads it. The gate decides whether a tool runs and does not check its arguments. The files are examples in a temporary folder.

Up next in AI agents and MCP

More in AI agents and MCP

All AI agents and MCP videos

Related videos