Writing Effective Prompts
5.1: The Prompt Formula
Every effective prompt follows this structure:
[CONTEXT] + [TASK] + [CONSTRAINTS] + [OUTPUT FORMAT]Example:
CONTEXT: "We have a Next.js 14 app with Prisma and PostgreSQL."
TASK: "Add rate limiting to the /api/auth endpoints."
CONSTRAINTS: "Use Redis. Max 5 attempts per minute per IP. No external libraries."
OUTPUT: "Provide the middleware code and explain the approach."This is called the context sandwich: what exists, what you want, and what the limits are.
5.2: The Context Sandwich
The most reliable prompt pattern for any task:
[What exists] + [What you want] + [Constraints]Bad prompt:
Add authenticationGood prompt:
We have a Next.js app with Prisma (exists).
Add email/password auth using NextAuth.js (want).
Use the existing User model. No OAuth for now (constraints).The bad prompt gives AI zero context about your stack, existing patterns, or boundaries. It will guess -- and guess wrong.
5.3: The Incremental Build Pattern
Never ask for everything at once. Build in small, reviewable steps:
Step 1: "Create the database schema for user profiles"
-> Review, approve
Step 2: "Write the API endpoints for CRUD operations on profiles"
-> Review, approve
Step 3: "Build the frontend form for editing profiles"
-> Review, approveEach step is small enough to review, test, and catch mistakes before they compound.
When you ask AI to build an entire feature in one prompt, it makes assumptions. Those assumptions compound. By step 3, the code is based on guesses from step 1 that you never reviewed.
5.4: The Reference Pattern
Instead of describing what you want, point AI at existing code:
Create a ProductService following the exact pattern in
src/services/UserService.ts.
Same error handling, same return types, same validation approach.This works better than describing the pattern because AI can see the actual implementation, not your description of it.
In Cursor, use @filename to bring files directly into context.
5.5: The Constraint Frame
Constrain what AI should NOT do. This prevents the most common AI mistakes:
Requirements:
- No new dependencies
- No changes to the database schema
- Keep the existing API contract
- Under 50 lines
- Follow the pattern in AuthService.tsWithout constraints, AI will:
- Add unnecessary packages
- Over-engineer with abstractions
- Change things outside your scope
- Write 200 lines when 20 would work
5.6: Anti-Pattern Prompts
Things that never work well:
| Bad Prompt | Why It Fails | Better Version |
|---|---|---|
| "Add authentication" | No context about stack or patterns | "Add email/password auth using NextAuth.js with our User model" |
| "Build me an e-commerce site" | Way too broad, AI will guess everything | "Step 1: Create the product schema with these fields..." |
| "Make this faster" | Faster how? At what cost? | "Optimize this query to under 100ms. No schema changes allowed." |
| "Fix this" | Fix what? AI can't see the problem | "This returns undefined. Error log: [paste]. Expected: user object." |
5.7: Key Phrases That Work
| Situation | Phrase |
|---|---|
| Prevent hallucination | "Ask me clarifying questions first" |
| Force planning | "Don't write code yet -- just the plan" |
| Ensure testing | "Write the test FIRST, then implement" |
| Safe refactoring | "Comment out the original before moving code" |
| Root cause analysis | "Explain WHY this fails, don't just fix it" |
| Current practices | "Research current best practices for this" |