Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:1jehuang Jcode Bounded Retries

From Leeroopedia
Knowledge Sources
Domains SDK, Reliability
Last Updated 2026-10-18 00:00 GMT

Overview

Structured output correction has a maximum retry count to prevent infinite correction loops when LLM output keeps failing schema validation.

Description

The Bounded_Retries heuristic caps the number of correction attempts when an LLM's response fails JSON Schema validation. The runStructured() method in the TypeScript SDK accepts a maxRetries option, validated by assertRetryCount() to ensure it is a non-negative safe integer. When the LLM output fails validation, the SDK sends a correction prompt back to the model with the validation errors, up to maxRetries times. After exhausting retries, the last error is returned to the caller.

Usage

Use this heuristic when calling client.runStructured() for JSON Schema-validated responses. Set maxRetries to a reasonable value (default is typically 2-3). This prevents infinite loops when the model consistently produces invalid output.

The Insight (Rule of Thumb)

  • Action: Set maxRetries in RunStructuredOptions when calling runStructured().
  • Value: Must be a non-negative safe integer (validated by assertRetryCount).
  • Trade-off: Higher values give more correction chances but increase latency and token cost; lower values fail faster.
  • Compatibility: Uses AJV for JSON Schema validation; correction prompts include validation errors.

Reasoning

Without a retry bound, a model that consistently produces schema-invalid output would cause the SDK to loop indefinitely, consuming tokens and never returning. The assertRetryCount guard ensures the value is sane (non-negative, safe integer) before beginning the correction loop.

Code Evidence

Retry count validation from sdk/typescript/src/structured.ts:116-120:

export function assertRetryCount(maxRetries: number): void {
  if (!Number.isSafeInteger(maxRetries) || maxRetries < 0) {
    throw new HarnessError("invalid_request", "maxRetries must be a non-negative safe integer");
  }
}

Usage in structured output from sdk/typescript/src/client.ts (runStructured):

const result = await client.runStructured(session.session_id, "Classify this issue", {
  schema: { ... },
  maxRetries: 2,
});

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment