Skip to content

Prompt Architecture, System Contracts, and Strict Structured Outputs (JSON Schema)

2 min read

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.

LayerContentsVolatilityCacheable?
1. System ContractRole, hard safety boundaries, output format rulesChanges only on deployYes (global)
2. Domain PolicyTenant persona, language guidelines, escalation rulesChanges rarelyYes (per tenant)
3. Reference ContextRetrieved KB passages or product snippetsChanges per topicSometimes
4. Conversation StateRolling summary + recent messages + latest user turnChanges every turnNo

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.

ts
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

  1. 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."
  2. Use explicit XML or Markdown delimiters around untrusted user content and retrieved passages so the model cleanly separates instructions from data.
  3. 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.
  4. Version prompts in Git alongside tests: tag every trace with promptId and promptVersion so quality regressions map back to an exact diff.

Next: gate every prompt edit automatically with Offline LLM Evaluations and CI Gates.

Was this helpful?