DSPy — Programming (Not Prompting) LLMs

You are an expert in DSPy, the Stanford framework that replaces prompt engineering with programming. You help developers define LLM tasks as typed signatures, compose them into modules, and automatically optimize prompts/few-shot examples using teleprompters — so instead of manually crafting prompts, you write Python code and DSPy finds the best prompts for your task.

25 stars

Best use case

DSPy — Programming (Not Prompting) LLMs is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

You are an expert in DSPy, the Stanford framework that replaces prompt engineering with programming. You help developers define LLM tasks as typed signatures, compose them into modules, and automatically optimize prompts/few-shot examples using teleprompters — so instead of manually crafting prompts, you write Python code and DSPy finds the best prompts for your task.

Teams using DSPy — Programming (Not Prompting) LLMs should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/dspy/SKILL.md --create-dirs "https://raw.githubusercontent.com/ComeOnOliver/skillshub/main/skills/TerminalSkills/skills/dspy/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/dspy/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How DSPy — Programming (Not Prompting) LLMs Compares

Feature / AgentDSPy — Programming (Not Prompting) LLMsStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

You are an expert in DSPy, the Stanford framework that replaces prompt engineering with programming. You help developers define LLM tasks as typed signatures, compose them into modules, and automatically optimize prompts/few-shot examples using teleprompters — so instead of manually crafting prompts, you write Python code and DSPy finds the best prompts for your task.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# DSPy — Programming (Not Prompting) LLMs

You are an expert in DSPy, the Stanford framework that replaces prompt engineering with programming. You help developers define LLM tasks as typed signatures, compose them into modules, and automatically optimize prompts/few-shot examples using teleprompters — so instead of manually crafting prompts, you write Python code and DSPy finds the best prompts for your task.

## Core Capabilities

### Signatures and Modules

```python
import dspy

lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)

# Define task as a signature (not a prompt)
class SentimentAnalysis(dspy.Signature):
    """Classify the sentiment of a review."""
    review: str = dspy.InputField()
    sentiment: str = dspy.OutputField(desc="positive, negative, or neutral")
    confidence: float = dspy.OutputField(desc="0.0 to 1.0")

# Use it
classify = dspy.Predict(SentimentAnalysis)
result = classify(review="Great product, fast shipping!")
print(result.sentiment)     # "positive"
print(result.confidence)    # 0.95

# Chain of Thought (automatic reasoning)
classify_cot = dspy.ChainOfThought(SentimentAnalysis)
result = classify_cot(review="It works but the manual is confusing")
print(result.reasoning)     # Shows step-by-step reasoning
print(result.sentiment)     # "neutral"
```

### Composable Modules

```python
class RAGModule(dspy.Module):
    def __init__(self, num_passages=3):
        self.retrieve = dspy.Retrieve(k=num_passages)
        self.generate = dspy.ChainOfThought("context, question -> answer")

    def forward(self, question):
        context = self.retrieve(question).passages
        return self.generate(context=context, question=question)

rag = RAGModule()
answer = rag(question="What is DSPy?")

# Multi-hop reasoning
class MultiHop(dspy.Module):
    def __init__(self):
        self.generate_query = dspy.ChainOfThought("context, question -> search_query")
        self.retrieve = dspy.Retrieve(k=3)
        self.generate_answer = dspy.ChainOfThought("context, question -> answer")

    def forward(self, question):
        context = []
        for _ in range(2):  # 2 hops
            query = self.generate_query(context=context, question=question).search_query
            passages = self.retrieve(query).passages
            context = deduplicate(context + passages)
        return self.generate_answer(context=context, question=question)
```

### Automatic Optimization

```python
from dspy.teleprompt import BootstrapFewShot

# Training data
trainset = [
    dspy.Example(question="What is Python?", answer="A programming language").with_inputs("question"),
    dspy.Example(question="Who created Linux?", answer="Linus Torvalds").with_inputs("question"),
]

# Metric
def accuracy(example, prediction, trace=None):
    return example.answer.lower() in prediction.answer.lower()

# Optimize — finds best few-shot examples and instructions
teleprompter = BootstrapFewShot(metric=accuracy, max_bootstrapped_demos=4)
optimized_rag = teleprompter.compile(RAGModule(), trainset=trainset)

# optimized_rag now has automatically selected few-shot examples
# that maximize accuracy — no manual prompt engineering
```

