> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ninjachat.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Quality gates

> Evaluate important outputs in your application and escalate to a stronger model when needed.

Evaluate outputs against your own criteria, then retry with another model when needed.

```typescript TypeScript SDK theme={null}
const draft = await client.responses.create({
  model: "gpt-5.6-luna",
  input: customerPrompt,
});

const review = await client.responses.create({
  model: "claude-sonnet-5",
  input: `Score this answer from 0 to 1 for factual support and completeness.\n\n${draft.output_text}`,
  text: {
    format: {
      type: "json_schema",
      name: "quality_review",
      strict: true,
      schema: {
        type: "object",
        properties: {
          score: { type: "number" },
          reason: { type: "string" },
        },
        required: ["score", "reason"],
        additionalProperties: false,
      },
    },
  },
});
```

## Escalate only when needed

Parse the review and retry with a stronger frontier model when it falls below your threshold:

```typescript TypeScript SDK theme={null}
const quality = JSON.parse(review.output_text) as { score: number; reason: string };

const final = quality.score >= 0.85
  ? draft
  : await client.responses.create({
      model: "gpt-5.6-sol",
      input: customerPrompt,
      instructions: `Improve the answer. Reviewer feedback: ${quality.reason}`,
    });

console.log(final.output_text);
console.log("Total cost:", draft.cost_usd + review.cost_usd + (final === draft ? 0 : final.cost_usd));
```

For deterministic checks—schema validity, required citations, policy rules, code compilation—run those before invoking a model judge. Store every NinjaChat `request_id` so failures remain traceable.

## When to use a gate

| Workload              | Useful check                                   |
| --------------------- | ---------------------------------------------- |
| Structured extraction | JSON Schema plus business validation           |
| RAG                   | Citation presence and source coverage          |
| Code generation       | Typecheck, tests, and static analysis          |
| Customer support      | Policy compliance and escalation triggers      |
| High-stakes synthesis | Independent model review with a written rubric |

<Tip>
  For infrastructure reliability, use ordered [fallbacks](/fallback-chains). Quality gates are for output acceptance, not provider uptime.
</Tip>
