Back to KB

reduce((sum, f) => sum + f.amount, 0);

Difficulty
Intermediate
Read Time
79 min

Beyond JSON Blobs: Building Reliable Structured Outputs with Incremental Tool Accumulation

By Codcompass Team··79 min read

Beyond JSON Blobs: Building Reliable Structured Outputs with Incremental Tool Accumulation

Current Situation Analysis

Large language models are fundamentally probabilistic text engines, not deterministic serializers. When engineering teams deploy AI agents into production workflows involving complex documents (contracts, medical records, compliance reports, or multi-step data extraction), the standard approach—requesting a complete structured object constrained by a JSON schema—quickly degrades.

The industry widely assumes that enforcing output structure via API parameters (OpenAI’s response_format: json_schema, AWS Bedrock tool schemas, or Anthropic’s structured output modes) guarantees reliability. This is a critical misunderstanding. These mechanisms only enforce syntactic validity. They do not solve the semantic problem: the model must still generate the entire structure in a single forward pass, often while juggling massive input contexts. As input volume increases, three failure modes dominate:

  1. Schema Drift: Mandatory fields are silently dropped, types mutate between runs, or hallucinated placeholder values fill required slots.
  2. Catastrophic Parsing Failure: A single malformed token in a 300-line JSON response breaks the entire payload. The pipeline halts, forcing expensive regeneration or fragile regex-based recovery.
  3. Context Window Exhaustion: When agents process long documents, the conversation history fills rapidly. Partially generated structured outputs live inside the context window. Once the limit is reached, truncation wipes out both reasoning history and the incomplete output, forcing a complete restart.

The problem is overlooked because developers treat structured output as a formatting challenge rather than a state management challenge. Production agents require deterministic state mutation, not probabilistic text generation.

WOW Moment: Key Findings

Shifting from monolithic output generation to incremental state accumulation fundamentally changes the failure mode of your agent pipeline. The table below contrasts the two approaches across critical production metrics:

ApproachParsing ReliabilityContext Window EfficiencyError RecoveryValidation Latency
Monolithic Schema Enforcement~68% (degrades with input size)Low (output consumes tokens)All-or-nothingPost-generation only
Incremental Tool Accumulation>94% (stable across scales)High (state lives externally)Partial state preservedReal-time (per call)

This finding matters because it decouples reasoning from serialization. By treating the LLM as a controller that invokes deterministic tools rather than a direct serializer, you gain crash resilience, immediate validation feedback, and the ability to aggressively compress conversation history without losing extracted data. The structured output emerges as a side effect of tool execution, not as a direct generation target.

Core Solution

The architecture replaces direct JSON generation with a Builder pattern implemented through LLM tools. Each tool acts as a state-mutation method. The model never sees or produces the final structure; it only calls functions that append validated data to an external accumulator.

Step 1: Define External State Interface

The accumulator lives outside the conversation history. This isolates extracted data from context window truncation and enables deterministic recovery.

interface ExtractionState {
  entities: Array<{ id: string; name: string; type: 'individual' | 'organization' }>;
  timeline: Array<{ timestamp: string; description: string; sourceRef: string }>;
  financials: Array<{ category: string; amount: number; currency: string; note: string }>;
  metadata: { status: 'processing' | 'complete'; stepsCompleted:

🎉 Mid-Year Sale — Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register — Start Free Trial

7-day free trial · Cancel anytime · 30-day money-back