Prompt engineering
Write effective AI prompts that produce accurate, maintainable code — core principles, reusable templates, advanced techniques, and common pitfalls.
Prompt engineering
Well-crafted prompts are the difference between AI output you can ship and output you have to rewrite. This guide covers proven techniques for getting accurate, efficient, maintainable code from the AI agents and Agent workflows.
Core principles
Five principles cover most of what makes a prompt work. Each pairs a vague prompt with a specific one.
| Principle | Weak prompt | Strong prompt |
|---|---|---|
| Be specific | "Make a user component" | "Create a React component for a user profile card with avatar, name, email, and bio" |
| Provide context | "Add filters" | "In our e-commerce app using Next.js and Tailwind, add a product filter sidebar" |
| Specify format | "Make a blog type" | "Generate a TypeScript interface with JSDoc comments for a blog post" |
| Include examples | "Add user API" | "Create API endpoints like GET /api/users, POST /api/users, PUT /api/users/:id" |
| Set constraints | "Validate email" | "Create a function under 50 lines that validates email without regex" |
Prompt templates
Reusable scaffolds keep prompts consistent. Fill in the bracketed parts.
Create a React functional component called [ComponentName] that:
- Accepts props: [list props with types]
- Renders: [describe UI elements]
- Handles: [list interactions]
- Uses: [hooks, libraries]
- Follows: [style guide, patterns]Create a [METHOD] endpoint at /api/[resource] that:
- Accepts: [request body/params]
- Validates: [validation rules]
- Returns: [response format]
- Handles errors: [error cases]
- Uses middleware: [auth, logging]Advanced techniques
- Chain of thought — ask the AI to reason step by step: "First analyze the requirements, then design the architecture, then implement."
- Few-shot learning — provide several examples of the output you want, then ask for more of the same.
- Role playing — assign expertise: "As a senior React developer with accessibility expertise, review this component."
- Iterative refinement — build complex solutions across multiple prompts instead of one giant request.
- Negative examples — state what to avoid: "Avoid inline styles,
anytypes, andconsole.log. Use CSS modules, proper types, and a logger."
A complete example
A production-ready prompt is specific, scoped, and explicit about constraints.
Create a complete authentication system for a Next.js 14 app.
Requirements:
- Email/password and Google OAuth login
- JWT tokens with refresh token rotation
- Secure password hashing with bcrypt
- Protected API routes middleware
Technical stack:
- Next.js 14 with App Router, TypeScript strict mode
- Prisma ORM with PostgreSQL
- Zod for validation
Include:
1. Database schema for users and sessions
2. API routes for login, logout, register, refresh
3. React hooks for auth state
4. A protected-route wrapper component
Follow security best practices: CSRF protection, rate limiting,
secure cookie settings, input sanitization.Model-specific tips
| Model | Strengths | Tip |
|---|---|---|
| GPT-4 / GPT-4 Turbo | Complex multi-step tasks, detailed instructions | Use system prompts for consistent behavior |
| Claude 3 | Code analysis and refactoring, long context (200K tokens) | Provide examples for best results |
| Google Gemini | Very fast, 1M token context | Be explicit about output format |
| Local models (Ollama) | Full privacy, no API cost | Keep prompts concise — smaller context windows |
Iterative refinement strategy
Build complex features progressively rather than in one prompt:
- Start simple — "Create a basic todo list component with add and delete."
- Add features — "Add edit functionality and local storage persistence."
- Enhance UX — "Add animations, loading states, and keyboard shortcuts."
- Optimize & polish — "Optimize renders with memo, add error boundaries, improve accessibility."
- Test & document — "Write unit tests with Jest and add JSDoc comments."
Common pitfalls
- Vague instructions — "Make it better" gives the AI nothing to work with. Say what "better" means.
- Missing context — "Add authentication" omits the stack. Name the framework, library, and data store.
- No success criteria — "Make a form" has no definition of done. Spell out validation rules and states.
- Information overload — dumping an entire codebase buries the request. Break large tasks into focused prompts.
Checklist before you submit: Is the task clearly defined? Have you given the tech stack and constraints? Did you specify the output format? Are there examples? Is the scope reasonable for one prompt?
Next steps
- Apply these prompts inside a pipeline with Agent workflows.
- Review what each agent is best at in AI agents.
- Use prompts programmatically through the API overview.