Prompting Patterns for Agentic Coding
Effective prompt structures for different coding scenarios - features, bugs, refactoring, and more.
The Foundation
Good prompts for coding AI follow a consistent structure:
- Context - What exists, what you're working with
- Goal - What you want to accomplish
- Constraints - Limits and requirements
- Output format - How you want the response
Pattern 1: Feature Implementation
Feature Implementation
For adding new functionality
I need to implement: {{feature}} ## Current Context - Working in: [file/module] - Related code: [relevant files] - Tech stack: [frameworks/libraries] ## Requirements {{constraints}} ## Expected Deliverables 1. Implementation code 2. Explanation of key decisions 3. Suggested tests Start by explaining your approach.
Pattern 2: Bug Fix
Bug Fix Request
For fixing specific issues
I'm seeing unexpected behavior. ## Symptom {{symptom}} ## Expected {{expected}} ## Actual {{actual}} ## Relevant Code [paste code here] What's the minimal fix? Don't refactor—just fix the bug.
Pattern 3: Code Review
Code Review Request
For reviewing code before commit
Review this code for: 1. Bugs or logic errors 2. Security vulnerabilities 3. Performance issues 4. Code style/conventions ## Code to Review [paste code] ## Context - This code does: [purpose] - It will be called from: [callers] Be specific. If something is fine, say so briefly.
Pattern 4: Refactoring
Refactoring Request
For improving existing code
Refactor this code to: {{goal}} ## Current Code [paste code] ## Constraints - Preserve existing behavior - Maintain the same public interface - {{constraints}} ## Deliverables 1. Refactored code 2. Explanation of changes 3. How to verify nothing broke
Pattern 5: Explanation
Code Explanation
For understanding unfamiliar code
Explain this code like I'm a developer who's new to this codebase. ## Code [paste code] ## What I Want to Know 1. What does this code do at a high level? 2. What are the key functions/methods? 3. What are the inputs and outputs? 4. What might trip me up?
Pattern 6: Test Writing
Test Generation
For creating tests for existing code
Write tests for this function/component. ## Code to Test [paste code] ## Testing Framework [jest/vitest/pytest/etc] ## Requirements - Cover happy path - Cover edge cases - Cover error conditions - Use descriptive test names
Anti-Patterns to Avoid
| Anti-Pattern | Problem | Better Approach |
|---|---|---|
| "Fix this" | Too vague | Describe symptom and expected behavior |
| "Make it better" | Undefined goal | Specify what "better" means |
| No code context | AI guesses | Include relevant code snippets |
| Huge code dumps | Overwhelming | Focus on the specific area |
Context Management
Too Little Context
Fix the login bug.
AI can't help without seeing the code.
Too Much Context
Here's my entire application... [10,000 lines]
AI loses focus on the actual problem.
Just Right
The login function in auth.ts (lines 45-67) throws
an error when the password contains special characters.
[paste just those lines]
Iterative Refinement
When the first response isn't right:
-
Be specific about what's wrong
"The solution works but uses a deprecated API. Use X instead."
-
Add missed constraints
"I forgot to mention: this needs to work with React 18."
-
Ask for alternatives
"That approach won't work because of X. What are other options?"
Verify Understanding
Before implementing, verify the AI understood:
"Before writing code, summarize:
- What you understand the task to be
- What approach you'll take
- Any assumptions you're making"