К содержанию
Статья ещё не опубликована на языке 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

GuardrailRecommended DefaultWhat It Prevents
Max Reasoning Steps4 to 8 iterations per user turnInfinite investigate-retry loops
Cumulative Token BudgetFixed cap per run (input + output)Context snowballing from large tool outputs
Wall-Clock Deadline15 to 45s (interactive); longer for async jobsHanging HTTP connections and poor UX
Duplicate Call DetectorBlock identical (tool, args) repeated > 2xModel hammering a failing or empty query
Per-Tool Payload CapTruncate large tool outputs before re-injectionSingle 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.

Было ли это полезно?