Page
1
M A N N I N G Serj Smorodinsky Brett Kennedy Replacing manual prompts with systematic optimization
Page
2
Building LLM Applications with DSPyLicensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
3
iiLicensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
4
Building LLM Applications with DSPy REPLACING MANUAL PROMPTS WITH SYSTEMATIC OPTIMIZATION SERJ SMORODINSKY BRETT KENNEDY M A N N I N G SHELTER ISLAND Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
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 by 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. 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. Manning Publications Co. Development editor: Doug Rudder 20 Baldwin Road Technical editor: Martin Welß PO Box 761 Review editor: Kishor Rit Shelter Island, NY 11964 Production editor: Kathy Rossland Copy editor: Kari Lucke Proofreader: Katie Tennant Typesetter and cover designer: Marija TudorISBN 9781633435018 Printed in the United States of America Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
6
We dedicate this book to our families Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
7
brief contents 1 ■ Introduction to prompt programming and DSPy 1 2 ■ Basic prompting and DSPy 15 3 ■ Classifying user intent 37 4 ■ Evaluating DSPy programs 59 5 ■ Optimizing prompt examples 89 6 ■ Optimizing prompt instructions 121 7 ■ Custom modules 157 8 ■ Summarization and more effective metric functions 197 9 ■ Creating an agentic RAG-based chatbot 227 index 267vi Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
8
contents preface xii acknowledgments xiv about this book xv about the authors xviii about the cover illustration xix 1 Introduction to prompt programming and DSPy 1 1.1 What makes prompt programming different? 2 The goals of prompt programming 2 ■ Prompt programming vs. prompt engineering 3 1.2 Introducing DSPy 4 DSPy code example 4 ■ Advantages of working with code 5 A methodical search for optimal prompts 6 ■ The bitter lesson 6 A data-driven approach to tuning prompts 7 ■ Adapting to change 8 ■ Making the best use of LMs 8 1.3 Working with complex LM-based applications in DSPy 8 1.4 Where is DSPy useful? 10 1.5 Working with LangChain 10 1.6 Building LLM applications through baselines and optimization 11 1.7 What this book covers 13vii Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
9
CONTENTSviii 2 Basic prompting and DSPy 15 2.1 Prompts 16 Classification prompt example 16 ■ The components of an effective prompt 18 2.2 A full DSPy application 20 2.3 The main concepts in DSPy 22 2.4 The language model 22 Storing API keys in .env files 23 ■ Calling LMs directly 24 Using LiteLLM to access LMs 25 ■ LM caching 26 Setting LM parameters 27 ■ Switching between LMs 28 2.5 Signatures 29 Example asking for a confidence score 30 ■ Summarization example 31 ■ Translation example 31 ■ Entailment example 32 ■ Style transfer example 32 2.6 Modules 33 2.7 Predictions 35 2.8 Code readability and code history 35 3 Classifying user intent 37 3.1 Creating a baseline classifier 38 Utterances 38 ■ Multiclass, multilabel, and dynamic classification 39 3.2 Downloading and preparing the ATIS dataset 39 3.3 Building a DSPy intent classifier 43 The signature 46 ■ The module 47 3.4 Using the OpenAI API directly 48 Classification using the OpenAI API 48 ■ System and user roles 50 ■ Comparing DSPy with direct use of LM APIs 51 3.5 Dynamic labels vs. static labels 52 Dynamic intent classification 53 ■ Static intent classification 53 3.6 Handling multiple intents 54 3.7 Viewing the history 55 4 Evaluating DSPy programs 59 4.1 Creating a dataset for evaluation 61 Using the Example class 62 ■ Dividing the data into train and test sets 65 ■ The sizes of the sets 66 ■ Splitting the ATIS data 67 Using DSPy to generate examples 68 Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
10
CONTENTS ix 4.2 Evaluating a module with a test set 69 Defining an evaluation metric 69 ■ Defining a metric for the baseline model 71 ■ Calculating a final evaluation for the module 72 ■ Evaluation for tuning versus final evaluation 73 Testing the metric function 73 4.3 Evaluating the DSPy baseline model 75 Using custom Python code for evaluation 75 ■ Using DSPy Evaluate 76 ■ Rate limits 79 4.4 Evaluating a manually created prompt using the OpenAI API 79 4.5 Evaluating per-class performance 80 4.6 Evaluating the consistency of responses 82 4.7 Evaluating other LMs and modules 84 5 Optimizing prompt examples 89 5.1 General approaches to optimization 90 5.2 The LabeledFewShot optimizer 93 Executing the LabeledFewShot optimizer 97 ■ Executing the LabeledFewShot optimizer repeatedly in a loop 100 ■ Working with ChainOfThought 102 5.3 BootstrapFewShot 103 Working with ChainOfThought 108 ■ Specifying a teacher 110 5.4 BootstrapFewShotWithRandomSearch 110 5.5 KNN: Finding examples dynamically 112 5.6 Evaluation results 116 gpt-4o-mini 116 ■ gpt-4.1-nano 119 6 Optimizing prompt instructions 121 6.1 COPRO 122 What are the prefixes for the output fields? 123 ■ What are the prompt instructions? 124 ■ Executing the COPRO optimizer 125 ■ Using a prompt LM 127 ■ How COPRO generates candidate instructions 128 ■ Executing multiple optimizers 130 6.2 MIPROv2 130 Grounding 134 ■ Using Bayesian hyperparameter tuning for trial optimization 1356.3 InferRules 137 Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
11
CONTENTSx 6.4 SIMBA 138 Optimizing the airline classifier with SIMBA 139 ■ Taking advantage of feedback 142 6.5 GEPA 144 Genetic algorithms 144 ■ The Pareto front 145 ■ Generating new candidates 148 ■ Using GEPA for the airline classifier 148 6.6 Ensemble 151 6.7 Saving the programs generated by DSPy 153 6.8 Comparing optimizers 153 7 Custom modules 157 7.1 Types of DSPy modules 158 7.2 Coding a custom module 159 7.3 DSPy’s built-in modules 163 ChainOfThought 163 ■ BestOfN 166 ■ MultiChain- Comparison 167 ■ Refine 169 7.4 Examples of custom modules 173 Adding a closing section 173 ■ Rewording the inputs 174 Separating out toxic comments 175 ■ Module for checking security 176 ■ Chain of Draft 178 ■ Creating a critic-based module 179 7.5 Parallelism in custom modules 181 The asyncio library 182 ■ Calling modules in parallel 185 Parallel execution within modules 186 ■ Asynchronous Skeleton-of- Thought 187 ■ Optimizing custom modules 189 ■ Using BootstrapFewShot 189 ■ Optimizing using MIPROv2 190 Examining the set of submodules 191 7.6 Creating complex vs. simple modules 192 7.7 Using DSPy to create custom modules 192 8 Summarization and more effective metric functions 197 8.1 Summarization 198 Types of summarization 198 ■ Evaluating summarization 199 8.2 The DialogSum dataset 202 8.3 Baseline summarizer 204 8.4 Evaluation by lexical similarity 205Precision and recall 206 ■ The F1 score 207 Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
12
CONTENTS xi 8.5 Evaluation using embeddings 208 8.6 Evaluation using LLM-as-a-judge 209 Metrics used with LLM-as-a-judge 209 ■ Metric functions using LLM-as-a-judge 211 ■ The volume of LM calls when using LLM-as-a-judge 212 ■ Using LLM-as-a-judge with DSPy 212 Creating the datasets for the judge programs 213 Groundedness 215 ■ Completeness 217 ■ Testing both groundedness and completeness 218 ■ Evaluation by Q&A 220 An example of creating and using a judge 221 9 Creating an agentic RAG-based chatbot 227 9.1 The WixQA dataset 228 9.2 Creating a DSPy RAG application 229 Embedding the article titles 232 ■ The baseline RAG module 233 The factuality evaluation metric 236 ■ Calculating the overall accuracy of the baseline 238 ■ Multihop RAG 240 HyDE 242 9.3 Agents and ReAct 243 Creating a ReAct calculator 244 ■ Agentic RAG 250 Agents using DSPy’s Python interpreter 253 9.4 Maintaining memory 254 9.5 Self-correction with DSPy Refine 258 9.6 Connecting agents to MCP 259 MCP protocol intro 260 ■ Exposing the Wix knowledge base through an MCP server 261 ■ Connecting a DSPy agent to an MCP server 263 index 267Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
13
preface Everyone’s road into data science and AI is different. Mine has always passed through software engineering. More than a decade ago, while completing my BS and MS, I studied bioinformat- ics, the intersection of computer science and biology. We used software to solve prob- lems related to living organisms, working with algorithms for sequence comparison, clustering, support vector machines, decision trees, and other methods that were close to what we would now call data science. During that time, I also realized some- thing important: I loved coding. Software was going to be my craft. A few years later, while working at SafeDK, later acquired by AppLovin, I found my way back to those roots. We needed to build an image classifier, and I was eager to take it on. My CEO decided to bring in an experienced advisor instead, which was probably the right decision. I followed the advisor closely, joined meetings, and took notes. One thing stayed with me: whenever he mentioned precision and recall, everyone in the room nodded in silence. The concepts clearly mattered, but they also seemed mysteri- ous. That moment sparked my motivation to understand machine learning more deeply and to make these ideas clearer to others. Since then, I have worked on classification problems involving tabular data, images, audio signals, and natural language in conversational AI. My software engi- neering background has always shaped the way I approach these problems. I care about clear interfaces, reliable systems, and code that other people can understand and build on. I also care about working closely with product managers because AI sys- tems are rarely just technical artifacts. They are built to serve real product goals.xii When ChatGPT and the OpenAI API arrived, I was both fascinated and suspicious. Suddenly, many tasks that once required training models could be solved with a simple Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
14
PREFACE xiii API call. Prompt engineering became the norm, but to me it often felt like feature engi- neering in a new costume: powerful and useful, but also fragile and hard to trust. Then I found DSPy, and once again, I felt at home. DSPy brought back the ideas I trusted from both software engineering and machine learning: clear inputs and outputs, explicit interfaces, measurable behavior, and optimization against a metric. Instead of treating prompts as fragile strings, DSPy lets us define language model behavior as a program that can be evaluated and improved. We hope that this book will contribute to your understanding of how to build reli- able LLM applications. Whether you are writing your own code or using coding agents, DSPy will help you think about baselines, evaluation, and optimization for prompt-based tasks. —SERJ SMORODINSKY Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
15
acknowledgments We thank the people who created DSPy (including hundreds of contributors), our families, the editors who worked on this book, the reviewers, especially acquisition edi- tor Jonathan Gennick, without whom this endeavor wouldn’t exist, development edi- tor Doug Rudder, who generously helped us to make the book clear but also authentic, and everyone at Manning. Also, many thanks go to our technical editor Martin Welß. Martin has worked for more than 30 years as a software engineer and architect with Java, Python, and Linux. Since 2019, his focus has been on AI, and he leads the open source project Eclipse Graphene, which is a system to create cognitive architectures from reusable building blocks. To all the reviewers—Ako Heidari, Alejandro Cuevas Rivero, Alisson Sol, Allen Ding, Amogh Mishra, Anuj Ashok Potdar, Ariel Pérez, Arnab Saha, Benoy Maniara, Breno Brito, Clyde Kallahan, Cyrus Nouroozi, Dan Klose, Dhar Rawal, Dhyey Dhar- mendrakumar Mavani, Dima Timofeev, Hanna Moazam, Heng Zhang, Herumb Shandilya, Isaac Miller, Jim Matlock, Jiri Pik, Thadaka Kalyan Chakravarthy, Kaushik Varadha Rajan, Krishna Rekapalli, Kurt McKee, Louis Luangkesorn, Maitrik Patel, Matteo Mazzola, Mirerfan Gheibi, Juhyeong Lee, Patrick D. Mobley, Pratap Ramamur- thy, Sarbani Paul, Saurabh Vinayak Sakalkar, Sebastian Krämer, Sergio Govoni, Shang- yin Tan, Stepan Plotytsia, Thomas J. Kelly, Xiao Cui, and Yijun Xie—your suggestions helped make this a better book. We also thank the open source community. The datasets, tools, and research made publicly available by countless contributors have made this book possible. Their will-xiv ingness to share knowledge continues to advance the entire field, and we are grateful to build upon that collective work. Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
16
about this book Building LLM Applications with DSPy was written to help you develop prompt-based tasks in an efficient way using the DSPy framework. We start by explaining the basic machinery of DSPy, such as signatures, using basic prompt examples. Then we show- case the three-stage methodology of development: build a baseline classifier and eval- uate and optimize on four different projects, the first of which is intent classification of an airline customer service. Who should read this book Building LLM Applications with DSPy is for AI engineers and data scientists who want to ship large language model (LLM)–based features faster and in a more reliable way. Even if a coding agent is generating the code, it’s crucial to work methodologically; otherwise, it is too easy to automate slop rather than something valuable. DSPy will help you get the extra mile by letting you evaluate your features. How this book is organized: A road map The book is divided into nine chapters: Chapter 1 introduces prompt programming and DSPy, before discussing DSPy’s advantages over prompt engineering, including a nod toward the “bitter lesson.” Chapter 2 covers the basic DSPy concepts of signatures and LLMs, using basic prompt examples such as classification, question answering, style transfer, and summarization. Chapter 3 introduces the three main stages of prompt programming develop-xv ment. We get to know the ATIS dataset through an EDA and build our baseline classifier through a DSPy signature. Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
17
ABOUT THIS BOOKxvi Chapter 4 does a deep dive into evaluation by evaluating the baseline ATIS clas- sifier from the previous chapter. We get to know additional DSPy concepts, such as DSPy examples that represent dataset rows and metric functions. We evaluate objects that use them to calculate a metric score. The chapter ends with a sum- mary table listing scores for different language models. Chapter 5 focuses on prompt optimization by utilizing prompt examples on the ATIS dataset. We introduce different optimizers, explaining how they operate under the hood. The chapter ends with two summary tables with all optimizer scores on the ATIS task. Chapter 6 focuses on prompt optimization of the prompt instructions. These optimizers can handle more complex tasks and have a higher inner complexity as well. The chapter ends with two summary tables of metric results of instruc- tion optimizers on the ATIS dataset. Chapter 7 showcases additional DSPy optimizers, such as Refine, and custom modules that enable complex compositional prompt flows. Chapter 8 introduces the LLM-as-a-judge concept and additional metrics through the DialogSum dataset. We show the different stages of the LLM as a judge calibration and the use of custom modules. Chapter 9 starts with explaining retrieval-augmented generation (RAG), agen- tic RAG, and the HyDE method, and ends with a full example of a chatbot that has persistent memory. The chapter utilizes the WixQA dataset for all the examples. About the code This book contains many examples of source code both in numbered listings and in line with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (➥). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts. You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/building-llm-applications-with-dspy. The complete code for the examples in the book is available for download from the Manning website at https://www.manning.com/books/building-llm-applications-with- dspy, and from GitHub at https://github.com/SerjSmor/LLM-Applications-DSPy. liveBook discussion forum Purchase of Building LLM Applications with DSPy includes free access to liveBook, Man- ning’s online reading platform. Using liveBook’s exclusive discussion features, you Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
18
ABOUT THIS BOOK xvii 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 authors and other users. To access the forum, go to https://livebook .manning.com/book/building-llm-applications-with-dspy/discussion. Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray! The forum and the archives of previous discussions will be accessible from the pub- lisher’s website as long as the book is in print. Other online resources The DSPy documentation and API can help you figure things out: https://dspy.ai/.Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
19
about the authors SERJ SMORODINSKY is a software developer, AI engineer, and data leader with over a decade of experience across software develop- ment and data science. He leads teams that bridge the gap between research and production, turning experimental AI into reliable, real-world systems. He also teaches at Nebius Academy and creates friendly content that makes AI feel a little less mysterious. BRETT KENNEDY is a software developer with over 30 years of expe- rience in the field, including 15 years working in data science and AI. He remembers the AI winter of the 1990s, when just about everything in Python was named after something from Monty Python, when XGBoost finally made boosted models effective, and when the first word vectors appeared. He lives in Toronto with his spouse and two children. xviii Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>
Page
20
about the cover illustration The figure on the cover of Building LLM Applications with DSPy, titled “Le garçon d'amphithéâtre,” or “dissecting-room attendant,” is taken from a book by Louis Cur- mer published in 1841. Each illustration is finely drawn and colored by hand. In those days, it was easy to identify where people lived and what their trade or sta- tion in life was just by their dress. Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional cul- ture centuries ago, brought back to life by pictures from collections such as this one. xix Licensed to OSCAR GUADA VERDE <oscarguada@outlook.com>