The Anatomy of a Production Prompt
The Core Components:- Role/Persona - Sets behavior and expertise level ( values: user, assistant, system)
- Context - Background information needed
- Instructions - What to do, step by step
- Constraints - What NOT to do, output format
- Examples - Few-shot demonstrations (optional)
- Input - The actual data to process
- Write a prompt that includes role, context, instructions, constraints, examples, and input.
- Expected: All 6 components present with explicit constraints and a clear output schema.
- Try this too: Compare the outputs if you remove the structure and use less powerful models
- Cost impact: Clear output schemas reduce parsing errors and retries (fewer re-runs).
- Reliability: Structured prompts are easier to validate and monitor.
- Performance: Slight token overhead; mitigate with caching (see Model Selection & Cost Optimization)
XML Tags: Your Secret Weapon
Why XML Tags Work:- LLMs were trained on HTML/XML (web data)
- Tags create clear boundaries in the context
- Reduced hallucinations in controlled studies
Practical Implication
Compare these two approaches to structuring prompts:❌ Antipattern
✅ Best Practice
In Production:- Cost Impact: Tagging adds tokens; mitigate with caching .
- Reliability: Clear boundaries reduce off-context responses; improves evaluability.
- Performance: Slight overhead; offset by fewer retries and clearer parsing.
- Real Example: Teams report 40–60% fewer hallucinations when tags + validation are combined.
Example 1: Schema in Prompt
This approach includes the XML schema and example in the prompt itself, instructing the model to follow the XML structure.Example 2: Structured Outputs (XML Mode)
This approach uses prompt instructions to request XML output combined with XML parsing and validation. Note that OpenAI’s API doesn’t have a native XML response format like json_object, so we rely on prompt engineering and parsing.Few-Shot Examples: Teaching by Showing
When to Use Few-Shot:- Complex or subjective tasks
- Specific output format required
- Edge cases need clarification
- 3-5 examples usually enough
- More examples = more tokens = higher cost
- Examples should cover edge cases, not just obvious ones
Structured Outputs with JSON Schemas
Why: Schema enforcement reduces parsing errors and retries; makes outputs machine-checkable. Compare these two approaches:Example 1: Schema in Prompt
This approach includes the JSON schema in the prompt itself, instructing the model to follow the schema structure.Example 2: Structured Outputs (JSON Mode)
This approach uses OpenAI’s structured outputs feature (response_format: json_object) combined with schema validation. This is more reliable than just including the schema in the prompt.
See: OpenAI Structured Outputs (2025), Anthropic Prompt Engineering (2025), Gemini Prompting Strategies (2025) in Additional Resources.
Model-Specific Prompt Optimization
Different models have different “personalities.” Here’s what works best for each:GPT-4 / GPT-4 Turbo
Prompt
Claude (Sonnet/Opus)
Prompt
Gemini 1.5 Pro
Prompt