skills/ai-making-consistent/SKILL.md
Make your AI give the same answer every time. Use when AI gives different answers to the same question, outputs are unpredictable, responses vary between runs, you need deterministic AI behavior, or your AI is unreliable. Also used for same input gives different output every time, prompt sensitivity causes output changes with minor wording tweaks, reordering examples shifts accuracy dramatically, same prompt gives different results every run, AI is non-deterministic, need reproducible AI results, LLM output keeps changing, how to make LLM deterministic, consistent JSON from LLM, reduce output variance, AI flaky in production, stable AI outputs for production.
npx skillsauth add lebsral/dspy-programming-not-prompting-lms-skills ai-making-consistentInstall 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 making their AI give reliable, predictable outputs. This is different from "wrong answers" — the AI might be right 80% of the time but unpredictably different each run.
If the AI is consistent but wrong, use /ai-improving-accuracy instead — consistency without accuracy just means reliably wrong.
Ask the user:
import dspy
lm = dspy.LM("openai/gpt-4o-mini", temperature=0) # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
results = []
for i in range(5):
result = my_program(question="What is the capital of France?")
results.append(result.answer)
print(f"Run {i+1}: {result.answer}")
# Check consistency
unique = set(results)
print(f"\n{len(unique)} unique answers out of 5 runs")
If outputs vary, apply the fixes below in order — each one adds a layer of consistency.
| Technique | Fixes | Effort | Impact |
|-----------|-------|--------|--------|
| temperature=0 | Random sampling variation | One line | High — fixes most issues |
| Literal types | Category string variation ("positive" vs "Positive") | Signature change | High for classification |
| Pydantic models | Structural format variation | Model definition | Medium for complex outputs |
| dspy.Refine | Length, format, content drift | Reward function | Medium — catches edge cases |
| BootstrapFewShot | Style and pattern variation | Optimization run | High — teaches consistent patterns |
| Caching | Identical input re-runs | Enabled by default | Perfect for repeated inputs |
The single biggest consistency fix. Temperature controls randomness:
lm = dspy.LM("openai/gpt-4o-mini", temperature=0) # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
Some providers may still have slight variation at temperature=0 due to floating point non-determinism, but it is minimal.
from typing import Literal
class Classify(dspy.Signature):
"""Classify the text."""
text: str = dspy.InputField()
# BAD: label: str — AI can say "positive", "Positive", "pos", "POSITIVE", etc.
# GOOD: locked to exact values
label: Literal["positive", "negative", "neutral"] = dspy.OutputField()
from pydantic import BaseModel, Field
class StructuredOutput(BaseModel):
category: str
confidence: float = Field(ge=0.0, le=1.0)
tags: list[str]
class MySignature(dspy.Signature):
"""Process the input."""
text: str = dspy.InputField()
result: StructuredOutput = dspy.OutputField()
class ConsistentResponder(dspy.Module):
def __init__(self):
self.respond = dspy.ChainOfThought(MySignature)
def forward(self, text):
return self.respond(text=text)
def consistency_reward(args, pred):
score = 1.0
# Hard constraint — large penalty if violated
if len(pred.answer) >= 200:
score -= 0.8
# Soft constraint — small penalty if not met
if not pred.answer.endswith("."):
score -= 0.1
return max(score, 0.0)
validated = dspy.Refine(
module=ConsistentResponder(),
N=3,
reward_fn=consistency_reward,
threshold=0.9,
)
Optimization teaches consistent patterns through few-shot examples:
optimizer = dspy.BootstrapFewShot(
metric=consistency_metric,
max_bootstrapped_demos=4,
)
optimized = optimizer.compile(my_program, trainset=trainset)
For best consistency, make your metric penalize inconsistency:
def consistency_metric(example, prediction, trace=None):
correct = prediction.answer.lower().strip() == example.answer.lower().strip()
right_length = 5 <= len(prediction.answer.split()) <= 30
no_hedging = not any(w in prediction.answer.lower() for w in ["maybe", "perhaps"])
return correct and right_length and no_hedging
DSPy caches LM calls by default (in-memory + on-disk). For identical inputs, you always get the same output:
# First call — hits the API
result1 = my_program(question="What is Python?")
# Second call with same input — returns cached result (instant, identical)
result2 = my_program(question="What is Python?")
# Disable caching if needed
dspy.configure_cache(enable_disk_cache=False, enable_memory_cache=False)
After applying fixes, measure improvement:
from collections import Counter
def measure_consistency(program, input_data, n_runs=10):
"""Run the same inputs multiple times and measure output stability."""
results = []
for _ in range(n_runs):
result = program(**input_data)
results.append(str(result.answer).strip().lower())
counts = Counter(results)
most_common_count = counts.most_common(1)[0][1]
consistency_rate = most_common_count / n_runs
print(f"Consistency: {consistency_rate:.0%} ({len(counts)} unique answers in {n_runs} runs)")
return consistency_rate
# Before fixes: ~60% consistency
# After temperature=0: ~95% consistency
# After temperature=0 + Literal types: ~99% consistency
temperature=0 but forgets the LM already exists. If dspy.configure(lm=lm) was called earlier in the code with a different LM, setting temperature on a new LM does not affect existing modules. Always reconfigure after changing the LM.dspy.Refine retries burn tokens. When the reward threshold is not met, dspy.Refine retries up to N times. For high-volume classification, a poorly tuned threshold can multiply your API costs by N. Monitor token usage after adding Refine wrappers.dspy.configure_cache(enable_disk_cache=False, enable_memory_cache=False).Literal types only work with static values, not runtime lists. Claude writes Literal[my_list] which fails. For dynamic categories from a database or config, use Literal[tuple(categories)] to convert at definition time.Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/ai-improving-accuracy/ai-fixing-errors/ai-following-rules/dspy-signatures/dspy-chain-of-thought/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.