Self-Improving Prompt Systems
Feedback → critique → rewrite loops that optimize prompts automatically.
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]All lessons in this course
- What Is Meta-Prompting?
- Prompts That Generate Prompts
- Self-Improving Prompt Systems
- Evaluation and Selection in Self-Improvement