Overview

1 Introduction to prompt programming and DSPy

The chapter introduces the early, often frustrating stage of building LLM applications, where small prompt changes can lead to very different results and developers spend significant time guessing, testing, and retesting. It contrasts traditional prompt engineering, which relies on manually rewriting prompts, with prompt programming, a higher-level approach in which developers describe the task in code and let a framework generate and refine the prompts. DSPy is presented as the leading example of this approach, aiming to improve prompt quality while reducing development and maintenance effort.

DSPy’s key idea is to treat prompt creation like software development: define inputs, expected outputs, and evaluation criteria, then use automation to search for strong prompts. The chapter emphasizes the value of modular, testable, reusable code and explains that DSPy can systematically generate candidate prompts, evaluate them on data, and select the best-performing ones. This data-driven process is framed as a practical response to the limitations of manual prompt tuning, especially when prompts must be reliable, adaptable, and efficient across different language models.

The chapter also shows why this matters for real applications, including summarization, classification, RAG, chatbots, and agents, where many model calls and complex workflows make manual prompt tuning impractical. DSPy is positioned as especially useful for large or long-running systems, but also helpful in simpler cases because it can produce better prompts with little code. The recommended workflow is to build a baseline, evaluate it thoroughly, optimize prompts automatically, and then deploy, with the broader goal of making LLM application development more systematic, scalable, and maintainable.

Layers of code when working with DSPy. We usually need only work at the top level, which is the source code we create – using classes and functions provided by the DSPy layer below.
Prompt of optimization of a customer service intent classifier. The optimization process generated 3 candidate prompts that are each evaluated. In the end, the best is selected. In this case, that is the 2nd prompt, which has the highest score, 90%. DSPy also supports processes that modify and re-evaluate the prompts over several iterations.
An example control flow in which several LM calls are made. The specific set of LM calls executed and their specific content are determined by previous LM calls and calls to tools.
The three main stages of building an LM-based application with DSPy
Example creating a simple, baseline application in DSPy
Once we have a baseline application, or any other version of the application, we can evaluate this.
DSpy supports automatically optimizing the prompts used by an application.

Summary

  • To get good results from an LM, it’s necessary to ensure that it’s given a good prompt.
  • Manually creating and tuning prompts is often slow and ad-hoc. It also must be repeated for each LM that’s considered.
  • Prompt programming, which supports automatically creating, evaluating, and optimizing prompts, provides a more modern alternative.
  • DSPy is the state of the art in prompt programming. It allows for clean, understandable, and simple code that can be easily re-executed to test new LMs or new prompting techniques.
  • Working with DSPy allows us to develop quickly, as much of the work interacting with LMs is now handled automatically by the framework.
  • Using DSPy, it’s recommended to first create a baseline application, then evaluate it, then optimize it.
  • Optimization works by having another LM suggest many candidate prompts and by carefully evaluating each of these.
  • Optimization may execute over multiple iterations, and uses established optimization techniques to identify, during each iteration, progressively stronger candidate prompts.
  • DSPy supports defining complex workflows.
  • Where workflows contain multiple prompts, DSPy allows us to optimize the full set of prompts together, allowing us to create very effective applications.

FAQ

What is prompt programming in the context of DSPy?Prompt programming is a higher-level approach where developers specify in code what they want a language model to do—such as the inputs it receives, the expected output format, and how results should be evaluated—while DSPy generates and optimizes the actual prompts automatically.
How is prompt programming different from prompt engineering?Prompt engineering relies on manually rewriting and testing prompts through trial and error, while prompt programming uses code and automation to generate, evaluate, and improve prompts systematically.
What does DSPy stand for?DSPy stands for Declarative Self-improving Python. “Declarative” means you specify the task you want done, “self-improving” refers to automatic prompt optimization, and “Python” reflects that it is built in Python.
What are the main goals of using DSPy?DSPy aims to improve prompt quality, reduce development time, make it easier to switch between language models, support experimentation with different prompting techniques, and allow prompt development code to be rerun easily at any time.
Why is prompt engineering considered limited?Prompt engineering can be slow, hard to reproduce, difficult to evaluate rigorously, and messy when prompts grow large. It can also be hard to track why prompt changes were made or how to improve them later.
How does DSPy help optimize prompts?DSPy can automatically generate many candidate prompts, evaluate them against test data, learn which ones perform best, and refine them over multiple iterations using search and optimization methods.
Why is evaluation such an important part of DSPy?Because language model outputs are stochastic and different prompts may perform better on different inputs, DSPy relies on evaluation to determine which prompts truly work best across many examples and runs.
What kinds of applications can be built with DSPy?DSPy can be used to build a wide range of LM applications, including classification, summarization, question answering, RAG systems, agentic workflows, chatbots, and other complex multi-step pipelines.
Why can DSPy be especially useful for smaller language models?Smaller models are often more sensitive to prompt quality, making them harder to use manually. DSPy’s automated search and evaluation can discover stronger prompts, helping smaller models work well enough for production and lower-cost applications.
When should someone consider using DSPy instead of direct prompting?DSPy is most useful when you need high-quality prompts, repeated optimization, complex workflows, or long-running applications. For casual one-off tasks where perfection is not important, direct manual prompting may still be sufficient.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building LLM Applications with DSPy ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building LLM Applications with DSPy ebook for free