◉CLAUDE LABJP
●CC 2.1.330 — Claude Code 2.1.330 fixes the multi-minute freeze right after compacting a long conversation●SONNET 4.5 — Sonnet 4.5 retires from the Claude API on 11/30, 8 days left. The suggested replacement is claude-sonnet-5-5●MCP 401 — Users report MCP servers that return 401 time out silently instead of showing an error●NEW — New: Desktop creates a worktree for every session? Three places to check●OPUS 4.5 — The 11/24 date for Opus 4.5 is a not-sooner-than floor, not a retirement notice●SCHEMA — Same schema, different shape: compare structured output from the Agent SDK and the Messages API●CC 2.1.330 — Claude Code 2.1.330 fixes the multi-minute freeze right after compacting a long conversation●SONNET 4.5 — Sonnet 4.5 retires from the Claude API on 11/30, 8 days left. The suggested replacement is claude-sonnet-5-5●MCP 401 — Users report MCP servers that return 401 time out silently instead of showing an error●NEW — New: Desktop creates a worktree for every session? Three places to check●OPUS 4.5 — The 11/24 date for Opus 4.5 is a not-sooner-than floor, not a retirement notice●SCHEMA — Same schema, different shape: compare structured output from the Agent SDK and the Messages API
Articles/API & SDK
⬡ API & SDK/2026-06-27Advanced

Designing the Give-Up Condition in Self-Repair Loops: Four Error Classes, Four Retry Budgets

LLM self-repair loops break on the fantasy that 'if you keep fixing, it eventually passes.' Classify errors into four classes, give each its own retry budget. Working TypeScript and real cost numbers included.

Claude API148self-repairretry designerror handling5production111

✦ Premium Article

Late at night, the cost of an unattended generation pipeline spiked to three times the previous day.

Nothing had crashed. The opposite, in fact: every job was logged as "finally succeeded." Tracing it, one job had rebuilt the same output 27 times. The loop had dutifully thrown "try again" at an output that could never pass validation.

LLM self-repair loops have a quiet trap. Write the loop on the assumption that "fixing makes it pass," and it will try to fix errors that are unfixable, forever. What matters in production is not how cleverly you repair, but when you decide to stop.

This piece covers a design that classifies errors into four classes and assigns a retry budget per class. The code is shown in copy-and-run form.

Why naive retry breaks in production

The loop most of us write first looks like this.

// Anti-pattern: keep fixing until it passes
async function generateUntilValid(prompt: string) {
  while (true) {
    const out = await callClaude(prompt);
    if (validate(out).ok) return out;
    prompt = `${prompt}\n\nThe previous output failed validation. Please fix it.`;
  }
}

This is fine when a human is watching interactively. They give up after a few rounds and fix it by hand.

It breaks under unattended operation. A bug in the validator, a constraint that can't be satisfied, a transient model hiccup — none of these are solved by "please fix it." Yet the loop keeps spinning and keeps burning tokens.

The root issue is treating all retries uniformly. A transient overload (429 or 529) and a structurally unsatisfiable constraint demand completely different actions. The former heals if you wait; the latter never heals no matter how many times you resend.

Classify errors into four classes

What actually drives the retry decision in production is this four-way split. The trick is to divide by "how should I respond," not by where the error originates.

ClassTypical examplesCorrect responseSuggested retry budget
transient429 / 529 / timeout / network dropWait with exponential backoff and resend. Do not change the prompt5–7 (including backoff)
repairableBroken JSON / schema mismatch / missing required fieldRebuild once or twice with the error attachedUp to 2
semantic-invalidFactual error / constraint violation / quality-gate failureDo not repeat the same request. Change the approach itself1 (with a different strategy)
hard-fail401 / 400 (bad input) / model not found / unsatisfiable constraintAbort immediately. Hand to a human or fallback0

The value of this split is that the give-up condition falls out naturally per class. hard-fail is zero; semantic-invalid is one, with a different strategy. Plain retry of the same request only ever makes sense for transient.

The distinction between repairable and semantic-invalid is the most important one in the design. repairable means "the shape is broken," so showing the error fixes it. semantic-invalid means "the content does not meet the requirement," so repeating the same ask just goes in circles. Those 27 attempts at the top were a textbook case of mistaking semantic-invalid for repairable.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦A classifier that maps errors into transient / repairable / semantic-invalid / hard-fail and assigns a separate retry budget per class (working TypeScript)
✦Why an unattended pipeline's token cost balloons several-fold without an explicit give-up condition, and how to set retry budgets and a hard cost ceiling
✦How to add structured attempt logs and fallbacks (switch strategy, hand off to a human) so a silent loop never spins forever undetected
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-11-18
Before Your Customers Pay Through Linked Billing: Reading a 402 or 403 to Find Whose Wallet Is Empty
Linked billing, released in beta on November 17, lets you choose who pays on every Claude API request through billing_policy. When a request billed to a customer comes back as a 402 or 403, here is how I work out which wallet is the cause, when to pay for it myself, and a small Python wrapper that keeps that decision in one place.
⬡ API & SDK2026-10-22
You Hit Your Limit and Got a 400 — Telling Apart Your Own Spend Limit, the Tier Cap, and Rate Limits
Claude API limit errors come in three kinds, and they differ in status code and in whether waiting helps. A limit you set returns 400, the tier cap returns 429, and so do ordinary rate limits. Code and a decision table.
⬡ API & SDK2026-05-24
Archiving Claude API Responses to Cloudflare R2: An Implementation Memo for Audit, Replay, and Incident Analysis
An implementation memo on persisting Claude API requests and responses into Cloudflare R2 so you can audit, replay, and triage production incidents — covering Workers waitUntil patterns, PII masking, and a D1 metadata index for fast lookups.