← All posts
·11 min read

Prompt Engineering for Code Generation: Patterns That Actually Work

Most developers write vague prompts and get vague code. Learn the 7 patterns that consistently produce production-quality output.

prompt engineeringbest practicestutorial

After analyzing thousands of prompt-commit pairs in Qmmit, clear patterns emerge. Developers who consistently get high-quality code from AI tools share specific prompting habits. Here are the 7 patterns that work.

1. Specify the Interface First

Instead of "build a user service," write "create a UserService class with methods: createUser(email, password) returning User, getUserById(id) returning User or null, updateUser(id, partial) returning User. Use TypeScript, throw typed errors." The more specific your interface contract, the better the output.

2. Provide Examples of Existing Patterns

AI models excel at pattern matching. If your codebase uses a specific error handling pattern, paste an example: "Follow this pattern for error handling: [paste existing code]. Now implement the same pattern for the payment module." This produces code that matches your codebase style perfectly.

3. Constrain the Solution Space

Unbounded prompts produce generic code. Add constraints: "Use only the dependencies already in package.json. Do not add new libraries. Use the existing database client from lib/db.ts. Follow the repository pattern we use in src/repos/." Constraints force the model to work within your architecture.

4. Decompose Into Steps

Large prompts produce large, often incorrect outputs. Break complex features into steps: first the types, then the database schema, then the repository layer, then the service, then the API route, then the tests. Each step builds on verified output from the previous one.

5. Include Failure Cases

Most prompts describe the happy path. Add: "Handle these error cases: invalid input (return 400), not found (return 404), duplicate email (return 409), database connection failure (retry 3 times then throw)." This produces robust code on the first try.

6. Reference File Paths

AI tools with file context (Cursor, Kiro) perform dramatically better when you reference specific files: "Look at src/auth/middleware.ts and implement the same pattern for rate limiting in src/api/middleware.ts." This gives the model concrete examples to follow.

7. State What You Do NOT Want

Negative constraints are powerful: "Do not use any deprecated APIs. Do not add console.log statements. Do not modify existing tests. Do not change the public interface of the module." This prevents common AI mistakes.

Measuring Prompt Quality

Qmmit AI Score includes a prompt efficiency factor: prompts-per-commit ratio. The optimal range is 3-5 prompts per commit. Fewer means you are under-utilizing AI. More means your prompts are too granular or you are iterating too much on bad output. Track this metric to improve over time.

Start tracking your AI prompts

One command. Zero workflow changes. Works with 7 AI tools.

curl -fsSL https://qmmit.dev/install.sh | bash