<- all tokdocs

DSPy and GEPA: Stop Writing Prompts Manually and Let the Framework Optimize Them

Watch on TikTok

View on TikTok ->

Author: helloahad_

Every time you switch models or tweak your AI pipeline, you have to go back and manually rewrite prompts from scratch. DSPy is a Python framework that eliminates that cycle entirely. Instead of crafting prompt strings, you define what your pipeline should do in code -- inputs, outputs, logic -- and DSPy automatically generates the best prompt to make it happen.

DSPy: Pipelines in Code, Not Prompt Strings

DSPy flips the standard approach to working with LLMs. Rather than spending time on prompt wording, you write a program that declares what the pipeline should accomplish. The framework handles prompt generation behind the scenes. When you build RAG pipelines or agentic flows with DSPy, you write the structure once. Switching models no longer breaks everything because the framework regenerates appropriate prompts for whatever model you target.

DSPy diagram showing "Define pipeline in code, not prompt strings" with a code snippet transforming into a system prompt

The practical benefit is portability. A pipeline built with DSPy can move from one model to another without manual prompt rewriting. The structure and intent stay the same; only the generated prompts change.

DSPy Optimizers: Automatic Prompt Improvement

DSPy includes a feature called optimizers -- algorithms that automatically improve prompts by testing variations against your data. You provide examples of correct behavior, and the optimizer iterates on the prompt until it performs well on those examples. No manual tuning required.

DSPy Optimizers slide showing "Automatically improve prompts" and "Test against real data" with a feedback loop diagram

The concept is similar to hyperparameter tuning in traditional ML, but applied to prompt engineering. The optimizer runs experiments, measures results, and converges on better prompts through systematic testing rather than human intuition.

GEPA: The Smart Optimizer

The video highlights GEPA as the most impressive optimizer currently available in DSPy. What sets GEPA apart from other prompt optimization approaches is that it does not blindly try random prompt variations. Instead, it uses a second, more powerful LLM to reflect on what went wrong with a failing prompt. That second model reads the failure case, diagnoses the issue in plain English, and rewrites the prompt with that understanding baked in.

The result is better prompts in far fewer iterations compared to reinforcement learning approaches, which rely on trial and error without understanding why something failed. GEPA's reflect-then-rewrite loop is more sample-efficient because each iteration carries forward a diagnosis, not just a signal.

The speaker presenting the title "Stop Writing Prompts Manually -- Let AI Do It"

Key Takeaways

  • DSPy lets you define LLM pipelines as code structures rather than prompt strings, making them portable across models
  • DSPy optimizers automatically improve prompts by testing variations against your data
  • GEPA is an optimizer that uses a second LLM to diagnose prompt failures and rewrite with that understanding, producing better results in fewer attempts than RL-based approaches
  • The framework eliminates the prompt rewriting cycle that happens every time you switch models or modify your pipeline

Published April 27, 2026. Writeup generated from a favorited TikTok.