Статья ещё не опубликована на языке Russian. Показана версия на English.
Preventing Runaway Agents: Step Budgets, Token Caps, Timeouts, and Safe Fallbacks
Опубликовано
2 мин на чтение
An unconstrained agent encountering a flaky API or an ambiguous user request can easily repeat the exact same tool call 20 times in a row, racking up tens of thousands of tokens and timing out the caller. Hard execution guardrails make worst-case cost and latency strictly predictable.
Essential Execution Guardrails
| Guardrail | Recommended Default | What It Prevents |
|---|---|---|
| Max Reasoning Steps | 4 to 8 iterations per user turn | Infinite investigate-retry loops |
| Cumulative Token Budget | Fixed cap per run (input + output) | Context snowballing from large tool outputs |
| Wall-Clock Deadline | 15 to 45s (interactive); longer for async jobs | Hanging HTTP connections and poor UX |
| Duplicate Call Detector | Block identical (tool, args) repeated > 2x | Model hammering a failing or empty query |
| Per-Tool Payload Cap | Truncate large tool outputs before re-injection | Single huge JSON blob blowing context window |
Implementing a Duplicate Tool-Call Circuit Breaker
ts
export class LoopGuard {
private readonly callCounts = new Map<string, number>();
constructor(private readonly maxIdenticalCalls = 2) {}
checkAndRecord(toolName: string, args: unknown): { allowed: boolean; reason?: string } {
const signature = `${toolName}:${JSON.stringify(args)}`;
const next = (this.callCounts.get(signature) ?? 0) + 1;
this.callCounts.set(signature, next);
if (next > this.maxIdenticalCalls) {
return {
allowed: false,
reason: `Tool "${toolName}" already called ${this.maxIdenticalCalls} times with identical arguments. Stop retrying and summarize available information.`,
};
}
return { allowed: true };
}
}
Graceful Degradation Beats Hard Failures
When a step cap or token cap triggers, inject a brief system reminder telling the model that tool execution is now closed and asking it to deliver the best honest partial summary (or escalate to a human). Users receive a helpful response instead of an opaque error banner.
Next: test agent behavior systematically before every release in Evaluating Tool-Calling Agents.
Было ли это полезно?