Page
1
M A N N I N G Richard Davies Rafael Fischer Design, test, and improve AI prompts
Page
2
Prompt Engineering in Practice Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
3
Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
4
MANN I NG Shelter ISland Richard Davies Rafael Fischer Prompt Engineering in Practice Design, test, and improve AI prompts Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
5
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com © 2027 Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid- free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. ∞ Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 ISBN 9781633436305 Printed in the United States of America The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Development editor: Ian Hough Technical editor: Maris Sekar Review editor: Angelina LazukiÊ Production editor: Andy Marinkovich Copy editor: Kari Lucke Proofreader: Katie Tennant Typesetter: Tamara ŠveliÊ SabljiÊ Cover designer: Marija Tudor Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
6
To my wife, Kseniya, for her constant support throughout the years of work, thought, and experience that ultimately became this book, and for being beside me through every part of the journey —Richard Davies To my parents and my partner, for all the support and companionship throughout this journey, and especially to my daughter, Laura, who gives it all meaning —Rafael Fischer Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
7
vi brief contents Part 1 Foundations ..................................................................1 1 ■ Prompt engineering: The blueprint 3 Part 2 Prompt Design ............................................................19 2 ■ Prompt design: Structural elements 21 3 ■ Prompt design: Linguistic elements 43 4 ■ Patterns 60 5 ■ Templates 83 6 ■ Prompt types 96 Part 3 Advanced Prompting ............................................... 121 7 ■ Contextual prompting 123 8 ■ Prompt sampling 135 9 ■ Advanced prompt patterns 148 Part 4 Practice .................................................................... 165 10 ■ Security 167 11 ■ Managing Prompts in Production 191 index 219 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
8
vii contents preface xiii acknowledgments xv about this book xvii about the authors xxi about the cover illustration xxii Part 1 Foundations ...................................................1 1 Prompt engineering: The blueprint 3 1.1 The prompt engineering mental model 4 1.2 From casual prompting to prompt engineering 6 1.3 Why prompt engineering matters for software engineers 9 1.4 Prompts as designed artifacts 10 1.5 The prompt engineering lifecycle 11 1.6 Three diagnostic lenses 13 Prompts as specifications 13 ■ Prompts must be self-contained 14 Inspecting the prompt first when output varies 14 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
9
viii contents Part 2 Prompt Design .............................................19 2 Prompt design: Structural elements 21 2.1 Overview of structural elements 22 2.2 Instructions 24 Practical example 1: Explaining HTTP 429 errors 24 Practical example 2: Generating code review feedback 24 2.3 Constraints 25 Practical example: Writing secure logging guidelines 26 2.4 Context 27 Practical example 1: Generating an architecture decision record summary 27 ■ Practical example 2: Generating an incident handoff conversation 28 2.5 Input parameters 29 Practical example 1: Analyzing error logs 29 Practical example 2: Generating release notes 31 2.6 Output format 33 Practical example 1: Generating a deployment checklist 33 Practical example 2: Generating an API endpoint summary 34 2.7 Delimiters 35 Practical example 1: Summarizing an incident report 35 Practical example 2: Answering questions from a runbook 36 Defensive prompting tip 37 2.8 Combining structural elements 38 Practical example 1: Generating incident status updates 38 Practical example 2: Generating a deployment GO/NO-GO brief 39 ■ Hands-on practice 41 3 Prompt design: Linguistic elements 43 3.1 Precision 46 Practical example 1: API migration notice 46 Practical example 2: Sequence diagram generation 47 3.2 Directness 50 Practical example 1: Backend API change review 51 Practical example 2: Flaky CI test strategy 52 3.3 Brevity 52 Practical example 1: Incident summary analysis 53 Practical example 2: Technical diagram generation 54 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
10
ixcontents 3.4 Additional linguistic considerations 57 Question vs. statement formatting 57 ■ Hands-on practice 58 4 Patterns 60 4.1 Understanding prompt patterns 61 4.2 Role assignment pattern 62 Practical example: Pull request description generation 62 4.3 Delimiter pattern 64 Practical example: Multisource bug analysis 65 4.4 Template pattern 67 Practical example: Incident postmortem summaries 67 4.5 Repetition pattern 68 When to use the repetition pattern 69 ■ Practical example: Selecting a logging approach 69 4.6 Self-reflection pattern 71 Practical example: Technical onboarding plan 71 4.7 Inversion pattern 73 Practical example: Architecture proposal review 73 4.8 Refinement pattern 75 Practical example: API documentation improvement 76 4.9 Comment-driven generation 77 Practical example: Technical Request for Comments review 79 Hands-on practice 81 5 Templates 83 5.1 Prompt templates as reusable frameworks 84 5.2 Practical example 1: Feature implementation plan generator 84 Prompt template components 84 ■ Prompt template 87 Prompt with input parameters injected 88 5.3 Practical example 2: Legacy module refactor plan generator 90 Prompt template components 90 ■ Prompt template 91 Prompt with input parameters injected 91 Hands-on practice 94 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
11
x contents 6 Prompt types 96 6.1 Chat model architecture 97 Three message types 97 ■ What you design: System and user prompts 99 6.2 System prompts 101 Purpose and principles 101 ■ Quick reference: Mapping chapter 2 elements to chat prompts 101 ■ Practical example: Linear issue assistant 103 6.3 User prompts 105 Types and templates 105 ■ Practical example: Harvest daily report assistant 106 6.4 Prompt decomposition framework 110 The core question 110 ■ Worked example: Decomposing step by step 110 ■ Quick decisions 114 ■ When to use architectural separation 114 ■ Common mistakes 116 ■ Hands-on practice 118 Part 3 Advanced Prompting ................................ 121 7 Contextual prompting 123 7.1 In-context learning 124 7.2 Zero-shot prompting 124 7.3 Few-shot prompting 126 Practical example 1: CI pipeline failure handoff generation 127 Format consistency 129 ■ Example selection 131 ■ Advanced variations 132 ■ Hands-on practice 134 8 Prompt sampling 135 8.1 Sampling fundamentals 136 Temperature and diversity 137 ■ Sample size 137 Sampling in practice 137 8.2 Mean 137 Practical example: Runbook quality scoring 138 8.3 Mode 139 Practical example: Alert severity routing 139 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
12
xicontents 8.4 Self-consistency 140 Practical example: Incident root-cause draft 141 Checking self-consistency 143 8.5 Best-of-N 144 Practical example: Deployment status update for executives 144 8.6 When to use sampling 145 Hands-on practice 146 9 Advanced prompt patterns 148 9.1 Chain of thought 149 Practical example: Deployment risk reasoning 151 9.2 Meta prompting 154 The meta prompt template 155 ■ Practical example: Generating a pull request risk classification template 155 9.3 ReAct 158 Practical example: Data pipeline staleness investigation 160 Hands-on practice 162 Part 4 Practice ..................................................... 165 10 Security 167 10.1 Why prompts are attackable 168 Practical example 1: The unguarded prompt 169 10.2 Prompt injection 171 Practical example 1: The support chatbot 172 Practical example 2: The injected document 173 10.3 Jailbreaking and instruction conflict 175 Practical example 1: The code review assistant 176 Practical example 2: The policy chatbot 178 10.4 Prompt-hardening techniques 180 Practical example 1: Hardening a deployment notification bot 182 Practical example 2: Hardening a ticket triage prompt 184 10.5 The limits of prompt-only security 186 Practical example 1: When the prompt fails 186 Hands-on practice 188 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
13
xii contents 11 Managing Prompts in Production 191 11.1 Why prompt management matters 192 The six pillars of prompt management 193 ■ Build vs. buy 194 Practical example 1: The support bot audit 194 11.2 The maturity journey 195 Stage 1: Inline prompts 195 ■ Practical example: The weekend hack that shipped 196 ■ Stage 2: Config files and version control 196 ■ Practical example: The product manager problem 197 ■ Stage 3: Prompt store 197 ■ Practical example: The stale prompt discovery 198 ■ Stage 4: Dedicated prompt management system 198 ■ Practical example: The promotion without an approval record 199 11.3 The prompt lifecycle 200 Draft 200 ■ Practical example: The underspecified draft 202 Test 202 ■ Practical example: The invisible regression 203 Approve 204 ■ Deploy 204 ■ Observe 205 Improve 205 ■ Retire 206 11.4 Governance and collaboration 206 Role-based access control patterns 206 ■ Approval flow patterns 207 ■ Practical example: The tone drift incident 207 Audit trail and rollback 208 ■ Inline collaboration 208 11.5 Using management signals to guide improvement 209 The management signal inventory 209 ■ The improvement loop 211 ■ Practical example 1: The quarterly prompt review 211 ■ Practical example 2: The test set that stopped growing 212 11.6 Common pitfalls and anti-patterns 213 Prompt sprawl 213 ■ Blind updates 213 ■ Metadata black hole 214 ■ Ship equals redeploy 214 ■ Git hotfix rollback 214 Shadow edits 215 ■ Manual playground testing 215 Observability as an afterthought 215 ■ Overoptimizing early 215 ■ One prompt to rule them all 216 ■ Hands-on practice 216 11.7 What you can do now 217 index 219 Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
14
xiii preface This book began as a goal. Richard was mapping out the year ahead, and his main aim was to write a book. The groundwork was already there. For years, he had been building AI systems: med- ical AI at Intelligent Ultrasound and Tendertec, trademark search at the Intellectual Property Office, and machine translation at Guildhawk. Alongside this, he had built a library of roughly 600 books on AI, most read over the past eight years. He was work- ing in generative AI while it was still cutting-edge research, well before ChatGPT and Claude Code carried it into the daily work of software engineers. His mentor, Felix Hovsepian, knew this. Two weeks later, while Richard was away on holiday, Felix introduced him to Andy, an acquisitions editor at Manning. That intro- duction became an unexpected invitation: would he write a book on prompt engineer- ing and modern AI? He said yes. The invitation made sense for the same reason you are holding this book. In their own work, the prompt was usually the least engineered thing in the codebase: a string literal, tuned until a demo passed, copied between services, and owned by no one. They wanted to talk about a prompt the way we talk about a function or a class: to point at what is failing and fix it deliberately, rather than by trial and error. That needed a vocabulary, and prompt engineering did not yet have one that satis- fied them. So, they assembled what they had read into a single system: the structural elements that every prompt is composed of, and a library of prompt patterns, the prompting equivalent of software design patterns. Rafael soon joined as a full coauthor. He and Richard had built AI systems side by side for years at Guildhawk and Vera, and he had long wanted to write about AI. A Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
15
xiv preface generative AI engineer with a PhD and years of shipping AI-native products across the US, Europe, and Brazil, he took ownership of whole chapters and helped shape the book. Even as they wrote, the field kept moving: from prompting language models directly to prompting agents with tools and function calling, skills, memories, and harnesses. They wrote this book for software engineers in that shift: those pairing with agentic coding tools like Claude Code and Codex, and those building products on language models and agents. Whatever you are prompting, the design discipline is the same, and the rest is built on it. They kept the book model agnostic and no-code, so what you learn outlasts the next model release. Their hope is that, by the end, you see prompts for what they really are: artifacts you design on purpose and with intent. Thank you for reading. Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
16
xv acknowledgments A book takes far more people than the two names on its cover, and we are grateful to all of them. At Manning, we thank Andy Waldron, the acquisitions editor who commissioned the book, and the team who carried it through production: our development editor, Ian Hough; head of production, Aleksandar Dragosavljevic; production editor, Andy Marinkovich; copyeditor, Kari Lucke; proofreader, Katie Tennant, typesetter, Tamara Švelić Sabljić, and graphics supervisor, Azra Dedic. We are also grateful to Maris Sekar, our technical editor, for the careful technical review and whose feedback helped make the book sharper and more useful. Maris is a seasoned engineer and principal data scientist with expertise in AI, having led innovative initiatives for top organizations like Equite Association, PwC, Shell Canada, and TC Energy. His passion for data science has earned him prestigious awards and led to significant contributions through published papers, articles, and a recent book on applied machine learning. Thanks also go to all the reviewers who read the manuscript at various stages and provided such valuable feedback: Albert Lardizabal, Aleksandr Alekseitsev, Alex Kro- tov, Alexandru Dragan, Arik Leonidov, Bill Mitchell, Bruce Park, Cosimo Attanasi, Dary Merckens, Deniz Vehbi, Dewang Sultania, Eduardo Chiarotti, Francis Setash, Guill- ermo Alcantara, Jason Richter, Jay Dave, Jay Kim, Jeremie Clos, Lara Thompson, Louis Luangkesorn, Mark Lattimore, Mauricio Coria, Miguel Isidoro, Roman Kozak, Saioa Picado, Sleiman Salameh, Sriram Macharla, Steven Edwards, Sumit Bhattacharyya, Tatiana Bayr, Tom Wright, Ulina Small, Uros Cirkovic, and Ursin Stauss—your sugges- tions helped make this a better book. Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
17
xvi acknowledgments Richard Davies: I thank my wife, Kseniya Davies, for her unwavering support, and my brothers, David Davies and Thomas Davies, for always being there for me. I am especially grateful to Felix Hovsepian, my mentor in both life and work, for the guidance and support he has given me over the years. I am particularly thankful for Felix’s contributions throughout the writing of this book, including the many long phone calls, discussions, and exchanges of ideas that helped shape it from beginning to end. Thanks also go to my coauthor, Rafael, for his collaboration, friendship, and support throughout the writing of this book. I greatly enjoyed working with Rafael over the years, including our time together at AI-XON, Vera, and Guildhawk, and particularly the experience of writing this book alongside him. Rafael has been an excellent coau- thor; I wish him every success in the future and look forward to what lies ahead for us both. Finally, I want to acknowledge the companies and organizations that have formed part of my professional journey, including Meridias, Beyla, Vera, AI-XON, Guildhawk, IPO, Davies Group, Tendertec, and Intelligent Ultrasound, as well as the many talented people I’ve had the opportunity to work alongside over the years. Rafael Fischer: I would like to thank Richard Davies, a brilliant coauthor and a true partner in this project, whose care pushed the work forward and made the long hours feel worthwhile. I am also grateful to the many teachers and colleagues who shaped my professional development over the years and provided the theoretical grounding to take on something like this book. I thank my parents, who always believed in education as a path to personal and professional growth, and supported me all the way through my PhD. Last but not least, I thank my wife for her endless patience and encourage- ment, and my daughter, Laura, for being the light that makes everything else make sense. Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
18
xvii about this book Prompt Engineering in Practice treats prompt engineering as a design discipline, rather than as a bag of tricks that may or may not still work next quarter. Prompts have become the interface between your code and language models such as ChatGPT, Claude, and Copilot. Yet most prompts are still written by feel: tweaked word by word until a run looks good enough, then quietly breaking in production when nobody changed anything obvious. The goal of this book is to give you a systematic method for designing prompts that meet a specification, fail in diagnosable ways, and improve safely over time. To do that, the book builds a formal system from two halves: ¡ The structural elements a prompt is composed of ¡ A curated library of prompt patterns, the prompting equivalent of software design patterns On top of that foundation, we add templates, prompt types, contextual prompting, sampling, security, and management. By the end, you will have not just a set of tech- niques, but a shared vocabulary and method you can apply, review, and teach. Every example is drawn from work software engineers actually do: pull request descriptions, code review triage, incident summaries, refactor plans, runbook scoring, and support-bot audits. Who should read this book? This book is for software engineers and technical professionals who already use lan- guage models in their day-to-day work and want to stop guessing why their prompts sometimes regress. Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
19
xviii about this book Picture the senior engineer who has shipped a Copilot or chatbot feature and wants a more systematic approach to prompt design, or the team lead who wants to bring the same engineering rigour to prompts that they bring to the rest of the stack. To get the most from this book, you should be comfortable with software engineering fundamentals such as APIs, version control, debugging, and tradeoffs. You should also have casual experience using a chat interface such as ChatGPT or Claude. No machine learning background is required. This book is not for: ¡ Machine learning researchers ¡ Engineers training or fine-tuning models ¡ Readers looking for an agent-framework tutorial ¡ Readers looking for a guide to production AI systems architecture Those topics belong to a different book. This one stays firmly inside prompt engineering. Its companion volume, AI Engineering in Practice, covers the wider stack: language models, agents, harnesses, context engineering, and the rest of modern AI engineering. How this book is organized: A roadmap The book has eleven chapters across four parts. Part 1 frames prompts as engineered artifacts. ¡ Chapter 1 defines what prompt engineering is, introduces the prompt lifecycle, and establishes the mental models that anchor the rest of the book. Part 2 gives you the building blocks that turn that framing into reusable, production- ready prompts: ¡ Chapter 2 covers the six structural pieces every effective prompt is built from. ¡ Chapter 3 treats precision, directness, and brevity as deliberate design choices. ¡ Chapter 4 presents eight foundational patterns for recurring prompting problems. ¡ Chapter 5 turns those patterns into reusable, parameterized prompts. ¡ Chapter 6 distinguishes system prompts from user prompts and shows how to architect them in chat applications. Part 3 covers the techniques you reach for when one generation is not enough: ¡ Chapter 7 uses zero-shot, few-shot, and in-context learning deliberately rather than by accident. ¡ Chapter 8 improves reliability with mean, mode, self- consistency, and best-of-N. ¡ Chapter 9 applies chain-of-thought, meta prompting, and ReAct for higher-level reasoning and agentic workflows. Part 4 is what you do once these prompts are in production: ¡ Chapter 10 defends against prompt injection, jailbreaking, and instruction con- flict with five hardening techniques. Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>
Page
20
xixabout this book ¡ Chapter 11 covers versioning, governance, lifecycle, and collaboration on a shared prompt library. Chapters 1 through 9 build on one another and are best read in sequence. Chapters 10 and 11 stand alone, and you can dip into them whenever production concerns become real for you. Most chapters close with a set of hands-on exercises. These are deliberately designed for independent problem-solving, so we do not provide model answers. The goal is to build your own judgment about prompt design rather than to match ours. About the code This is a no-code, model-agnostic book. There is no source code to download, install, or run, and nothing here is tied to a particular provider, SDK, or framework. To follow along, you only need access to a chat interface or API for a modern lan- guage model such as ChatGPT, Claude, or Copilot. No machine learning background, GPU, or local model setup is required. The book contains examples of prompts and model responses, presented in blocks. The following terms describe the inputs and outputs used in these examples: ¡ Prompt: Input supplied to a model to guide its output. ¡ System prompt: Instructions that define the model’s role, behavior, or constraints. ¡ User prompt: The user’s request or input for a particular interaction. ¡ Prompt template: A reusable prompt structure with placeholders for variable content. ¡ Model response: The output generated by the model in response to the input. These blocks are set off from the surrounding prose so you can always tell what was sent to the model and what came back. We have often reformatted prompts and trimmed model responses to the part being discussed. Callouts and figure captions accompany many of them, drawing attention to the structural elements, patterns, and techniques in play. liveBook discussion forum Purchase of Prompt Engineering in Practice includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach comments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical questions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/ book/prompt-engineering-in-practice/discussion. The single most useful thing you can do for the book is tell us which examples felt true to your work and which felt off. We read every comment. Those signals shape the next revision more than anything else. Licensed to THIAGO BANDEIRA <thiago@lar.ifce.edu.br>