Skip to main content

The silent failure

Your LLM returns this:
{
  "tier": "hot",
  "score": 25,
  "reason": "",
  "qualified": true
}
Valid JSON. Schema passes. Your CRM accepts it and routes it to your sales team. But hot leads require a score above 70. And the reason is empty. And qualified: true with a score of 25 makes no sense. Your sales team calls a cold lead thinking it’s hot. Nobody catches it because nothing threw an error.

With Boundary

Attempt 1 → { tier: "hot", score: 25, reason: "", qualified: true }
             ✕ hot leads must have a score above 70
             ✕ reason cannot be empty
             ✕ qualified requires score > 50
             ↻ violations sent back to model

Attempt 2 → { tier: "cold", score: 25, reason: "Low intent", qualified: false }
             ✔ all rules pass
             ✔ ACCEPTED — safe to use in CRM
The model fixed its own mistakes. Your CRM only saw the correct output.

The contract

import { z } from "zod";
import { defineContract } from "@boundary/contract";

const schema = z.object({
  tier: z.enum(["hot", "warm", "cold"]),
  score: z.number().min(0).max(100),
  reason: z.string(),
  qualified: z.boolean(),
});

const leadContract = defineContract({
  schema,
  rules: [
    // hot leads must have a high score
    (lead) => lead.tier === "hot" ? lead.score > 70 : true,

    // cold leads must have a low score
    (lead) => lead.tier === "cold" ? lead.score < 30 : true,

    // every scoring decision needs a reason
    (lead) => lead.reason.length > 0 || "reason cannot be empty",

    // qualification must be consistent with score
    (lead) => !lead.qualified || lead.score > 50
      || `qualified is true but score is ${lead.score} (requires > 50)`,
  ],
});

Full example

const result = await leadContract.accept(async (attempt) => {
  const res = await openai.responses.create({
    model: "gpt-4.1",
    input: [
      {
        role: "user",
        content: "Score this lead: signed up 2 days ago, visited pricing page 3 times, opened 1 email",
      },
      ...attempt.repairs,
    ],
    text: {
      format: {
        type: "json_schema",
        name: "lead_score",
        schema: { /* your JSON schema */ },
      },
    },
  });
  return res.output_text;
});

if (result.ok) {
  // ✔ safe to use in CRM
  await crm.updateLead(leadId, result.data);
}

Result: safe to use in CRMEvery lead score that enters your system has been validated against your domain rules. Tier matches score. Reason is present. Qualification is consistent.