Skip to main content

Prompt Engineering

Anatomy of a prompt​

PartPut here
System promptRole, audience, rules, tone, output format, what to do when unsure
ContextDocuments, retrieved chunks, user profile, tool results
Examples1-5 input/output pairs showing the exact format and edge cases
TaskThe concrete question or instruction, last

Principles​

  • Be explicit: say what you want, the format, the length, and the audience. The model cannot read your mind.
  • Explain why: a reason behind a rule ("short, because it is read on mobile") generalizes better than the bare rule.
  • Structure with delimiters: wrap inputs in XML-like tags or Markdown headings so instructions and data do not mix.
  • Show, do not just tell: diverse examples beat long descriptions; make them match the format exactly.
  • Give an escape hatch: "If the answer is not in the documents, say you do not know."
  • Let it think on hard tasks: allow reasoning (thinking/effort settings or an explicit reasoning section) before the final answer.
  • Positive instructions: "Write in plain prose" works better than a list of "do not" rules.
  • One job per call: chain several focused prompts instead of one giant prompt.

System prompt template​

You are a support assistant for ACME's billing product.
Audience: non-technical customers. Answer in at most 4 sentences.

Rules:
- Use only the information inside <docs>. If it is not there, say so and offer to escalate.
- Never promise refunds; explain the refund policy instead.
- Reply in the user's language.

Output: plain text, no Markdown headings.
const msg = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 500,
system: SYSTEM_PROMPT,
messages: [
{ role: "user", content: `<docs>\n${docs}\n</docs>\n\nQuestion: ${question}` },
],
});

Techniques cheat table​

TechniqueWhen
Few-shot examplesOutput format or tone must be exact
Prompt chainingMulti-step tasks (extract → analyze → write)
Prefilled / structured outputNeed machine-readable output - prefer schema-constrained outputs
Self-checkAsk the model to verify its answer against the sources before finalizing
Quote first, then answerLong documents: extract relevant quotes, then answer from them

Iterate like engineering​

  1. Write success criteria and a small eval set first (see Evals).
  2. Change one thing at a time; re-run the evals.
  3. Version prompts in Git next to code.
  4. Re-test prompts when you change model - prompts tuned for one model do not always transfer.