How to Write Better Prompts for Claude Code
I observe that effective prompting with Claude Code is more about clear communication than clever tricks. The key insight I've developed through extensive usage is being specific about what you want while leveraging CLAUDE.md for persistent context.
Core Principles
- Be Specific: Instead of "improve this code," say "add error handling to the user authentication function to catch network timeouts"
- Provide Context: Mention your framework, coding style, or constraints. "I'm using React with TypeScript and prefer functional components"
- Set Clear Scope: Define boundaries. "Only modify the login component, don't change the routing logic"
- Include Examples: Show Claude the patterns you want. "Use the same error handling pattern as in the profile component"
Effective Prompt Patterns
Feature Development Good: "Add a search function to UserList.js that filters users by name and email, with debounced input and loading states"
Better: "Add a search function to UserList.js that filters users by name and email. Use the same debounced input pattern as ProductSearch.js, include loading states, and return results in the existing user card format."
Code Review Good: "Review this function for bugs"
Better: "Review the calculateShipping function in utils/pricing.js for edge cases, performance issues, and consistency with our error handling patterns."
Refactoring Good: "Clean up this component"
Better: "Refactor the UserProfile component to extract the form logic into a custom hook, while keeping the same prop interface and validation behavior."
Context-Setting Strategies
- Project Overview: Start sessions with "This is a React e-commerce app using TypeScript, Redux, and Material-UI"
- File Relationships: Explain connections. "This component is used in the checkout flow and receives user data from the parent"
- Constraints: Mention limitations. "We need to maintain backwards compatibility with the existing API"
- Preferences: Share your style. "I prefer async/await over promises and functional programming patterns"
Iteration Techniques
- Build Incrementally: Start with basic functionality, then add features. "First implement basic user search, then we'll add advanced filters"
- Ask for Alternatives: "Show me two different approaches for handling this state management"
- Request Explanations: "Explain why you chose this pattern over alternatives"
- Refine Results: "The error handling looks good, but make the error messages more user-friendly"
Advanced Prompting
Architecture Discussions "I need to add real-time notifications to my app. Consider my existing tech stack (React, Node.js, PostgreSQL) and suggest approaches with trade-offs."
Performance Optimization "The dashboard component is re-rendering too often. Analyze the component and suggest specific optimizations using React.memo, useMemo, or useCallback where appropriate."
Testing Strategy "Write comprehensive tests for the payment processing module. Include unit tests for calculation logic, integration tests for API calls, and edge cases for validation."
Common Pitfalls to Avoid
- Vague Requests: "Make this better" gives Claude no direction
- Missing Context: Requesting changes without explaining the existing system or constraints
- Too Broad: Asking Claude to "rewrite the entire app" instead of focusing on specific improvements
- No Examples: When you have specific patterns or styles, show Claude examples from your codebase
CLAUDE.md Supremacy
I observe that CLAUDE.md instructions are followed much more strictly than user prompts. This creates an adherence hierarchy where CLAUDE.md content is treated as immutable system rules while user prompts are interpreted as flexible requests.
Instead of repeatedly explaining your project context in prompts, I recommend documenting everything in CLAUDE.md:
- Project structure and coding standards for consistent context across sessions
- Specific file access permissions (which files Claude can read vs those he should avoid)
- Complete workflow examples with multiple scenarios to prevent instruction bleeding
- Your preferred patterns with references to existing code files
Meta-Prompting Techniques
When Claude fails mid-task, use meta-prompting to refine your approach. Ask Claude to identify what aspect of your CLAUDE.md instructed him to make that decision. He may reveal there's no logical reason, then request suggestions about what to change to prevent the same mistake.
This ultra-think approach helps you iteratively improve your CLAUDE.md for better results.
Sanity Check Your Configuration
Add your name at the top of CLAUDE.md, then ask "What is my name?" This simple check verifies that Claude is reading your configuration correctly. If he doesn't know your name, something is wrong with your setup.
Working within token limits forces better prompting habits. Use explicit file selection, clear task definition, and precise requests. These skills make you more effective regardless of context size. See Context Window Constraints for detailed strategies.