Argo AI All articles
Evaluation & Testing

Prompts Are Contracts: Building Regression Gates for Non-Deterministic LLM Features

Argo AI
Prompts Are Contracts: Building Regression Gates for Non-Deterministic LLM Features

Photo: Intel Free Press, CC BY-SA 2.0, via Wikimedia Commons

Most teams would not merge a database schema change without a migration review, a rollback plan, and a test suite. Yet the same teams routinely merge prompt revisions that alter output shape, refusal behavior, and tool-call semantics with nothing more than a spot check in a notebook. The prompt is an interface. It encodes a contract between the application and a stochastic model, and that contract deserves the same engineering discipline as an API schema. If a prompt can change what the product does, it should also change what the test suite checks.

The challenge is that LLM outputs are not deterministic. Teams often respond by avoiding tests altogether or by writing brittle exact-match assertions that fail for harmless wording changes. Neither approach survives contact with production. A better model is to treat prompt revisions as schema migrations: define the behavioral contract, maintain a representative set of inputs, assert on properties rather than strings, and gate merges on measured evidence.

Start With the Behavioral Contract

Before writing a single test, document what the prompt is supposed to guarantee. Split the contract into four layers. The first is structural: output must parse as JSON, include required fields, or call only approved tools. The second is semantic: the answer must be supported by retrieved evidence, preserve entity relationships, or avoid unsupported claims. The third is stylistic and operational: length bounds, tone constraints, locale handling, and latency budgets. The fourth is safety and policy: refusal categories, privacy handling, and escalation paths.

This contract should live next to the prompt in version control. Each assertion in the test harness should map back to a clause in the contract. When a product manager asks why a prompt change is blocked, the answer should be a failing assertion and a contract clause, not an opinion about output quality.

Build Golden Sets That Represent the Input Domain

A golden set is not a random sample of production traffic. It is a stratified sample of the input domain, weighted toward cases that matter. Include high-frequency intents, known edge cases, adversarial inputs, multilingual or regional variants, and cases that previously caused incidents. For a customer-support assistant, that might mean refund requests, subscription changes, ambiguous account states, and prompts that tempt the model to promise outcomes outside policy.

Keep the golden set small enough to run on every pull request but broad enough to detect meaningful drift. A few hundred well-chosen cases often outperform tens of thousands of low-value samples. Store expected properties, not expected strings. For each case, record the input, the contract clauses under test, the evaluator configuration, and any provenance notes. Rotate a portion of the set on a schedule to avoid overfitting the prompt to the test suite.

Layer Assertions Instead of Chasing Exact Matches

Use deterministic checks wherever possible. Schema validators, tool-call allowlists, required-field checks, numeric range checks, and citation presence checks are cheap and stable. They should run first and fail fast. For semantic behavior, use a combination of embedding similarity with calibrated thresholds, task-specific classifiers, and rubric-based judges. Do not rely on a single LLM judge without calibration. Judges drift with model versions, can be sensitive to prompt order, and often reward fluency over correctness.

Run multiple samples per case when the feature is stochastic. Assert on distributions rather than single outputs: pass rates, variance, refusal frequency, and the share of outputs that violate a hard invariant. Set thresholds from a baseline run, then require a review when a revision moves a metric beyond a tolerance band. The goal is not to eliminate nondeterminism. The goal is to make changes in behavior visible and attributable.

Use Semantic Diffs to Explain Revision Impact

A prompt diff shows what changed in the instructions. A semantic diff shows what changed in behavior. Run the old and new prompt against the same golden set, then compare outcomes at the case level. Classify each case as unchanged, improved, regressed, or unstable. Summarize regressions by contract clause, intent category, and input length. This turns a vague review into a triage list.

For free-text outputs, store embeddings and extract structured facts before comparison. For tool-using systems, compare selected tools, arguments, and sequence. For retrieval-augmented systems, compare citation coverage and whether the answer remains grounded when the retrieved context changes. Semantic diffs are most useful when they answer a practical question: which user segments or behaviors will notice this revision?

Gate Merges on Statistical Evidence

Put the harness in CI and make prompt files subject to the same review rules as code. A pull request should run the fast deterministic suite on every commit and the full semantic suite before merge. Require a baseline comparison, a regression summary, and sign-off from the feature owner. Set hard gates for safety and structural failures, and softer gates for quality metrics that require human review.

After merge, deploy behind a canary or shadow route. Sample production traffic, run the same evaluators offline, and compare against the golden-set results. If production diverges from the test environment, treat that as a harness gap and add the new cases. The regression suite should evolve with the product, not fossilize around the first launch.

Prompts will keep changing because language, products, and models keep changing. The discipline is to make those changes legible. A prompt without a behavioral contract is an untested interface. A prompt with one is an engineered system.

All Articles

Related Articles

The Tail Tax: Designing Model Routers That Keep Cost Savings From Inflating p99 Latency

The Tail Tax: Designing Model Routers That Keep Cost Savings From Inflating p99 Latency

The Closed Loop Problem: Detecting Distribution Collapse in Synthetic Training Data

The Closed Loop Problem: Detecting Distribution Collapse in Synthetic Training Data

Seven Failure Modes Every Agentic System Reveals After the Demo Ends

Seven Failure Modes Every Agentic System Reveals After the Demo Ends