learning-it by example

#27 AI agents and MCP 5.7 min

Agent Guardrails: Limit Steps, Cost and Tools

My agent loop could run forever. Here are the three limits that stop it, and one thing I did not expect from a real model.

1. Which three limits can be built around a small agent loop to stop a runaway agent?
2. With a token budget of 200 and a scripted stand-in that spends 80 tokens a step, how many tokens had been spent when the agent stopped?
3. In the video's single run of llama3.2 through Ollama (temperature 0, seed 1), the agent had a lookup tool that always said no result and a loose limit of 10 steps. What did the model do?
4. When testing an agent guard, why use a scripted stand-in that never stops instead of only a real model?
5. For an agent loop, what do a step limit and a token budget cap?

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

  1. Run the three limits

    Copy the code folder first. Run python stuck.py. It uses a scripted stand-in, so the model is not asked anything. Check the three lines against the video: the step limit stops at 4 steps and 320 tokens, the token budget stops at 3 steps and 240 tokens, and the tool list stops delete_file at step 2.

    A hint

    The stand-in always spends 80 tokens per step, and the delete_file call is scripted as 60.

  2. Raise the step limit

    In stuck.py change the first Guard to allow 6 steps instead of 4. Predict the steps and tokens in the printed line, then run it. We saw stopped: step limit 6 (steps 6, tokens 480). The label before it still says step limit 4 because it is only text, so edit it if you like.

    A hint

    Each step spends 80 tokens.

  3. Find where the budget overshoots

    Change the token budget in the second line of stuck.py from 200 to 160, then to 240, then to 241. Before each run predict the steps and tokens at the stop. We saw 2 steps and 160 tokens, then 3 steps and 240 tokens, then 4 steps and 320 tokens. The label token budget 200 stays on the line because it is only text. Write one sentence on why 241 overshoots by so much.

    A hint

    The budget is only checked before a step starts, and each step spends 80 tokens.

  4. Allow the dangerous tool

    In stuck.py add delete_file to the TOOLS list. Predict what the tool allowlist line now prints, then run it. We saw answered after 3 steps with the answer 'done'. Write one sentence on why an allowed list is a safety limit and why adding a tool to it should be a careful choice.

    A hint

    The scripted stand-in calls lookup, then delete_file, then finish. In this folder the guard only checks the name, and the loop only runs lookup.

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

What this does not show

One small model (llama3.2), one question and one lookup tool that is broken on purpose. The stand-in that never stops is scripted, not a model. No time limit and no repeat detection is built.

Up next in AI agents and MCP

More in AI agents and MCP

All AI agents and MCP videos

Related videos