AI Development

DSPy: Build and Optimize Agentic AI Apps Using Declarative Programming Techniques

Discover how DSPy revolutionizes AI app development by shifting from manual prompt engineering to automated optimization. Learn to create robust agentic systems with this hands-on course from deeplearning.ai.

J

Jennifer Yu

Workflow Automation Specialist

December 29, 2025 min read
Share:

Introduction to DSPy for Agentic Applications

DSPy represents a paradigm shift in how developers construct and refine applications powered by large language models (LLMs). Unlike traditional approaches that rely heavily on hand-crafted prompts and iterative trial-and-error, DSPy introduces a declarative programming framework. This means you define what your AI program should accomplish—such as retrieving information or reasoning step-by-step—while DSPy automatically compiles and optimizes the underlying prompts and pipelines.

In this short course offered by deeplearning.ai, you'll gain practical skills to build agentic apps: autonomous systems that can plan, act, and self-improve. By the end, you'll understand how to leverage DSPy to create reliable, high-performing AI agents that outperform brittle prompt chains. This is particularly valuable in real-world scenarios where LLMs face diverse inputs and require consistent results.

For instance, consider a traditional setup where you manually tweak prompts for a question-answering bot. With DSPy, you declare a simple module like a retriever-reader, provide examples, and let the framework optimize it via techniques like bootstrapping or Bayesian search. The DSPy GitHub repository serves as your primary resource, packed with examples and the latest updates.

Key Learning Outcomes

Participants in this course will master several core concepts that form the backbone of modern agentic AI development:

  • Signatures: Define input-output behaviors declaratively, e.g., question -> answer, abstracting away prompt details.
  • Modules: Reusable components like ChainOfThought or ReAct that implement standard agent patterns.
  • Teleprompters: Optimization engines that tune your programs using metrics, demonstrations, and search algorithms.
  • Datasets and Metrics: Curate data and define success criteria to guide automatic compilation.
  • Multi-Module Programs: Compose complex agents, such as those integrating retrieval-augmented generation (RAG) with planning.
  • Bootstrap Few-Shot Learning: Generate high-quality demonstrations on-the-fly to enhance few-shot performance.

These elements enable a systematic approach, contrasting sharply with imperative prompting. In a comparison:

Traditional PromptingDSPy Declarative Approach
Manual prompt writing and A/B testingAutomatic optimization via teleprompters
Brittle to LLM changesRobust pipelines that self-tune
Hard to scaleModular, composable programs

Detailed Syllabus Breakdown

The course is structured into seven concise lessons, each building progressively with hands-on coding exercises. Expect 2-3 hours total, making it accessible for busy developers.

Lesson 1: From Chains to Programs

Start by contrasting basic LLM chains with DSPy programs. Chains chain calls sequentially, but programs treat the entire flow as a compilable unit.

Key insight: DSPy uses LM clients seamlessly. Set up like this:

import dspy

turbo = dspy.OpenAI(model='gpt-3.5-turbo')
dspy.settings.configure(lm=turbo)

# Declare a simple signature
class BasicQA(dspy.Signature):
    """Answer questions with short factoid answers."""
    question: str = dspy.InputField()
    answer: str = dspy.OutputField(desc="often between 1 and 5 words")

# Use as a module
qa = dspy.ChainOfThought(BasicQA)

This declarative style scales to agents, adding context on why it reduces hallucination risks compared to raw prompts.

Lesson 2: Optimizing DSPy Programs

Dive into compilation: provide a metric (e.g., exact match accuracy) and few-shot examples, then let a teleprompter optimize.

Example metric:

 def validate_answers(example, pred, trace=None):
    return example.answer.lower().strip() == pred.answer.lower().strip()

# Compile
from dspy.teleprompt import BootstrapFewShot

tp = BootstrapFewShot(metric=validate_answers)
compiled_qa = tp.compile(qa, trainset=trainset)

