communicationengineering
AI Prompt Writing
Write effective prompts for AI coding tools — structure task context, specify constraints, provide examples, and iterate based on output quality. Targets developers using Claude Code, Copilot, Cursor, and similar tools.
ai-promptsprompt-templatesAIcoding-assistantClaude-CodeCopilotCursor
Works well with agents
Works well with skills
$ npx skills add The-AI-Directory-Company/(…) --skill ai-prompt-writingSKILL.md
Markdown
| 1 | |
| 2 | # AI Prompt Writing |
| 3 | |
| 4 | ## Before you start |
| 5 | |
| 6 | Gather the following from the user. If anything is missing, ask before proceeding: |
| 7 | |
| 8 | 1. **What tool are you prompting?** — Claude Code, GitHub Copilot, Cursor, Gemini CLI, ChatGPT, or another AI coding tool |
| 9 | 2. **What task should the prompt accomplish?** — Code generation, refactoring, debugging, review, documentation, or explanation |
| 10 | 3. **What is the codebase context?** — Language, framework, project conventions, relevant files |
| 11 | 4. **What does a good result look like?** — Expected output format, quality criteria, or a reference example |
| 12 | 5. **What has gone wrong so far?** — Previous prompt attempts and how the output missed the mark |
| 13 | |
| 14 | ## Prompt writing procedure |
| 15 | |
| 16 | ### 1. State the Task Before the Context |
| 17 | |
| 18 | Lead with what you want, then provide supporting information. AI tools process instructions sequentially — burying the task after paragraphs of context reduces accuracy. |
| 19 | |
| 20 | **Structure:** |
| 21 | ``` |
| 22 | [What to do] — 1-2 sentences |
| 23 | [Constraints] — format, style, boundaries |
| 24 | [Context] — relevant code, files, architecture |
| 25 | [Examples] — input/output pairs if helpful |
| 26 | ``` |
| 27 | |
| 28 | Bad: "Here is my React component that uses useState and useEffect to fetch data from /api/users and display it in a table. The table has sorting and pagination. I want you to..." |
| 29 | |
| 30 | Good: "Refactor this React component to replace useState+useEffect data fetching with React Query. Keep the existing sorting and pagination behavior. Here is the component: ..." |
| 31 | |
| 32 | ### 2. Be Specific About Constraints |
| 33 | |
| 34 | Vague prompts get vague results. Constrain the output to match your project's conventions: |
| 35 | |
| 36 | - **Language/framework version**: "Use TypeScript 5, React 19, Next.js 15 App Router" |
| 37 | - **Style conventions**: "Follow the existing pattern in src/hooks/ — named exports, no default exports" |
| 38 | - **What NOT to do**: "Do not add new dependencies. Do not change the public API." |
| 39 | - **Scope boundaries**: "Only modify the fetchUsers function. Do not touch the rendering logic." |
| 40 | |
| 41 | Each constraint eliminates a category of wrong answers. |
| 42 | |
| 43 | ### 3. Provide Concrete Examples |
| 44 | |
| 45 | When the task involves a pattern, show the pattern instead of describing it: |
| 46 | |
| 47 | ``` |
| 48 | Convert this REST endpoint to the project's tRPC pattern. |
| 49 | |
| 50 | Existing tRPC endpoint for reference: |
| 51 | // src/server/routers/posts.ts |
| 52 | export const postsRouter = router({ |
| 53 | list: publicProcedure |
| 54 | .input(z.object({ limit: z.number().default(10) })) |
| 55 | .query(async ({ input, ctx }) => { |
| 56 | return ctx.db.post.findMany({ take: input.limit }); |
| 57 | }), |
| 58 | }); |
| 59 | |
| 60 | REST endpoint to convert: |
| 61 | // src/pages/api/users.ts |
| 62 | [paste the code] |
| 63 | ``` |
| 64 | |
| 65 | One concrete example communicates format, naming, error handling, and style better than a paragraph of instructions. |
| 66 | |
| 67 | ### 4. Scope the Context Window |
| 68 | |
| 69 | AI tools have limited context. Include only what matters for the task: |
| 70 | |
| 71 | - **Include**: Files the tool needs to read or modify, type definitions it must conform to, test files that show expected behavior |
| 72 | - **Exclude**: Unrelated modules, boilerplate, configuration files unless they affect the task |
| 73 | - **Reference, do not paste**: For large files, describe the relevant section instead of pasting 500 lines — "The User type is defined in src/types.ts with fields: id, email, name, role" |
| 74 | |
| 75 | When using file-aware tools (Claude Code, Cursor), point to files by path instead of pasting content. The tool reads them with full context. |
| 76 | |
| 77 | ### 5. Specify the Output Format |
| 78 | |
| 79 | Tell the tool exactly how to deliver the result: |
| 80 | |
| 81 | - "Output only the modified function, not the entire file" |
| 82 | - "Return a unified diff I can apply with `git apply`" |
| 83 | - "Create the file at src/hooks/useUsers.ts with the complete implementation" |
| 84 | - "List each change as: file path, line range, description of change" |
| 85 | |
| 86 | Unspecified format leads to incomplete snippets, unnecessary explanations, or code wrapped in markdown when you wanted a raw file. |
| 87 | |
| 88 | ### 6. Use Iterative Refinement |
| 89 | |
| 90 | First-attempt prompts rarely produce perfect results. Iterate systematically: |
| 91 | |
| 92 | 1. **Run the prompt** and examine the output |
| 93 | 2. **Identify the gap** — wrong format, missing edge case, incorrect assumption, or style mismatch |
| 94 | 3. **Add a constraint** that addresses the specific gap — do not rewrite the entire prompt |
| 95 | 4. **Re-run** and compare to the previous output |
| 96 | 5. **Repeat** until the output meets your quality criteria. Save effective prompts as reusable templates. |
| 97 | |
| 98 | ### 7. Prompt Templates for Common Tasks |
| 99 | |
| 100 | **Bug fix:** |
| 101 | ``` |
| 102 | Fix this bug: [describe the symptom and reproduction steps]. |
| 103 | The relevant code is in [file path]. The expected behavior is [X], but the actual behavior is [Y]. |
| 104 | Root cause hypothesis: [your guess, if any]. |
| 105 | Do not change the function signature or public API. |
| 106 | ``` |
| 107 | |
| 108 | **Code review:** |
| 109 | ``` |
| 110 | Review this diff for: correctness bugs, performance issues, security concerns, and style violations against [project conventions]. |
| 111 | For each issue, state: file, line, severity (critical/warning/nit), and a specific fix. |
| 112 | Do not comment on things that are correct. |
| 113 | ``` |
| 114 | |
| 115 | **Refactoring:** |
| 116 | ``` |
| 117 | Refactor [function/component] to [specific goal: extract hook, split component, reduce complexity]. |
| 118 | Preserve all existing behavior — the existing tests must still pass without modification. |
| 119 | Follow the pattern established in [reference file]. |
| 120 | ``` |
| 121 | |
| 122 | ## Quality checklist |
| 123 | |
| 124 | Before sending a prompt to an AI coding tool, verify: |
| 125 | |
| 126 | - [ ] The task statement comes first, before context and examples |
| 127 | - [ ] At least 2 specific constraints narrow the output (format, style, scope, exclusions) |
| 128 | - [ ] Concrete examples are provided for pattern-based tasks |
| 129 | - [ ] Only relevant files and context are included — no unrelated code |
| 130 | - [ ] Output format is explicitly specified |
| 131 | - [ ] The prompt has been tested and refined at least once based on initial output |
| 132 | |
| 133 | ## Common mistakes |
| 134 | |
| 135 | - **Dumping an entire codebase and saying "fix it."** Without a specific task and scope, the tool guesses what you want. Narrow the ask. |
| 136 | - **Describing the pattern instead of showing it.** "Use our standard hook pattern" means nothing to the tool. Paste a reference implementation. |
| 137 | - **Omitting what NOT to do.** AI tools are eager to help. Without exclusions, they add dependencies, change APIs, or refactor code you did not ask about. |
| 138 | - **Giving up after one attempt.** The first output is a draft. Add a constraint, re-run, and compare. Most prompts need 2-3 iterations. |
| 139 | - **Treating all AI tools identically.** Claude Code reads files from disk. Copilot autocompletes inline. Cursor uses file context. Tailor the prompt to the tool's interface and reference files by path when the tool can read them directly. |
| 140 |