DevTools Logo

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
ElementPurposeExample
RoleSet persona"You are a senior reviewer."
ContextProvide background"This is a Node.js API."
TaskState the goal"Find bugs in this code."
FormatSpecify output shape"Return a bulleted list."
ConstraintsBound the answer"Do not invent APIs."

Techniques

Table
TechniqueHow
Few-shotProvide 2-3 input/output examples.
Chain-of-thoughtAsk for step-by-step reasoning.
Role promptingAssign an expert persona.
Output schemaRequest JSON with specific keys.
Self-consistencySample 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
PitfallFix
Vague taskState the exact deliverable.
No formatSpecify the output structure.
Overlong contextTrim to relevant facts.
Ambiguous constraintsAdd explicit "do not" rules.
No examplesAdd one good few-shot pair.

References

Related tools