#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.
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.
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.
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.
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
- #01 Local AI Agent in Python: No API Key (Ollama)
- #12 Multi-Agent Patterns in Plain Python: Pipeline, Router, Loop
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python
- #23 Agent Memory: What to Keep and What to Forget
- #24 Approval Gates for Risky Agent Actions
- #25 Tool Errors: Retries, Fallbacks and Made-Up Answers
Related videos
- #231 MCP Gateway: Per-Tool Policy for Agent Tools (Python) Enterprise architecture and AI agents
- #92 Excessive Agency: Limit What Your AI Agent Can Do AI security and governance
- #06 Test Your LLM App with pytest: An Eval Harness in 40 Lines LLM engineering and evals
- #102 Red-Team Your AI Agent With a Checklist AI security and governance
