DSPy From First Principles: Programs, Metrics, Optimizers, Agents, and Reasoning Search

Front Cover
Ernan Hughes, Sep 4, 2026 - Computers

What if building applications with language models stopped being an exercise in hand-writing prompts and became an engineering discipline?

DSPy From First Principles develops that idea from the ground up.

Starting with the simplest language-model call, the book gradually turns prompts into typed programs, examples into experimental data, metrics into objectives, and optimization into a controlled engineering process.

Along the way, you will build and examine:


DSPy Signatures and Modules

structured language-model programs

few-shot optimization

instruction optimization

reflective optimization with GEPA

metrics and adversarial metric testing

agents and tool use

retrieval, memory, and long-context systems

reasoning search and Monte Carlo Tree Search

leakage controls and evidence boundaries

evaluation, promotion, and production deployment

a complete self-improving engineering program

The emphasis throughout is not simply on making a model produce better answers. It is on understanding why a system improved, what evidence supports that conclusion, and what information an optimizer or agent was allowed to see.

The experiments also include failures and null results. A metric that appears reasonable can reward the wrong behaviour. An optimizer can exploit a weak objective. A tree-search implementation can draw a tree without genuinely exploring one. A stronger model can make expensive reasoning search unnecessary.

By the end, DSPy becomes more than a prompt framework. It becomes a way to treat language-model behaviour as an experimental variable inside a larger software system.

This book is for developers, AI engineers, researchers, and technically curious readers who want to understand DSPy from first principles and build language-model systems that can be measured, tested, optimized, and trusted.

 

Contents

Section 1
Section 2
Section 3
Section 4
Section 5
Section 6
Section 7
Section 8
Section 23
Section 24
Section 25
Section 26
Section 27
Section 28
Section 29
Section 30

Section 9
Section 10
Section 11
Section 12
Section 13
Section 14
Section 15
Section 16
Section 17
Section 18
Section 19
Section 20
Section 21
Section 22
Section 31
Section 32
Section 33
Section 34
Section 35
Section 36
Section 37
Section 38
Section 39
Section 40
Section 41
Section 42
Section 43

Common terms and phrases

Bibliographic information