An agent API bill can rise because of more requests, longer inputs, more output, weaker cache reuse or a change in applicable rates. A single user request can also trigger several model calls, browser operations and retries. Start by separating workload volume from unit pricing before choosing an optimization.
Follow one task from start to finish
Assign a task identifier before the first model call. Attach it to planning, tool calls, recovery attempts and the final result. Record provider-reported usage alongside the model and billing category; keep tool and infrastructure charges separately.
For a hypothetical task with six calls, count all six. The last call might produce a short answer while earlier calls processed most of the input. A final-message token counter would miss that history.
Separate volume from price
Input, output and cached input can have different rates. A raw total of one million tokens therefore does not describe one fixed cost. Use the categories and applicable rates from the provider’s billing records. Check whether a category is already included in a reported total before adding it again.
A provider’s bill remains the source of truth. Your application’s estimate should explain any difference, including rounding, failed requests, delayed usage records, tool charges or a pricing change. Do not silently replace missing usage with zero.
Look for repeated work
Compare similar tasks. Did the agent load the same long tool output on every step? Did it keep asking for the same page? Did a retry repeat an action that had already succeeded? These are different causes and need different fixes.
Cache stable context where supported, reuse validated procedures where appropriate, and inspect uncertain outcomes before retrying. Switching models may lower the price of each call, but it will not explain why a workflow suddenly makes twice as many calls.
Set a budget that includes failure
Choose a maximum spend or bounded step count for each task class. Define a useful stopping point and an observable success condition. When a task exceeds its budget, retain enough diagnostic information to understand the failure without exposing private page content.
Review cost per successful task alongside failure rate. A low average bill is not an improvement if users must manually redo the work. Start with one frequent workflow and change one cost driver at a time.
A minimal task-level cost record
Start with one row per provider request: task ID, request ID, model, timestamp, reported usage categories, estimated charge, outcome and retry reason. Store browser infrastructure charges in a separate linked record so they are not counted twice. For one completed task, sum every linked attempt, then compare that estimate with provider billing. Missing usage should remain visibly unknown, not be filled with zero.
Sign in ↗