Immutable solution version
Agent loops with cost guards: task budgets, caps, and graceful stops
Complete solution
Long agentic loops need a token budget the model can see plus hard caps your code enforces. Use task budgets for self-regulation, max_tokens per turn as a backstop, and context editing so old tool results do not eat the budget. Never rely on vibes to end a loop.
Build the cost guardrails before the loop runs long.
- Set a task budget for the whole loop. Add taskbudget under outputconfig with the beta header task-budgets-2026-03-13. The model sees a running countdown of its remaining tokens across thinking, tool calls, tool results, and output, and uses it to prioritize and finish gracefully instead of cutting off mid-action. Check the docs for which models support it: unsupported models ignore it silently or reject it.
- Pair it with effort, not instead of it. The effort parameter controls how thoroughly the model reasons about each step; the task budget caps total work across the loop. High effort with a small budget makes the model rush; match them to the task.
- Know what the budget does not tell you. The countdown is visible only to the model. API responses carry no remaining-budget field, so your code cannot read it: enforce your own hard caps in parallel.
- Enforce hard caps in code. Cap total turns, total input tokens, total output tokens, and wall-clock time per task. When a cap trips, stop the loop and return a partial summary, never another model call to "wrap up".
- Clear stale context as the loop grows. Use context editing to drop old tool results that no longer matter and to clear thinking blocks when using extended thinking. Server-side compaction is the preferred strategy for long conversations; the fine-grained clearing is for agentic loops with heavy tool use where you need control over what gets dropped.
- Log usage per turn. Record inputtokens, outputtokens, cachereadinputtokens, and cachecreationinputtokens from every response's usage object. Alert when a task crosses 80 percent of its budget so you can inspect before it hits the wall.
Evidence
Claims verified against Anthropic's official docs (platform.claude.com) on 2026-09-26. Evidence URLs in public_references.
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_AO4PtOmLqJpl6c2hUGiBOQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Agent loops with cost guards: task budgets, caps, and graceful stops <span>— Vectle</span></a>