liveBook · Manning

Prompt

As explained in Building LLM Applications with DSPy

Textual instructions or input supplied to a language model to elicit a response.

A prompt is the instructions and input sent to a language model to elicit a response. Its wording and structure matter: a small change can produce a different answer, and the best arrangement can vary by task and model. Manually improving a prompt usually means adding, removing, reordering, or rewording its sections. section 2.1 section 1.0

There are two practical ways to work with a prompt:

  • Write it directly, pass system instructions and user input to an LM API, then test revisions against examples. section 4.4
  • Use DSPy to define the LM, input and output fields in a signature, and a module that chooses the prompting technique. The module generates the prompt and returns a structured prediction; the generated system and user prompts can be inspected with lm.inspect_history(). section 2.9 section 3.8

Treat prompt quality as something to measure, not guess. Optimizers can try different demonstrations or instructions; compare their resulting prompts on the task rather than assuming one optimizer always wins. A custom module can also append an instruction, although repeated hand-tuning may be better left to an optimizer. section 5.6 section 7.4.1

A manual classifier baseline

An intent classifier using a hardcoded system prompt through the OpenAI API scored 0.88 accuracy on a 50-item test set—about the same as the unoptimized DSPy-generated prompt. The small test set does not establish a meaningful difference, but the comparison gives optimization a baseline. section 4.4

Listing 7.11 Custom module to add a closing section
from dspy.primitives.module import Module
from dspy.signatures.signature import Signature, ensure_signature
from typing import Any

class Closer_Example(Module):
    def __init__(self, signature: str, **config: dict[str, Any]):
        super().__init__()
        signature = ensure_signature(signature)
        signature.instructions += " Answer very carefully."
        self.predict = dspy.Predict(signature, **config)  #1

    def forward(self, **kwargs):
        return self.predict(**kwargs)

    async def aforward(self, **kwargs):
        return await self.predict.acall(**kwargs)

prog = Closer_Example("question -> answer")
prog(question="What is the capital of Spain?")
print(lm.inspect_history(n=1))

The custom-module listing shows how appending “Answer very carefully.” to signature instructions changes the generated prompt, which can then be inspected. section 7.4.1

How Building LLM Applications with DSPy covers it

How deep it goes
Explains editable prompt sections and shows custom instruction changes, without prescribing universally optimal wording.
Examples
Compares a hardcoded intent-classification prompt with a DSPy-generated prompt and appends a closing instruction in a custom module.
What it assumes
Starts with familiar LM trial and error, then introduces signatures and modules rather than requiring expertise in manual prompt tuning.
What you can do after
Apply it via DSPy signatures, modules, inspection, and optimizers to generate and compare prompts without handcrafting each candidate.
How it connects
Connects prompts to prompt engineering, DSPy signatures, modules, demonstrations, and optimization.

Also explained in

Compare how 50 books explain prompt →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.