Performance Issues
Diagnose and resolve agent performance problems
Performance Issues
Diagnose and resolve performance problems with your SwiftClaw agents.
Identifying Performance Issues
Key Metrics
Monitor these metrics:
# View performance metrics
swiftclaw metrics my-agent --metric performance
# Output:
# Response Time (P50): 850ms
# Response Time (P95): 2100ms
# Response Time (P99): 3500ms
# Throughput: 45 req/s
# Error Rate: 0.5%
# CPU Usage: 65%
# Memory Usage: 1.2GBPerformance Baselines
Establish baselines for comparison:
# Create baseline
swiftclaw baseline create my-agent --name production-baseline
# Compare current performance
swiftclaw baseline compare my-agent --baseline production-baselineCommon Performance Issues
Slow Response Times
Symptoms: High P95/P99 latency
Diagnosis:
# Analyze slow requests
swiftclaw analyze my-agent --slow-requests --threshold 2s
# View request breakdown
swiftclaw trace my-agent --slow --breakdownSolutions:
- Optimize Model Selection
# Use faster models for simple tasks
agent = Agent(
routing={
"simple": "llama-3", # 500ms
"complex": "gpt-4" # 3000ms
}
)- Enable Response Caching
@agent.cache(ttl="1h")
async def handle_common_query(query: str):
return await agent.generate(query)- Reduce Context Window
# Limit context size
agent = Agent(
max_context_tokens=4000,
context_strategy="recent" # Keep only recent messages
)High Memory Usage
Symptoms: Memory usage >80%, OOM errors
Diagnosis:
# Check memory usage
swiftclaw metrics my-agent --metric memory --breakdown
# Identify memory leaks
swiftclaw analyze my-agent --memory-leaksSolutions:
- Configure Memory Limits
{
"memory": {
"shortTerm": {
"maxSize": "10MB",
"ttl": "1h"
},
"longTerm": {
"maxSize": "50MB",
"ttl": "7d"
}
}
}- Implement Memory Cleanup
@agent.schedule("0 */6 * * *") # Every 6 hours
async def cleanup_memory():
await agent.memory.cleanup(older_than="7d")- Use Memory Pagination
# Paginate large memory searches
results = await agent.memory.search(
query="user data",
limit=10,
offset=0
)High CPU Usage
Symptoms: CPU usage >80%, throttling
Diagnosis:
# Check CPU usage
swiftclaw metrics my-agent --metric cpu
# Profile CPU usage
swiftclaw profile my-agent --cpu --duration 5mSolutions:
- Optimize Tool Calls
# Bad: Sequential tool calls
result1 = await agent.call_tool("tool1")
result2 = await agent.call_tool("tool2")
# Good: Parallel tool calls
results = await asyncio.gather(
agent.call_tool("tool1"),
agent.call_tool("tool2")
)- Reduce Processing Overhead
# Use efficient data structures
from collections import deque
# Limit context history
context = deque(maxlen=10)- Enable Auto-Scaling
{
"scaling": {
"enabled": true,
"minInstances": 2,
"maxInstances": 10,
"targetCPU": 70
}
}Low Throughput
Symptoms: Low requests per second
Diagnosis:
# Check throughput
swiftclaw metrics my-agent --metric throughput
# Identify bottlenecks
swiftclaw analyze my-agent --bottlenecksSolutions:
- Enable Batch Processing
@agent.batch(max_size=10, max_wait="100ms")
async def process_messages(messages):
return await agent.generate_batch(messages)- Optimize Database Queries
# Bad: N+1 queries
for user_id in user_ids:
user = await db.get_user(user_id)
# Good: Batch query
users = await db.get_users(user_ids)- Use Connection Pooling
# Configure connection pool
db = Database(
pool_size=20,
max_overflow=10
)External API Latency
Symptoms: High latency from external APIs
Diagnosis:
# Check external API latency
swiftclaw metrics my-agent --metric external-api-latency
# View API call logs
swiftclaw logs my-agent --filter external-apiSolutions:
- Cache API Responses
@cache(ttl="30m")
async def fetch_external_data(query: str):
return await external_api.get(query)- Implement Timeouts
import asyncio
async def fetch_with_timeout(url: str):
try:
return await asyncio.wait_for(
external_api.get(url),
timeout=5.0
)
except asyncio.TimeoutError:
return None- Use Fallback Data
async def fetch_data(query: str):
try:
return await external_api.get(query)
except Exception:
# Return cached or default data
return await get_cached_data(query)Performance Optimization
Model Optimization
# Use streaming for better UX
async def handle_message(message):
async for chunk in agent.generate_stream(message):
yield chunk
# Reduce token usage
agent = Agent(
max_tokens=500,
temperature=0.7
)Memory Optimization
# Use hybrid search
agent = Agent(
memory={
"search": {
"type": "hybrid",
"vectorWeight": 0.7,
"textWeight": 0.3
}
}
)
# Implement memory pruning
@agent.middleware
async def prune_memory(context):
if context.memory.size() > 100:
await context.memory.prune(keep=50)Network Optimization
# Use HTTP/2
http_client = HTTPClient(http2=True)
# Enable compression
http_client = HTTPClient(compression=True)
# Reuse connections
http_client = HTTPClient(
pool_connections=10,
pool_maxsize=20
)Performance Testing
Load Testing
# Run load test
swiftclaw load-test my-agent \
--requests 1000 \
--concurrency 50 \
--duration 5m
# Output:
# Requests: 1000
# Duration: 5m
# RPS: 3.33
# P50: 850ms
# P95: 2100ms
# P99: 3500ms
# Errors: 5 (0.5%)Stress Testing
# Run stress test
swiftclaw stress-test my-agent \
--ramp-up 10 \
--max-concurrency 200 \
--duration 10mBenchmark Testing
# Run benchmark
swiftclaw benchmark my-agent \
--scenario production \
--compare baselineMonitoring Performance
Real-Time Monitoring
# Live performance dashboard
swiftclaw dashboard my-agent --view performance
# Live metrics
swiftclaw metrics my-agent --livePerformance Alerts
# Alert on high response time
swiftclaw alerts create \
--agent my-agent \
--metric response-time-p95 \
--threshold 2s \
--window 5m
# Alert on low throughput
swiftclaw alerts create \
--agent my-agent \
--metric throughput \
--threshold 10 \
--comparison belowPerformance Reports
Generate Reports
# Daily performance report
swiftclaw report my-agent \
--type performance \
--period 24h
# Weekly comparison
swiftclaw report my-agent \
--type performance \
--period 7d \
--compare previousExport Metrics
# Export metrics to CSV
swiftclaw metrics export my-agent \
--format csv \
--period 30d \
--output metrics.csvBest Practices
1. Monitor Continuously
# Set up comprehensive monitoring
swiftclaw monitor my-agent --enable-all2. Optimize Incrementally
# Test optimizations in staging
swiftclaw deploy my-agent --env staging --optimized
# Compare performance
swiftclaw benchmark my-agent --compare staging,production3. Use Appropriate Models
# Route based on complexity
agent = Agent(
routing={
"simple": "llama-3",
"medium": "gpt-3.5-turbo",
"complex": "gpt-4"
}
)4. Cache Aggressively
# Cache common queries
@agent.cache(ttl="1h", strategy="semantic")
async def handle_query(query: str):
return await agent.generate(query)Continuous Optimization: Performance optimization is an ongoing process. Monitor, measure, and iterate.
Next Steps
How is this guide ?
Last updated on