What Is a Prompt?
A prompt is the instruction you give to an AI. It can be a single sentence, a structured paragraph, or a multi-section document with context, constraints, and examples. Think of it as a work brief: include the information needed to understand and check the task, without adding irrelevant detail.
Prompt quality affects how relevant and usable the response is, but even a detailed prompt can produce incorrect code that needs review and testing.
AI models predict the most likely helpful response based on your instruction, the context you provide, and patterns from their training. The clearer your context, the better your results. Whatever the prompt leaves out, the model has to infer, and inferred details are a common source of wrong answers.
Why Prompts Work
Understanding why prompts produce different quality outputs helps you write better ones intuitively. AI language models generate text one token at a time (roughly, a word or word fragment), each conditioned on everything that came before it. Your prompt sets the trajectory.
A vague prompt leaves more intent for the model to infer. A specific, structured prompt narrows that ambiguity and gives you clearer criteria for reviewing the response.
Within a prompt, three things you control shape the result:
- Your instruction — what you explicitly ask for
- Context — the background information that shapes interpretation
- Examples — concrete demonstrations of what you want (few-shot prompting)
Bad Prompts vs. Good Prompts
The difference between a vague and a specific request is easiest to see side by side.
Beginner Prompt
Write React code
Problems:
- Far too vague — what kind of component?
- No structure or constraints specified
- No target outcome described
- AI must guess everything
Detailed Prompt
Create a React component in TypeScript.
Requirements:
- Functional component with hooks
- CSS Grid layout, responsive
- Accepts a list of activities as props
- Each activity shows name, time, person
- Clean, readable structure
Show complete code.
The second prompt gives AI clear direction on technology (React, TypeScript), pattern (functional components, hooks), layout (CSS Grid, responsive), data shape (activities with specific fields), and output format (complete code). The model still has to assume details such as the time format and what an empty list should show, so check those in the response.
Anatomy of a Great Prompt
Many useful prompts contain some combination of these five elements. You do not need all five every time; include the ones that reduce a real ambiguity or define how the response will be checked.
Let's break down what each element contributes:
- Goal — What you want to achieve. "Build a user registration form" is a goal. "Write code" is not.
- Context — Background information that shapes the solution. What project is this for? What exists already? What's the use case?
- Technology — The tools, languages, frameworks, and versions used by the project. For example, specify the project's React version and TypeScript rather than asking only for "JavaScript."
- Constraints — Boundaries and requirements. Performance needs, accessibility requirements, patterns to follow or avoid.
- Desired output — What the response should look like. Complete code? A plan first? Explanation with code? Just pseudocode?
The Developer Prompt Template
A structured template is a practical starting point when working with AI. It isn't rigid — adapt it to your needs. It makes omissions easier to spot, but it does not guarantee correct output.
ROLE:
You are a senior [technology] developer.
GOAL:
[Describe what should be built or solved]
REQUIREMENTS:
- [requirement 1]
- [requirement 2]
- [requirement 3]
CONTEXT:
[Any relevant background — existing code, architecture, constraints]
OUTPUT FORMAT:
[How you want the response structured — full code, explanation first, etc.]
Example using the template
ROLE:
You are a senior React/TypeScript developer.
GOAL:
Build a weekly schedule component that displays family activities.
REQUIREMENTS:
- Functional component with hooks
- CSS Grid for the weekly layout
- Responsive — stacks on mobile
- TypeScript interfaces for all props
- Each activity shows: name, time, person, color
CONTEXT:
This is part of a family planner app. The component receives
activities via props. No backend needed yet.
OUTPUT FORMAT:
Show complete, runnable code with brief comments explaining
key design decisions.
This prompt makes the goal, technical requirements, project context, and output format explicit. That makes omissions easier to spot, but the code still needs to be run, tested, and reviewed.
Iterative Prompting
Treat AI-assisted programming as an evidence-driven iteration rather than a one-shot question. Evaluate the first result against explicit criteria, then revise the prompt or implementation only where a gap remains.
Each iteration refines the output. You might start with a working component, then ask AI to add error handling, then improve the types, then refactor for readability. For many tasks, a few focused iterations are easier to review than trying to specify everything upfront in one massive prompt.
Chain Prompting
Instead of writing one enormous prompt that tries to cover everything, developers break complex tasks into a sequence of focused prompts. This approach is called chain prompting, and it keeps each step small enough to review.
Why? Smaller, reviewable steps reduce the number of assumptions made at once. Chain prompting works like this:
This mirrors how senior developers actually work: they don't write an entire application in one sitting. They plan, build incrementally, and review continuously. Chain prompting applies that same discipline to AI collaboration.
Giving AI the Right Context
One useful prompt technique is sharing the relevant existing code. This can help the model match your types and conventions, but it does not guarantee seamless integration. Before sharing, remove secrets, credentials, personal data, private logs, and proprietary material you are not permitted to send.
Here is my existing code:
[paste your current component/function/module]
Problem:
The component re-renders unnecessarily when the parent updates.
Goal:
Optimize rendering without changing the component's API.
Constraints:
- Don't use external libraries
- Maintain current TypeScript types
This pattern — existing code + specific problem + clear goal + constraints — works well for debugging, refactoring, and incremental development. It gives the model the evidence it needs for a targeted response that you can check against the stated goal.
Pro Tip: Context Window Awareness
AI models have a limited context window (the amount of text they can process at once). When sharing code, include only the relevant portions. Don't paste your entire codebase — paste the specific file or function that's relevant, plus any interfaces or types the AI needs to understand the code's contract.
Common Mistakes
- Missing context — A one-line prompt may force the model to make important assumptions. Add the specific constraints, examples, or project details needed to evaluate the response.
- Cramming too many requirements at once — If your prompt has 15 requirements, AI may lose track. Split complex tasks into chains.
- No iteration — Treating AI as a one-shot oracle instead of a conversation partner. The first response is a draft, not a final product.
- No clear goal description — "Make it better" is not actionable. "Improve readability by extracting the validation logic into a separate function" is.
- Forgetting to specify output format — If you want TypeScript, say so. If you want comments, say so. If you want explanation before code, say so.
Advanced: Few-Shot Prompting
A useful advanced technique is showing AI examples of what you want before asking it to produce output. This is called few-shot prompting, and it is a practical way to establish patterns, coding styles, and output formats.
I want you to write API endpoint handlers following this pattern:
Example:
export const getActivities = async (req: Request, res: Response) => {
try {
const activities = await db.activity.findMany();
res.json({ success: true, data: activities });
} catch (error) {
res.status(500).json({ success: false, error: 'Failed to fetch activities' });
}
};
Now write handlers for:
- createActivity (POST)
- updateActivity (PUT)
- deleteActivity (DELETE)
Follow the exact same pattern, error handling, and response format.
Showing the pattern first makes consistent output more likely. This is especially valuable when building APIs, component libraries, or any codebase where consistency matters. The model also copies the example's weaknesses: this handler has no input validation and no 404 response for a missing record, so state what the create, update, and delete handlers must add, and review the result.
Advanced: Negative Prompting
Sometimes it's just as important to tell AI what you don't want as what you do. Negative constraints prevent common AI tendencies that might not match your needs.
Build a form component in React.
DO NOT:
- Use any external form libraries (no Formik, react-hook-form)
- Use class components
- Add inline styles
- Include unnecessary comments
DO:
- Use native form validation
- Use CSS modules for styling
- Keep it under 80 lines
This technique is particularly useful when AI keeps defaulting to patterns you don't want, such as adding excessive dependencies or over-engineering simple solutions.
Take a problem you've recently worked on — or pick something simple like "build a to-do list component." Write three versions of the prompt:
- Version 1: A one-line beginner prompt
- Version 2: A structured prompt using the template (role, goal, requirements, context, output format)
- Version 3: A chain — first ask for a plan, then implement step by step
Compare the three outputs against the same acceptance criteria. Note which approach makes fewer unsupported assumptions and produces the easiest result to verify.
Key Takeaways
- Clear goals and relevant context usually improve AI output, but do not guarantee correctness
- Every good prompt includes some combination of: goal, context, technology, constraints, and desired output
- AI programming is iterative — expect to refine through conversation, not get perfection in one shot
- Chain prompting (plan → review → implement step by step) keeps complex tasks reviewable
- Share relevant existing code as context so the output is more likely to fit your project
- Use few-shot examples to establish patterns, and negative constraints to prevent unwanted defaults
Related Guides
AI Prompt Library
Apply prompt structure with reusable templates for real coding tasks.
TypeScript and React Prompts
See structured prompts applied to a specific frontend stack.