Prompts are code
A prompt that was polished by hand breaks at the next model change. With a signature, a metric, and an optimizer it becomes software that can be measured.
The usual way to build with a language model: someone writes a long text of instructions, tries a few examples, changes the text, and tries again. The prompt grows until nobody on the team understands all of it.
That is enough for a prototype. In production the weaknesses show:
- A new model, and the prompt behaves differently.
- Two prompts in a row, and the errors of the first end up in the second.
- Whether a change is better is decided by the impression from five examples.
Describe instead of phrasing
DSPy, a framework from the NLP group at Stanford University, turns this around. You no longer write how the model is asked, but what goes in and what comes out:
from typing import Literal
import dspy
class Triage(dspy.Signature):
"""Assign a support ticket
to a team."""
ticket: str = dspy.InputField()
team: Literal[
"billing",
"tech",
"sales",
] = dspy.OutputField()
lm = dspy.LM("provider/model")
dspy.configure(lm=lm)
triage = dspy.Predict(Triage)
triage(ticket="Charged twice.")
This signature is an interface. The framework generates the prompt for it, and which model answers is set in a single place.
The metric makes the difference
The second part is a metric: a function that says, for one example, whether the answer is right. With a metric and a few dozen examples an optimizer can improve the program without anyone hand-tuning the text. It tries instructions and examples and keeps what scores better by the metric.
def match(gold, pred, trace=None):
return gold.team == pred.team
optimizer = dspy.MIPROv2(
metric=match, auto="light"
)
better = optimizer.compile(
triage, trainset=examples
)
better.save("triage.json")
The result is a file with the optimized program. It lives in the repository like other code and is generated again when the model, the data, or the metric change.
What GEPA adds
A newer optimizer is called GEPA. It does not hand the model just a number, but the whole course of an attempt with intermediate steps and tool calls, and has it put into words what went wrong. New variants of the prompt come out of that. The authors report, across six tasks, results 6 percent better on average than with reinforcement learning (GRPO), with up to 35 times fewer rollouts.
Such numbers come from the authors' benchmarks. How much of that carries over to your own project, only your own metric can show.
When it pays off
It pays off when a program has more than one model call, when a good answer can be measured, when there are examples, and when the model is likely to be swapped.
It does not pay off for a single prompt that is needed once, and not without a metric: without measurement there is nothing to optimize.
The real gain is not the framework. It is being forced to write down what correct means.
Sources
- Khattab et al., DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines, 2023
- Agrawal et al., GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning, 2025
- Florian Zeba, Stop Tinkering With Strings: A Practical Tour of DSPy and Reflective Prompt Evolution, 2026