Cheat Sheet
Reference
intermediate
Prompt Engineering Cheat Sheet
The Universal Prompt Formula
[ROLE] + [CONTEXT] + [TASK] + [CONSTRAINTS] + [FORMAT] + [EXAMPLES]
Not every prompt needs all elements. Use what adds clarity:
| Element | When to Include | Example |
|---|---|---|
| Role | Complex/specialized tasks | "As a senior TypeScript developer..." |
| Context | Task requires background | "In a Next.js 14 app with Prisma..." |
| Task | Always | "Create a pagination utility..." |
| Constraints | Avoid over-engineering | "Max 30 lines, no external deps..." |
| Format | Need specific output shape | "Return as a named export with JSDoc..." |
| Examples | Ambiguous expected output | "Input: [1,2,3] => Output: 6" |
Prompt Starters by Intent
| Intent | Starter Phrase |
|---|---|
| Generate new code | "Create / Build / Implement..." |
| Modify existing code | "Refactor / Update / Modify..." |
| Fix a bug | "Fix the bug where... / Debug why..." |
| Explain code | "Explain how... / Walk through..." |
| Review code | "Review this for... / Find issues in..." |
| Optimize | "Optimize this for [metric]..." |
| Convert | "Convert this from X to Y..." |
| Test | "Write tests for... covering..." |
Constraint Keywords
| Keyword | Effect |
|---|---|
| "Only" | Limits scope: "Only modify the return statement" |
| "Without" | Excludes: "Without using external libraries" |
| "Must" | Hard requirement: "Must handle null input" |
| "Maximum" | Size limit: "Maximum 50 lines" |
| "Using" | Tool/pattern lock: "Using Zod for validation" |
| "Matching" | Style lock: "Matching the pattern in [file]" |
| "No" | Prohibition: "No default exports" |
Quality Modifiers
Add these to improve output quality:
| Modifier | When to Use |
|---|---|
| "Production-ready" | Need error handling, edge cases |
| "Type-safe" | Need comprehensive TypeScript types |
| "Idiomatic" | Need language-native patterns |
| "Minimal" | Want fewest lines possible |
| "Documented" | Need inline comments or JSDoc |
| "Testable" | Need dependency injection, pure functions |
| "Performant" | Need optimized for speed/memory |
Context Provision Patterns
Inline context (short):
Given this type: { id: string; name: string; active: boolean }
Create a filter function that returns only active items.
Reference context (medium):
Using the User interface from src/types/user.ts:
[paste interface]
Create a validation function for user registration.
Architectural context (large):
This project uses:
- Repository pattern for data access
- Service layer for business logic
- Controller layer for HTTP handling
- Zod schemas for validation at boundaries
Create a new "orders" module following this architecture.
The Refinement Phrases
When the first output is close but not right:
| Situation | Refinement Prompt |
|---|---|
| Too complex | "Simplify this. Remove [specific abstraction]." |
| Missing edge case | "Also handle the case where [condition]." |
| Wrong style | "Rewrite matching this style: [example]" |
| Too verbose | "Reduce to under [N] lines without losing functionality." |
| Missing types | "Add TypeScript types to all parameters and returns." |
| Wrong approach | "Use [pattern] instead of [current approach]." |
| Incomplete | "Add error handling for [cases]." |
Output Format Specifications
# For a function:
"Return only the function. Named export. Include types. No comments."
# For a component:
"Single file. Props interface above component. No default export."
# For a module:
"One file per concern. Index file with re-exports. Types separate."
# For tests:
"Group by behavior with describe blocks. Use arrange-act-assert."
# For documentation:
"JSDoc format. Include @param, @returns, @throws, @example."
Token Efficiency Tips
| Instead Of | Write | Saves |
|---|---|---|
| Long preamble about who you are | Skip it, go straight to task | 20-50 tokens |
| "Could you please..." | Direct imperative: "Create..." | 5-10 tokens |
| Repeating context from earlier | "Same context as above" or reference | 50-200 tokens |
| Pasting entire files | Paste only relevant sections | 100-500 tokens |
| Explaining what you do not want | State what you DO want | 10-30 tokens |
Multi-Turn Strategy
| Turn | Purpose | Example |
|---|---|---|
| 1 | Establish context + core task | "Create the data model for..." |
| 2 | Add complexity | "Now add validation for..." |
| 3 | Edge cases | "Handle these edge cases:..." |
| 4 | Integration | "Wire this up to..." |
| 5 | Polish | "Add error messages and types" |
Emergency Phrases (When Stuck)
| Problem | Recovery Prompt |
|---|---|
| Output is wrong | "That's not what I need. Let me clarify: [restate clearly]" |
| AI is confused | "Ignore previous context. Fresh start: [clean prompt]" |
| Too much output | "Just show me [specific part], nothing else" |
| Not enough detail | "Expand on [specific section] with full implementation" |
| Wrong language/framework | "Use [correct tool]. Not [wrong tool]." |