🔄 LOOP ENGINE
Generate → Evaluate → Refine → Converge
Perfect for • Claude • Kiro • Cursor • Codex • LangChain • n8n • Any AI Workflow
🎬 See It In Action
from loop_engine import run_loop
result = run_loop(
task="Create a FastAPI user auth endpoint",
success_criteria="JWT tokens, password hashing, type hints",
failure_criteria="Hardcoded secrets, no validation",
max_iterations=5
)
# ✨ Automatically iterates until perfect!
🔥 How It Works
⚡ Why Loop Engine Beats Single-Shot AI
| 🤖 Traditional AI | 🔄 Loop Engine |
|---|---|
| One attempt only | ✅ Iterates until perfect |
| Manual re-prompting | ✅ Automatic refinement |
| No quality control | ✅ Built-in scoring (1-10) |
| Often misses requirements | ✅ Checks YOUR criteria |
| Generic output | ✅ Converges to YOUR standards |
| Unsafe code changes | ✅ Git isolation (optional sandbox) |
| No validation | ✅ Test execution integration |
Install
pip install loop-engine[all] # All providers
# or
pip install loop-engine # Just core
Set API key (any provider):
export ANTHROPIC_API_KEY="your_key"
# or OPENAI_API_KEY, GOOGLE_API_KEY, etc.
Quick Start
CLI
loop-engine run # Interactive mode
Python
from loop_engine import run_loop
result = run_loop(
task="Create a FastAPI user auth endpoint",
success_criteria="JWT tokens, password hashing, type hints, error handling",
failure_criteria="Hardcoded secrets, no validation, missing docstrings",
max_iterations=5,
provider="anthropic" # auto-detects if not specified
)
print(result.final.output)
print(f"Quality: {result.final.quality_score}/10")
Supported Providers
| Provider | Models | Setup |
|---|---|---|
| Anthropic | Claude 3.5 Sonnet, Claude 3 Opus | ANTHROPIC_API_KEY |
| OpenAI | GPT-4, GPT-4o | OPENAI_API_KEY |
| Gemini 2.0 Flash, Gemini 1.5 Pro | GOOGLE_API_KEY |
|
| OpenRouter | 200+ models | OPENROUTER_API_KEY |
| Groq | Llama 3.3, Mixtral (ultra-fast) | GROQ_API_KEY |
| DeepSeek | DeepSeek Chat, DeepSeek Coder | DEEPSEEK_API_KEY |
| AWS Bedrock | Claude, Llama on AWS | AWS credentials |
| Azure OpenAI | GPT-4 on Azure | AZURE_OPENAI_API_KEY |
| Ollama | Local models (free) | Ollama running locally |
Built-in Tools
Enable with --tools or enable_tools=True:
read_file- Read file contentswrite_file- Write to fileslist_directory- List directory contentsexecute_command- Run shell commandssearch_files- Find files by patterncreate_directory- Create directories
Test Execution (NEW!)
Run tests automatically during iterations:
result = run_loop(
task="Write a function to validate emails",
success_criteria="Regex validation, type hints, passes tests",
failure_criteria="No validation, tests fail",
test_command="pytest tests/test_email.py", # ✨ NEW!
max_iterations=5
)
# Tests run each iteration - quality score boosted if they pass!
Git Isolation (NEW!)
Safe code modifications in isolated environment:
result = run_loop(
task="Refactor auth module to use async/await",
success_criteria="All async, passes tests",
failure_criteria="Breaking changes",
enable_tools=True,
test_command="pytest tests/",
use_isolation=True, # 🔒 Safe isolation!
isolation_path=".", # Your git repo
max_iterations=8
)
# Creates temporary worktree → AI edits → Tests → Merges if good!
How Git Isolation Works:
- Creates temporary copy (worktree) of your code
- AI modifies the COPY, not your real files
- Runs tests in isolation
- If successful → merges back to main
- If fails → deletes copy, your code untouched
When to use:
- ✅ Automated refactoring of production code
- ✅ Large multi-file changes
- ✅ When you want safety guarantees
- ❌ Simple code generation (overkill)
Custom Tools
from loop_engine import register_custom_tool
def my_tool(arg: str) -> dict:
return {"result": f"Processed: {arg}"}
register_custom_tool(
name="my_tool",
func=my_tool,
definition={
"name": "my_tool",
"description": "My custom tool",
"parameters": {
"type": "object",
"properties": {
"arg": {"type": "string", "description": "Argument"}
},
"required": ["arg"]
}
}
)
Examples
Check /examples directory:
basic_example.py- Simple usage (2 seconds)tools_example.py- File operations with toolscomplete_system_test.py- Full system validation
Best Practices
For Simple Tasks (Fast)
# Just basic loop - 2-5 seconds per iteration
run_loop(task, success, failure, max_iterations=3)
For Code with Tests
# Add test execution for validation
run_loop(
task, success, failure,
test_command="pytest tests/",
max_iterations=5
)
For Production Code (Maximum Safety)
# Full safety: isolation + tests
run_loop(
task, success, failure,
enable_tools=True,
test_command="pytest tests/",
use_isolation=True, # Safe sandbox
isolation_path=".",
max_iterations=10
)
Tips for Best Results
- Be specific: "TypeScript, uses async/await, <200 lines" > "good code"
- Set hard failures: "No eval(), no hardcoded secrets"
- Start with 3-5 iterations: Most tasks converge quickly
- Use tools for file ops: Enable tools when working with existing code
- Add context: Provide project structure, patterns, requirements
Configuration
Create ~/.loop-engine/config.yaml:
max_iterations: 5
convergence_threshold: 8
provider: auto
tools:
enabled: false
allowed_tools: [all]
providers:
anthropic:
model: claude-sonnet-4-20250514
max_tokens: 4000
Or use loop-engine init to generate automatically.
License
MIT License - see LICENSE file
Support
- Examples:
/examplesdirectory - Issues: GitHub Issues
- Config:
loop-engine statusto see current setup
Metadata
Release files for loop-prompting 2.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| loop_prompting-2.1.2.tar.gz | 33.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| loop_prompting-2.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.7 kB
Release files / loop_prompting-2.1.2.tar.gz
| Download URL | loop_prompting-2.1.2.tar.gz |
|---|---|
| Size | 33.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
206d74497e144014ade60537f2eaecba26dbc94c52c9d7c997167fc48220c4a0
|
|
BLAKE2b-256 checksum How to use checksums |
140d35b55a6cea47eb0e9d6dda2b1b8741fe3c83c363c45550bbe313f5b3a303
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Release files / loop_prompting-2.1.2-py3-none-any.whl
| Download URL | loop_prompting-2.1.2-py3-none-any.whl |
|---|---|
| Size | 36.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
34609a0bac3e8fe9a0c8983f1e3c6c32a227a99f3d1f11c6ac06cc3fe6aef2a2
|
|
BLAKE2b-256 checksum How to use checksums |
234a4377a156f44c0e52d9723dffbf9e6583ade99bf76295029dc62403c192ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|