liveBook · Manning

Chapter 6: Optimizing prompt instructions

What you will learn in chapter 6 of Building LLM Applications with DSPy

The chapter introduces instruction optimization in DSPy, explains its meta-prompting foundation, and frames how to compare optimizers and their costs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces instruction optimization in DSPy, explains its meta-prompting foundation, and frames how to compare optimizers and their costs.

Key terms: instruction optimization, demonstrations, meta-prompting, candidate prompts, OPRO, COPRO, MIPROv2, BootstrapFewShot, SIMBA, GEPA

6.1 COPRO

This section explains how COPRO improves prompt instructions by generating alternatives, evaluating them, and keeping the best candidate for the next round.

Key terms: COPRO, prompt instructions, candidate prompt, validation set, coordinate ascent, hill climbing

6.1.1 What are the prefixes for the output fields?

This section explains how DSPy adapters construct prompts and what output-field prefixes are, including their limited role in COPRO optimization.

Key terms: DSPy adapter, prompt template, signatures, demonstrations, prefix to the output fields, output fields, COPRO, instructions

6.1.2 What are the prompt instructions?

This section explains where DSPy places prompt instructions and why a signature docstring is useful even when an optimizer may later replace those instructions.

Key terms: prompt instructions, System message, demonstrations, signature, docstring, default instructions, optimizers

6.1.3 Executing the COPRO optimizer

This section shows how to run COPRO on a DSPy program, configure its prompt search and evaluation, and inspect the resulting scores.

Key terms: COPRO, compile(), optimized program, history, metric function, breadth, depth, candidate prompts, trainset, validation set

6.1.4 Using a prompt LM

Explains how COPRO can use a separate LM to propose prompt instructions, how those prompts are evaluated, and how COPRO handles tied scores.

Key terms: prompt LM, task LM, compilation, candidate instructions, validation set, iteration, inspect_history(), dspy.inspect_history(n=1000), original prompt, identical score

6.1.5 How COPRO generates candidate instructions

This section explains how COPRO asks a prompt LM for candidate instructions and output prefixes, first from the original prompt and then from scored attempts.

Key terms: candidate instructions, original instructions, prompt LM, prefix for the output fields, Predict, n, breadth-1, original prompt, attempted instructions, instructions

6.1.6 Executing multiple optimizers

This section explains how to chain optimizers so a DSPy program can gain demonstrations and then have its instructions updated.

Key terms: COPRO, LabeledFewShot, demonstrations, instructions, compile

6.2 MIPROv2

This section explains how MIPROv2 jointly searches for demonstrations and instructions, configures that search, and evaluates candidate prompts to select a final program.

Key terms: MIPROv2, demonstrations, instructions, BootstrapFewShot, COPRO, zero-shot, candidate instructions, prompt LM, task LM, Bayesian optimization

6.2.1 Grounding

This section explains how MIPROv2 grounds instruction generation in task information, how its grounding options work, and why it cannot use evaluation history at that stage.

Key terms: MIPROv2, grounding, prompt LM, task LM, dataset summary, source code, candidate instructions, evaluation, tips, tip_aware_proposer

6.2.2 Using Bayesian hyperparameter tuning for trial optimization

This section explains how MIPROv2 uses Bayesian optimization to choose promising combinations of demonstrations and instructions without evaluating every possible prompt.

Key terms: MIPROv2, demonstrations, instructions, Bayesian optimization, trials, exploration, exploitation, uncertainty, Optuna, surrogate function

6.3 InferRules

This section explains how InferRules turns patterns in training examples into prompt rules, why those rules may be useful, and how its example run is configured.

Key terms: InferRules, demonstrations, rules, instructions, grounding, prompt LM, input tokens, num_candidates, num_rules, train

6.4 SIMBA

Introduces SIMBA as a reflective prompt optimizer that iteratively adds demonstrations or rules, while noting that optimizer performance depends on the task and language model.

Key terms: SIMBA, demonstrations, rules, reflective process, GEPA, optimizer, LM

6.4.1 Optimizing the airline classifier with SIMBA

This section shows how SIMBA optimizes an airline classifier by testing candidate prompts, reflecting on their performance, and adding rules or demonstrations, while cautioning that optimization scores are not final evaluation results.

Key terms: minibatch, candidate prompt, pool, demo, rule, grounded process, prompt LM, trajectories, suggested rule, compile()

6.4.2 Taking advantage of feedback

Explains how metric-function feedback helps SIMBA improve prompts, using an intent-classification example and a more complex metric to show why explanations are useful alongside scores.

Key terms: feedback, metric function, reflective optimizers, SIMBA, signature, labels, character-level overlap, numeric scores, Boolean scores, metric functions

6.5 GEPA

The section introduces GEPA as a DSPy optimizer, identifies its named foundations, and notes how its developers recommend using it.

Key terms: GEPA, Genetic-Pareto, genetic algorithm, Pareto front, reflection, DSPy

6.5.1 Genetic algorithms

This section explains how genetic algorithms optimize prompts by maintaining and refining a pool of candidates rather than improving just one prompt.

Key terms: genetic algorithms, hill climbing, pool of candidate solutions, initial pool of candidates, mutating, crossover, GEPA, pool, top-performing prompts, diversity

6.5.2 The Pareto front

This section explains how GEPA uses per-example validation scores to maintain a Pareto front and select prompts for further optimization.

Key terms: GEPA, metric function, numeric scores, validation set, Pareto front, dominates, mutation, crossover, exploration, exploitation

6.5.3 Generating new candidates

This section explains how GEPA uses reflection to generate prompt candidates through mutation and crossover, and why reflection helps maintain a diverse pool of prompts.

Key terms: GEPA, reflective mutation, LM calls, metric functions, crossover, system-aware merge, rationale, candidate prompt, reflection, Pareto front

6.5.4 Using GEPA for the airline classifier

Shows how to configure GEPA for an airline classifier, choose its data and optimization budget, and inspect the resulting instructions.

Key terms: pred_name, pred_trace, metric function, SimpleSignature, train set, validation set, reflective updates, Pareto front, reflection_lm, prompt LM

6.6 Ensemble

This section explains how DSPy combines multiple programs into an ensemble, reduces their outputs to one prediction, and selects a small set of programs that works well together.

Key terms: Ensemble optimizer, inference time, programs, reduce function, majority, Prediction, Uncertain, validation set, majority vote, complement each other

6.7 Saving the programs generated by DSPy

This section explains why to save optimized DSPy programs and contrasts saving a full program with saving its metadata.

Key terms: optimized program, compilation, pickle file, metadata, save_program, JSON file, demos, instructions

6.8 Comparing optimizers

The section explains how to compare prompt optimizers for an application and interprets results from two language models without treating any optimizer as universally best.

Key terms: optimizers, hyperparameters, prompt, optimization time, diminishing returns, breadth, depth, heavy, demos, GEPA

6.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Optimizing prompt examples Chapter 7: Custom modules →

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.