LLM Prompt Engineering Cheat Sheet
Prompt structure, roles, few-shot examples, chain-of-thought, and common techniques for better LLM output.
Reference
prompt-engineering
llm
ai
Prompt engineering is the craft of writing instructions that steer a language model toward accurate, useful output. Clear structure, concrete examples, and explicit output constraints outperform vague requests.
Prompt structure
Table
| Element | Purpose | Example |
|---|---|---|
| Role | Set persona | "You are a senior reviewer." |
| Context | Provide background | "This is a Node.js API." |
| Task | State the goal | "Find bugs in this code." |
| Format | Specify output shape | "Return a bulleted list." |
| Constraints | Bound the answer | "Do not invent APIs." |
Techniques
Table
| Technique | How |
|---|---|
| Few-shot | Provide 2-3 input/output examples. |
| Chain-of-thought | Ask for step-by-step reasoning. |
| Role prompting | Assign an expert persona. |
| Output schema | Request JSON with specific keys. |
| Self-consistency | Sample multiple answers and pick the majority. |
Example prompt
code
Role: You are an expert TypeScript reviewer.
Context: This function parses user input in a web app.
Task: Identify security and correctness issues.
Format: Return a JSON array of {line, severity, issue, fix}.
Constraints: Only flag real bugs; do not restyle the code.
<code>
function parse(input) { return JSON.parse(input); }
</code>
Common pitfalls
Table
| Pitfall | Fix |
|---|---|
| Vague task | State the exact deliverable. |
| No format | Specify the output structure. |
| Overlong context | Trim to relevant facts. |
| Ambiguous constraints | Add explicit "do not" rules. |
| No examples | Add one good few-shot pair. |