In 2026, writing prompts by hand is increasingly viewed as writing low-level assembly. A Prompt Compiler (like DSPy) compiles a declarative system architecture into optimized prompts automatically.
Instead of tweaking words, you define a program structure (e.g. Question -> Answer), choose a model, define a programmatic metric (like accuracy or semantic similarity), and provide training examples. The compiler then optimizes the instructions and few-shot examples automatically, searching the space of options and even generating synthetic data.
import dspy
class MathQA(dspy.Signature): """Solve math word problems with step-by-step reasoning.""" question = dspy.InputField() solution = dspy.OutputField(desc="Step-by-step solution and final number")
math_bot = dspy.ChainOfThought(MathQA)
teleprompter = dspy.teleprompt.BootstrapFewShot(metric=math_metric) compiled_bot = teleprompter.compile(math_bot, trainset=train_data)
import dspy
class RouteQuery(dspy.Signature): """Route user queries to the correct retrieval source.""" query = dspy.InputField() reasoning = dspy.OutputField(desc="Why this source is selected") source = dspy.OutputField(desc="One of: 'database', 'web_search', or 'direct_reply'")
class QueryRouter(dspy.Module): def init(self): super().init() self.router = dspy.Predict(RouteQuery) def forward(self, query): return self.router(query=query)
teleprompter = dspy.teleprompt.BootstrapFewShot(metric=router_accuracy_metric) compiled_router = teleprompter.compile(QueryRouter(), trainset=router_train_data)
dspy.Predict or dspy.ChainOfThought) replace manual template strings.Claude's long context allows DSPy to bootstrap larger few-shot contexts with extensive reasoning paths. Ensure your DSPy signatures translate well to Claude's XML preference.