jcardena.com Blog Evaluating LLM output like a data pipeline
145 posts
EN ES

Evaluating LLM output like a data pipeline

Software

Learn to evaluate LLM outputs with the discipline of data engineering. This post covers the architectural choice between constraining generation and post-hoc validation.

Evaluating LLM output like a data pipeline

An LLM that subtly breaks the JSON structure for the tenth time is no longer an interesting research problem; it’s a production liability. For a long time, my approach to evaluating agentic outputs was manual and impressionistic. I’d eyeball the results, get a “feel” for the quality, and move on. This is the fast path to getting paged at 3am.

The system becomes reliable when you stop treating the LLM as a creative oracle and start treating it as a non-deterministic transformation step in a data pipeline. In data engineering, we don't eyeball transformations. We define contracts, build validation gates, and measure failure rates. It's time we brought that same discipline to AI.

The Core Choice: Constrain or Validate?

Before building, you face a fundamental architectural decision. Do you force the model to be correct during generation, or do you give it freedom and validate the output afterward? Each is a valid strategy with real trade-offs.

  • Generation-Time Constraining: This approach uses tools to force the LLM’s output into a predefined structure. For instance, you can use a library like Microsoft's Guidance to ensure the model can only produce valid JSON that conforms to your schema. The upside is a guarantee of structural correctness. The downside is added complexity and potential coupling to a specific framework.
  • Post-Hoc Validation: This approach lets the model generate its output freely and then runs that output through a series of checks. It’s model-agnostic and highly flexible, treating the LLM as a replaceable component. The trade-off is that you are responsible for building and maintaining the validation pipeline itself.

For most of my work building systems that need to be resilient and model-agnostic, I lean towards post-hoc validation. It lets me build a durable architecture around a volatile component.

LLM GeneratesOutputCheck StructureCheck FactualGroundingCheck ConstraintsOutput is Valid
Basic Post-Hoc Validation Flow

Building the Validation Pipeline

A robust validation pipeline isn't a single check; it's a series of gates, ordered from cheapest and fastest to most expensive. An output that fails any gate is immediately rejected or routed for remediation, saving cost and time.

First, Structural Conformance. Does the output have the right shape? If you expect JSON, wrap the parse call in an exception handler and validate it against a Pydantic schema. This is the simplest gate, costs microseconds, and catches a huge number of common failures.

Second, Factual Grounding. Does the output align with a specific source of truth? For a RAG system, this means ensuring the answer is supported by the retrieved documents. Manually checking this is impossible at scale, but you can automate it. This is a domain where open-source frameworks like RAGAS provide standardized metrics for "faithfulness" and "answer relevance," turning a subjective check into a measurable score.

Third, Constraint Adherence. Does the output follow the business rules defined in the prompt? For simple rules, like "must not contain an email address," a regular expression is perfect. For nuanced constraints like "maintain a professional tone," the "LLM-as-a-judge" pattern is common. This involves a second, targeted LLM call to evaluate the first. This concept is backed by research, such as the G-Eval paper from Microsoft Research on using GPT-4 for evaluation. But it’s not a silver bullet. Your judge model can also be wrong, and its prompt requires the same level of care as your primary prompt. For recurring checks, a smaller, fine-tuned classification model is often faster, cheaper, and more reliable than a massive general model.

What a Pipeline Mindset Unlocks

Once you have a reliable validation system, the LLM stops being an unpredictable black box. You now have a component with measurable, predictable behavior, which unlocks powerful capabilities.

You gain meaningful monitoring and metrics. The `validation_pass_rate` becomes a core health metric for your system. If a new prompt drops that rate from 99% to 85%, you have a clear, quantifiable regression. You can compare models not on vague feelings but on their concrete ability to produce valid outputs.

You can build intelligent error handling. An invalid output is no longer a fatal error. A schema failure can trigger an automatic retry with a simpler prompt or a lower temperature. A factual grounding failure can fall back to a safer, deterministic response or escalate the task to a human review queue. This is how you build resilience.

And finally, you enable systematic prompt improvement. A/B testing prompts becomes a science. You can deploy two prompts in parallel and measure which one produces a higher rate of valid outputs across all gates. This data-driven loop is far more effective than tweaking prompts based on a few spot checks.

The Complete System Architecture

This validation pipeline doesn't exist in a vacuum. It's a critical component within a larger architecture that ingests data, orchestrates agentic and deterministic work, and serves a reliable result. The goal is not to eliminate non-determinism, but to bound it within a system that is, as a whole, deterministic and reliable.

SOURCES & INPUTSUser PromptsInternal DocsEvent StreamsGENERATION & PROCESSINGLLM AgentGeneration-TimeConstraintsPost-HocValidation…Vector DBVALIDATION GATES (POST-HOC)Schema CheckFactual GroundingCheckConstraint CheckLLM-as-a-JudgeSERVING & MONITORINGApplication APIError HandlingMetrics DashboardHuman Review Queue
Architecture for a Validated Agentic System

Takeaways for Production Systems

To move from demos to durable systems, a shift in mindset is required. Here are the principles that have held up for me.

  • Decide your strategy upfront. Consciously choose between constraining generation or validating post-hoc based on your system's needs for flexibility versus guaranteed correctness.
  • Build a multi-stage validation pipeline. Start with the cheapest checks first and fail fast. A schema check is far cheaper than an LLM-as-a-judge call.
  • Validate against a bounded context. Don't try to check facts against the entire internet. Validate that the output is faithful to the specific source documents you provided.
  • Instrument your validation rates. Your pass/fail rates for each gate are the most important health metrics for your AI features. Get them on a dashboard.
  • Treat validation logic as production code. Your judge prompts, validation rules, and fallback mechanisms are first-class artifacts. They need to be versioned, tested, and maintained with the same rigor as the rest of your application.
JC
Juan Cardena
Enterprise Architect, Data & AI

Enterprise architect with 25 years across web, software, data, and AI. MIT CDAO ’25. Writing on agentic AI in production.