Self-Improving Prompt Systems
Feedback → critique → rewrite loops that optimize prompts automatically.
Self-Improving Prompt Systems is a free AI Prompt Engineering lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Self-Improvement Loop
A self-improving prompt system creates a feedback loop: run the prompt on test cases, evaluate the outputs, critique the prompt, rewrite it, and repeat. Each iteration should produce measurably better results. This is automated prompt optimization without human intervention in each loop.
Loop Architecture Overview
The self-improvement loop has five components: Executor (runs the prompt), Evaluator (scores outputs), Critic (identifies prompt weaknesses), Rewriter (improves the prompt), and History (tracks all iterations). Each is implemented as an LLM call.
import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
class SelfImprovingPromptSystem:
def __init__(self, initial_prompt, test_cases, eval_fn, max_iterations=5):
self.current_prompt = initial_prompt
self.test_cases = test_cases
self.eval_fn = eval_fn
self.max_iterations = max_iterations
self.history = [] # [(iteration, prompt, score, critique)]
def run(self):
for i in range(self.max_iterations):
print(f'=== Iteration {i+1}/{self.max_iterations} ===')
score = self._evaluate_prompt()
print(f'Score: {score:.2f}')
self.history.append((i, self.current_prompt, score))
if score >= 0.95:
print('Target score reached. Stopping.')
break
critique = self._critique_prompt(score)
self.current_prompt = self._rewrite_prompt(critique)
return self.best_prompt()
def best_prompt(self):
return max(self.history, key=lambda x: x[2])[1]The Executor: Running the Prompt
The executor applies the current prompt to every test case and collects outputs. This is a standard LLM call loop — nothing special here, but tracking which test case produced which output is essential.
def _execute_prompt(self, prompt):
outputs = []
for case in self.test_cases:
response = client.messages.create(
model='claude-haiku-4-5', # use cheaper model for execution
max_tokens=500,
messages=[
{'role': 'user', 'content': prompt + '\n\nInput: ' + case['input']}
]
)
outputs.append({
'case_id': case['id'],
'input': case['input'],
'expected': case['expected'],
'actual': response.content[0].text
})
return outputs
# Bind method to class (demonstration)
SelfImprovingPromptSystem._execute = _execute_prompt
# Sample test cases
test_cases = [
{'id': 1, 'input': 'The meeting was cancelled.', 'expected': 'negative'},
{'id': 2, 'input': 'Great product, love it!', 'expected': 'positive'},
{'id': 3, 'input': 'It arrived on time.', 'expected': 'neutral'},
]
print(f'Test suite: {len(test_cases)} cases')The Evaluator: Scoring Outputs
The evaluator scores the executor's outputs against expected answers. It can use exact match, fuzzy match, or a separate LLM judge for open-ended tasks.
def _evaluate_prompt(self):
outputs = self._execute(self.current_prompt)
correct = 0
failed_cases = []
for out in outputs:
# Exact match for classification tasks
if out['expected'].lower() in out['actual'].lower():
correct += 1
else:
failed_cases.append(out)
score = correct / len(outputs)
self._last_failed_cases = failed_cases
return score
# For open-ended tasks: LLM-as-judge evaluator
JUDGE_PROMPT = '''Rate the quality of this AI response (1-5).
Task: {task_description}
Input: {input}
Expected approach: {expected}
Actual response: {actual}
Return only the integer score (1-5). No explanation.'''
def llm_judge_score(task_desc, input_text, expected, actual):
response = client.messages.create(
model='claude-haiku-4-5', max_tokens=5,
messages=[{'role': 'user', 'content':
JUDGE_PROMPT.format(
task_description=task_desc, input=input_text,
expected=expected, actual=actual
)}]
)
try:
return int(response.content[0].text.strip()) / 5.0
except ValueError:
return 0.5The Critic: Identifying Prompt Weaknesses
The critic analyzes failed cases and the current prompt to identify specific weaknesses. This is the key meta-prompting step — the model critiques its own prompt.
CRITIC_PROMPT = '''You are a prompt engineering expert analyzing why a prompt fails.
Current prompt:
{current_prompt}
Failed test cases (where the prompt gave wrong outputs):
{failed_cases}
For each failure, explain:
1. What went wrong in the output
2. Which part of the prompt caused or failed to prevent this
3. A specific, actionable fix
End with a prioritized list of the top 3 prompt improvements to make.
Be specific — quote the relevant prompt section and suggest the exact replacement.'''
def _critique_prompt(self, score):
failed_json = '\n'.join(
f'Input: {c["input"]}\nExpected: {c["expected"]}\nActual: {c["actual"]}'
for c in self._last_failed_cases[:5]
)
response = client.messages.create(
model='claude-opus-4-5', max_tokens=1000,
messages=[{'role': 'user', 'content':
CRITIC_PROMPT.format(
current_prompt=self.current_prompt,
failed_cases=failed_json
)}]
)
return response.content[0].textThe Rewriter: Improving the Prompt
The rewriter takes the critic's analysis and produces an improved version of the prompt. Key constraint: it should be a targeted improvement, not a complete rewrite.
REWRITER_PROMPT = '''You are a prompt engineer. Improve the prompt based on the critique below.
Current prompt:
{current_prompt}
Critique and suggested improvements:
{critique}
Rules for rewriting:
1. Make TARGETED changes based on the critique — do not rewrite everything
2. Keep all parts of the prompt that were working well
3. Apply all prioritized fixes from the critique
4. Do not add unnecessary verbosity — conciseness is a quality
5. Output ONLY the improved prompt, no explanation
Improved prompt:'''
def _rewrite_prompt(self, critique):
response = client.messages.create(
model='claude-opus-4-5', max_tokens=1000,
messages=[{'role': 'user', 'content':
REWRITER_PROMPT.format(
current_prompt=self.current_prompt,
critique=critique
)}]
)
new_prompt = response.content[0].text.strip()
print(f'Prompt updated. Length: {len(new_prompt)} chars '
f'(was {len(self.current_prompt)} chars)')
return new_promptFull Loop: Putting It Together
Here is the complete self-improvement loop with all components wired together and basic convergence detection.
def run_improvement_loop(initial_prompt, test_cases, max_iterations=5,
target_score=0.90):
history = []
current_prompt = initial_prompt
last_failed_cases = []
for i in range(max_iterations):
print(f'\n--- Iteration {i+1} ---')
# Execute
outputs = execute_prompt(current_prompt, test_cases)
# Evaluate
score, failed_cases = evaluate_outputs(outputs)
last_failed_cases = failed_cases
print(f'Score: {score:.2f} ({len(failed_cases)} failures)')
history.append({'iteration': i, 'prompt': current_prompt, 'score': score})
if score >= target_score:
print(f'Target score {target_score} reached!')
break
if not failed_cases:
print('No failures to learn from. Stopping.')
break
# Critique and rewrite
critique = critique_prompt(current_prompt, failed_cases)
current_prompt = rewrite_prompt(current_prompt, critique)
best = max(history, key=lambda x: x['score'])
print(f'\nBest prompt at iteration {best["iteration"]+1} with score {best["score"]:.2f}')
return best['prompt'], historyCost Management in Self-Improvement Loops
Self-improvement loops can be expensive — each iteration makes multiple API calls. Cost management strategies keep the loop affordable.
# Cost optimization strategies
# 1. Use cheap model for execution, expensive model for critique/rewrite
def execute_prompt(prompt, test_cases):
# Use cheapest capable model
model = 'claude-haiku-4-5'
# ... execute ...
pass
def critique_prompt(prompt, failed_cases):
# Use best model for reasoning about why the prompt fails
model = 'claude-opus-4-5'
# ... critique ...
pass
# 2. Limit test suite size (sample from larger set)
import random
def get_evaluation_sample(full_test_suite, sample_size=20):
if len(full_test_suite) <= sample_size:
return full_test_suite
return random.sample(full_test_suite, sample_size)
# 3. Early stopping: stop if score doesn't improve
def has_converged(history, patience=2, min_delta=0.02):
if len(history) < patience + 1:
return False
recent_scores = [h['score'] for h in history[-patience:]]
best_recent = max(recent_scores)
baseline = history[-(patience+1)]['score']
return (best_recent - baseline) < min_delta
print('Cost optimization: cheap model for execution, expensive for critique')Tracking Prompt Lineage
In a self-improvement loop, every prompt version should be traceable back to the failure analysis that triggered it. This lineage helps debug unexpected regressions and supports human review.
class PromptLineage:
def __init__(self):
self.lineage = []
def record(self, iteration, prompt, score, critique=None,
failed_case_ids=None):
self.lineage.append({
'iteration': iteration,
'prompt': prompt,
'score': score,
'critique_summary': critique[:100] if critique else None,
'failed_case_ids': failed_case_ids or [],
'prompt_length': len(prompt.split())
})
def print_history(self):
print('Prompt improvement history:')
for entry in self.lineage:
print(
f' Iter {entry["iteration"]}: '
f'score={entry["score"]:.2f} '
f'words={entry["prompt_length"]} '
f'failures={len(entry["failed_case_ids"])}'
)
def get_best(self):
return max(self.lineage, key=lambda x: x['score'])
lineage = PromptLineage()
lineage.record(0, 'Initial simple prompt', 0.60)
lineage.record(1, 'Improved with examples', 0.75, 'Missing edge cases')
lineage.record(2, 'Added edge case handling', 0.92, 'Minor format issue')
lineage.print_history()Prompt Version History Visualization
Visualizing the score progression across iterations helps identify whether the loop is converging and how quickly. A simple text chart makes trends immediately visible without a graphics library.
def visualize_improvement_history(history):
'''
history: list of (iteration, prompt, score)
Prints an ASCII chart of score progression.
'''
if not history:
print('No history to visualize.')
return
max_score = 1.0
bar_width = 40
print('\nScore Progression:')
print('-' * (bar_width + 20))
for iteration, prompt, score in history:
filled = int(score * bar_width)
bar = '#' * filled + '-' * (bar_width - filled)
marker = ' <-- BEST' if score == max(h[2] for h in history) else ''
print(f'Iter {iteration:2d}: [{bar}] {score:.3f}{marker}')
final_score = history[-1][2]
best_score = max(h[2] for h in history)
gain = best_score - history[0][2]
print('-' * (bar_width + 20))
print(f'Initial: {history[0][2]:.3f} -> Best: {best_score:.3f} (gain: +{gain:.3f})')
# Example
sample_history = [
(0, 'v0', 0.60),
(1, 'v1', 0.72),
(2, 'v2', 0.78),
(3, 'v3', 0.77),
(4, 'v4', 0.85)
]
visualize_improvement_history(sample_history)Regression Detection
A rewritten prompt may fix some failures while breaking previously passing cases — a regression. Always check for regressions after each iteration by comparing which specific test cases changed status.
def detect_regressions(old_outputs, new_outputs):
old_results = {o['case_id']: o['correct'] for o in old_outputs}
new_results = {o['case_id']: o['correct'] for o in new_outputs}
regressions = []
improvements = []
for case_id in old_results:
was_correct = old_results[case_id]
is_correct = new_results.get(case_id, False)
if was_correct and not is_correct:
regressions.append(case_id)
elif not was_correct and is_correct:
improvements.append(case_id)
print(f'Fixed: {len(improvements)} cases. Regressed: {len(regressions)} cases.')
if regressions:
print(f'WARNING: Regression on cases: {regressions}')
print('Consider reverting to previous prompt version.')
return regressions, improvements
# Example
old = [{'case_id': 1, 'correct': True}, {'case_id': 2, 'correct': False}]
new = [{'case_id': 1, 'correct': False}, {'case_id': 2, 'correct': True}]
detect_regressions(old, new) # Fixed: 1, Regressed: 1Quick Check
In a self-improving prompt loop, what is the purpose of the Critic component?
Self-Improving Prompt Systems Summary
Self-improving prompt systems automate the prompt optimization cycle:
- Loop structure: Execute → Evaluate → Critique → Rewrite → repeat
- Executor: runs prompt on test cases (use cheap model)
- Evaluator: scores outputs with exact match or LLM judge
- Critic: meta-prompt that identifies specific prompt weaknesses from failures
- Rewriter: targeted improvement using critique (use best model)
- Convergence: stop at target score or when score stops improving
- Regression detection: always check that improvements do not break passing cases
Frequently asked questions
Is the “Self-Improving Prompt Systems” lesson free?
Yes — the full text of “Self-Improving Prompt Systems” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Self-Improving Prompt Systems”?
Feedback → critique → rewrite loops that optimize prompts automatically. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Self-Improving Prompt Systems” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is Meta-Prompting?
- Prompts That Generate Prompts
- Self-Improving Prompt Systems
- Evaluation and Selection in Self-Improvement