Prompt Engineering
Master the art of crafting effective prompts that guide agent behavior.
What is Prompt Engineering?
Prompt engineering is the art and science of designing inputs that guide AI models to produce desired outputs. For agentic systems, this includes:
- System prompts that define agent behavior
- User messages that convey tasks clearly
- Tool descriptions that guide tool selection
- Few-shot examples that demonstrate patterns
The System Prompt
The system prompt is the foundational instruction that shapes how an agent behaves. It's like programming the agent's personality and capabilities.
You are an AI coding assistant. Your role is to help users with software development tasks. ## Capabilities - Read and write files - Execute shell commands - Search codebases - Generate and modify code ## Guidelines - Always read files before editing them - Prefer editing existing code over creating new files - Keep changes minimal and focused - Explain your reasoning when making decisions ## Safety Rules - Never execute destructive commands without confirmation - Don't modify files outside the project directory - Avoid introducing security vulnerabilities
Chain of Thought (CoT)
Chain of Thought prompting encourages the model to reason step-by-step before answering. This dramatically improves accuracy on complex tasks.
Prompt:
"What files do I need to modify to add a dark mode toggle?"
Response:
"You need to modify App.tsx, styles.css, and create a ThemeContext.tsx"
Effective Prompting Techniques
1Be Specific
"Fix the bug"
"Fix the null pointer exception in UserService.getUser() when the user ID doesn't exist"
2Provide Context
"Add authentication"
"Add JWT authentication to our Express API. We're using MongoDB for users and already have a User model."
3Define Expected Output
"Explain this code"
"Explain this code in 2-3 sentences, focusing on what it does rather than how"
4Use Examples (Few-Shot)
Convert these function names to camelCase: get_user_name -> getUserName calculate_total_price -> calculateTotalPrice Now convert: fetch_api_data ->
CLAUDE.md Files
For coding agents like Claude Code, CLAUDE.md files provide project-specific instructions. These files are read automatically and inform the agent about:
# CLAUDE.md ## Project Overview This is a Next.js e-commerce application using TypeScript. ## Development Commands - `npm run dev` - Start development server - `npm run build` - Production build - `npm test` - Run tests ## Code Style - Use functional components with hooks - Prefer named exports - Keep components under 200 lines ## Important Files - src/lib/api.ts - API client - src/contexts/CartContext.tsx - Shopping cart state - src/types/index.ts - Shared TypeScript types ## Don't Modify - .env files (contain secrets) - migrations/ (database migrations)
Common Pitfalls
Ambiguous Instructions
"Make it better" - Better how? Faster? More readable? More secure? Be explicit about your criteria.
Missing Constraints
"Add a search feature" - Without constraints, the agent might over-engineer. Specify scope: "Add a simple client-side search filter for the user list."
Assuming Context
The agent doesn't know what you know. If you reference "the bug," specify which one. If you mention "the API," clarify which endpoint.
Iterative Refinement
Great prompts often come from iteration. Start simple and refine:
// First attempt "Add tests" // Better - more specific "Add unit tests for the UserService class" // Even better - includes criteria "Add unit tests for UserService covering: - createUser with valid input - createUser with invalid email - getUser when user exists - getUser when user doesn't exist Use Jest and follow existing test patterns in __tests__/"
Key Takeaways
- System prompts define the agent's behavior and capabilities
- Chain of Thought prompting improves reasoning accuracy
- Be specific, provide context, and define expected outputs
- CLAUDE.md files give project-specific guidance
- Iterate on prompts to improve results