AI models have become powerful coding partners but how much value you get depends on how you prompt.
Writing better prompts isn’t just about phrasing; it’s about thinking in reusable patterns that map to how the model interprets instructions.
Here are six prompt patterns every developer should know, complete with examples, when to use them, and why they work.
1. The Role Pattern
Act as a senior Rust developer who writes production ready code with comments and tests
This pattern defines who the model should be. By assigning a role, you bias the model’s responses toward the knowledge, tone, and priorities of that persona.
Use it when:
You want the model to emulate a specific expertise or mindset e.g, code reviewer, DevOps engineer, or mentor.
Example:
You are a TypeScript expert helping me refactor React code for performance.
Explain your reasoning as comments in the code.
Why it works:
The model’s pretraining includes text from a wide range of personas. Specifying the role narrows down its context and improves consistency and relevance.
2. The Step-by-Step Pattern
Let's solve these bugs step by step, analyse the issue and suggest options for possible solutions to start
This classic pattern guides the model to reason more methodically. It prevents “jump to answer” behaviour and improves accuracy for complex tasks.
Use it when:
You’re debugging logic, designing algorithms, or exploring architecture trade-offs.
Example:
We need to design a token bucket rate limiter in Go.
Explain the algorithm step by step before showing the code.
Why it works:
By requesting intermediate reasoning, you slow the model down and reduce logical leaps. This yields more structured, verifiable answers – especially for multi step or technical problems.
3. The Input–Output Pattern
Given X, produce Y
This pattern frames your request as a pure function. It minimizes noise and makes prompts easier to reuse programmatically (e.g, for API calls or automation).
Use it when:
You want predictable structure for code generation, data transformations, or documentation.
Example:
Input: SQL schema
Output: TypeORM entity definitions in TypeScript.
Why it works:
It mirrors the mental model of coding, mapping inputs to outputs, which helps the model focus on transforming rather than chatting.
4. The Constraint Pattern
Write a Python script under 30 lines that uses only built in libraries
Adding constraints helps you control scope, style, or complexity. It’s especially useful for getting concise, production friendly results.
Use it when:
You need short, clean code snippets, or want to enforce limits (dependencies, performance, or output size).
Example:
Write a Node.js function that downloads a file using HTTPS.
Constraint: No third party modules, under 25 lines.
Why it works:
Constraints act as a specification, pushing the model to optimise within boundaries instead of over delivering irrelevant details.
5. The Template Pattern
Use this structure: Title, Description, Example
You give the model a format or schema and it fills in the blanks. Great for repeatable outputs like documentation, blog drafts, or code comments.
Use it when:
You’re generating structured text or templated responses.
Example:
Generate 3 commit messages for this diff using this format:
- Title: One line summary
- Description: Why this change was made
Why it works:
Models are excellent at pattern completion. Giving them a skeleton (with sections, headings, or labels) yields consistent, formatted results.
6. The Critic Pattern
Review this code for security issues and logic issues, suggest improvements
Instead of asking the model to create, ask it to criticise. This leverages its analytical capabilities, often surfacing bugs, design flaws, or optimization opportunities.
Use it when:
You need a second pair of eyes – for code review, system design critique, or threat modelling.
Example:
Critique this Solidity smart contract for gas efficiency and potential reentrancy risks.
Why it works:
Critic prompts activate different reasoning pathways – shifting from generative to evaluative mode – and can produce highly actionable insights.