Chapter 9: Prompt engineering
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
- Value of effective prompts. Explain why developers benefit from improving their prompts when using generative AI tools.
- Effective coding prompt elements. Explain why a detailed coding prompt is more useful than a vague request, and describe how to include context, clear instructions, expected deliverables, and examples when helpful.
- Value of clear prompts. Explain why prompt engineering matters for AI-assisted coding: clear, specific prompts work like good requirements and can improve the output while reducing back-and-forth and debugging.
- Relevant task context. Explain why a prompt needs relevant background and describe how the MySQL example supplies it through the Python version, operating system, remote database, and intended operations.
- Clear actionable instructions. Distinguish instructions to an LLM from a search query and describe how to make instructions direct, simple, and manageable, including specifying what the response should contain.
- Concrete output examples. Explain how including an example of the desired result can guide an LLM's output, using the section's sample CSV format as an illustration.
- LLM-assisted prompt refinement. Describe how to give an LLM a draft prompt to improve: ask it to identify logical steps and missing context, ask essential clarifying questions, and refine the wording before producing an optimized prompt.
- Effective prompt design principles. Explain how clear objectives, relevant context, unambiguous instructions, examples, and output specifications can guide an LLM’s response. Describe why a prompt should provide enough structure and information without overloading the model.
- LLM assisted prompt refinement. Describe how the section uses an LLM to improve an initial programming prompt: ask targeted clarifying questions, supply the missing context, and turn the answers into a more precise prompt. Explain that the result may be useful but is not guaranteed and can be tuned further.
- Task-dependent prompt forms. Explain that a prompt can be a simple question, a statement, or a complex instruction, and that its form depends on the task.
- Zero-shot prompt characteristics. Explain that a zero-shot prompt gives an LLM a task with very little context or background data, relying on knowledge from its training rather than detailed guidance in the prompt.
- Few-shot example guidance. Explain how few-shot prompting uses a few examples to guide an LLM’s response. Describe how the Python function examples signal the desired task, formatting, and coding style.
- Open-ended prompt uses. Explain that open-ended prompts invite a wide range of responses and are useful for generating ideas, creative content, or advice, including exploratory programming questions.
- Creative range and control. Distinguish the benefits of open-ended prompts from their downsides, and explain when a constrained prompt may be preferable.
- Constrained prompt criteria. Explain how a constrained prompt limits a model’s response by specifying criteria, such as an exact number of items or a code-length limit, without guaranteeing compliance or accuracy.
- Iterative prompt refinement. Describe how the example moves from a short multiplication prompt to more specific instructions after each unsatisfactory answer. Explain that iterative prompting means using the response to decide what to change in the next prompt.
- Structured prompt format. Explain how a structured prompt guides a model by specifying the sections or format of its response, as in an outline for Python documentation.
- Stepwise problem solving. Explain how chain-of-thought prompting breaks a complex task into logical steps while keeping the task’s context in view, rather than relying on a single input-to-output exchange.
- Decomposition and focused prompts. Describe how to start by decomposing a task, then ask focused questions for its parts; explain that you can ask the model to suggest the steps when you do not know what to ask.
- Evaluation and feedback integration. Describe how to review each response, ask for improvements where it falls short, and feed that feedback into a revised response or code.
- Recursive prompting cycle. Explain how an initial response is evaluated, used to refine the next prompt, and improved through repeated stages until the desired quality is reached.
- Context manipulation in prompts. Explain how context manipulation steers a model's responses by supplying relevant information, examples, or instructions within a prompt.
- Python prompt setup example. Describe how the section's Python example establishes a pandas-only rule and a required function structure before requesting a specific CSV-processing task.
- Refined prompt components. Describe how to refine a coding request by stating its objective, breaking complex requirements into actionable steps, and specifying constraints, context, and expected output. Apply those elements to the section’s JSON-to-database example.
- Output control guidelines. Explain how specifying output rules in a prompt shapes an AI response, and describe when those rules are useful.
- Stepwise prompt methods. Distinguish chain-of-thought prompting, which breaks a large problem into smaller steps, from recursive prompting, which improves an answer over successive steps.
- Context directions and presentation. Describe how context manipulation, instruction refinement, and output control target different parts of an AI interaction.
- Explicit implementation requirements. Explain how to state technical requirements and constraints up front when asking an AI assistant to generate code.
- Task context and scenarios. Describe how prompts can supply existing code, observed problems, business rules, edge cases, and scenarios relevant to a development task.
Section by section
Introduction
The introduction explains why prompt engineering matters for developers using generative AI, while previewing the techniques covered in the chapter.
- Value of effective prompts: Explain why developers benefit from improving their prompts when using generative AI tools.
Why it matters: Better prompts can make AI assistance more useful without giving up control of the result.
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.
- Effective coding prompt elements: Explain why a detailed coding prompt is more useful than a vague request, and describe how to include context, clear instructions, expected deliverables, and examples when helpful.
Why it matters: A well-specified prompt gives a coding assistant enough direction to produce a response you can use.
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.
- Value of clear prompts: Explain why prompt engineering matters for AI-assisted coding: clear, specific prompts work like good requirements and can improve the output while reducing back-and-forth and debugging.
Why it matters: Clearer prompts can help you spend less time correcting AI-generated work.
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.
- Relevant task context: Explain why a prompt needs relevant background and describe how the MySQL example supplies it through the Python version, operating system, remote database, and intended operations.
Why it matters: Relevant background helps the assistant tailor its answer to the task you actually have. - Clear actionable instructions: Distinguish instructions to an LLM from a search query and describe how to make instructions direct, simple, and manageable, including specifying what the response should contain.
Why it matters: Clear instructions tell the assistant what to produce instead of leaving it to guess. - Concrete output examples: Explain how including an example of the desired result can guide an LLM's output, using the section's sample CSV format as an illustration.
Why it matters: Showing the shape of a good answer can prevent misunderstandings that instructions alone leave open.
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.
- LLM-assisted prompt refinement: Describe how to give an LLM a draft prompt to improve: ask it to identify logical steps and missing context, ask essential clarifying questions, and refine the wording before producing an optimized prompt.
Why it matters: You can use the model to improve a prompt before relying on it for the task itself.
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.
- Effective prompt design principles: Explain how clear objectives, relevant context, unambiguous instructions, examples, and output specifications can guide an LLM’s response. Describe why a prompt should provide enough structure and information without overloading the model.
Why it matters: These principles give you a way to make a prompt clearer without adding detail that does not help the task. - LLM assisted prompt refinement: Describe how the section uses an LLM to improve an initial programming prompt: ask targeted clarifying questions, supply the missing context, and turn the answers into a more precise prompt. Explain that the result may be useful but is not guaranteed and can be tuned further.
Why it matters: Clarifying missing requirements before rewriting a prompt can produce instructions better matched to your coding task.
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.
- Task-dependent prompt forms: Explain that a prompt can be a simple question, a statement, or a complex instruction, and that its form depends on the task.
Why it matters: This gives you a starting point for choosing how to phrase a request to an AI tool.
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.
- Zero-shot prompt characteristics: Explain that a zero-shot prompt gives an LLM a task with very little context or background data, relying on knowledge from its training rather than detailed guidance in the prompt.
Why it matters: Recognizing what the prompt leaves to the model helps you decide when a short request is enough. - Practical value of zero-shot prompts: Describe why zero-shot prompts can be useful for straightforward tasks or rapid prototyping, using the section’s concise Python requests as examples.
Why it matters: A concise prompt can be a useful first try when you want a quick result without preparing examples.
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.
- Few-shot example guidance: Explain how few-shot prompting uses a few examples to guide an LLM’s response. Describe how the Python function examples signal the desired task, formatting, and coding style.
Why it matters: Examples can communicate the shape of the answer you want and reduce back-and-forth.
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.
- Open-ended prompt uses: Explain that open-ended prompts invite a wide range of responses and are useful for generating ideas, creative content, or advice, including exploratory programming questions.
Why it matters: This helps you choose a prompt when you want to explore possibilities rather than dictate an answer. - Creative range and control: Distinguish the benefits of open-ended prompts from their downsides, and explain when a constrained prompt may be preferable.
Why it matters: This tradeoff helps you decide whether to invite exploration or ask for a more prescribed result.
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.
- Constrained prompt criteria: Explain how a constrained prompt limits a model’s response by specifying criteria, such as an exact number of items or a code-length limit, without guaranteeing compliance or accuracy.
Why it matters: Precise criteria can make an AI response more useful, but you still need to check what it produces. - Uses and tradeoffs of constraints: Describe when constrained prompts are useful and what they may sacrifice compared with open-ended prompts.
Why it matters: Choosing constraints makes sense when a focused answer matters more than exploring new ideas.
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.
- Iterative prompt refinement: Describe how the example moves from a short multiplication prompt to more specific instructions after each unsatisfactory answer. Explain that iterative prompting means using the response to decide what to change in the next prompt.
Why it matters: When an answer misses the mark, you can give the model more useful direction rather than simply repeating the same request. - Limits of prompted arithmetic: Explain why the example’s correct final answer does not make the prompt a reliable way to calculate. State that LLMs are weak at computation and that the same prompt can produce bad answers on other runs.
Why it matters: A better prompt can improve an answer without turning an LLM into a dependable calculator.
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.
- Structured prompt format: Explain how a structured prompt guides a model by specifying the sections or format of its response, as in an outline for Python documentation.
Why it matters: A specified outline can make generated documentation easier to organize and use. - Structured prompt trade-offs: Describe the benefits and costs of using structured prompts for software documentation and other complex, organized outputs.
Why it matters: The extra effort is most worthwhile when a consistent, detailed output is useful. - Prompt types by use case: Distinguish the section’s example uses for open-ended, constrained, and structured prompts.
Why it matters: Matching the prompt type to the task helps you ask for the kind of output you need.
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.
- Stepwise problem solving: Explain how chain-of-thought prompting breaks a complex task into logical steps while keeping the task’s context in view, rather than relying on a single input-to-output exchange.
Why it matters: Breaking a task into steps gives you opportunities to catch mistakes before they carry through to the final answer. - Decomposition and focused prompts: Describe how to start by decomposing a task, then ask focused questions for its parts; explain that you can ask the model to suggest the steps when you do not know what to ask.
Why it matters: You do not have to know every diagnostic question in advance to begin working through a problem. - Evaluation and feedback integration: Describe how to review each response, ask for improvements where it falls short, and feed that feedback into a revised response or code.
Why it matters: Reviewing and revising each step helps reveal errors and improve the final result.
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.
- Recursive prompting cycle: Explain how an initial response is evaluated, used to refine the next prompt, and improved through repeated stages until the desired quality is reached.
Why it matters: It gives you a way to improve a complex result without trying to specify the perfect answer in one prompt. - Targeted factorial refinements: Trace how the factorial example moves from an initial function request to prompts for edge-case handling and, optionally, an iterative implementation. Explain why each refinement targets a particular improvement.
Why it matters: Specific follow-up requests make each round of improvement easier to direct and evaluate.
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.
- Context manipulation in prompts: Explain how context manipulation steers a model's responses by supplying relevant information, examples, or instructions within a prompt.
Why it matters: Clear context helps the model interpret what your project needs rather than guessing from an isolated request. - Python prompt setup example: Describe how the section's Python example establishes a pandas-only rule and a required function structure before requesting a specific CSV-processing task.
Why it matters: Stating project constraints up front makes generated code easier to incorporate into a larger project. - Benefits of clear context: Explain why supplying context is useful when generating code or documentation for an existing project.
Why it matters: Outputs that fit existing conventions take less work to review and integrate.
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.
- Refined prompt components: Describe how to refine a coding request by stating its objective, breaking complex requirements into actionable steps, and specifying constraints, context, and expected output. Apply those elements to the section’s JSON-to-database example.
Why it matters: A detailed prompt gives the model requirements it cannot infer from a vague request. - Refinement benefits and fit: Explain why detailed instructions tend to produce more predictable, useful code and identify the kinds of tasks for which this technique is useful, as well as its limitation for creative work.
Why it matters: Knowing the tradeoff helps you use refinement when consistency matters more than creative latitude.
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.
- Output control guidelines: Explain how specifying output rules in a prompt shapes an AI response, and describe when those rules are useful.
Why it matters: Clear output requirements can reduce the work of reformatting or trimming a response. - Python output constraints: Describe how the section's Python example combines a functional request with explicit code-style and content constraints.
Why it matters: The example shows how to ask for code that meets project conventions as well as functional requirements.
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.
- Stepwise prompt methods: Distinguish chain-of-thought prompting, which breaks a large problem into smaller steps, from recursive prompting, which improves an answer over successive steps.
Why it matters: These are two different ways to use steps to get better AI results. - Context directions and presentation: Describe how context manipulation, instruction refinement, and output control target different parts of an AI interaction.
Why it matters: Separating background, directions, and presentation helps you choose what to change in a prompt.
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.
- Explicit implementation requirements: Explain how to state technical requirements and constraints up front when asking an AI assistant to generate code.
Why it matters: Stating requirements gives the assistant concrete criteria to work toward instead of leaving important choices implicit. - Task context and scenarios: Describe how prompts can supply existing code, observed problems, business rules, edge cases, and scenarios relevant to a development task.
Why it matters: Relevant context helps focus generated tests, refactoring, and optimization on the situations that matter. - Staged design and tradeoffs: Explain how to ask for steps, design alternatives, or tradeoffs before requesting a complete implementation.
Why it matters: Staging a request creates an opportunity to examine the proposed approach before the assistant commits to code. - Purpose-shaped responses: Describe how to specify the response structure, intended audience, or evaluation checklist in a prompt.
Why it matters: A specified format, checklist, or audience makes a response easier to use for its intended purpose.
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
- The initial prompt to build our game (chapter 10) · Crafting a prompt that communicates the intended result.
- First concept (chapter 10) · The value of specificity in prompt engineering.
- Making changes to our game (chapter 10) · Prompt engineering principles for giving an AI tool specific instructions.
- Adding context (chapter 10) · Adding context to a coding request draws on this practice of supplying task-specific information.
Concepts explained in this chapter
- prompt engineeringPrompt engineering is the systematic design and refinement of instructions and context provided to an AI system to elicit desired outputs.
- promptA prompt is natural-language input that instructs a generative artificial-intelligence system what response or result to produce.
- continuous improvementIterative evaluation is the repeated assessment of generated results followed by changes intended to improve their quality or accuracy.
- chain-of-thought promptingChain-of-thought prompting is a prompting technique that guides an AI model through a problem using a sequence of intermediate reasoning steps.
- contextContext is the surrounding conversation history, instructions, assumptions, and other information that determines the meaning and intent of an input.
- context-awarenessContextual awareness is the ability to preserve and apply surrounding task information when interpreting inputs and producing responses.
- sequential thinkingA reasoning approach that decomposes a task into ordered logical steps and considers the actions or decisions required at each step.
- output controlOutput control is a prompting technique that constrains the format, structure, content, or other properties of an AI model's response.
- instructionA directive that specifies an action or response an artificial-intelligence model should produce.
- decompositionProblem decomposition is the division of a complex task into smaller, manageable subtasks.
- feedback integrationFeedback integration is the incorporation of quality or correctness feedback into a subsequent version of an output or implementation.
- requestsRequests is a Python library for sending HTTP requests and receiving responses from web services.
- clarifying questionA question asked to obtain missing information needed to specify a task, audience, tone, or constraint.
- anti-pattern promptingAnti-pattern prompting asks an AI system to identify harmful or ineffective implementation approaches and explain how to avoid them.
- attack-scenario promptingAttack-scenario prompting asks an AI system to design or evaluate an implementation against specified security attacks.
- comparative promptingComparative prompting asks an AI system to evaluate multiple approaches against specified criteria and explain their tradeoffs.
- component-first promptingComponent-first prompting asks an AI system to design and implement individual frontend components and their relationships before assembling the larger interface.
- maintenance-focused promptingMaintenance-focused prompting specifies long-term maintainability concerns such as clarity, observability, documentation, and team handoff.
- role-reversal promptA role-reversal prompt instructs an AI system to adopt a specified professional role while addressing the user's problem from that role's perspective.
- scenario-based promptingScenario-based prompting asks an AI system to account for explicitly described usage situations, conditions, or workflows.
- state-transition promptingState-transition prompting asks an AI system to model a workflow as states and explicitly defined transitions, including failure transitions.
- recursive promptingRecursive prompting is a prompting technique that repeatedly uses prior outputs or intermediate results to generate subsequent prompts or responses.
- output formatAn output format is the specified structural arrangement and representation that a generated response must follow.
- instruction refinementInstruction refinement is the systematic process of making AI instructions clearer, more specific, and less ambiguous to improve the usefulness and predictability of generated outputs.
- context manipulationContext manipulation is the deliberate arrangement of information, examples, instructions, and behavioral constraints in a prompt to influence an AI model's responses.
- parameterized queryA parameterized query is a database query whose values are supplied separately from the query structure to reduce injection risk and improve safe query execution.
- role-playingA prompting method that assigns an assumed role or identity to an AI system to shape its perspective and response behavior.
- context cueA prompting technique that supplies hints or surrounding information to steer a model toward an appropriate approach.
- iterative promptingA prompting technique that repeatedly revises inputs based on model outputs until the response better satisfies the intended goal.
- multiple methodsA problem-solving technique that requests or applies several independent approaches and compares their results.
- requirementsSoftware requirements are specified capabilities, constraints, and qualities that a software system must satisfy.
- language modelA large language model is a neural language model trained on extensive data to understand and generate language, including programming-language text.
- HTTP GETAn HTTP request that retrieves a resource or representation from a specified server endpoint.
- performanceThe degree to which software executes efficiently in terms of time and computational-resource use.
- JSONJSON is a text-based standard format for representing structured data with objects, arrays, and primitive values.
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.
