Preventing Coding Agent Oscillation with Token Budgets
说明与用途
工程师发现由于缺乏令牌限制,编码代理产生了失控的差异。解决方案是一个Python包装器,用于限制令牌使用量和编辑次数,防止无限优化循环。
问题
Coding agents can enter infinite loops or oscillate between design choices when given excessive token budgets, resulting in massive, unnecessary diffs and wasted resources.
结果
The implementation of a token-budget harness successfully prevented runaway agent behavior. By treating the token budget as a first-class constraint similar to a test assertion, the workflow ensures agents produce minimal, correct diffs rather than exploring indefinitely.
工作流
- Identify that the agent is oscillating or producing excessive diffs despite passing tests.
- Create a Python harness script (budget_harness.py) to wrap the agent execution.
- Implement a token estimation heuristic (e.g., characters / 4) to track usage.
- Implement a diff analysis function to count edits per file using git diff.
- Set strict constraints: a maximum token budget (e.g., 150,000) and a maximum number of edits per file (e.g., 8).
- Run the agent through the harness, which terminates the process if limits are exceeded.
- Review the failed run logs to adjust prompts or constraints if necessary.
证据
Three hours later I returned to a two-thousand-line diff for what should have been a forty-line change.
The root cause was not the model and not the prompt; it was the absence of a budget as a first-class constraint.
The fix was a small Python harness that treats token budget the way a test treats an assertion.
- 记录日期: 2026-09-06T22:21:25.803Z