← All guides
Education Resource

What is DSPy?

The framework that treats prompting as a programming and optimization problem instead of hand-written strings.

Updated 13 June 2026 5 min read 3 sections
TL;DR

DSPy is an open-source framework that treats prompting as a programming and optimization problem: instead of hand-writing prompt strings, you declare what each step of an agent should do and a metric for success, and DSPy compiles and tunes the prompts against your data. It is one of the leading approaches to automated prompt optimization in the agent improvement loop.

How does DSPy optimize prompts?

You define signatures (the input/output contract of a step) and compose them into modules, then give DSPy a metric and a dataset. Its optimizers search over prompt phrasings, few-shot examples and chains to maximise that metric — effectively compiling your high-level program into the prompts that score best. The shift is from 'write the perfect prompt' to 'define the task and let the optimizer find the prompt', which scales far better than hand-tuning across a multi-step agent.

When should you use DSPy instead of hand-tuning?

Use it when the prompt search space is large, the agent has many steps, or you are re-tuning often as models change — places where manual iteration does not scale. Hand-tuning still wins for a quick fix or when you lack a dataset to optimize against, because DSPy is only as good as the metric and examples you give it. In practice teams hand-tune to a baseline, then bring in an optimizer once they have evals worth optimizing against.

How does DSPy fit the agent optimization loop?

DSPy is an optimizer, not the whole loop. It improves prompts against a metric — but you still need the eval dataset that defines the metric, and production monitoring to confirm the optimized prompts hold up on live traffic and do not regress. So DSPy slots into the 'change' step of observe → evaluate → optimize: evaluation produces the metric and the cases, DSPy proposes better prompts, and you re-evaluate to confirm the gain.

Optimize and evaluate agent prompts in production with Prefactor

Prefactor gives enterprises runtime governance, observability, and control over every AI agent in production.

Book a demo →

Ready to control your agents?

Maintain visibility and control across agents, frameworks, and AI providers. Prefactor helps teams monitor activity, enforce boundaries, and manage operational risk.