At 0:47 one March morning in 2026, my indie SaaS pager went off with Unexpected end of JSON input. A response from the Claude API had been piped into JSON.parse, and the closing brace simply was not there. The failure rate was eight requests out of about twelve hundred, or roughly 0.66%. That sounds like noise, until you notice one of those eight came from the billing flow and forced me to roll back a charge in the middle of the night.
The official "Use prefilled responses" page makes prefill sound airtight: stick a { in the assistant message and Claude will dutifully complete a JSON object. I believed that at first, too. But once you process thousands of requests a month, you start to see all the corners that simple framing does not cover. Responses get cut off when max_tokens is reached. A small fraction of calls suddenly take a detour into a tool_use block. Adding stop_sequence to keep things tidy ends up amputating nested structures. After running into every one of these in production, I converged on a four-layer pipeline that combines prefill with three defensive fallbacks. This article is the field journal of that design.
What hit me hardest about putting AI into a production flow was this: when the output is probabilistic, everything around it has to be deterministic — and that seam is usually the thinnest part of the codebase. Prompt technique gets written about endlessly. How you catch what comes back does not. The four-layer defense below is a field record of the catching side.
One more thing before we start. While revisiting this piece I re-ran the repair code I originally published against a truncation corpus, and found that the function I described as recovering "more than 80%" of truncations recovered exactly zero of them on any payload containing an array. That section is now rewritten with measured numbers, and I am sorry the broken version stood here as long as it did.
Before You Copy Anything: Prefill Returns a 400 on Claude 4.6 and Later
There was a second problem I could not ignore while revisiting this piece. The technique in the original title, prefill, no longer works on current models. Anthropic's docs say that Claude 4.6 and later models reject a request whose last message is an assistant turn:
| Model | Assistant-turn prefill |
|---|---|
| Claude Sonnet 4.5 / Haiku 4.5 and earlier | Accepted |
| Claude 4.6 and later (Sonnet 4.6, Sonnet 5.5, Opus 5.5 and others) | 400 invalid_request_error |
The error text is This model does not support assistant message prefill. The conversation must end with a user message. (source: the Claude Platform Docs errors page and the Sonnet 5.5 migration guide, as of October 2026). The documented replacements are structured outputs through output_config.format, instructions in the system prompt, or moving the text you wanted to continue into the user message.
My original code passed a prefill to claude-sonnet-4-6. Run it today and the API rejects it before any parsing happens. It worked when I wrote it in May, and a pipeline that dies the day you bump the model ID is precisely the kind of failure this article is supposed to warn you about.
I did not retire the piece, though, because the argument underneath it survives: a probabilistic output still needs a deterministic seam, even when the API holds the format for you. Only Layer 1 changes. Layers 2 through 4 carry over as they are. Layer 1 below is rewritten around output_config.format, and prefill moves to a short appendix for anyone pinned to an older model.
Three Production Failure Modes That Prefill Alone Cannot Cover
Prefilling, in the Claude API, means appending an assistant message to the messages array so the model continues from text you already supplied. Open with { "result": and Claude will, with high probability, finish a coherent JSON object. The trouble is that "high probability" and "100%" are very different animals when you are running production traffic.
Case A: silent truncation at max_tokens
In one of my products I needed to extract roughly thirty fields from a document. I had set max_tokens to 2,048. One day a particularly long input pushed the response past that ceiling, and Claude stopped mid-comma at token 2,049. The stop_reason was max_tokens. The opening { had landed because of prefill, but nothing closed it. JSON.parse had no chance.
Case B: collisions with tool_use blocks
When you enable tools and prefill { in the same call, Claude occasionally hesitates between continuing the JSON and emitting a tool_use block. The docs allude to this with a single sentence about behavior changes, but in practice I saw a handful of cases where Claude ignored the prefilled { and returned a tool_use instead. The downstream handler had been written assuming content[0].type === "text" and crashed with undefined.text. The frequency was below 0.1%, which is exactly the rate of bug you want to design out, not paper over.
Case C: stop_sequence cannibalizing the structure
Trying to be clever, I once added stop_sequences: ["}"] to terminate as soon as JSON closed. This is fine until any nested object appears. The first } from an inner object slammed the door before the outer one closed. That is the kind of self-inflicted wound that makes you respect your own future debugging more.
Accepting that prefill alone could not eliminate these three cases is what finally pushed me into a layered design. Cases B and C are failures you only get because you use prefill: with output_config.format there is no { to place, so B's entry point closes. Case A survives. The docs say plainly that when stop_reason is max_tokens the output may not match your schema.
The Four-Layer Pipeline at a Glance
The architecture has four independent layers, each able to fall back to the next:
- Layer 1 — Format lock hands a JSON Schema to the API through
output_config.format(older models can use prefill instead) - Layer 2 — Schema Validation checks the raw output with a strict Zod or Pydantic schema
- Layer 3 — Automatic Repair attempts mechanical fixes first, then asks Claude to repair if needed
- Layer 4 — Graceful Retreat returns a safe default and pages operations when everything else fails
The reason for separating these responsibilities is simple: deterministic recovery beats probabilistic recovery whenever you can use it. When an incident happens, being able to name the exact layer that failed narrows the investigation to a single entry point. I value that legibility more than I value the raw success rate.
Layer 1 — Locking the Format with output_config.format
On models that reject prefill, you hand the format to the API. Pass a JSON Schema in output_config.format and the response conforms to it. The older output_format parameter is deprecated and slated for removal, so use the new location.
// extract.ts — Layer 1: lock the JSON shape with output_config.format
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
// Numeric and length constraints (minimum, maxLength and friends) are not
// supported in the schema, so only types and required fields go here.
// Value ranges are checked in Layer 2 with zod.
export const RESULT_JSON_SCHEMA = {
type: "object",
properties: {
result: {
type: "object",
properties: {
title: { type: "string" },
tags: { type: "array", items: { type: "string" } },
score: { type: "number" },
},
required: ["title", "tags", "score"],
additionalProperties: false,
},
},
required: ["result"],
additionalProperties: false,
} as const;
export async function extractStructured(input: string): Promise<string> {
const response = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 4096, // Wider than you think you need; protects against Case A
system: "You are a strict JSON extractor.",
messages: [
{ role: "user", content: `Extract structured data from the following text:\n\n${input}` },
],
// An older SDK will show a type error here. Update to a release that knows output_config.
output_config: { format: { type: "json_schema", schema: RESULT_JSON_SCHEMA } },
});
// A response may open with a thinking block, so select by type, not by position
const block = response.content.find((b) => b.type === "text");
if (!block || block.type !== "text") {
throw new Error("No text block in response");
}
if (response.stop_reason !== "end_turn") {
// max_tokens or refusal: the output may not match the schema. Layers 2+ will catch it.
console.warn(`stop_reason=${response.stop_reason}`);
}
return block.text; // No prefill to prepend. Still no JSON.parse here.
}Three things differ from the prefill version. First, nothing to concatenate: the text block is the whole JSON document. Second, I stopped reading content[0] and look for the text block by type, since some responses begin with a thinking block. Third, I read stop_reason. The docs are explicit that a max_tokens stop can leave the output incomplete, and that a refusal returns a 200 whose refusal message takes precedence over the schema.
One easy-to-miss limitation: structured-output schemas cannot express minimum/maximum, minLength/maxLength, or array bounds beyond minItems of 0 or 1. That is why the schema above says nothing about "at most 20 tags" or "score between 0 and 1". The SDK helpers strip those constraints and re-validate the response in your own code. The division of labour becomes: the API guards the shape, you guard the values. It is the reason Layer 2 stays even when you adopt structured outputs.
I still leave stop_sequences empty by default, for the Case C reason above.
If You Are Pinned to a Model That Still Accepts Prefill
For Sonnet 4.5, Haiku 4.5 and earlier, here is the original approach, kept for reference.
// extract-legacy.ts — Sonnet 4.5 / Haiku 4.5 and earlier: start the JSON via prefill
export async function extractWithPrefill(input: string) {
const response = await client.messages.create({
model: "claude-sonnet-4-5", // Switching to 4.6+ returns a 400
max_tokens: 4096,
system: "You are a strict JSON extractor. Output must conform exactly to the requested schema.",
messages: [
{ role: "user", content: `Extract structured data from the following text:\n\n${input}` },
{ role: "assistant", content: '{\n "result":' },
],
});
const block = response.content[0];
if (block.type !== "text") {
throw new Error(`Unexpected content block type: ${block.type}`); // Case B detector
}
// The prefill is not part of the response, so prepend it yourself
return '{\n "result":' + block.text;
}Pinning to an old model is a loan, and the migration will come due eventually. Layers 2 and onward accept the same string from either Layer 1, so the day you switch it is a one-line change.
Layer 2 — Schema Validation with a Discriminating Failure Reason
Layer 2 takes the raw string, parses it, and validates against a schema. The catch is that you should distinguish two failure modes — a syntax break and a schema mismatch — because they want different recovery strategies.
// validate.ts — Layer 2: structural and syntactic validation
import { z } from "zod";
const ResultSchema = z.object({
result: z.object({
title: z.string().min(1),
tags: z.array(z.string()).max(20),
score: z.number().min(0).max(1),
}),
});
export type ValidatedResult = z.infer<typeof ResultSchema>;
export function validateRaw(raw: string):
| { ok: true; data: ValidatedResult }
| { ok: false; reason: "syntax" | "schema"; detail: string } {
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch (e) {
return { ok: false, reason: "syntax", detail: (e as Error).message };
}
const r = ResultSchema.safeParse(parsed);
if (!r.success) return { ok: false, reason: "schema", detail: r.error.message };
return { ok: true, data: r.data };
}A syntax error usually means truncation or an extra comma — fixable mechanically. A schema error means the structure exists but the values are wrong — that needs Claude's help. Conflating them wastes API calls and money. The .min(1) and .max(20) in this zod schema also carry the constraints that Layer 1's schema could not hold.
Layer 3 — How Much Mechanical Repair Actually Recovers (I Measured It)
This is the layer with the highest production payoff. It is also the layer where the code I originally published here was wrong.
3a: Re-measuring Syntax Repair
Start with a more permissive parser. json5 accepts trailing commas, single quotes, and inline comments. If even that fails, the original plan was to count brackets and close whatever was missing.
I re-ran that bracket closer exactly as published. The corpus: one JSON document shaped like the schema in this article — result containing title, a 20-element tags array, score, and a 6-element sections array, 1,101 characters in total. I truncated it one character at a time from offset 20 to the character before the end, producing 1,081 distinct "cut off mid-response" samples. A max_tokens ceiling can land anywhere, so every cut point was weighted equally.
Each sample went through three approaches, scored in two stages: (a) does JSON.parse accept the repaired string, and (b) does the parsed object then pass schema validation (title at least one character, tags an array of strings, score a number between 0 and 1)?
| Approach | Syntactically recovered | Passed schema |
|---|---|---|
| The bracket closer originally published here | 0 / 1,081 (0.0%) | 0 / 1,081 (0.0%) |
| Stack-based closer that tracks bracket type (fixed) | 1,026 / 1,081 (94.9%) | 588 / 1,081 (54.4%) |
json_repair 0.62.0 (Python) | 1,081 / 1,081 (100.0%) | 643 / 1,081 (59.5%) |
Zero. The function I described as recovering "more than 80%" recovered none of these. There were two bugs.
First, it appended } without ever looking at which bracket had been opened. A response truncated inside tags or sections needs a ], but the old code simply stacked closing braces until the counts matched. Of the 1,081 cut points, 1,022 land inside an array, and none of those can be recovered that way.
Second, the dangling-string check was inverted. The old code counted the quotes appearing before the final " and appended a closing quote when that count was odd. But in a response cut off mid-string, that final " is the opening quote, so the count before it is always even. The check declined to close a string in precisely the case where closing was required. That is why even the 59 cut points with no open array recovered nothing.
Here is the rewritten version. It keeps a stack of open brackets and closes each with its matching partner.
// repair.ts — Layer 3a: syntax repair (stack-based, corrected)
import JSON5 from "json5";
export function repairSyntax(raw: string): string | null {
// (1) Try JSON5: tolerates trailing commas, comments, single quotes
try {
return JSON.stringify(JSON5.parse(raw));
} catch {}
// (2) Track open brackets on a stack and close each with its match
const stack: string[] = [];
let inString = false;
let escaped = false;
for (const ch of raw) {
if (inString) {
if (escaped) escaped = false;
else if (ch === "\\") escaped = true;
else if (ch === '"') inString = false;
continue;
}
if (ch === '"') inString = true;
else if (ch === "{" || ch === "[") stack.push(ch);
else if (ch === "}" || ch === "]") stack.pop();
}
let candidate = raw;
if (inString) candidate += '"'; // truncated inside a string literal
candidate = candidate.replace(/[,\s]+$/, "");
// Drop a key whose value never arrived ("key": and bare "key")
candidate = candidate.replace(/,\s*"(?:[^"\\]|\\.)*"\s*:\s*$/, "");
candidate = candidate.replace(/\{\s*"(?:[^"\\]|\\.)*"\s*:\s*$/, "{");
candidate = candidate.replace(/,\s*"(?:[^"\\]|\\.)*"$/, "");
candidate = candidate.replace(/[,\s]+$/, "");
// Close in reverse order of opening, with the matching bracket
for (let i = stack.length - 1; i >= 0; i--) {
candidate += stack[i] === "{" ? "}" : "]";
}
try {
JSON.parse(candidate);
return candidate;
} catch {
return null;
}
}That gets to 94.9%. The remaining gap against json_repair's 100% comes down to finer cleanup rules, mostly around partially written keys. On the Python side I would simply use json_repair; if you want this layer in TypeScript without adding a dependency, the stack approach above is close to the minimum viable version.
The most useful thing the measurement surfaced was not the recovery rate at all. It was the 40-point gap between syntactic recovery (94.9%) and schema pass (54.4%). Closing brackets makes JSON.parse happy. It does nothing about the fields that were never emitted. In this corpus the most frequently missing field was score, absent in 438 samples, simply because it sits after the long tags array. The operational consequence is direct: anything Layer 3a returns must be sent back through Layer 2. That is why the integrated run.ts below re-runs validateRaw after every repair rather than trusting the repaired string.
Whatever survives syntax repair but fails the schema goes to Layer 3b, where Claude fills the gaps back in.
3b: Asking Claude to Repair the JSON
When mechanical repair gives up, ask Claude itself. Keep the prompt short, deterministic, and free of pleasantries.
// repair.ts — Layer 3b: Claude-driven repair
import { RESULT_JSON_SCHEMA } from "./extract";
export async function repairWithClaude(raw: string): Promise<string | null> {
const response = await client.messages.create({
model: "claude-haiku-4-5", // Repair is cheap and Haiku does it cleanly
max_tokens: 4096,
system: "You repair broken JSON to strictly match the given schema. No prose, no commentary.",
messages: [
{
role: "user",
content: `Repair the following broken JSON so that it conforms to the schema. Where a value is missing, use an empty value rather than guessing.\n\n[Broken JSON]\n${raw}`,
},
],
output_config: { format: { type: "json_schema", schema: RESULT_JSON_SCHEMA } },
});
const block = response.content.find((b) => b.type === "text");
return block && block.type === "text" ? block.text : null;
}I switched from Sonnet to Haiku 4.5 for repair because the task is mechanically simple: given a broken JSON, make it valid against a schema. Sonnet sometimes "improves" the data in ways that violate the original intent. Haiku tends to repair surgically. In my logs the success rate of Haiku-based repair sits at about 92%, at roughly 0.0008 USD per call as of May 2026.
Layer 4 — Graceful Retreat with a Distinct Operational Signal
For the sub-0.05% of requests that survive all three layers, you need a deliberate retreat. The principle is to separate what the user sees from what operations sees.
// run.ts — wiring up the four layers
import { extractStructured } from "./extract";
import { validateRaw } from "./validate";
import { repairSyntax, repairWithClaude } from "./repair";
export async function run(input: string) {
// Layer 1
const raw = await extractStructured(input);
// Layer 2
let v = validateRaw(raw);
if (v.ok) return { ok: true, data: v.data, layer: 1 };
// Layer 3a — mechanical repair
if (v.reason === "syntax") {
const fixed = repairSyntax(raw);
if (fixed) {
v = validateRaw(fixed);
if (v.ok) return { ok: true, data: v.data, layer: "3a" };
}
}
// Layer 3b — Claude repair
const repaired = await repairWithClaude(raw);
if (repaired) {
const r2 = validateRaw(repaired);
if (r2.ok) return { ok: true, data: r2.data, layer: "3b" };
}
// Layer 4 — graceful retreat
await alertOps({ raw, lastDetail: v.ok ? "" : v.detail });
return { ok: false, layer: 4, fallback: defaultFallback() };
}
function defaultFallback() {
return { result: { title: "", tags: [], score: 0 } };
}
async function alertOps(payload: unknown) {
// For solo work, a Slack webhook plus a Sentry breadcrumb is enough
await fetch(process.env.SLACK_WEBHOOK_URL!, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: ":warning: JSON 4-layer fallback used" }),
});
}My rule of thumb is: return an empty-but-shaped result to the user so the application keeps moving, and ping operations immediately so the rare miss gets investigated. Before this layer existed, I was being woken up roughly two or three nights a week. After it landed, the wake-up rate dropped below one per month.
A Python Sketch for the Same Pipeline
For Python users, here is the equivalent of Layers 1 and 2 with pydantic and json_repair. A caveat: the output of model_json_schema() contains constraints such as maxItems that structured outputs reject. The official SDK's client.messages.parse() strips them for you, and I would lean on that in production; treat the sketch below as a skeleton to show the structure. I lean on json_repair for what would otherwise be Layer 3a in TypeScript.
# extract.py — Python version of Layer 1 + 2 with built-in repair
from anthropic import Anthropic
from pydantic import BaseModel, ValidationError, Field
import json
import json_repair
client = Anthropic()
class Inner(BaseModel):
title: str = Field(min_length=1)
tags: list[str] = Field(max_length=20)
score: float = Field(ge=0.0, le=1.0)
class Result(BaseModel):
result: Inner
def extract(input_text: str):
resp = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=4096,
system="You are a strict JSON extractor.",
messages=[
{"role": "user", "content": f"Extract structured data from:\n\n{input_text}"},
],
# Schema generated from Pydantic; min/max style constraints stay with Layer 2
output_config={"format": {"type": "json_schema", "schema": Result.model_json_schema()}},
)
block = next((b for b in resp.content if b.type == "text"), None)
if block is None:
raise RuntimeError("no text block")
raw = block.text
try:
return Result.model_validate_json(raw)
except (json.JSONDecodeError, ValidationError):
# json_repair gives us a one-liner equivalent of Layer 3a
repaired = json_repair.repair_json(raw)
return Result.model_validate_json(repaired)The json_repair package on PyPI (GitHub link) is the single most underrated dependency in my Python stack. It quietly handles the long tail of broken JSON cases without me having to think.
Production Numbers: Before and After
Here are the actual numbers from my indie SaaS, which sits at about 4,200 monthly extraction calls:
- Before (prefill only)
- Parse success rate: 99.34% (28 of 4,200 failed)
- Monthly operational alerts: 11
- Mean time to manual recovery: 14 minutes (after a user reported it)
- After (four-layer defense)
- Parse success rate: 99.98% (1 of 4,200 reaches Layer 4, and even that user sees a usable empty shell)
- Monthly operational alerts: under 1
- Mean time to recovery: 0 minutes (automatic retreat)
- Additional cost: roughly 7 to 9 USD per month from Layer 3b Haiku calls
What mattered more than the numbers was how failures started reaching me. Before, I learned about them from a user report and fixed them at 2 a.m. Now a small bump appears in the Layer 3a counter on a dashboard and I look into it the next morning with a clear head. When you run AI in production on your own, operational quality translates directly into hours you get back.
Five Pitfalls Worth Naming
Here are the mistakes I actually made along the way. You can skip them.
- Do not use Sonnet for Layer 3b repair. It is more expensive, and it has a tendency to "improve" the data rather than restore it. Haiku stays surgical, which is what you want.
- Keep Layer 3b's prompt schema in sync with your validator. I had a week where my Zod schema added a field but the repair prompt still referenced the old one. Repair worked — into the wrong shape.
- A safe default is not always an empty default. For search results, an empty array is fine. For pricing or counters, returning
0can show users misleading information. Pick a fallback value that is safe in your domain, not just empty. - Measure your repair function against the shape of your own schema. This is the one that stung. The identical closing routine recovered 0% or 94.9% of truncations depending on nothing more than whether the payload contained an array. Whether you adopt a library or write your own, generate truncation samples from your real schema and run them through before you quote a recovery rate anywhere. The harness is about twenty lines.
- Search for prefill before you bump a model ID. This article's own correction came from skipping that step. Any call that ends its
messageswithrole: "assistant"will start returning 400s on Claude 4.6 and later. A repository-wide search the day before the switch is all it takes.
The same layered instinct, applied before the call instead of after it, is in A Five-Layer Preflight Design for Claude API. This article defends the response; that one defends the request. When the problem is the content of the output rather than its shape, Production-Grade Hallucination Defense for Claude API is the closest neighbour. And if Layer 3b's repair calls start showing up on your bill, the approach in How I Cut My Claude API Bill in Half With Prompt Caching transfers directly.
Observability: Know Which Layer Saved You
Once the pipeline is in place, the most useful operational metric is not the global success rate but the distribution across layers. Knowing that 0.40% of requests are saved by Layer 3a and 0.05% by Layer 3b tells you whether your prompts are deteriorating, whether max_tokens is too tight, or whether a recent schema change has introduced new failure modes.
// observability.ts — emit a counter per layer outcome
type LayerOutcome = "1" | "3a" | "3b" | "4";
export function recordOutcome(outcome: LayerOutcome) {
// Cloudflare Analytics Engine, Sentry breadcrumbs, or a Postgres counter all work
fetch(process.env.METRICS_ENDPOINT!, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ metric: "json_layer_outcome", outcome, ts: Date.now() }),
}).catch(() => {});
}In my dashboard I keep three trend lines: success-at-Layer-1 should hover above 99%, Layer 3a should stay under 1%, and Layer 3b should be vanishingly small. When Layer 3a starts climbing, it usually means the input distribution has shifted (a new client started sending longer documents) and max_tokens needs to grow. When Layer 3b climbs, it usually means the underlying prompt or schema has drifted and the fix is upstream.
Tying this to alert thresholds is straightforward. I page myself when the seven-day Layer 3b rate exceeds 0.2%, because that consistently correlates with a real upstream regression rather than transient noise. Anything below that is "interesting but not urgent" and gets reviewed at the weekly check-in.
Cost Math You Can Reason About
For solo developers worried about the marginal cost of adding repair calls, here is the model I use. Let r be the failure rate at Layer 1, m3a the share of those failures recovered by 3a, and m3b the share of remaining failures recovered by 3b.
- Cost added per request =
r * (1 - m3a) * cost_haiku_repair - For my numbers,
r ≈ 0.0066,m3a ≈ 0.83(measured on the Python path, where Layer 3a isjson_repair),cost_haiku_repair ≈ 0.0008 USD, giving roughly0.0066 * 0.17 * 0.0008 ≈ 0.0000009 USDper request, or under one ten-thousandth of a cent. At 4,200 monthly requests, that is barely measurable. Worth stressing after the correction above:m3ais a property of your repair implementation, not a constant. Substitute your own measurement before using this model.
The reason the cost is negligible is that Layer 3b only fires for the small slice that 3a failed on. The dominant cost remains Layer 1 itself, where you are paying full Sonnet rates for the actual extraction. If anything, this design lets you raise max_tokens on Layer 1 without anxiety, because the repair pipeline catches whatever truncation slips through.
If you want to push costs even lower, two changes work:
- Cache the system prompt and schema description with prompt caching. Layer 1 calls share the same system message, so you can capture roughly 60 to 80% of input-token cost at break-even after a few hundred calls per day.
- Move Layer 3b to a smaller off-hours model when latency is not user-facing. For batch jobs, I run Layer 3b on a queued worker that delays repair by up to thirty seconds without anyone noticing.
Production Edge Cases: Tools and Streaming
Tools and Structured Output in One Endpoint (Case B Revisited)
Case B was the rare collision between prefill and tool definitions. Without prefill the collision is gone, but an endpoint that needs both tool routing and a structured final answer still benefits from keeping the two jobs apart. This is the pattern I have settled on:
// dual.ts — separating tool routing from JSON extraction
import Anthropic from "@anthropic-ai/sdk";
import { RESULT_JSON_SCHEMA } from "./extract";
const client = new Anthropic();
export async function extractOrToolCall(input: string, tools: any[]) {
// First call: decide whether tools are needed; no output format attached
const decide = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 1024,
system: "Decide if any of the listed tools are required to answer the user. If yes, call the tool. If no, respond with the literal text NEED_EXTRACTION.",
tools,
messages: [{ role: "user", content: input }],
});
if (decide.stop_reason === "tool_use") {
return { kind: "tool", response: decide };
}
// Second call: pure extraction with a locked format, no tools attached
const extract = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 4096,
system: "Output strict JSON matching the schema.",
messages: [{ role: "user", content: input }],
output_config: { format: { type: "json_schema", schema: RESULT_JSON_SCHEMA } },
});
const block = extract.content.find((b) => b.type === "text");
return { kind: "extract", raw: block && block.type === "text" ? block.text : "" };
}The trick is to separate the routing decision from the extraction call. This adds one round trip, but only for the small fraction of routes that need tools. For routes that always extract, skip the routing step entirely.
Handling Streaming Responses Inside the Pipeline
Many deployments stream tokens to the client as they arrive. The four-layer design still applies, but you need to buffer the stream until the response completes and the stop_reason is known; otherwise validation has nothing solid to work on.
// stream.ts — buffering before validation
import Anthropic from "@anthropic-ai/sdk";
import { RESULT_JSON_SCHEMA } from "./extract";
const client = new Anthropic();
export async function streamThenValidate(input: string) {
const stream = await client.messages.stream({
model: "claude-sonnet-5-5",
max_tokens: 4096,
messages: [{ role: "user", content: input }],
output_config: { format: { type: "json_schema", schema: RESULT_JSON_SCHEMA } },
});
let buffer = "";
for await (const event of stream) {
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
buffer += event.delta.text;
// You can still ship deltas to the user UI here, just don't validate yet
}
}
return buffer; // Now hand this to validateRaw()
}For products with strict latency SLAs, you can ship the partial buffer to the client for visual feedback and run validation only once the stream completes. If validation fails, you fall through to Layers 3 and 4 server-side without exposing the failure to the front end.
When the Pipeline Is Overkill
A few situations where the four-layer pipeline is overkill:
- One-shot internal scripts with no SLA and no user. Just catch the parse error and retry once.
- Prompts that emit free-form prose, not JSON. This pipeline is purely for structured outputs.
- Use cases where the cost of a wrong answer is so high that you should not silently fall back at all. In that case, Layer 4 should hard-fail and surface the error to the caller rather than returning a default. The pipeline still applies, but the "graceful retreat" becomes a "loud refusal."
The right test for whether you need this is to look at how often you have manually recovered from a JSON parse failure in the last quarter. If the answer is "more than zero," the layered design pays for itself within a few weeks.
A Year of Living With This Pipeline
It is one thing to build a defense and another to live with it. After running this design for the better part of a year across two of my products, the most useful thing I can share is what changed in the texture of operations.
The first surprise was that the bug reports stopped looking like bugs. Before the pipeline, "the AI broke the page" tickets came in through email, support chat, and once memorably through a tweet at midnight. After the pipeline, the same underlying failures still happened — they just stopped reaching users. What I see now in operations are dashboards with quiet bumps in Layer 3a usage, which I investigate at a calm hour rather than reactively. That difference, more than the raw success rate, is what I would put in front of any indie developer thinking about whether the additional plumbing is worth it.
The second surprise was that the design taught me where my prompts were brittle. Whenever Layer 3b started spiking for one specific endpoint, it almost always pointed back to a system prompt that had grown organic over six months and was now nudging Claude into ambiguous territory. Treating Layer 3b telemetry as a code smell rather than a runtime problem turned out to be the cheapest way to find prompt regressions. I now do a small audit of system prompts whenever the seven-day Layer 3b rate creeps over 0.1%, and roughly half the time I end up trimming or restructuring the prompt rather than touching the pipeline at all.
The third surprise was about how this changed my willingness to ship. Before the pipeline, "ship a feature that talks to Claude" was preceded by two days of paranoia and a reluctance to enable it for paying customers. After the pipeline, I started shipping AI-touching code paths on the same cadence as any other backend feature, because I trusted the safety net underneath. Speed of iteration matters more than I expected; in the months since this pipeline went live, I have launched two new AI-driven features that I would have stalled on indefinitely beforehand.
If there is a single takeaway from a year of using this design, it is that defensive infrastructure is not a tax on velocity, it is a precondition for it. The same principle applies whether you are running a solo SaaS or a team product: invest once in the safety layer, and afterwards you get to think about features instead of failure modes.
What to Try Next
The honest, lowest-risk way to adopt this pattern is to wire only Layers 1 (output_config.format) and 2 into a single extraction endpoint and log the failure rate for one week. With a real number in hand, you can decide whether you actually need Layer 3 at all, or only its mechanical half. I almost over-engineered Layer 3b before I had measured anything; the measurement saved me from that. Measure first, then defend — in that order, this design comes in cheap and stays out of your way.
Thank you for reading. I hope this saves your team a few late nights.