Prompt engineering cheatsheet.
Anatomy of a good prompt
- Role / context: who/what the LLM is.
- Task: what to do.
- Input: data to operate on.
- Format: structure of output.
- Constraints: tone, length, things to avoid.
- Examples: optional but powerful.
You are an expert technical writer.
Summarize the following article in 3 bullet points.
Each bullet must be one sentence and under 20 words.
Use neutral tone. Don't include opinions.
Article:
{text}
Output as JSON: {"summary": ["...", "...", "..."]}
System message
Used for stable behavior across calls. Keep concise:
You are a helpful coding assistant. Answer in concise Python.
Always include type hints.
If unsure, say so explicitly.
Zero-shot
Translate "Hello" to Spanish.
Few-shot
Translate English to Spanish.
en: Hello
es: Hola
en: Goodbye
es: Adiós
en: Thank you
es:
3-5 examples typically enough. Match desired output exactly.
Chain-of-thought (CoT)
Q: There are 3 apples. I eat 1. How many remain?
A: Let's think step by step. Started with 3. Ate 1. So 3-1=2.
The answer is 2.
Often unnecessary with reasoning models (o-series, Claude with extended thinking). Use for non-reasoning models.
Self-consistency
Ask multiple times, vote. Costs more, more accurate for hard tasks.
Tree of thoughts
Branch and prune reasoning paths. Complex; consider just using a reasoning model.
Structured input/output
Input as JSON:
{"name": "Alice", "age": 30}
Output as JSON only:
{"summary": "<summary>"}
Do not include explanation. Do not wrap in markdown.
For reliable JSON: use the API’s JSON mode / tool calling.
Constraints
- Do not invent facts.
- If you don't know, say "I don't know."
- Cite sources from <context>.
- Keep response under 200 words.
- Use Markdown headings.
Persona
You are a senior software engineer with 20 years of Linux experience.
You are blunt and concise.
Subtle effect. Don’t rely on persona alone for correctness.
XML tags (Anthropic prefers)
<task>Summarize the article</task>
<article>
...
</article>
<format>3 bullet points</format>
Clarifies structure for Claude especially.
Markdown headings
# Article
# Task
Summarize ...
Most LLMs respect markdown structure.
Avoiding hallucinations
- Provide source material; ask LLM to only use it.
- “If unsure, say I don’t know.”
- Validate output against schema / facts.
Use ONLY the information in <context> below.
If the answer isn't there, respond with "NOT_FOUND".
Long context
- Put critical instructions at start AND end.
- Use clear sectioning.
- LLMs sometimes ignore middle. Test.
Refusal mitigation
Frame requests legitimately. “Educational” / “for security research” can help if genuine.
Decoding from output
For when LLM is verbose:
Respond with ONLY the JSON object.
Do not include "```json" or any commentary.
Few-shot for format
Even ZERO content examples can show desired format:
Examples of output format:
{"name": "...", "score": 95}
{"name": "...", "score": 80}
Now process: <data>
Eval prompts
For evaluating other LLM outputs:
Rate the following response 1-5 on helpfulness, accuracy, and tone.
Return JSON: {"helpful": N, "accurate": N, "tone": N, "reason": "..."}
Temperature for prompt design
- Test prompts at low temp (0-0.3) to see “default” behavior.
- Use higher temp for variety / brainstorming.
Iterative refinement
Treat prompts as software:
- Define test cases.
- Try prompt.
- Note failures.
- Adjust.
- Re-test.
- Version-control.
Anti-patterns
- “Be creative” without specifics → drift.
- Negative-only constraints (“don’t do X”) → unclear what TO do.
- 50 instructions in one prompt → some ignored.
- Single long prompt vs structured conversation → conversation often clearer.
- Trust LLM math without verification.
Reasoning models (o1, o3, Claude with extended thinking)
- Less prompt engineering needed.
- Don’t use CoT — model does it internally.
- Give task + constraints; let it reason.
Solve this problem step by step. Output only the final answer in <answer> tags.
Problem: ...
Reusable prompt templates
PROMPT = """
You are {role}.
Task: {task}
Input:
{input}
Output format:
{format}
Constraints:
- {c1}
- {c2}
"""
Use Jinja2 / f-strings.
Common mistakes
- Vague prompts → vague outputs.
- Mixing examples with current task without separators.
- Asking LLM to “be concise” without specifying length.
- Forgetting that LLM continues your text.
- Not testing on edge cases.
Read this next
If you want my prompt library, it’s at rajpoot.dev .
Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .