An agent loop with MCP tools can keep calling tools without ever answering: each tool result suggests another tool call. The fix is a hard cap on tool calls per task and a rule that, when the cap is reached, the model must answer from the results it already has. Set the cap before the task starts, not after the loop has run.
Prerequisites
- An agent or client that calls MCP tools in a loop.
- A way to set a maximum number of tool calls, in the client or in your own loop code.
- A task with a clear done condition, so you can tell a loop from progress.
Steps
Step 1: Spot the loop
Watch the call log: the same tool called with the same or nearly the same arguments, or a growing chain where each result adds no new information, is a loop. Note the call count when you spot it.
Step 2: Cap the calls
Set a maximum, such as 5 tool calls per task, in the client or loop code. When the cap is hit, stop calling tools even if the model asks for one more.
Step 3: Force an answer
After the cap, send one final message: answer from the tool results already collected, and say what is still missing. Record the missing piece as a follow-up task instead of letting the loop continue.
Worked example: Bluebell Bakery supplier lookup
The fictional Bluebell Bakery asks an agent to find a flour supplier. The agent calls search_catalogue 6 times with the same query before the cap stops it.
| Call | Tool | New information | Action |
|---|---|---|---|
| 1 | search_catalogue | 3 suppliers listed | Continue |
| 2 | search_catalogue | Same 3 suppliers | Continue |
| 3 | search_catalogue | Same 3 suppliers | Loop spotted |
| 4-5 | search_catalogue | Same 3 suppliers | Cap of 5 reached |
| Final | Answer from results | Lists the 3 suppliers | Done |
The loop is stopped at 5 calls, and the final answer lists the 3 suppliers found in the first call, with the missing price noted as a follow-up.
Expected result. The agent answers from the results it has once the cap is reached, instead of calling the same tool again. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- A cap that is too low stops useful work; a cap that is too high wastes calls. Start at 5 and adjust from the call log.
- A cap does not fix a bad tool description; if the model keeps calling the wrong tool, fix the description.
- This guide does not cover billing or rate limits, which are a separate cap.
