How to Write Prompts (Coding Edition)

AI Navigate Original / 5/16/2026

共有:

Key Points

  • Coding AI output changes greatly with how you prompt
  • 5 rules: enough context, completion condition, constraints, file refs, plan first
  • Use frequent patterns for features, bug fixes, refactors, tests
  • Avoid vague asks; language-specific tips; persist via Rules files

Basics of Prompting Coding AI

AI coding tools' output quality changes greatly with how you ask. Here are coding-specific tips distinct from general prompts.

5 Golden Rules

1. Give Enough Context

Over "fix this code," say "this project uses Next.js 16, TypeScript strict, Vitest. Fix module X like Y."

2. State the Completion Condition

Over "make it nice," say "all tests pass, no lint errors, existing API compatible."

3. Convey Constraints

  • "Libraries you may use: existing dependencies only"
  • "Don't call external APIs"
  • "O(N) or less"
  • "Within 100 lines"

4. Use File References

  • Cursor: reference with @filename
  • Claude Code: "refer to @auth.ts"
  • Copilot Chat: auto-references the file open in the editor

5. Ask for a Plan First

"First a 5-step implementation plan.
Start implementing after I OK it.
Ask if questions arise midway"

Frequent Patterns

1. New Feature Addition

Feature: users can upload a profile image
Requirements:
- Format: JPG/PNG, under 5MB
- Storage: S3
- DB: save url in the profile table
- Use the existing auth middleware
- Add tests too

First make a plan, implement after my OK.

2. Bug Fix

Symptom: top page 404s after login
Repro steps:
1. Access /login
2. Enter email/password
3. Click "Log in"
→ /home is 404

Expected: /home displays normally
Recent change: feat/auth-refactor PR

Investigate and propose cause and fix.

3. Refactor

Target: all of lib/auth/*
Goal: migrate to JWT (currently session)
Constraints:
- Maintain existing API compatibility
- All existing tests pass
- Staged: first run JWT in parallel, remove session later

Present plan and impact range first.

4. Add Tests

Target: src/utils/parseDate.ts
Coverage: 0% → 90%
Test framework: Vitest
Cases:
- Normal (multiple formats)
- Abnormal (invalid input, null, undefined)
- Edge (leap year, timezone)

After implementing, confirm coverage and report.

Patterns to Avoid

  • "Make it better": vague, AI interprets arbitrarily
  • "In the latest style": criterion unclear
  • "Cool": over-seasoned
  • A huge "all": split it

Language-Specific Tips

TypeScript

  • State "no any, strict mode"
  • Specify "type-validate with Zod / Valibot"
  • Expected type inference when using generics

Python

  • "Type hints with typing, mypy strict"
  • "Manage with pyproject.toml and uv"
  • "With Pydantic v2"

Rust

  • "No unsafe blocks"
  • "Address all Clippy warnings"
  • "State lifetimes explicitly"

Recovery on Failure

Tell It "It Doesn't Work"

"Test X fails with the earlier code.
Error: [full error message]
Analyze the cause and fix"

When the Direction Is Wrong

"The approach is wrong.
I want to go in the ~ direction.
Discard existing code and rebuild from a plan"

In-Session Tips

  • If too long, a new session (prevent context contamination)
  • Commit big results each time
  • Periodically confirm "summarize so far in 5 lines"

Persist with Skill / Rules

Put instructions you write every time in CLAUDE.md / .cursor/rules / .github/copilot-instructions.md. AI auto-follows them next time.

Next Step

To the deep-dive articles "Mobile App Development with Claude Code" and "Claude Code Agent Features."