4.4.10Alignment, Prompting & RAG

Prompt engineering best practices

1,997 words9 min readdifficulty · medium6 backlinks

What a prompt actually does


The best practices (with WHY / HOW)

1. Be specific and unambiguous

Bad: "Summarize this." Good: "Summarize the text below in 3 bullet points, each ≤15 words, for a non-technical manager."

2. Give the model a role / persona

3. Provide examples — few-shot prompting

4. Ask for step-by-step reasoning — Chain-of-Thought (CoT)

5. Structure the prompt

6. Control the output format

Ask for JSON, tables, or a fixed schema. WHY: downstream code needs parseable output; specifying the schema reduces free-form drift.

7. Let the model say "I don't know"

8. Iterate: Forecast → Verify → Refine

Predict what output your prompt will yield, run it, compare, and steel-man the gap. Prompting is empirical.

Figure — Prompt engineering best practices

Worked examples


Common mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine a super-smart parrot that finishes your sentences using every book it ever heard. If you mumble, it finishes with any random ending. If you say clearly, "Pretend you're a math teacher, show me each step, and answer at the end," the parrot copies that careful style and gets it right. Prompt engineering is just learning to ask the parrot in the way that makes it copy the smart voice — with clear instructions, a couple of examples, and space to "think out loud."


Active-recall flashcards

What does a prompt fundamentally change about the model's output?
The conditional probability distribution P(answerprompt)P(\text{answer}\mid\text{prompt}) over next tokens — it steers which learned region is sampled.
Why does Chain-of-Thought improve hard reasoning?
Transformers do fixed compute per token; emitting reasoning tokens spreads computation across many tokens, giving more effective serial compute (a scratchpad).
Zero-shot vs few-shot?
Zero-shot = no examples in the prompt; few-shot = k1k\ge1 input→output examples enabling in-context learning without weight updates.
What is in-context learning?
The model inferring a task's pattern from examples/instructions in the prompt at inference time, with no parameter updates.
Why use delimiters like <doc>...</doc>?
To separate instructions from data, reducing ambiguity and blocking prompt-injection attacks.
Why explicitly allow "I don't know"?
The default most-probable continuation to a question is an answer, so the model hallucinates; permission to abstain raises the probability of honest refusal.
Steel-man: why isn't "longer prompt = better"?
Filler dilutes attention and models under-attend to mid-context tokens ("lost in the middle"); high signal-to-noise beats length.
When should you NOT use CoT?
On trivial lookup/classification tasks — it adds cost, latency, and can inject over-thinking errors.
One requirement for good few-shot examples?
They must be both correctly formatted AND correctly labeled, and ideally diverse.

Connections

  • Chain-of-Thought prompting — dedicated deep dive on reasoning tokens.
  • In-context learning — the mechanism behind few-shot.
  • Retrieval-Augmented Generation (RAG) — supplies factual context to reduce hallucination.
  • Prompt injection & LLM security — why delimiters matter.
  • Temperature and sampling — how randomness interacts with prompt steering.
  • Alignment & RLHF — why models follow instructions at all.
  • Hallucination in LLMs — the failure mode good prompting mitigates.

Concept Map

conditioned on

reshapes

has no goal, only

is a

steers sampling toward

shaped by best practices

reduce entropy via

condition on competence

define task by demo

spread compute

triggers

adds

more tokens equals

LLM next-token predictor

Context window

Prompt

P answer given prompt

Retrieval key into distribution

Correct region

Best practices

Be specific

Role / persona

Few-shot examples

Chain-of-Thought

In-context learning

Reasoning tokens as scratchpad

More effective compute

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, LLM koi "samajhne wala insaan" nahi hai — woh basically ek super-powerful next-word predictor hai. Aap jo bhi text doge (prompt), model uska sabse probable continuation generate karta hai. Iska matlab: aapka prompt hi decide karta hai ki model apni learned knowledge ke kaunse region se answer nikaalega. Isiliye clear, specific instruction dena zaroori hai — vague prompt doge toh model confuse hoke random ya galat direction pakad leta hai.

Best practices simple hain but powerful. Pehla: specific bano — task, format, length, audience sab likho. Doosra: role do ("you are an expert doctor") taaki high-quality wala style aaye. Teesra: few-shot examples do — 2-3 input→output pairs, model unse pattern seekh leta hai (yeh in-context learning kehlaata hai, bina training ke). Chautha aur sabse important reasoning tasks ke liye: Chain-of-Thought — "step by step socho" bolna. Kyunki transformer har token pe fixed compute karta hai, reasoning tokens likhwa ke aap use ek scratchpad de dete ho, aur difficult problems accurate ho jaate hain.

Kuch galतiyan avoid karo: "zyada lamba prompt matlab better" — galat hai, faltu text attention ko dilute karta hai. "CoT hamesha use karo" — nahi, simple lookup tasks pe woh sirf cost aur error badhaata hai. Aur "model samajh jayega main kya chahta hoon" — bilkul nahi, woh intention nahi predict karta, tokens predict karta hai, toh sab kuch explicitly likho.

Ek aur pro tip: user ke data ko delimiters (<doc>...</doc>) mein wrap karo taaki instruction aur data mix na ho — yeh prompt injection attacks bhi rokta hai. Aur model ko "I don't know" bolne ki permission zaroor do, warna woh jhooth (hallucination) bana ke bhi answer de dega. Prompting empirical cheez hai — forecast karo, run karo, verify karo, refine karo. Bas yahi loop maste ka aata hai.

Go deeper — visual, from zero

Test yourself — Alignment, Prompting & RAG

Connections