skills/dspy-chain-of-thought/SKILL.md
Use when the task benefits from intermediate reasoning before producing an answer — multi-step logic, analysis, math, or complex classification where direct prediction fails. Common scenarios - classification tasks where the model needs to reason about edge cases, math word problems, multi-step analysis, complex question answering, legal or medical reasoning, any task where thinking before answering improves quality. Related - ai-reasoning, dspy-predict, dspy-multi-chain-comparison. Also used for dspy.ChainOfThought, CoT prompting in DSPy, think step by step, show your reasoning, intermediate reasoning steps, LLM gives wrong answer without thinking, reasoning before output, make AI explain its logic, step-by-step problem solving, when to use ChainOfThought vs Predict, add reasoning to any DSPy module, let the model think, chain of thought for classification.
npx skillsauth add lebsral/dspy-programming-not-prompting-lms-skills dspy-chain-of-thoughtInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Guide the user through using DSPy's ChainOfThought module -- the go-to module for tasks that benefit from intermediate reasoning before producing an answer.
dspy.ChainOfThought is a drop-in replacement for dspy.Predict that automatically injects a reasoning field before your output fields. Same signature, one-word swap -- the LM reasons step-by-step before answering. The reasoning field is always available on the result even though your signature doesn't declare it.
ChainOfThought improves accuracy when the task requires the LM to work through intermediate steps before arriving at an answer:
Rule of thumb: If a human would need to think through the problem before answering, use ChainOfThought.
ChainOfThought adds latency and token cost because the LM generates extra reasoning text. Skip it when:
Predict is enough.Predict.When in doubt, start with ChainOfThought and switch to Predict later if profiling shows the reasoning is unnecessary.
class ReviewDecision(dspy.Module):
def __init__(self):
self.analyze = dspy.ChainOfThought("application -> decision: str, risk_level: str")
self.summarize = dspy.Predict("decision, reasoning -> summary")
def forward(self, application):
analysis = self.analyze(application=application)
# Pass the reasoning to the next step
summary = self.summarize(
decision=analysis.decision,
reasoning=analysis.reasoning,
)
return dspy.Prediction(
decision=analysis.decision,
risk_level=analysis.risk_level,
reasoning=analysis.reasoning,
summary=summary.summary,
)
| | dspy.Predict | dspy.ChainOfThought |
|---|---|---|
| Output fields | Only what the signature declares | Signature fields + reasoning |
| Latency | Lower | Higher (generates reasoning tokens) |
| Cost | Lower | Higher (more output tokens) |
| Accuracy on complex tasks | Lower | Higher |
| Accuracy on simple tasks | Same | Same (but wastes tokens) |
| Best for | Lookups, extraction, simple classification | Analysis, judgment, multi-step problems |
ChainOfThought works with all the same type constraints as Predict -- Literal, int, float, bool, list[str], and Pydantic models.
import dspy
from pydantic import BaseModel
from typing import Literal
lm = dspy.LM("openai/gpt-4o-mini") # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
class RiskAssessment(BaseModel):
risk_score: float
factors: list[str]
recommendation: Literal["approve", "review", "deny"]
class AssessRisk(dspy.Signature):
"""Assess the risk level of a financial transaction."""
transaction_details: str = dspy.InputField()
assessment: RiskAssessment = dspy.OutputField()
assessor = dspy.ChainOfThought(AssessRisk)
result = assessor(
transaction_details="Wire transfer of $50,000 to a new recipient in a high-risk jurisdiction"
)
print(result.reasoning) # detailed risk analysis
print(result.assessment.risk_score) # 0.85
print(result.assessment.factors) # ["high amount", "new recipient", "high-risk jurisdiction"]
print(result.assessment.recommendation) # "review"
The LM reasons through the problem first, then produces the structured output. The reasoning happens before type enforcement, so the LM has space to think before committing to typed fields.
ChainOfThought benefits significantly from optimization. When you run an optimizer, DSPy discovers high-quality reasoning traces and uses them as few-shot demonstrations:
import dspy
from dspy.evaluate import Evaluate
lm = dspy.LM("openai/gpt-4o-mini") # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
# Your CoT module
classifier = dspy.ChainOfThought("ticket_text -> priority: str, team: str")
# Training data
trainset = [
dspy.Example(
ticket_text="Site is down for all users",
priority="critical",
team="infrastructure",
).with_inputs("ticket_text"),
dspy.Example(
ticket_text="Typo on the pricing page",
priority="low",
team="content",
).with_inputs("ticket_text"),
# ... more examples
]
# Metric
def ticket_metric(example, prediction, trace=None):
priority_correct = prediction.priority == example.priority
team_correct = prediction.team == example.team
return priority_correct + team_correct
# Optimize -- the optimizer generates and selects good reasoning traces
optimizer = dspy.BootstrapFewShot(metric=ticket_metric, max_bootstrapped_demos=4)
optimized = optimizer.compile(classifier, trainset=trainset)
# The optimized program now includes few-shot examples with reasoning
result = optimized(ticket_text="Users can't upload files larger than 10MB")
print(result.reasoning) # higher quality reasoning, guided by learned demos
print(result.priority)
print(result.team)
# Save for production
optimized.save("ticket_classifier.json")
What optimization does for ChainOfThought:
BootstrapFewShot, MIPROv2, BootstrapFewShotWithRandomSearch all support CoT modulesChainOfThought is a sub-module like any other. Use it in dspy.Module for multi-step pipelines:
import dspy
class CodeReviewer(dspy.Module):
def __init__(self):
self.find_issues = dspy.ChainOfThought("code -> issues: list[str], severity: str")
self.suggest_fix = dspy.ChainOfThought("code, issues -> fixed_code: str")
def forward(self, code):
analysis = self.find_issues(code=code)
if analysis.severity == "none":
return dspy.Prediction(
issues=[],
severity="none",
fixed_code=code,
reasoning=analysis.reasoning,
)
fix = self.suggest_fix(code=code, issues=analysis.issues)
return dspy.Prediction(
issues=analysis.issues,
severity=analysis.severity,
fixed_code=fix.fixed_code,
reasoning=analysis.reasoning,
)
Both sub-modules use CoT because code review and fix suggestion both benefit from step-by-step thinking. When optimized, DSPy tunes each sub-module's reasoning independently.
reasoning as an explicit output field in the signature. DSPy injects it automatically — declaring it yourself creates a duplicate field that confuses the prompt. Just use dspy.ChainOfThought("question -> answer") and access result.reasoning on the output.dspy.Predict instead — CoT adds cost without improving accuracy on straightforward tasks.max_tokens too low, truncating reasoning before output fields. The reasoning trace is generated before the actual output fields. If max_tokens is tight, the LM runs out of space mid-reasoning and never produces the output fields, causing parse failures. Leave headroom — at least 500 tokens beyond what you expect the output fields to need.reasoning is available on the result object. After calling a ChainOfThought module, the reasoning trace is always at result.reasoning even though the signature does not declare it. Claude sometimes re-derives reasoning or asks the LM to explain its answer in a separate call when the trace is already there.rationale_field to rename the reasoning field but does not update downstream references. dspy.ChainOfThought(sig, rationale_field=dspy.OutputField(prefix="Thinking:")) changes the prompt prefix, but the field is still accessed as result.reasoning on the output. Claude sometimes tries to access result.thinking or result.rationale after renaming, which fails silently (returns None).Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/dspy-predict/dspy-signatures/dspy-modules/ai-reasoning/ai-do if you do not have it — it routes any AI problem to the right skill and is the fastest way to work: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill ai-dotools
See what is happening during optimizer.compile() instead of waiting blind. Use when you want to watch optimization progress, see scores as they come in, know if your optimizer is working, check if optimization is stuck, understand why optimization is taking too long, get live progress during compile, monitor convergence, detect overfitting during optimization, interpret optimization results, or pick the right tool for watching optimization. Also used for optimizer progress bar, is my optimizer doing anything, optimization seems stuck, how long will optimization take, watch GEPA run, watch MIPROv2 run, live optimization dashboard, optimizer not improving, scores not going up, optimization taking forever, see what optimizer is doing, debug slow optimization, optimization visibility, optimizer metrics, track compile progress, optimization observability.
testing
Use when you want the highest-quality prompt optimization DSPy offers — jointly optimizes instructions and few-shot demos, with auto=light/medium/heavy presets. Common scenarios - you want the best possible accuracy from prompt optimization, jointly tuning instructions and few-shot demonstrations, using auto presets for different compute budgets, or when COPRO or BootstrapFewShot alone are not reaching your accuracy target. Related - ai-improving-accuracy, dspy-copro, dspy-bootstrap-few-shot. Also used for dspy.MIPROv2, best DSPy optimizer, highest quality optimization, auto=light medium heavy, joint instruction and demo optimization, most powerful prompt optimizer, MIPROv2 vs COPRO vs BootstrapFewShot, which optimizer should I use, state of the art prompt optimization, when to use MIPROv2, optimize both instructions and examples, heavy optimization for production, best optimizer for accuracy.
testing
Use LangWatch for DSPy auto-tracing and real-time optimizer progress. Use when you want to set up LangWatch, langwatch.dspy.init, auto-tracing DSPy, real-time optimization dashboard, optimizer progress tracking, app.langwatch.ai, or DSPy optimizer dashboard. Also used for langwatch setup, pip install langwatch, langwatch trace, optimizer progress, real-time optimization, watch optimizer run, LangWatch self-hosted, langwatch docker, langwatch vs langtrace, langwatch autotrack_dspy.
data-ai
Use when you want to optimize instructions without few-shot examples — a lightweight alternative to COPRO when you do not have or do not want to use demonstrations. Common scenarios - optimizing instructions when you do not have or do not want to use few-shot demonstrations, lightweight instruction search as a first step, tasks where examples in the prompt confuse the model, or when you want fast instruction optimization without the cost of COPRO. Related - ai-improving-accuracy, dspy-copro, dspy-miprov2. Also used for dspy.GEPA, instruction optimization without demos, lightweight prompt optimization, optimize instructions only, no few-shot examples needed, GEPA vs COPRO, quick instruction search, when demonstrations hurt performance, zero-shot optimization, instruction-only optimizer, simplest instruction tuner, fast prompt optimization, skip few-shot and just tune instructions, optimize Pydantic field descriptions, GEPA structured output, GEPA does not optimize field desc.