Compare: Manual few-shot selection vs. DSPy's bootstrap, which generates demos via self-improvement, often boosting accuracy by 10-20%.

Lesson 3: Retrieval-Augmented DSPy Programs

Enhance with ColBERTv2 retriever for RAG pipelines. Declare Retrieve and Predict modules:

class GenerateAnswer(dspy.Signature):
    """Answer questions with short factoid answers."""
    context: list[str] = dspy.InputField(desc="may contain relevant facts")
    question: str = dspy.InputField()
    answer: str = dspy.OutputField(desc="often between 1 and 5 words")

rm = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')
class RAG(dspy.Module):
    def __init__(self, num_passages=3):
        super().__init__()
        self.retrieve = dspy.Retrieve(rm, k=num_passages)
        self.generate_answer = dspy.ChainOfThought(GenerateAnswer)
    def forward(self, question):
        context = self.retrieve(question).passages
        prediction = self.generate_answer(context=context, question=question)
        return dspy.Prediction(answer=prediction.answer)

Real-world application: Knowledge bases where retrieval grounds responses, outperforming naive chains.

Lesson 4: Self-Improving Agents

Build acting agents with ReAct module for tool use and planning. Optimization via bootstrapping creates few-shot examples automatically.

Breakdown: Agents decompose tasks (plan), execute (act), observe, and repeat—DSPy optimizes the entire loop.

Lesson 5: Bigger is Better

Scale to three-module agents: decompose, retrieve sub-questions, answer each. Compare to single-module baselines, showing compounding gains.

class Agent(dspy.Module):  # Multi-module
    def __init__(self):
        super().__init__()
        self.decompose = ...
        self.retrieve = ...
        self.answer = ...

Lesson 6: When to Use DSPy?

Guidance: Ideal for complex pipelines needing optimization; less for one-off prompts. Pros: Systematic tuning; Cons: Requires metric definition.

Lesson 7: Advanced Topics and Next Steps

Explore Bayesian optimization, multi-LM support, custom modules. Extend to production with the DSPy GitHub.

Meet the Instructors

  • Omar Khattab: Stanford NLP group, DSPy creator, focuses on LLM programming.
  • Krista Opsahl-Ong: Honeycomb, applies DSPy to production AI.
  • Aparna Dhinakaran: Honeycomb, expert in observability for AI systems.
  • Nate Durr: Stanford CRFM, benchmarks LLMs.
  • Michael Ryan: Stanford NLP, contributes to DSPy evolution.

Their combined expertise ensures actionable insights from research to deployment.

Prerequisites and Accessibility

Basic Python proficiency and familiarity with ML/LLMs suffice. No advanced math required—focus is practical coding. Access free via deeplearning.ai platform.

Why DSPy Stands Out: A Deeper Comparison

Traditional vs. DSPy:

  • Development Speed: DSPy cuts iteration time by automating prompt design.
  • Reliability: Optimized programs maintain performance across LLM versions.
  • Scalability: Modular design suits enterprise agents.

Real-world example: Optimizing HotPotQA baselines yields state-of-the-art results with minimal effort. Start experimenting today via the GitHub repo for immediate impact in your projects.


<div style="text-align: center; margin-top: 2rem;"> <a href="https://www.deeplearning.ai/short-courses/dspy-build-optimize-agentic-apps/" target="_blank" rel="noopener noreferrer" class="view-full-resource-btn" style="display: inline-block; background-color: #f97316; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600; transition: background-color 0.2s;">View Full Resource</a> </div>
The #1 Newsletter in AI

Stay ahead of the AI curve

The most important updates, news, and content — delivered in one weekly newsletter.

No spam. Unsubscribe anytime. Privacy policy

DSPy
AI Agents
deeplearning.ai
Prompt Optimization
LLM Frameworks
ai-agents
J

About Jennifer Yu

Workflow Automation Specialist

Jennifer covers workflow strategy, no-code platforms, and clear implementation guidance for teams adopting automation.

Comments (0)