DSPy, Concept by Concept

Programming language models instead of prompting them — signatures, modules, composed programs, metrics and evaluation, optimizers that compile your prompts, and building RAG and agents in DSPy that improve themselves against a metric.

8 parts · written by Pratik Dhanave. Start with Part 1 →

← All series · All posts

Part 1 · ·5 min read

DSPy: Programming, Not Prompting

Hand-tuned prompt strings are brittle, model-specific, and impossible to systematically improve — DSPy replaces them with declarative modules and an optimizer that writes the prompts for you.

Hand-tuned prompt strings are brittle, model-specific, and impossible to improve systematically. DSPy replaces them with declarative modules and an optimizer that writes the prompts for you.

Part 2 · ·5 min read

Signatures: Declaring What, Not How

A DSPy signature is the contract that says what goes in and what comes out of a step — and by declaring the transformation instead of writing the prompt, it becomes something the optimizer can improve.

A DSPy signature declares what goes in and what comes out of a step — and by declaring the transformation instead of writing the prompt, it becomes something the optimizer can improve.

Part 3 · ·5 min read

Modules: Strategies for Calling a Model

If a signature says what a step does, a module says how to get the model to do it — and because modules are parameterized, swapping one for another changes the reasoning strategy without touching your intent.

If a signature says what a step does, a module says how to get the model to do it — Predict, ChainOfThought, ReAct — and because modules are parameterized, swapping one changes the strategy without touching your intent.

Part 4 · ·4 min read

Composing Programs

Real tasks are pipelines, not single calls, and in DSPy you build them the way you build a neural network — subclass a base module, declare sub-modules, and wire them together in a forward method.

Real tasks are pipelines, not single calls. In DSPy you build them like a neural network — subclass a base module, declare sub-modules, and wire them together in a forward method.

Part 5 · ·5 min read

Metrics and Evaluation

DSPy's optimizer improves whatever your metric rewards, which makes the metric the single most consequential thing you write — get it right and compilation makes your program better; get it wrong and it optimizes confidently toward the wrong target.

DSPy's optimizer improves whatever your metric rewards, which makes the metric the single most consequential thing you write — get it right and compilation makes your program better; get it wrong and it optimizes toward the wrong target.

Part 6 · ·5 min read

Optimizers: Letting DSPy Write Your Prompts

This is the payoff of the whole framework: you hand an optimizer your program, your metric, and some examples, and it searches for the instructions and demonstrations that make the program measurably better — the prompts you never had to write.

The payoff of the framework: hand an optimizer your program, metric, and examples, and it searches for the instructions and demonstrations that make the program measurably better — the prompts you never had to write.

Part 7 · ·5 min read

Building RAG and Agents in DSPy

The two systems everyone builds — retrieval-augmented generation and tool-using agents — are where DSPy's declarative approach pays off most, because they are exactly the multi-step pipelines whose prompts are hardest to tune by hand.

RAG and tool-using agents are where DSPy's declarative approach pays off most — they are exactly the multi-step pipelines whose prompts are hardest to tune by hand.

Part 8 · ·5 min read

DSPy in Production

A compiled DSPy program is an artifact — you optimize it once, save it, and serve it — which turns the framework's compile step into an ordinary part of a build pipeline rather than something that runs on every request.

A compiled DSPy program is an artifact — optimize once, save, and serve — which turns compilation into a build step, and makes a model swap a recompile rather than a prompt rewrite.

This series is part of a larger body of work by Pratik Dhanave, an Agentic AI Architect writing about production AI systems, distributed systems, and cloud-native engineering. Explore all course series, browse every post, or find topics via the tag index.