Back to agents
Agent #5
⚡ Performance Optimization Agent
This agent analyzes code for performance bottlenecks, profiling results, and database queries. It identifies inefficiencies and suggests optimizations including algorithmic improvements, caching strategies, and resource usage. python 05_perf_optimization.py /path/to/slow_function.py python 05_perf_optimization.py "Function takes 5 seconds to process 100 items"
Key Features
- ✓Bottleneck identification
- ✓Algorithm complexity analysis
- ✓Caching strategies
- ✓Memory optimization
- ✓Benchmarking guidance
Implementation
Download .py05_perf_optimization.py
#!/usr/bin/env python3
"""
Performance Optimization Agent
This agent analyzes code for performance bottlenecks, profiling results,
and database queries. It identifies inefficiencies and suggests optimizations
including algorithmic improvements, caching strategies, and resource usage.
Usage:
python 05_perf_optimization.py /path/to/slow_function.py
python 05_perf_optimization.py "Function takes 5 seconds to process 100 items"
"""
import sys
from anthropic import Anthropic
def main():
if len(sys.argv) < 2:
print("Usage: python 05_perf_optimization.py <file_path_or_description>")
sys.exit(1)
target = " ".join(sys.argv[1:])
client = Anthropic()
system_prompt = """You are a performance optimization specialist. Your job is to:
1. Identify performance bottlenecks in code
2. Analyze algorithm complexity (Big O)
3. Spot N+1 query problems and memory leaks
4. Suggest caching strategies
5. Recommend data structure changes
6. Provide benchmarking guidance
Be specific: show actual code improvements with expected speedup percentages."""
task = f"""Optimize the following for performance:
{target}
Analyze and provide:
1. Performance bottleneck identification
2. Current complexity analysis (Big O)
3. Root causes of slowness
4. Optimized code with:
- Algorithm improvements
- Better data structures
- Caching strategies
- Batch operations
5. Before/after performance comparison
6. Benchmarking code to measure improvement
7. Memory usage reduction opportunities"""
print("⚡ Starting Performance Optimization Agent...\n")
print("=" * 60)
try:
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=3000,
system=system_prompt,
messages=[{"role": "user", "content": task}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n" + "=" * 60)
print("\n✅ Performance optimization complete!")
except Exception as e:
print(f"\n❌ Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
How to Use
This agent is designed to be run from the command line:
python 05_perf_optimization.py <input>The agent will stream its response to the console as it works through the task. Use Ctrl+C to interrupt at any time.
Quick Download
05_perf_optimization.pyFile
05_perf_optimization.py
Language
Python 3.8+
Dependencies
anthropic
Setup
- 1. Install:
pip install anthropic - 2. Download the .py file
- 3. Set ANTHROPIC_API_KEY
- 4. Run:
python 05_perf_optimization.py