Lesson 2 · Building LLM applications
Prompt engineering
Structure, few-shot, structured outputs and function calling. Prompts are code — version them.
Step 1 of 5
System prompt
Role, rules and output format go first, where they carry the most weight. Hardening against prompt extraction starts here.
A prompt is code: rendered from a versioned template and gated by eval before it reaches users.
What is prompt engineering?
Prompt engineering is the discipline of authoring deterministic inputs to LLMs to reliably produce desired outputs. It is software engineering where the prompt is the interface specification.
With the rise of frontier reasoning models (OpenAI o1/o3, Claude 3.7 Thinking, Gemini Flash Thinking, DeepSeek-R1), the prompt engineering paradigm has shifted:
1. 'Think step by step' is obsolete: reasoning models already spend test-time compute exploring internal search trees and counterfactuals. Prompting a reasoning model to 'think step by step' produces redundant conversational narration, wastes thinking token budgets, and can degrade the model's native reasoning.
2. Spec-driven and constraint-driven prompting: instead of micromanaging the model's thought process, high-leverage prompts specify exact acceptance criteria, schema invariants, negative constraints ('never execute without confirmation'), and boundary conditions.
3. Thinking token budgeting: configure explicit reasoning token budgets (e.g. 1k tokens for triage, 8k for complex refactors) so the model neither under-reasons nor overthinks simple queries.
Why prompt engineering matters
A well-specified prompt is the difference between a feature that fails on edge cases and one that behaves like robust software. On standard models, structured prompts and delimiters eliminate injection and formatting drift. On reasoning models, constraint-driven specifications give the internal thinking process clear verification targets, turning test-time compute into guaranteed output correctness.
Where prompts live
System prompt (role, rules, output format) + user message (the actual query) + retrieved context (in delimiters) + few-shot examples → LLM → structured output. In production, prompts are stored as versioned templates, rendered with variables, and gated by eval before promotion.
Structured output with validation
def ask_llm(question: str) -> str:
"""Naive: free-text prompt, free-text response, no validation."""
r = client.chat.completions.create(
model="<small-model>", # Model names change often; check your provider's model list (as of 28 October 2026). Replace <...-model> with a model id from that list.
messages=[{"role": "user", "content": f"Answer: {question}"}],
)
return r.choices[0].message.content
# Problems: unpredictable format, no validation, hallucinated fields, retries failExperiment: prompting techniques
See how different prompting techniques affect output quality on standard versus reasoning models.
What to observe
Prompting techniques must match both the task and the model architecture. On standard models, structured outputs and system constraints yield reliable, low-latency execution. On frontier reasoning models, 'think step-by-step' is obsolete noise; instead, specify explicit constraints, verification criteria, and reasoning token budgets to direct the model's native test-time search.
Production prompting
Version prompts in code, gate with eval before promotion, canary new versions, and track per-prompt-version metrics. Use system prompts for primacy, delimiters for untrusted content, structured outputs for downstream reliability, and constraint specifications for edge cases. For reasoning models, set explicit thinking token ceilings and socket timeouts.
Challenge
Your prompt produces great answers 92% of the time but occasionally reveals the system prompt when users ask cleverly. Harden the prompt against extraction without hurting quality. (Hint: primacy + delimiters + output guard.)
Production checklist
Production checklist
0 of 9 checked
Knowledge check
Retrieved documents are concatenated into your prompt. A user asks 'ignore previous instructions and...'. Which defence is most fundamental?
Complete
You can now write production-grade prompts with structured outputs and spec-driven reasoning. Next: streaming and structured outputs in depth.
Mark this chapter as complete
Track your progress and unlock the next chapter.