The application executes the tool requests a model returns. If the model keeps asking for another lookup, your loop decides whether to keep spending time and memory or stop with the evidence already collected.
Ollama’s chat API can return several tool calls in one response. Count both rounds and individual calls, limit each tool result, and reject missing or unknown tool names. The loop below also gives Ollama fetches a shared deadline; tools need cancellation or execution limits appropriate to their own work.
A bounded local example
Prerequisites: Node.js 20 or newer, Ollama running on localhost, and a model that supports tool calling already available in Ollama. This example exposes only a bounded multiplication function. It makes no external requests besides the local Ollama API.
Save as bounded-agent.mjs and run OLLAMA_MODEL=your-model node bounded-agent.mjs (PowerShell: $env:OLLAMA_MODEL='your-model'; node .\bounded-agent.mjs ).
const model = process . env . OLLAMA_MODEL ; if ( !…