AI

Prompt Engineering Basics

Clear instructions, examples, structure, and evaluation — the foundation before fancy RAG.

Interview tip Framework: Role + Task + Context + Format + Constraints. Always show 1–2 few-shot examples for structured output.

① What you must know (30 sec)

Prompt engineering is designing inputs so LLMs reliably produce the output you need — without retraining. Core framework: Role (who), Task (what), Context (background), Format (shape), Constraints (rules). Techniques: few-shot examples, chain-of-thought, self-check, and temperature tuning.
Analogy: Prompting is like writing a great brief for a freelancer — vague briefs get vague work; precise briefs with examples get repeatable results.

② How it works

Role — "You are a senior backend engineer reviewing PRs"
Task — "List security issues in the diff below"
Context — requirements, audience, data the model needs
Format — JSON schema, bullet count, markdown table, max words
Constraints — "Only use provided context; say unknown if missing"
Technique picker
Need structure?Few-shot examplesNeed reasoning?Chain-of-thoughtNeed facts?RAG or tools
Always A/B test prompt changes on a fixed eval set — intuition lies; metrics don't.

③ Step-by-step (hands-on)

Step 1 — Define success criteria

What does good output look like? Write 5 ideal examples before crafting the prompt.

Step 2 — Draft RTCF prompt

Role + Task + Context + Format + Constraints in clear sections or XML tags.

Step 3 — Add 1–2 few-shot examples

Show input → expected output for classification, extraction, or formatting tasks.

Step 4 — Tune parameters

temperature=0 for deterministic facts; higher for brainstorming. Set max_tokens to prevent rambling.

Step 5 — Build eval set

20–50 inputs with golden outputs. Run prompt v1 and v2; compare accuracy or use LLM-as-judge.

Step 6 — Harden for production

Separate system vs user content; defend against injection; version prompts in git.

④ Code / config patterns

TechniquePurposeExample phrase
Few-shotTeach format by exampleInput: ... Output: ...
Chain-of-thoughtMulti-step reasoning"Think step by step"
Self-checkReduce errors"Verify against rules before answering"
DelimitersSeparate sections### Context ### ... ### Task ###
Negative constraintsPrevent bad behavior"Do not invent citations"
SYSTEM = """You are a ticket classifier.
Output JSON only: {"label": "billing|bug|feature", "confidence": 0-1}

Example:
Input: "I was charged twice"
Output: {"label": "billing", "confidence": 0.95}
"""
USER = f"Input: {ticket_text}"

⑤ Production & pitfalls

PitfallWhy it hurtsFix
Vague instructionsInconsistent outputsRTCF framework; explicit format
No examples for structured outputJSON syntax errorsFew-shot 2–3 examples in prompt
High temperature for factsHallucinationstemperature 0–0.2 for extraction/classification
Prompt too long without hierarchyModel ignores middleXML tags; repeat key rules at end
No eval loopRegression on "improvements"Fixed test set before/after changes
User content in system promptInjection overrides instructionsSanitize user input; delimiter boundaries
Production tips:
  • Store prompts in version control with changelog
  • Prompt templates with variable slots — not string concat in app code
  • Monitor output format parse failure rate
  • Fallback prompt or smaller model when primary fails

⑥ Interview / on-the-job Q&A

QuestionAnswer
What is few-shot prompting?Including example input/output pairs in the prompt to teach format and behavior.
What is chain-of-thought?Asking the model to reason step by step before the final answer — improves logic tasks.
Temperature 0 vs 1?0 = more deterministic; 1 = more random/creative.
When is prompting not enough?Need fresh facts (RAG), actions (tools), or consistent niche style at scale (fine-tune).
What is prompt injection?User text that tricks the model to ignore system instructions — mitigate with separation and validation.
How evaluate prompts?Labeled test set, accuracy/F1, human review, or LLM-as-judge with rubric.

⑦ Tools & ecosystem

  • Playgrounds: OpenAI Playground, Claude console
  • Eval: Promptfoo, LangSmith datasets
  • Libraries: Guidance, Instructor (structured output)
  • Versioning: git, PromptLayer
promptingfew-shotcotevaluationrtcf

⑧ Revision checklist

  • RTCF sections present in system prompt
  • 2+ few-shot examples for structured tasks
  • Temperature appropriate for task type
  • max_tokens set to prevent runaway output
  • Eval set of 20+ examples created
  • Prompts versioned in git
  • User input separated from system instructions
  • Parse failures logged and monitored
  • Documented when to escalate to RAG/tools