Autonomous Response Quality Optimizer β A multi-agent system that generates, critiques, and iteratively improves its own output until a high-quality result is achieved.
This system demonstrates core AI concepts like self-reflection, iterative improvement, and autonomous quality optimization using a pipeline of three specialized agents:
User Prompt
β
βΌ
ββββββββββββββββ
β GENERATOR β β Creates initial response
ββββββββ¬ββββββββ
β
βΌ
ββββββββββββββββ
β CRITIC β β Evaluates quality (0β10) across 5 dimensions
ββββββββ¬ββββββββ
β
βββ Score β₯ threshold? β STOP β
β
βΌ
ββββββββββββββββ
β IMPROVER β β Refines response based on critic feedback
ββββββββ¬ββββββββ
β
βββ Loop back to CRITIC (up to N iterations)
self-improving-ai-agent/
βββ main.py β Entry point β run this
βββ agents/
β βββ __init__.py
β βββ generator_agent.py β Produces initial response
β βββ critic_agent.py β Scores and critiques responses
β βββ improver_agent.py β Applies targeted improvements
βββ core/
β βββ __init__.py
β βββ loop_controller.py β Orchestrates the full pipeline
βββ utils/
β βββ __init__.py
β βββ logger.py β Colorful terminal output
βββ output/
β βββ history.json β Auto-generated improvement log
βββ requirements.txt
βββ README.md
cd self-improving-ai-agentRule-Based Mode (no API key needed β works out of the box):
# No installation needed! Python standard library only.
python main.py --mode rule --demoAPI Mode (optional β for real AI-powered improvement):
pip install anthropic # For Claude
# OR
pip install openai # For OpenAI
export ANTHROPIC_API_KEY=your_key_here
python main.py --mode api# Interactive mode (prompts for mode and input)
python main.py
# Rule-based mode with a demo prompt
python main.py --mode rule --demo
# API mode with custom settings
python main.py --mode api --iterations 5 --threshold 8.5
# Force 2 iterations, stop at score 7.0
python main.py --mode rule --iterations 2 --threshold 7.0| Argument | Type | Default | Description |
|---|---|---|---|
--mode |
rule or api |
Interactive | Operating mode |
--iterations |
int | 3 |
Max improvement cycles |
--threshold |
float | 8.0 |
Quality score to stop early |
--demo |
flag | off | Use built-in demo prompt |
The Critic Agent evaluates responses across 5 dimensions, each scored 0β10:
| Dimension | Weight | What it measures |
|---|---|---|
| Completeness | 30% | Coverage, depth, word count |
| Clarity | 25% | Readability, sentence variety, absence of vague language |
| Logic | 20% | Structure, transitions, sound reasoning |
| Specificity | 15% | Examples, statistics, concrete details |
| Tone | 10% | Professional language, appropriate register |
Final Score = Weighted average of all dimensions
Grade Scale:
- A+ (9.0β10.0) β Exceptional
- A (8.0β8.9) β High Quality β default stop threshold
- B (7.0β7.9) β Good
- C (6.0β6.9) β Acceptable
- D (5.0β5.9) β Needs Work
- F (0β4.9) β Poor
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π€ SELF-IMPROVING AI AGENT v1.0
Autonomous Response Quality Optimizer
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
STARTING IMPROVEMENT LOOP
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βΉ Prompt: "Explain machine learning to a complete beginner..."
βΉ Mode: RULE | Max Iterations: 3 | Threshold: 8.0/10
βΆ PHASE 1 Initial Generation
[GENERATOR] Creating initial response...
β Initial response generated (94 words)
βΆ PHASE 2 Initial Evaluation
[CRITIC] Evaluating response quality...
β Evaluation complete β Score: 4.8/10 (D)
Clarity [ββββββββββ] 4.5/10
Completeness [ββββββββββ] 3.5/10
Logic [ββββββββββ] 4.5/10
Tone [ββββββββββ] 5.0/10
Specificity [ββββββββββ] 3.0/10
βΆ ITERATION 1 Improvement Cycle
[IMPROVER] Applying improvements (iteration 1)...
β Improvement applied β 94 β 287 words (+193)
[CRITIC] Re-evaluating improved response...
β Evaluation complete β Score: 7.2/10 (B)
βΉ Score change: 4.8 β 7.2 (+2.40)
βΆ ITERATION 2 Improvement Cycle
[IMPROVER] Applying improvements (iteration 2)...
β Improvement applied β 287 β 412 words (+125)
[CRITIC] Re-evaluating improved response...
β Evaluation complete β Score: 8.1/10 (A)
βΉ Score change: 7.2 β 8.1 (+0.90)
β Quality threshold reached (8.1 β₯ 8.0)! Stopping early.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FINAL RESULT
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Best response achieved (Score: 8.1/10):
[Improved response printed here...]
βΉ Total iterations completed: 2
βΉ History saved to: output/history.json
Every session saves a complete audit trail:
{
"session_metadata": {
"timestamp": "2024-01-15T14:32:10.123456",
"prompt": "Explain machine learning...",
"mode": "rule",
"initial_score": 4.8,
"final_score": 8.1,
"total_iterations": 3
},
"iterations": [
{
"iteration": 0,
"label": "Initial Response",
"overall_score": 4.8,
"scores": {"clarity": 4.5, "completeness": 3.5, ...},
"weaknesses": ["Response is too brief (94 words)..."],
"response": "..."
},
{
"iteration": 1,
"label": "Iteration 1",
"overall_score": 7.2,
...
}
]
}- Add key to
WEIGHTSdict incritic_agent.py(ensure weights sum to 1.0) - Add scoring logic in
_evaluate_rule_based() - Add feedback logic in
_generate_feedback()
- Add a case to
_apply_fix()matching your new dimension - Implement
_fix_<your_dimension>()method
- Edit
_call_anthropic()in bothgenerator_agent.pyandcritic_agent.py - Change the
model=parameter to your preferred model
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β LoopController β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Generator β β Critic β β Improver β β
β β β β β β β β
β β Rule Mode: β β Rule Mode: β β Rule Mode: β β
β β Templates + β β Heuristics β β Pattern- β β
β β heuristics β β + metrics β β based fixes β β
β β β β β β β β
β β API Mode: β β API Mode: β β API Mode: β β
β β Claude/GPT β β Structured β β Guided β β
β β completion β β JSON eval β β rewriting β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
MIT License β free to use, modify, and distribute.
Built to demonstrate AI concepts: self-reflection, iterative improvement, and autonomous quality optimization.