{"record":{"id":"dc379e7c9831dd0c","repo":"JuliusBrussee/caveman","slug":"retry-loop-interrupted-tool-call-json-stringify","errorCode":null,"errorMessage":"retry loop interrupted: tool call ${JSON.stringify(signature)} repeated ${repeats} times (threshold ${threshold})","messagePattern":"retry loop interrupted: tool call (.+?) repeated (.+?) times \\(threshold (.+?)\\)","errorType":"exception","errorClass":"RetryLoopError","httpStatus":null,"severity":"error","filePath":"packages/sdk/typescript/src/index.ts","lineNumber":694,"sourceCode":"      serialized = String(args);\n    }\n    return `${name}(${serialized})`;\n  }\n\n  /**\n   * Record a tool call. Throws {@link RetryLoopError} once an identical call has\n   * repeated past the threshold. A different call resets the streak.\n   */\n  record(name: string, args: unknown): void {\n    const sig = this.signature(name, args);\n    if (sig === this.lastSignature) {\n      this.repeats += 1;\n    } else {\n      this.lastSignature = sig;\n      this.repeats = 1;\n    }\n    if (this.repeats > this.threshold) {\n      throw new RetryLoopError(sig, this.repeats, this.threshold);\n    }\n  }\n\n  /** Record the call (may throw) then invoke `fn`. */\n  async guard<T>(name: string, args: unknown, fn: () => Promise<T> | T): Promise<T> {\n    this.record(name, args);\n    return fn();\n  }\n\n  /** Clear the streak (e.g. when starting a new task). */\n  reset(): void {\n    this.lastSignature = null;\n    this.repeats = 0;\n  }\n}\n\n// ─── Async job client ─────────────────────────────────────────────────────────\n","sourceCodeStart":676,"sourceCodeEnd":712,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/typescript/src/index.ts#L676-L712","documentation":"RetryLoopGuard detects the model (or caller) repeating the same tool call with identical arguments beyond a threshold and throws RetryLoopError to break runaway loops. `guard(name, args, fn)` records each call and refuses execution once the same signature repeats too many times.","triggerScenarios":"The LLM keeps issuing the same tool call with identical name+args (e.g. a search tool returning results that don't change the model's next attempt) more than `threshold` times through `guard`.","commonSituations":"Model loops retrying a failing search/read; a tool whose output doesn't give the model enough signal to change strategy; a threshold set too low for legitimately repeated idempotent calls.","solutions":["Fix the underlying tool failure or make its output informative so the model changes strategy","Raise the guard threshold for tools where repeats are legitimate","Catch RetryLoopError and inject a corrective prompt/tool result telling the model to try a different approach"],"exampleFix":"// before\nconst result = await guard.guard(\"search\", args, () => search(args)); // throws after N repeats\n// after\ntry {\n  const result = await guard.guard(\"search\", args, () => search(args));\n} catch (e) {\n  if (e instanceof RetryLoopError) {\n    return instructModelToTryDifferentApproach(e.message);\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"let lastSig = null, repeats = 0;\nif (JSON.stringify([name, args]) === lastSig && ++repeats > threshold) {\n  throw new Error(\"would exceed retry threshold\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await guard.guard(name, args, fn);\n} catch (e) {\n  if (e instanceof RetryLoopError) {\n    console.warn(`retry loop on ${e.signature}; nudging model`);\n    return correctiveResponse(e.message);\n  }\n  throw e;\n}","preventionTips":["Make tool outputs explicit about failure causes so the model varies strategy","Set thresholds appropriate to tools where repeated idempotent calls are valid","Monitor RetryLoopError rates to find looping prompts"],"tags":["typescript","retry-loop","agent","guard"],"backgroundTag":"infinite-retry-loop","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}