How to Write Effective Prompts for Claude Code
Claude Code responds dramatically better to well-structured prompts than to casual messages. For foundational knowledge visit the getting-started hub. The difference between a prompt that produces mediocre results and one that unlocks precise, actionable output — similar to how prompt optimization improves skill accuracy often comes down to how you frame the task, provide context, and specify expectations. Clear prompts also help you make Claude Code write secure code by default.
This guide provides practical techniques for writing prompts that get results.
Start with Clear Task Framing
The first sentence of your prompt should define what you want Claude to do. Avoid ambiguity in the opening.
Weak:
Fix the authentication bug.
Strong:
Review the login flow in auth.js and identify why session tokens are not being cleared on logout.
The strong version specifies the file, the exact issue, and the expected outcome. Claude immediately knows where to look and what success looks like.
For complex tasks, use a structured format:
Task: Refactor the user service
File: services/user.ts
Goals:
- Extract validation logic into separate functions
- Add TypeScript types for all function parameters
- Maintain existing API signatures
Constraints:
- Do not change the public API
- Keep all existing unit tests passing
Provide Relevant Context
Claude performs better when it has the right context. Include the information Claude needs to make good decisions.
Include Code Context
When asking Claude to modify code, provide relevant surrounding code:
In the handlePayment function (shown below), the error handling is inconsistent with our
other API handlers. Refactor it to match our standard pattern.
```typescript
async function handlePayment(req: Request): Promise<Response> {
try {
const result = await processPayment(req.body);
return json(result);
} catch (e) {
return json({ error: e.message }, 500);
}
}
### Set the Environment
Tell Claude about your project structure, dependencies, and constraints:
Working on a Next.js 14 app with TypeScript. Using the app router, not pages router. We use Prisma with PostgreSQL and follow the repository pattern for data access.
This context prevents Claude from suggesting solutions that don't fit your stack.
## Break Down Complex Tasks
Large, undifferentiated requests produce shallow results. [Break complex tasks into explicit steps](/claude-skills-guide/best-way-to-scope-tasks-for-claude-code-success/):
I need to add user authentication to our API. Please:
- First, review our current Express setup in server.ts to understand middleware patterns
- Create a JWT-based authentication middleware in middleware/auth.ts
- Add the middleware to all routes in routes/api.ts that require authentication
- Write unit tests for the auth middleware covering: valid token, missing token, expired token, and invalid token scenarios ```
The numbered approach gives Claude a clear roadmap and lets you verify progress at each stage.
Use Skills for Domain-Specific Prompts
Claude Code’s skill system provides specialized knowledge for particular domains. Invoking the right skill dramatically improves output quality:
/pdf Create a quarterly report PDF with header, summary section, and data tables pulled from our analytics API.
/tdd Write unit tests for a rate limiter that allows 100 requests per minute per user ID, with burst allowance.
/frontend-design Create a modal component with smooth enter/exit animations, focus trapping, and escape-to-close.
/xlsx Parse the uploaded Excel file, validate the schema, and generate a summary with row counts and validation errors.
For complex features, break them into phases with checkpoints. Instead of “build an analytics dashboard,” start with the data layer, verify it works, then prompt for the visualization layer. This incremental approach prevents massive code dumps that don’t integrate cleanly.
Specify Output Format
Define exactly what you want the output to look like. This reduces back-and-forth and produces more useful results.
Code Output Examples
Generate a React component for displaying a todo list. Output should include:
- A TypeScript interface for TodoItem
- The complete component with props typing
- Basic styling using CSS modules
- Export statement as named export
Structured Text Output
For non-code outputs, specify the format:
Analyze the API response times from the logs and provide:
1. Average response time per endpoint (table format)
2. Top 3 slowest endpoints with their request counts
3. One-paragraph summary with recommendations
Use bullet points for the recommendations section.
Use Constraints to Guide Behavior
Explicit constraints prevent unwanted behavior and keep Claude focused:
Write a Python function that reads a CSV file and returns aggregated statistics.
- Use pandas only if already imported in the project
- Handle empty files by returning an empty dict
- Include docstring with type hints
- Do not print anything; return all output
Constraints are especially useful for avoiding common pitfalls:
- “Do not use any external libraries beyond what’s already in requirements.txt”
- “Preserve all existing comments in the refactored code”
- “Do not create new files; modify only the ones I specify”
Using Claude Code’s Skill System
Claude Code’s skill system lets you package effective prompts into reusable tools. A well-written skill combines all the techniques above into a reusable format.
Example skill structure:
---
name: code-review
description: Perform a focused code review on provided code
---
# Code Review Skill
You are a senior software engineer conducting a focused code review.
## Input Format
You will receive:
- A code snippet to review
- Optional: specific concerns to address
## Review Areas
For each snippet, assess:
1. **Correctness**: Does the code do what it's supposed to?
2. **Security**: Are there potential vulnerabilities?
3. **Performance**: Are there obvious inefficiencies?
4. **Readability**: Is the code maintainable?
## Output Format
Provide your review in this structure:
Summary
[2-3 sentence overview]
Issues Found
| Severity | Location | Description | Recommendation | |———-|———-|————-|—————–| | … | … | … | … |
Strengths
[What works well in this code]
## Iterative Refinement
Your first prompt rarely produces perfect results. Use follow-up prompts to refine:
1. **Clarify**: "Can you be more specific about the security issue in line 15?"
2. **Expand**: "Now add error handling to the function you just wrote."
3. **Narrow**: "That's too complex. Simplify it to use only native JavaScript, no libraries."
4. **Verify**: "Does this implementation handle the case where the API returns a 429 status?"
Each refinement teaches Claude your preferences and produces better subsequent output. Combining iterative prompting with [Claude's skill system](/claude-skills-guide/how-to-write-a-skill-md-file-for-claude-code/) lets you make your best prompts permanent and reusable.
## Practical Prompt Template
For repetitive tasks, create a template you can reuse:
Task
[What to do]
Context
- Project: [name/type]
- Tech stack: [relevant technologies]
- Code location: [files/paths]
Requirements
- [Specific requirement 1]
- [Specific requirement 2]
Constraints
- [What to avoid]
- [Boundaries to respect]
Output
[Expected format] ```
Summary
Effective prompts for Claude Code share common characteristics: clear task framing upfront, relevant context, broken-down steps for complex work, explicit output formats, and well-defined constraints. Practice these techniques and refine based on results. The better your prompts, the more precise and useful Claude Code’s responses become.
Related Reading
- Claude Skills Auto Invocation: How It Works — understand how skills auto-invoke so you can prompt more effectively
- How to Make Claude Code Make Smaller Focused Changes — prompt techniques for scoping Claude to precise changes
- How to Make Claude Code Not Over Engineer Solutions — prompt patterns that keep Claude solutions lean and maintainable
- How to Optimize Claude Skill Prompts for Accuracy — advanced prompt optimization to improve skill output quality
Built by theluckystrike — More at zovo.one