liveBook · Manning

Chapter 9: Prompt engineering

What you will learn in chapter 9 of Coding with AI

The introduction explains why prompt engineering matters for developers using generative AI, while previewing the techniques covered in the chapter.

After this chapter you will be able to

Section by section

Introduction

The introduction explains why prompt engineering matters for developers using generative AI, while previewing the techniques covered in the chapter.

Key terms: prompt engineering, strong prompts, generic responses

9.1 Understanding prompt engineering

This section shows how adding context, specific instructions, and expected deliverables makes prompts to AI coding assistants more useful.

Key terms: prompt engineering, context, clear instructions, examples, expected deliverables

9.1.1 Why prompt engineering matters

This subsection explains why clear, specific prompts improve AI-assisted development.

Key terms: prompt engineering, requirements

9.2 Understanding the anatomy of a prompt

This section explains how context, clear instructions, and concrete examples work together to make prompts more useful.

Key terms: context, relevance, clarity, specificity, clear instructions, simple language, direct, manageable steps, examples, desired outcomes

9.3 Crafting the ultimate prompt

Shows how to use an LLM to analyze and improve a draft prompt with a reusable refinement prompt.

Key terms: prompt engineering, Sequential Reasoning, Contextual Awareness, Iterative Refinement, clarifying questions

9.3.1 Prompt engineering principles

This section presents practical prompt-engineering principles and demonstrates how to use an LLM to refine a programming prompt through clarifying questions.

Key terms: clear objectives, contextual clues, clear and concise instructions, examples, format specifications, prompt length, clarifying questions, optimized prompt, prompt tuning

9.4 Fundamental prompt types

Introduces the variety of prompt forms and the idea that the task determines which form to use.

Key terms: prompt, type of prompt, task at hand

9.4.1 Zero-shot prompting

This section explains how zero-shot prompts rely on an LLM’s existing knowledge and why their brevity makes them useful for quick tasks.

Key terms: zero-shot prompting, preexisting model knowledge, simplicity and efficiency, versatility, resource optimization, rapid prototyping

9.4.2 Few-shot prompting

This section explains how a few examples in a prompt guide an LLM toward the requested task, format, and coding style.

Key terms: few-shot prompting, examples, pattern recognition, style control

9.4.3 Open-ended prompts

This section explains when open-ended prompts are useful and why their creative range can make responses harder to control.

Key terms: open-ended prompt, creative and diverse results, open-ended prompts, constrained prompts, prescriptive results

9.4.4 Constrained prompts

Constrained prompts use explicit criteria to focus AI responses, with tradeoffs in creativity and reliability.

Key terms: constrained prompts, specific guidelines, stochastic, debugging, mock data generation

9.4.5 Using iterative prompts

This section demonstrates how revising a prompt through conversation can improve an LLM’s answer, while showing why the result still needs checking.

Key terms: iterative process, explicit instructions, multiple methods, show the work, context cues, stochastic nature of LLMs, computation

9.4.6 Structured prompts

This section explains how structured prompts specify an output format and when to choose them instead of open-ended or constrained prompts.

Key terms: structured prompts, predefined format, constrained prompts, structure, flexibility, open-ended prompts

9.5 Advanced prompt types

This section introduces five advanced prompting techniques that later subsections will explain.

9.5.1 Chain-of-thought prompting

This section explains how to work through a problem with an LLM in smaller steps, checking and refining the results along the way.

Key terms: chain-of-thought prompting, sequential reasoning, contextual awareness, iterative refinement, problem decomposition, prompt design, subtasks, iterative evaluation, feedback integration

9.5.2 Recursive prompting

This section explains how recursive prompting improves an answer through repeated evaluation and targeted refinements, using a factorial function as an example.

Key terms: recursive prompting, refinement, recursive input, iterative process, factorial function, edge cases, non-integer inputs, negative numbers, iterative approach

9.5.3 Context manipulation

This section explains how supplying context in a prompt can steer AI-generated code and documentation toward a project's requirements.

Key terms: context manipulation, priming, system messages, pandas, process_data, initial setup, consistency, efficiency, code readability

9.5.4 Instruction refinement

Instruction refinement makes AI-generated code more predictable by turning vague requests into detailed, unambiguous instructions.

Key terms: instruction refinement, objective, constraints and preferences, relevant context, expected output, ambiguity, maintainability, creative endeavors

9.5.5 Output control

This section explains how to control an AI response by specifying its format, style, and content constraints in the prompt.

Key terms: output control, output structure, filters, style rules, process_json_data, PEP 8, snake_case, extra imports

9.5.6 Wrap up

This wrap-up summarizes five advanced prompting methods and how they can help developers get better AI results.

Key terms: chain of thought, recursive prompting, context manipulation, instruction refinement, output control

9.6 Prompt techniques for programmers

Introduces software-development-focused prompt techniques as an application of earlier advice and previews examples to come.

9.6.1 Examples

This section gives examples of prompts that make AI coding requests more specific, contextual, and useful for different development tasks.

Key terms: architectural patterns, performance constraints, backward compatibility requirements, edge cases, code smells, profiling data, scenario-based testing prompts, numbered steps, chain-of-thought prompting, tradeoffs

9.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: Building effective tests with generative AI Chapter 10: Vibe coding with Cursor →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.