Preventing Coding Agent Oscillation with Token Budgets
Описание и применение
Инженер столкнулся с тем, что кодировочный агент MonkeyCode создавал огромные диффы из-за отсутствия ограничений на токены. Решение заключалось в создании 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