## Installation

```bash
pip install dspy
```

## Best Practices

1. **Signatures over prompts** — Define typed inputs/outputs; DSPy generates and optimizes prompts automatically
2. **ChainOfThought** — Use for complex tasks; adds reasoning step that improves accuracy significantly
3. **Modules** — Compose LLM calls like neural network layers; chain retrieval + reasoning + generation
4. **Teleprompters** — Use BootstrapFewShot to automatically find optimal few-shot examples from training data
5. **Typed outputs** — OutputField descriptions constrain generation; more reliable than free-form prompts
6. **Evaluation-driven** — Define metrics first, then optimize; DSPy finds prompts that maximize your metric
7. **Model-agnostic** — Same code works with GPT-4, Claude, Llama, Gemini; optimization adapts per model
8. **Assertions** — Use `dspy.Assert` and `dspy.Suggest` for runtime output validation and self-correction

Related Skills

update-llms

25
from ComeOnOliver/skillshub

Update the llms.txt file in the root folder to reflect changes in documentation or specifications following the llms.txt specification at https://llmstxt.org/

remember-interactive-programming

25
from ComeOnOliver/skillshub

A micro-prompt that reminds the agent that it is an interactive programmer. Works great in Clojure when Copilot has access to the REPL (probably via Backseat Driver). Will work with any system that has a live REPL that the agent can use. Adapt the prompt with any specific reminders in your workflow and/or workspace.

create-llms

25
from ComeOnOliver/skillshub

Create an llms.txt file from scratch based on repository structure following the llms.txt specification at https://llmstxt.org/

systems-programming-rust-project

25
from ComeOnOliver/skillshub

You are a Rust project architecture expert specializing in scaffolding production-ready Rust applications. Generate complete project structures with cargo tooling, proper module organization, testing

shader-programming-glsl

25
from ComeOnOliver/skillshub

Expert guide for writing efficient GLSL shaders (Vertex/Fragment) for web and game engines, covering syntax, uniforms, and common effects.

video-prompting-guide

25
from ComeOnOliver/skillshub

Best practices and techniques for writing effective AI video generation prompts. Covers: Veo, Seedance, Wan, Grok, Kling, Runway, Pika, Sora prompting strategies. Learn: shot types, camera movements, lighting, pacing, style keywords, negative prompts. Use for: improving video quality, getting consistent results, professional video prompts. Triggers: video prompt, how to prompt video, veo prompts, video generation tips, better ai video, video prompt engineering, video prompt guide, video prompt template, ai video tips, video prompt best practices, video prompt examples, cinematography prompts

pair-programming

25
from ComeOnOliver/skillshub

AI-assisted pair programming with multiple modes (driver/navigator/switch), real-time verification, quality monitoring, and comprehensive testing. Supports TDD, debugging, refactoring, and learning sessions. Features automatic role switching, continuous code review, security scanning, and performance optimization with truth-score verification.

prompting

25
from ComeOnOliver/skillshub

Prompt engineering standards and context engineering principles for AI agents based on Anthropic best practices. Covers clarity, structure, progressive discovery, and optimization for signal-to-noise ratio.

Zig — Modern Systems Programming Language

25
from ComeOnOliver/skillshub

## Overview

Ollama — Run LLMs Locally

25
from ComeOnOliver/skillshub

You are an expert in Ollama, the tool for running open-source LLMs locally. You help developers run Llama, Mistral, Gemma, Phi, CodeLlama, and other models on their machine with a simple CLI and REST API — enabling private AI development, offline inference, fine-tuning experiments, and cost-free prototyping without sending data to cloud APIs.

GitHub Copilot — AI Pair Programming

25
from ComeOnOliver/skillshub

GitHub Copilot sits in your editor and writes code alongside you. It reads your current file, open tabs, and surrounding context to suggest completions that range from finishing a single line to generating entire functions. Beyond completions, Copilot Chat lets you ask questions, explain code, generate tests, and refactor — all without leaving your editor.

Aider — AI Pair Programming in Your Terminal

25
from ComeOnOliver/skillshub

You are an expert in Aider, the terminal-based AI coding assistant that reads your codebase, makes changes across multiple files, and creates proper git commits. You help developers use Aider for autonomous code generation, refactoring, bug fixing, and test writing — working with any LLM (Claude, GPT-4, Gemini, local models) while respecting project conventions and maintaining git history.