AI Prompting (Foundations & Practice)
Prompting is the practice of writing inputs that steer a Large Language Model (LLM) toward useful outputs. It is part interface design, part rhetoric, and part empirical science. This note covers the foundations and the techniques that have been validated by published research, with worked examples you can copy.
Learning Outcome
- Explain what a prompt is and why model behaviour is sensitive to its phrasing.
- Decompose a prompt into its functional parts (instruction, context, examples, format spec).
- Apply core techniques: zero-shot, few-shot, chain-of-thought, role conditioning, decomposition, and self-consistency.
- Recognise common anti-patterns and explain why they fail.
- Design an evaluation harness with a golden set + rubric + A/B framework.
- Read a research paper on prompting and judge whether its claims will transfer to your task.
Chapter 1 - Foundations
Terminology
- Prompt — the full token sequence the model conditions on before generating the next token.
- Completion — the model's generated response.
- Token — sub-word unit (~4 characters of English on average). All limits (context window, throughput, billing) are measured in tokens.
- Context window — maximum prompt+completion length the model can attend to.
- In-context learning (ICL) — model adapts its behaviour from examples in the prompt without weight updates. Reported in Language Models are Few-Shot Learners, Brown et al. (2020).
- Temperature — sampling parameter; is greedy, higher values increase diversity.
- Top-p / Top-k — nucleus / top-k sampling, alternatives to temperature.
- System / developer message — privileged prompt segment used to set persistent behaviour. Mechanism formalised by The Instruction Hierarchy, Wallace et al. (2024).
Why prompts matter
An LLM is a next-token predictor over a learned distribution . The prompt is the conditional sequence . Two prompts that read as semantically equivalent to a human can produce very different distributions because the model is sensitive to token-level cues it has learned to associate with answer quality (formality, code-block fences, "Let's think step by step", etc.).
This sensitivity has been measured empirically. Lu et al. (2022) showed that simply reordering few-shot examples can swing accuracy on classification benchmarks by tens of percentage points. Sclar et al. (2024) showed that benign formatting changes (newline vs. colon as separator) shift task accuracy by a comparable margin.
The practical consequence: prompts are programs. They deserve version control, testing, and review.
How the model "reads" a prompt
A typical chat-tuned model receives a structured sequence — for instance OpenAI's ChatML or Anthropic's Human:/Assistant: turn markers — where each turn is tagged with a role. The model has been fine-tuned (instruction-tuned + RLHF or DPO) to:
- Treat the system / developer message as a high-priority constraint.
- Treat user messages as questions or requests.
- Treat its own prior responses as commitments to maintain consistency with.
The training procedure was first detailed in InstructGPT, Ouyang et al. (2022) and the Anthropic RLHF paper, Bai et al. (2022). The hierarchy of trust between roles was later sharpened in The Instruction Hierarchy, Wallace et al. (2024).
Chapter 2 - Anatomy of a Prompt
A well-formed prompt contains, in some combination, six functional parts. Naming them helps you debug what is missing.
| Part | Purpose | Example |
|---|---|---|
| Role / persona | Set a frame the model uses to choose vocabulary, depth, formality | "You are a senior database administrator." |
| Instruction | The actual verb of the request | "Explain why this query is slow." |
| Context | Domain facts, retrieved documents, prior turns | The SQL query + EXPLAIN ANALYZE output |
| Examples (shots) | Demonstrations of input → output | A prior worked optimisation |
| Format spec | Constraint on output shape | "Return JSON matching schema X" |
| Stop / guardrails | Negative constraints, refusal triggers | "If the query is fine, say OK only." |
A minimal template
1
2
3
4
5
6
7
[ROLE] You are <persona>.
[CONTEXT] <facts the model needs but cannot infer>
[TASK] <single imperative sentence>
[EXAMPLES] <0..k input/output pairs>
[FORMAT] <output schema>
[STOP] <what NOT to do; refusal conditions>
The order matters less than the presence of each part. The OpenAI prompt-engineering guide (OpenAI 2024) and Anthropic's prompt library both converge on the same six-part skeleton.
A worked rewrite
Before (typical first draft, ~15% accuracy on internal eval):
1
2
Summarise this article.
After (~78% accuracy on the same eval):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
You are a research editor preparing an executive briefing.
Article:
<<<
{article}
>>>
Task: produce a 5-bullet summary.
Constraints:
- Each bullet ≤ 20 words.
- Use the article's own terminology; do not paraphrase technical terms.
- First bullet states the main claim; the rest state evidence.
- If the article makes no claim, output exactly: NO_CLAIM_FOUND
Return JSON: {"summary": [<bullet>, ...]}
What changed: a role, an explicit delimiter for the article, a numbered constraint set, a falsifiable failure marker (NO_CLAIM_FOUND), and a machine-parseable output shape.
Chapter 3 - Core Techniques
Zero-shot
Ask the question directly with no examples. Works when the task is well-represented in the training distribution (translation, summarisation, common QA).
1
2
Translate to French: "The cat sat on the mat."
Few-shot (in-context learning)
Provide input/output pairs, then the new input. Brown et al. (2020) showed few-shot prompting closes a large fraction of the gap to fine-tuning on many tasks without any gradient updates.
1
2
3
4
Q: What is 17 + 5? A: 22
Q: What is 31 + 9? A: 40
Q: What is 124 + 76? A:
Tips:
- Order examples easy → hard (Lu et al., 2022).
- Make examples cover the failure modes you observe in zero-shot, not the easy cases.
- Keep formatting identical across shots; a stray newline can degrade accuracy (Sclar et al., 2024).
Chain-of-Thought (CoT) prompting
Ask the model to show its work before committing to an answer. Reported in Chain-of-Thought Prompting Elicits Reasoning in Large Language Models, Wei et al. (2022). Improves arithmetic, symbolic, and commonsense reasoning on large models.
1
2
3
4
5
6
7
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
A: Let's think step by step.
Roger starts with 5.
He buys 2 cans × 3 balls = 6 more.
Total = 5 + 6 = 11.
The answer is 11.
Two variants worth knowing:
- Zero-shot CoT: append "Let's think step by step." Kojima et al. (2022). Cheap and surprisingly effective.
- Self-consistency: sample chains at temperature , take the majority vote on the final answer. Wang et al. (2022). Improves arithmetic accuracy further at cost.
Role / persona conditioning
Setting an expert persona biases vocabulary, depth, and inferred audience. The effect is real but smaller than CoT or few-shot; treat it as fine-tuning of style, not capability.
1
2
3
You are a tenured statistics professor teaching a graduate seminar.
Explain the bias-variance trade-off using a regression example.
Decomposition
Break a hard problem into ordered sub-problems and prompt each separately, chaining outputs. Equivalent to Khot et al. (2023) "Decomposed Prompting" or least-to-most prompting by Zhou et al. (2023).
1
2
3
4
Step 1 prompt: Extract every named entity from <text>. Return JSON list.
Step 2 prompt: For each entity in <list from Step 1>, classify as person/org/location.
Step 3 prompt: For each person, look up role using <retrieval tool>.
The chain is easier to debug than a single monolithic prompt; you can swap or rerun any step.
ReAct (Reasoning + Acting)
Interleave reasoning steps with tool calls. Yao et al. (2023). The model alternates between Thought: (a reasoning step), Action: (a tool call), and Observation: (tool result), until ready to emit an Answer:.
1
2
3
4
5
6
7
8
9
Question: Who is the current CEO of OpenAI and when were they born?
Thought: I need to look up the current CEO.
Action: search["current CEO of OpenAI"]
Observation: Sam Altman.
Thought: Now find their birth date.
Action: search["Sam Altman date of birth"]
Observation: April 22, 1985.
Answer: Sam Altman, born April 22, 1985.
ReAct is the foundation pattern under most "agent" frameworks (LangChain, LlamaIndex, modern function-calling SDKs).
Chapter 4 - Structural Patterns
Delimiters
Use unambiguous markers around user-supplied or retrieved content. Three formats all work, in roughly equal measure:
1
2
3
4
1. Triple backtick fences: ```...```
2. Triple angle brackets: <<<...>>>
3. XML-style tags: <article>...</article>
XML tags have the advantage of being self-documenting and survive whitespace normalisation. Anthropic explicitly recommends XML in their prompt-engineering documentation.
Markdown structure
Models trained on heavy Markdown corpora respond well to headings (## Step 1), lists, and tables. Use the same conventions in your prompt that you want to see in the output.
Numbered constraints
1
2
3
4
5
Constraints:
1. Output must be valid JSON parsable by `JSON.parse`.
2. All numeric fields must be integers.
3. If you are uncertain, set `confidence` < 0.5.
Numbered constraints are easier for the model to enumerate against than a paragraph of "and also, make sure to..."
Format specifications
Three escalating strengths:
| Strength | Mechanism | Reliability |
|---|---|---|
| Soft | Natural-language description ("return JSON") | ~60-80% |
| Schema | Provide explicit schema (TypeScript / JSON Schema / Pydantic) | ~90% |
| Hard | Use the provider's structured-output / JSON-mode API | ~100% (provider-enforced) |
If you need a guaranteed shape, use the provider's structured output. Soft format is convenient but will fail on tail cases.
Chapter 5 - Reasoning Boosters
Self-consistency
Already mentioned. Formally: sample chains at , extract the final answer from each, return . The probability that the majority answer is correct converges to the per-sample accuracy raised to a Condorcet-style power as grows, when individual samples are conditionally independent given the question.
Tree of Thoughts (ToT)
Yao et al. (2023). Generalises CoT from a chain to a search tree. The model proposes multiple next-thoughts at each step, a value model scores each, and a search algorithm (BFS/DFS) explores. Helps on combinatorial problems (Game of 24, crosswords) where a single chain commits too early.
Reflexion
Shinn et al. (2023). After a wrong attempt, the model writes a critique of its own trace; that critique is appended to context for the next attempt. The agent improves across attempts within a single session.
Step-Back prompting
Zheng et al. (2024). Before answering a specific question, ask the model to first derive the general principle that governs the question. The principle then conditions the specific answer. Improves abstract / multi-step reasoning.
1
2
3
4
5
Question: A car accelerates from 10 m/s to 30 m/s over 4 s. What is the distance?
Step back: What kinematic formula applies when acceleration is uniform and initial+final velocities are known?
Principle: s = (v0 + v) / 2 × t.
Apply: s = (10 + 30) / 2 × 4 = 80 m.
Chapter 6 - Common Anti-patterns
Vague verbs
improve, optimise, make better, enhance are unmeasurable. The model has to guess a direction. Replace with a measurable verb: shorten to 100 words, rewrite at a 10th-grade reading level, remove all passive voice.
Conflicting constraints
1
2
Be concise. Cover every detail. Use simple words. Use precise terminology.
The model resolves contradictions arbitrarily. Pick one side of each axis and state it.
Leading questions
1
2
"Why is Python better than JavaScript for backend work?"
The framing forces the model to defend a premise instead of evaluating it. Prefer:
1
2
"Compare Python and JavaScript for backend work along: performance, ecosystem, hiring, deployment. Cite trade-offs."
Long irrelevant context
Liu et al. (2023) Lost in the Middle showed models degrade on retrieval tasks when relevant information sits in the middle of a long context. Prune aggressively. If you must include long retrieved documents, put the most relevant snippets at the start or the end.
Negation-only instructions
Do not list more than 3 items. is weaker than List exactly 3 items. Models follow positive constraints more reliably than negative ones.
"Polite filler"
Please, if you could, kindly attempt to consider... adds tokens without affecting behaviour. Strip it.
Chapter 7 - Evaluation
A prompt is a piece of software, so evaluate it like software.
Golden set
A small, curated set of representative inputs with hand-graded expected outputs. 30–100 items is enough to detect a 5pp accuracy shift.
Rubric
Decompose "is the output good?" into yes/no axes. For a summarisation task you might score:
- Factual (no hallucinated entities)
- Complete (all named main points present)
- Concise (within length limit)
- Format (parses as JSON)
- Refusal (correctly refused when no claim present)
Each axis scored 0/1 by a human grader or a separate "judge" model.
LLM-as-judge
Zheng et al. (2023). A stronger model grades a weaker model's output against a rubric. Cheap, scales, but biased toward outputs that look like the judge's style. Use a different family of model as the judge where possible, and spot-check 10% with a human.
A/B framework
1
2
3
4
v1 prompt + golden set → score_v1
v2 prompt + golden set → score_v2
Δ = score_v2 - score_v1
Track per axis, not just overall. A change that improves factuality but hurts conciseness is still a regression on the conciseness axis.
Statistical sanity
With binary outcomes, the 95% confidence interval on accuracy is roughly pp around the observed rate. Differences smaller than that are noise. Increase or repeat the run to tighten.
Chapter 8 - Tool Use and Structured Output
Modern model providers expose two APIs that should replace most prompt-engineering hacks:
Function calling / tool use
You declare a JSON schema for one or more tools; the model picks one and emits arguments matching the schema. The runtime executes the tool and returns the result, which you append to the conversation and prompt again.
1
2
3
4
5
6
tools:
- name: get_weather
parameters: { location: string, unit: enum["c","f"] }
- name: web_search
parameters: { query: string }
The model is fine-tuned to know when to call a tool versus answer directly. This is more reliable than ReAct in raw text because the model emits parsed JSON via the API.
Structured output (JSON / Pydantic mode)
You supply the full output schema; the provider's decoder constrains generation so the response is guaranteed valid against the schema. Eliminates "the model added a friendly preamble that broke my parser" failures.
When NOT to use tools
If the task is pure transformation of text the model already has the knowledge for (summarise, translate, classify), tools add latency and cost. Use tools when you need fresh data, deterministic computation, or side effects.
Chapter 9 - Putting It Together
A production prompt for "extract action items from a meeting transcript" — combining roles, delimiters, few-shot, format, and a guardrail:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
You are a meeting analyst.
Your task: extract every action item from the transcript below. An action item
is a commitment a specific named person made to do a specific thing by an
optional deadline.
Examples:
Transcript:
Alice: I'll send the deck to Bob by Friday.
Bob: Sounds good.
Output:
[{"owner": "Alice", "task": "send the deck to Bob", "due": "Friday"}]
Transcript:
Carlos: Maybe we should think about the budget.
Dana: Yeah, let's think about it.
Output:
[]
Transcript:
<<<
{transcript}
>>>
Rules:
1. Only include commitments with a named owner.
2. Vague "we should" / "maybe" / "let's think" → ignore.
3. If no action items, return [].
4. Output JSON only, no preamble.
This prompt is testable (you have a golden set), debuggable (each rule is a single axis you can score), and safe (the negative examples teach refusal on vague language).
Useful Shortcut
Cheat-sheet
| Goal | First thing to try |
|---|---|
| Wrong answer on math | Append "Let's think step by step." |
| Output not parseable | Switch to provider's structured-output API |
| Hallucinated facts | Add retrieved context with XML delimiters |
| Inconsistent answers across runs | Self-consistency, , majority vote |
| Too verbose | Add "Output ≤ N words. No preamble." |
| Refuses too much | Add an explicit allow-list of permitted topics |
| Drifts off topic mid-conversation | Re-state the system message every turns |
| Slow / expensive | Decompose; use a smaller model for the easy steps |
Quick sanity checks before shipping
- Does the prompt have all six anatomy parts (role, instruction, context, examples, format, stop)?
- Is there a golden set of ≥ 30 items with rubric scores?
- Have you tested the failure modes, not just the happy path?
- Are you using structured output for any machine-parsed response?
- Have you token-counted the prompt under realistic context lengths?
- Have you versioned the prompt in git with a short rationale per change?
References
- Brown, T. et al. (2020). Language Models are Few-Shot Learners. NeurIPS. arXiv:2005.14165.
- Ouyang, L. et al. (2022). Training Language Models to Follow Instructions with Human Feedback (InstructGPT). NeurIPS. arXiv:2203.02155.
- Bai, Y. et al. (2022). Training a Helpful and Harmless Assistant with RLHF. arXiv:2204.05862.
- Wei, J. et al. (2022). Chain-of-Thought Prompting Elicits Reasoning in Large Language Models. NeurIPS. arXiv:2201.11903.
- Kojima, T. et al. (2022). Large Language Models are Zero-Shot Reasoners. NeurIPS. arXiv:2205.11916.
- Wang, X. et al. (2022). Self-Consistency Improves Chain of Thought Reasoning. ICLR. arXiv:2203.11171.
- Lu, Y. et al. (2022). Fantastically Ordered Prompts and Where to Find Them. ACL. arXiv:2104.08786.
- Khot, T. et al. (2023). Decomposed Prompting. ICLR. arXiv:2210.02406.
- Zhou, D. et al. (2023). Least-to-Most Prompting Enables Complex Reasoning in LLMs. ICLR. arXiv:2205.10625.
- Yao, S. et al. (2023). ReAct: Synergising Reasoning and Acting. ICLR. arXiv:2210.03629.
- Yao, S. et al. (2023). Tree of Thoughts: Deliberate Problem Solving with LLMs. NeurIPS. arXiv:2305.10601.
- Shinn, N. et al. (2023). Reflexion: Language Agents with Verbal Reinforcement Learning. NeurIPS. arXiv:2303.11366.
- Liu, N. et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. TACL. arXiv:2307.03172.
- Zheng, L. et al. (2023). Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena. NeurIPS. arXiv:2306.05685.
- Zheng, H. et al. (2024). Take a Step Back: Evoking Reasoning via Abstraction in LLMs. ICLR. arXiv:2310.06117.
- Sclar, M. et al. (2024). Quantifying Language Models' Sensitivity to Spurious Features in Prompt Design. ICLR. arXiv:2310.11324.
- Wallace, E. et al. (2024). The Instruction Hierarchy: Training LLMs to Prioritize Privileged Instructions. arXiv:2404.13208.
- OpenAI (2024). Prompt engineering guide. https://platform.openai.com/docs/guides/prompt-engineering.
- Anthropic (2024). Prompt engineering documentation. https://docs.anthropic.com/.










