Prompt Engineering
Generative & Agentic AI · Working with LLMs · Basics · 12 min
Talk to models clearly to get great results: no code changes needed.
A recipe card with blanks: 'You are a ___. Explain ___ to a ___.' Your code writes into the blanks for each request, and a clear card produces the same good dish every time.
The idea in everyday words
A prompt is a brief for a brilliant new intern who knows everything but nothing about YOUR situation. 'Write something about dogs' gets junk. 'Write a 3-line funny poem about a lazy golden retriever for a 7-year-old' gets gold.
The RCTF recipe
- Role – "You are a senior Python tutor."
- Context – who it's for, background info.
- Task – exactly what to do.
- Format – "Answer in 3 bullet points" / "Return JSON".
Power techniques
- Few-shot: show 2–3 examples of input → output.
- Chain of thought: "Think step by step" for maths/logic.
- Delimiters: put data inside
"""or XML tags so it's not confused with instructions. - System prompt: rules the model must always follow.
- Prompt injection: users may type "ignore previous instructions": never trust user text blindly.
Step by step
- Bad:
'Summarize this.'
- + Role:
'You are a news editor.'
- + Context:
'Readers are busy teens.'
- + Task & Format:
'Summarize in 3 bullets, max 12 words each.'
- Result:
Clear, short, on-target summary.
Code example
template = """You are a {role}.
Explain {topic} to a {audience} in {n} bullet points."""
prompt = template.format(role="friendly teacher", topic="recursion", audience="10-year-old", n=3)
print(prompt)Interview tips
Mention few-shot, chain-of-thought, structured output (JSON mode).
Explain prompt injection and mitigations (separate system/user, output validation).
Prompting is the cheapest first step before RAG or fine-tuning.
Challenge: Prompt Template Filler
Replace every {key} placeholder in template with the value from values (convert values to strings).
- Replace every occurrence of a placeholder, not just the first.
- Keys are plain text: a key like
a.bmatches only the exact text{a.b}. - A placeholder with no matching key stays exactly as it is.
Example: fill("Hi {name}, learn {topic}!", {"name": "Ana", "topic": "RAG"}) → "Hi Ana, learn RAG!"
Solve it in the browser with tests, hints and an AI coach.
Practice problems
- LeetCode: Evaluate the Bracket Pairs of a String (fill (key) slots from a dictionary) (Medium)
- DeepLearning.AI: ChatGPT Prompt Engineering for Developers
- Amazon Bedrock: prompt engineering concepts
- Anthropic: prompt engineering guide
More in Working with LLMs
- In-Context Learning & Inference Parameters
- Decoding: Greedy, Beam Search, Sampling & Structured Output
- Building With LLM APIs
- Context Engineering & Prompt Caching
- Embeddings & Vector Search
Previous: Reasoning Models & RL Post-Training · Next: In-Context Learning & Inference Parameters