7.1 · The mental model
An LLM samples the most likely continuation of everything you give it. Your prompt is the context that shapes that distribution. Five levers move it:
- Role — who is the model?
- Task — what should it do?
- Examples — what does success look like?
- Constraints — what must / must not happen?
- Format — what shape should the answer take?
A prompt, anatomically
7.2 · The anatomy of a great prompt
# --- SYSTEM ---
You are a senior data engineer. You answer in clear, ordered steps.
Refuse if the task involves PII you cannot verify the user owns.
# --- CONTEXT ---
Schema:
orders(id, customer_id, total, created_at)
customers(id, email, country)
# --- TASK ---
Write the SQL to find the top-10 customers by revenue in 2025.
# --- CONSTRAINTS ---
- Use Postgres syntax
- Round totals to 2 decimals
- Return ONLY the SQL, no commentary
# --- EXAMPLES (few-shot) ---
Q: top-3 by orders in 2024
A: SELECT customer_id, COUNT(*) c FROM orders
WHERE created_at >= '2024-01-01' AND created_at < '2025-01-01'
GROUP BY customer_id ORDER BY c DESC LIMIT 3;
7.3 · Core techniques — definition + example each
Zero-shot
Definition. Ask the model to perform a task with no examples, relying entirely on its pre-training.
Example. Translate to French: "Tomorrow we travel." → "Demain nous voyageons."
Few-shot
Definition. Show 2–5 worked input/output pairs before the real task, so the model can mimic the pattern.
Example. "Classify sentiment. Ex 1: 'love this!' → positive. Ex 2: 'never again' → negative. Now: 'service was okay' →" → neutral.
Role prompting
Definition. Open with a persona that narrows tone, vocabulary, and defaults.
Example. "You are a senior tax accountant in the UAE. Answer cautiously and cite the article number when relevant."
Chain-of-Thought (CoT)
Definition. Instruct the model to think step by step before producing the final answer, so it uses its hidden scratchpad.
Example. "If a shirt costs Rs 1,200 with 17% sales tax included, what's the pre-tax price? Think step by step, then give the final answer."
Self-consistency
Definition. Sample N reasoning paths (high temperature, CoT), then majority-vote the final answer.
Example. A maths problem solved 7 times; 5 paths agree on 42 → answer is 42, even if 2 paths went astray.
ReAct
Definition. Interleave Reasoning, Action (tool call), and Observation in a visible trace.
Example. "Is BTC above $80k today?" → Thought: need price → Action: price.get('BTC') → Observation: 81,440 → Answer: yes.
Tree-of-Thoughts (ToT)
Definition. Branch reasoning into multiple paths, score each, prune the weak ones.
Example. Game of 24 — explore several arithmetic combinations in parallel, drop dead-ends.
Self-Refine
Definition. Generate → critique own output → revise. Repeat 1–2 rounds.
Example. Draft a cover letter → "list 3 weaknesses" → rewrite addressing each.
CoT vs ToT — branching reasoning
7.4 · Advanced patterns
Tree-of-Thoughts (ToT)
Skeleton-of-Thought
Self-Refine / Reflexion
Plan-and-Execute
Meta-prompting
Prompt chaining
7.5 · Structured outputs — making LLMs machine-readable
Free text is for humans; downstream systems need structure. Three good techniques:
- JSON mode / response_format — most APIs let you constrain output to valid JSON.
- Schema-guided — pass a JSON Schema or Pydantic model; the API enforces it.
- Tool calling — declare a tool whose schema is your output (Ch 11).
# Tip: describe each field's meaning AND give one good example.
Return a JSON object exactly matching:
{
"intent": "billing" | "support" | "sales",
"urgency": 1..5,
"summary": <= 120 chars
}
Examples:
Input: "my card was charged twice"
Output: {"intent":"billing","urgency":4,"summary":"duplicate charge"}
7.6 · System prompts that work in production
- State the role. Senior, careful, concise.
- State the goal. What does success look like for the user?
- State the rules. What must never happen?
- State the tools. When to use each, when not to.
- State the format. Headings, JSON, length limits.
- State the fallback. What to do if data is missing or the request is unsafe.
7.7 · Five reusable templates
Summarise the <DOC> below in 5 bullets.
Each bullet:
- starts with a verb
- <= 14 words
- preserves numbers verbatim
<DOC>…
Classify the user message into exactly one label:
[billing | support | sales | spam | other]
If uncertain, choose "other". Return only the label.
Message: "{{text}}"
From the contract below extract:
- parties
- effective date (ISO 8601)
- jurisdiction
- termination clauses (verbatim)
Return as JSON. If a field is missing use null.
Think step by step. Show your reasoning under "Thinking:".
Then give the final answer under "Answer:".
If a step needs data you don't have, request it instead of guessing.
Write a 150-word LinkedIn post by <persona> about <topic>.
Voice: confident, specific, never breathless.
Open with a 1-line hook, end with one question.
Avoid: emojis, hashtags, the words "thrilled" and "leverage".
7.8 · Debugging a bad prompt — a 7-step rubric
- Is the task stated explicitly, in one sentence?
- Are the inputs separated from the instructions (e.g.
<DOC>tags)? - Did you give an example of the desired output?
- Are constraints testable (lengths, must-include, must-avoid)?
- Are edge cases covered (missing data, refusal)?
- Is the output format machine-parseable?
- Did you try a stronger model as a sanity check?
7.9 · Five anti-patterns
- "Be helpful." Empty calorie. Specify how.
- 10 paragraphs of system prompt. Truncates and dilutes.
- Conflicting rules. "Be concise" + "list every detail" — the model picks one.
- Trusting free text. Always validate JSON; never
eval()output. - Prompt-only solutions. If you need fresh data, that's RAG, not prompting (Ch 9).