Prompt Architecture, System Contracts, and Strict Structured Outputs (JSON Schema)
Treating prompts as giant concatenated strings makes them impossible to debug, cache, or review. Production prompts work best when structured as typed, layered contracts with strict output schemas enforced at decode time and validated again in application code.
Layering Prompts for Clarity and Cache Hit Rate
Order prompt sections from most static to most dynamic. Placing immutable system rules and tool schemas at the very top maximizes prefix prompt caching across requests, while volatile per-turn context sits at the bottom.
| Layer | Contents | Volatility | Cacheable? |
|---|---|---|---|
| 1. System Contract | Role, hard safety boundaries, output format rules | Changes only on deploy | Yes (global) |
| 2. Domain Policy | Tenant persona, language guidelines, escalation rules | Changes rarely | Yes (per tenant) |
| 3. Reference Context | Retrieved KB passages or product snippets | Changes per topic | Sometimes |
| 4. Conversation State | Rolling summary + recent messages + latest user turn | Changes every turn | No |
Structured Outputs With Zod and JSON Schema
Whenever downstream code branches on an LLM response (routing, classification, extraction, tool arguments), use constrained decoding / strict JSON Schema mode rather than regex-scraping Markdown fences.
import { z } from "zod";
export const SupportTriageSchema = z.object({
intent: z.enum(["billing", "technical_support", "sales_inquiry", "handoff"]),
urgency: z.enum(["low", "medium", "high"]),
requiresHumanReview: z.boolean(),
shortReason: z.string().max(180),
});
export type SupportTriage = z.infer<typeof SupportTriageSchema>;
Writing Instructions Models Actually Follow
- Prefer concrete boundaries over vague adjectives: replace "be concise and helpful" with "reply in 2 to 4 short sentences; ask at most one clarifying question."
- Use explicit XML or Markdown delimiters around untrusted user content and retrieved passages so the model cleanly separates instructions from data.
- Include 2 to 4 edge-case few-shot examples: examples showing when to abstain or escalate teach decision boundaries far better than three paragraphs of prose warnings.
- Version prompts in Git alongside tests: tag every trace with
promptIdandpromptVersionso quality regressions map back to an exact diff.
Next: gate every prompt edit automatically with Offline LLM Evaluations and CI Gates.