Cap AI agent retries and budgets
What stops your AI agent from retrying again and again? Param Harrison explains that retry until it works fits an API, where a failure has a clear cause. In a probabilistic system it gets costly. Set a maximum iteration no matter the result. Log and trace the failure and route it to a human. Also set a budget for each task and stop when the task goes past it. A gateway like LiteLLM lets you give each agent or person its own key and budget. After this lesson you can set limits in your code and gateway, not just at your cloud provider.
What this covers: retries, max iterations, task budgets, human in the loop, and a stolen API key. It also covers gateways like LiteLLM, slow cloud dashboards, and turn limits in agent frameworks.
Full session: Cut AI Agent Costs with Token Economics
#aiagents #llm #aicost
Chapters
- Retry until it works is costly
- Set a max iteration and a budget
- A stolen key with no budget cap
- Dashboards lag, so meter in code
- Agent frameworks and their turn limits
Transcript
Retry until it works is costly
Most of the time, this is one thing we will we often do, like we retry until it works. that will work on a probably that will work on an API kind of system where it is purely deterministic and the retry is failing because of network error or so, you will be able to infer what is the root cause of it. But in a probabilistic system, that will be very very costly.
Set a max iteration and a budget
So it's better to put maximum iteration regardless of what the result is going to be. If it is a failure, it is a failure. You need to log and trace it and understand what is the cause of the failure, and you need to root to human in the loop to avoid you know accumulating the cost by trying again again and again. This is one of the things what autonomous agents often do, like if you are not providing that that gate, then automatically it can go rogue and it can cause a heavy loss in loss in retrieval, and also one of the other things you need to put maximum iteration is one thing, another thing is you need to put how much of retrieval budget you can put like what is the cost which has happened if the cost is like going above what a single task need to have what is the maximum budget for a task then if that if if that is more than that then you need to cut from there and say like hey this need a human attention and this can't go like
A stolen key with no budget cap
this is what happened for a three person startup so basically they spend 80 dollars 180 dollars in with Gemini and then the a stolen key was they their key was stolen and their cost goes like 82k in 48 hours this is mainly because they are you know there is no budget cap which has been set on the layers where you know on the routing level on the agentic level you need to set that so for example why gateway solutions exist like LiteLLM kind of gateway why do we need that mainly for configuring like okay I am building this agent and for this agent or I am giving key for this person and this is the budget and you can able to clearly create your your goals over here so on the actual system you will be creating only one key but on your on your gateway you will be creating multiple keys using that one key and you will be controlling on the source level as well as on the destination level on the source level itself it will stop like okay. This key has already spent this much, even though on the destination level you might have budget, but this key which is specific to this experiment, it has already you know spent its budget. So source level as well as destination level, if you are able to control, then you will have multi-guarded budgeting system to operate on. Okay. Now
Dashboards lag, so meter in code
this is something from cloud itself. the cloud oftentimes have a very different way of very different way of showcasing what you are consuming because there will be a lag in terms of your data. This is where you need to, yeah. Once again, I am going into the same, like how a gateway solution is very important because oftentimes when you go and saw your dashboard, it might be lagging four hours or even 12 hours of data, it might not show the ground reality of what is the cost which has been accumulated in the last few minutes or so. So it is better to you know have a clear meter on your code base and clear meter on your gateway itself, so that you will be able to control that, not just your cloud provider. The cloud provider budget is for financial team, but the but your code base and gateway solution is for engineers and product managers to control, so both are needed. The cloud is more like budget, what your finance or your CFO is approval approving for that particular solution. You can you you don't need to play around that number often, but you need to play around you you the number you are controlling in your in your execution.
Agent frameworks and their turn limits
For example, open AI agent SDK have a maximum turn of 10 turns, and LangGraph can even go 10,000 steps. Claude agent doesn't even have any limit. It it it by default it has no limit, so it can run again and again and again all the time.
This lesson comes with Pro, $149 a year: Get Pro. This lesson is cut from the free Lightning session Cut AI Agent Costs with Token Economics. The Live Labs build the same system with you, free, on Thursdays: see the Live Labs.
