liveBook · Manning

Chapter 2: Basic prompting and DSPy

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

The introduction explains why software-driven LM applications need reliable prompts, notes that prompts often use task-dependent components, and explains why those components remain useful to understand when using DSPy.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why software-driven LM applications need reliable prompts, notes that prompts often use task-dependent components, and explains why those components remain useful to understand when using DSPy.

Key terms: LM calls, prompt engineering, prompt programming, standard sections, persona, demonstrations, DSPy, prompting techniques, prompt content

2.1 Prompts

The section contrasts editing prompt sections directly with specifying prompting techniques in DSPy.

Key terms: prompt engineering, prompting techniques, sections

2.1.1 Classification prompt example

The section uses a customer-intent classification prompt to show how a structured prompt guides an LM, why ambiguous messages remain difficult, and how prompt engineering improves prompts manually.

Key terms: prompt template, prompt, LM, customer intent, categories, Unknown, Update Email, prompt engineering, manual process

2.1.2 The components of an effective prompt

This section explains the purposes of common prompt sections and why their content, order, and wording should be chosen for the task and tested with the LM.

Key terms: prompt sections, prompt engineering, prompt programming, task description, context, input/output description, output format description, demonstrations, one-shot prompting, few-shot prompting

2.2 A full DSPy application

This section contrasts a full DSPy application with a simpler question-answering example and explains what a callable DSPy program does.

Key terms: DSPy application, prompt, optimization, LM, API key, question -> answer, Predict, module, program, prediction

2.3 The main concepts in DSPy

This section identifies DSPy’s core abstractions and introduces how optimization and evaluation relate to them.

Key terms: abstraction, LM, signature, module, prediction, optimization, metrics, examples, evaluations, optimizers

2.4 The language model

This section explains how to select and configure the language model DSPy uses for subsequent calls.

Key terms: LM, LM object, dspy.configure(), API key

2.4.1 Storing API keys in .env files

This section explains why LM API keys should be kept out of source code and how DSPy applications can obtain them from a .env file or Google Colab secrets.

Key terms: API key, authentication, hardcoded strings, python-dotenv, .env, load_dotenv(), os.environ, .gitignore, api_key, OPENAI_API_KEY

2.4.2 Calling LMs directly

This subsection shows how to call a DSPy LM directly to test handcrafted prompts and confirm that the LM and API key work.

Key terms: LM object, API key, prompt, handcrafted prompts, role, prior messages

2.4.3 Using LiteLLM to access LMs

This section explains how LiteLLM lets DSPy access many LM providers through a common interface and what varies when selecting a provider and model.

Key terms: LiteLLM, standard API, OpenAI-like interface, API key, LM providers, LM provider, LM name, mistral/mistral-tiny, gpt-4o-mini

2.4.4 LM caching

This section explains how DSPy caches LM responses by default and when and how to disable caching.

Key terms: cache, providers’ responses, LM, cache parameter, global caching settings, consistency, execution times

2.4.5 Setting LM parameters

This section explains how temperature and max_tokens affect an LM’s responses, and notes that some models require specific parameter values.

Key terms: temperature, LM, cache, max_tokens, prompt, GPT-5, reasoning models

2.4.6 Switching between LMs

This section explains how DSPy switches among language models and why an application might use more than one.

Key terms: LM object, configure(), set_lm(), context manager, different LMs, different tasks, diverse set of potential answers

2.5 Signatures

This section explains how DSPy signatures specify an LM task through named inputs and outputs, and why they are a concise alternative to writing full prompts.

Key terms: signature, inputs, outputs, prompt, descriptive_essay, input, output, function definition, output fields, reasoning

2.5.1 Example asking for a confidence score

Shows how a DSPy signature can request a confidence score alongside an answer and specify that the score should be numeric.

Key terms: confidence, output fields, signature, strings, numeric value, type

2.5.2 Summarization example

This subsection uses a poem-summarization example to show how a DSPy program’s input and output follow its signature, while noting a limit on interpreting the result.

Key terms: prog, signature, input field, poem, output field, summarization, LM, general knowledge, text that was passed in

2.5.3 Translation example

This subsection illustrates translation as a task that requires changing only the DSPy signature and notes what the example returned.

Key terms: signature, translation, gpt-4o-mini

2.5.4 Entailment example

The section uses a Socrates example to show that entailment depends on what premises establish, not whether a conclusion happens to be true, and notes how to request a binary answer.

Key terms: entailment tests, premises, signature, logically_valid: bool

2.5.5 Style transfer example

The section uses a poem rewrite to show how a DSPy signature specifies a style-transfer task and how the same approach extends to other LM tasks.

Key terms: signature, input fields, style transfer, signatures, LM

2.6 Modules

This section explains how DSPy modules turn signatures into prompts, execute LM calls, and let developers change prompting techniques without rewriting the task definition.

Key terms: signature, module, DSPy program, Predict, forward(), DSPy-created prompt, ChainOfThought, ProgramOfThought, modular, LM

2.7 Predictions

This section explains what a DSPy Prediction contains and how to access its output fields.

Key terms: Prediction, signature, output fields, square brackets, dot notation

2.8 Code readability and code history

The section explains how DSPy makes LLM applications easier to read, change, and review than applications built around large prompts.

Key terms: DSPy code, prompts, LMs, code history, signature, module, Predict, ChainOfThought, MultipleChainComparison

2.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Introduction to prompt programming and DSPy Chapter 3: Classifying user intent →

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.