Chapter 2

Prompt Engineering for Developers

The most important skill in AI-assisted programming isn't syntax, frameworks, or algorithms. It's how you communicate with AI. This skill is called prompt engineering — and it's what separates developers who struggle with AI from those who fly.

Last reviewed: Aug 28 2026


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.

Core Principle

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. This isn't a vague guideline — it's the fundamental mechanism of how these systems work.


Why Prompts Work

Understanding why prompts produce different quality outputs helps you write better ones intuitively. AI language models work by predicting the next most likely token (roughly, a word or word-fragment) given 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.

Three factors determine output quality:


Bad Prompts vs. Good Prompts

The gap between amateur and professional prompting is enormous. Let's look at a real comparison.

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 AI doesn't have to guess — it can execute.


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.

Goal Context Technology Constraints Desired Output

Let's break down what each element contributes:


The Developer Prompt Template

Senior developers often use a structured template when working with AI. This isn't rigid — adapt it to your needs — but it provides a reliable foundation that consistently produces quality 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.

1
Ask for a first version — Get something working, even if imperfect.
2
Test it — Run the code. Does it work? What's wrong?
3
Ask for improvements — Be specific about what needs to change.
4
Refactor — Once it works, ask AI to clean up the structure.

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. Three or four iterations typically produces better code 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 consistently produces better results than monolithic requests.

Why? Smaller, reviewable steps reduce the number of assumptions made at once. Chain prompting works like this:

1
Ask AI to plan — "Design the component architecture before writing any code."
2
Review the plan — Does the approach make sense? Ask for changes if needed.
3
Generate code step by step — One component, one function, one module at a time.

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 — is extremely effective for debugging, refactoring, and incremental development. It gives AI everything it needs to provide a targeted, useful response.

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

Prompt Anti-Patterns

Advanced: Few-Shot Prompting

One of the most effective advanced techniques is showing AI examples of what you want before asking it to produce output. This is called few-shot prompting, and it's remarkably effective for establishing 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.

By showing the pattern first, you ensure consistency across all generated code. This is especially valuable when building APIs, component libraries, or any codebase where consistency matters.


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.


🧪 Practical Exercise

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:

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

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.

Previous Chapter Introduction to Programming with AI
Next Chapter AI as Pair Programmer