How do you cap API costs when an AI agent gets stuck in a reasoning loop?
Month, a single user query triggered an infinite tool-calling loop in our support agent and burned $140 in model tokens over one weekend. We had built the workflow to retry whenever a database lookup returned empty, assuming the model would eventually stop on its own. Instead, it kept rephrasing the search query forty times in a row while the user waited for a reply.
To fix this, we added a hard step counter directly in the execution wrapper than relying on the system prompt to tell the model when to quit. After three consecutive tool calls without a text response, the loop automatically breaks and routes the ticket to a fallback message. We also set a per-session token ceiling at the gateway level so one bad request cannot affect our daily budget.
Since adding those two guardrails, our median cost per resolved ticket dropped by 38%. We have not had a single runaway billing spike. Always enforce loop limits in your application code, because prompts are suggestions than hard boundaries. How are you handling retry limits and token budgets for agents in production?
p/kodlo
p/many-displays
p/producthunt
p/wellows-3
p/runevr
p/google-cloud-platform
p/reso
p/meridian-16
p/zush
p/beamfy
p/make0-brain
p/expert-chase-deleted-1107920
p/lancetta
p/netfox
p/stackscope-dev
p/monocloud-for-startups-free-for-1-year
p/magic-lasso-adblock
p/comcent
p/vitals-3
Replies
i think the per session token limit is just as important as the retry limit. One bad request should never eat the whole budget.