Technical
Writing Effective Prompts for Code Generation
Bad prompts produce bad code, and most people write bad prompts. I have written thousands of prompts for AI coding tools over the past year, and the pattern is clear: specificity beats cleverness every single time.
The Prompt Spectrum
Prompts exist on a spectrum from vague to precise:
- Vague: 'Build me an API'
- Better: 'Build a REST API with FastAPI that has CRUD endpoints for blog posts'
- Best: 'Add a POST /posts endpoint that accepts a PostCreate Pydantic model with title, body, slug, and categories fields. Return 201 with the created post. Generate slug from title if not provided.'
The best prompts tell the AI exactly what to build, what tools and patterns to use, and what the output should look like. Ambiguity is the enemy of good AI output.
The WHAT-HOW-VERIFY Pattern
Every effective prompt has three parts:
- WHAT: What you want built (the business requirement)
- HOW: Constraints and patterns to follow (the implementation guide)
- VERIFY: How to confirm it works (the acceptance criteria)
WHAT: Add email validation to the subscriber endpoint
HOW: Use Pydantic's EmailStr type, return 422 for invalid emails
VERIFY: POST with 'not-an-email' returns 422, POST with 'test@example.com' returns 201When you include verification criteria in your prompt, the AI can test its own output. This dramatically reduces the number of iteration rounds needed.
Common Prompt Mistakes
Mistake 1: Too much context. Do not paste your entire codebase into the prompt. Give the AI the specific files it needs and describe the pattern to follow. More context is not always better. Relevant context is better.
Mistake 2: No constraints. 'Build a login system' could mean JWT tokens, session cookies, OAuth providers, or magic links. Specify your constraints: what library, what token format, what storage backend.
Mistake 3: Ambiguous scope. 'Improve the API' is not actionable. 'Add rate limiting to the POST /posts endpoint using a 60-second sliding window with a maximum of 10 requests per IP' is actionable.
Prompts for Different Tools
| Tool | Prompt Style | Example | |------|-------------|--------| | Claude Code | Conversational, multi-step | 'Read src/models.py, then add a Subscriber model following the same pattern' | | Cursor | Short, inline | 'Refactor to async' (with code highlighted) | | Copilot | Comment-driven | Write a code comment describing the next function |
The Meta-Skill
Prompting is the meta-skill of the AI era. It is not about memorizing templates or tricks. It is about clearly communicating what you want, what constraints exist, and how to verify the result. Practice by turning your next coding task into a prompt before you start writing code. If you cannot describe it clearly to an AI, you probably cannot describe it clearly to a human colleague either.
For more prompting strategies, see Anthropic's prompt engineering guide.
RELATED READING
The Consulting Shift I Am Making In Year Two
After a year of writing and building, my consulting practice is changing shape. Shorter engagements. Sharper outcomes.
ReadThe Frontend Shift: Shipping Less JavaScript In Year Two
A year ago I reached for Next.js for everything. This year I often reach for nothing.
ReadThe Serverless Lesson I Would Write On A Sticky Note
After a year of shipping serverless projects, one rule explains most of the wins and all of the losses.
Read