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.


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 brief you'd give to a skilled contractor: the more precise and complete the brief, the better the result.

The difference between a good prompt and a bad one often determines whether you receive perfectly working code — or something completely unusable.

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.

When you provide a vague prompt, the AI has to guess your intent from a vast space of possible meanings. When you provide a specific, structured prompt, you dramatically narrow that space — guiding the model toward exactly the output you need.

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

Senior 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

Every effective prompt contains some combination of these five elements. You don't always need all five, but the more you include, the more predictable and useful the output becomes.

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 eliminates ambiguity. The AI knows its role, the goal, exact technical requirements, project context, and how to format its response. The result is typically usable code on the first attempt.


Iterative Prompting

Here's a truth that surprises many beginners: AI programming is a conversation, not a one-shot question. Even expert prompt engineers rarely get the perfect result on the first try. The real skill is in the iteration loop.

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? Because AI performs best when focused on one clear task at a time. 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 of the most powerful — and most overlooked — prompt techniques is simply sharing your existing code with AI. When AI can see what you've already built, it can produce output that integrates seamlessly rather than starting from scratch.

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: true, 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. Notice how the structured and chained approaches produce dramatically more useful results. This gap is what prompt engineering is all about.


Key Takeaways

Previous Chapter Introduction to AI-Assisted Development
Next Chapter AI as Pair Programmer