Workflow-007: Debugging AI Applications
Document Control
- Workflow ID: 007
- Version: 1.0
- Status: Active
- Complexity: Variable (Low-High)
- Duration: 30 minutes - 4 hours
- Team Size: 1-2 developers
Overview
Debugging AI Applications involves systematic troubleshooting of issues in AI-powered systems, from model behavior problems to integration failures. This workflow provides structured approaches to identify, diagnose, and resolve issues in Claude-powered applications.
┌─────────────────────────────────────────────────────────────────────┐
│ AI APPLICATION DEBUGGING FLOW │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ [1] IDENTIFY [2] ISOLATE │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Symptoms │ │ Root Cause │ │
│ │ Error Types │ ────────► │ Components │ │
│ │ Scope │ │ Dependencies │ │
│ └──────────────┘ └──────────────┘ │
│ │ │ │
│ └─────────┬─────────────────┘ │
│ ▼ │
│ [3] DIAGNOSE [4] RESOLVE │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Deep Analysis│ │ Fix & Test │ │
│ │ Reproduce │ ────────► │ Validate │ │
│ │ Trace Issue │ │ Monitor │ │
│ └──────────────┘ └──────────────┘ │
│ │ │ │
│ └───────────────────────────┴─── [5] PREVENT │
│ │
└─────────────────────────────────────────────────────────────────────┘Prerequisites
Required SOPs
- [x] SOP-001: API Setup
- [x] SOP-012: Error Handling
- [x] SOP-014: Monitoring & Logging
- [x] SOP-011: Rate Limiting
- [x] SOP-010: Model Selection
Environment Setup
- Access to application logs
- Monitoring dashboards configured
- Debug tools and utilities available
- Test environment for reproduction
- Version control history access
Knowledge Prerequisites
- Understanding of AI application architecture
- Familiarity with Claude API behavior
- Knowledge of system integration points
- Experience with debugging methodologies
- Understanding of error patterns
Phase 1: IDENTIFY - Problem Recognition
Objective
Quickly identify and categorize the issue to determine debugging approach.
Steps
1.1 Gather Initial Symptoms
bash
# Document the problem symptoms
claude-code "Help me systematically document this issue's symptoms"
# Symptom documentation template:
"""
Issue Symptom Analysis:
Reported Problem: [Description from user/monitor]
Observable Symptoms:
├─ What is happening?
│ - Error messages
│ - Unexpected behavior
│ - Performance issues
│ - Incorrect outputs
│
├─ When does it occur?
│ - Specific times
│ - Under certain conditions
│ - With particular inputs
│ - During specific operations
│
├─ Where is it happening?
│ - Which environment (dev/staging/prod)
│ - Which components/services
│ - Which user groups
│ - Geographic regions
│
├─ Who is affected?
│ - All users or specific segments
│ - Internal systems
│ - External integrations
│ - Administrative functions
│
└─ How severe is the impact?
- Critical (system down)
- High (major functionality affected)
- Medium (workaround available)
- Low (minor inconvenience)
"""1.2 Classify the Issue Type
bash
# Categorize the problem for targeted debugging approach
claude-code "think: What type of AI application issue is this?"
# Issue classification framework:
"""
AI Application Issue Types:
1. Model Behavior Issues:
├─ Unexpected/incorrect responses
├─ Inconsistent outputs
├─ Performance degradation
├─ Timeout/latency issues
└─ Rate limiting problems
2. Integration Issues:
├─ API connectivity problems
├─ Authentication failures
├─ Data format mismatches
├─ Tool integration failures
└─ MCP configuration issues
3. Application Logic Issues:
├─ Prompt engineering problems
├─ Context management failures
├─ Memory/state issues
├─ Business logic errors
└─ Data processing failures
4. Infrastructure Issues:
├─ Network connectivity
├─ Service availability
├─ Resource constraints
├─ Configuration problems
└─ Deployment issues
5. Data Issues:
├─ Input validation failures
├─ Data corruption
├─ Schema mismatches
├─ Encoding problems
└─ Missing dependencies
"""1.3 Assess Scope and Impact
bash
# Determine the scope of the issue
claude-code "Assess the scope and business impact of this issue"
# Scope assessment template:
"""
Scope and Impact Assessment:
Affected Systems:
├─ Primary: [Core affected system]
├─ Secondary: [Systems affected by cascade]
└─ Dependent: [Systems that depend on affected ones]
User Impact:
├─ Number of affected users: [Count/percentage]
├─ Business functions impacted: [List]
├─ Revenue impact: [Estimated loss]
└─ SLA implications: [Breaches/risks]
Temporal Scope:
├─ When did it start: [Timestamp]
├─ Duration so far: [Time elapsed]
├─ Frequency: [Continuous/intermittent]
└─ Trend: [Getting worse/stable/improving]
Risk Assessment:
├─ Data integrity risk: [High/Medium/Low]
├─ Security implications: [None/Present]
├─ Compliance impact: [None/Present]
└─ Reputation risk: [High/Medium/Low]
"""1.4 Check Known Issues and Recent Changes
bash
# Review recent changes and known issues
claude-code "Check for recent deployments, known issues, and similar problems"
# Change and history analysis:
"""
Recent Changes Analysis:
Recent Deployments (48h):
├─ Application code changes
├─ Configuration updates
├─ Infrastructure changes
├─ Dependency updates
└─ Model parameter changes
Known Issues:
├─ Similar reported problems
├─ Ongoing incidents
├─ Scheduled maintenance
├─ Third-party service issues
└─ Environmental factors
Historical Patterns:
├─ Similar past incidents
├─ Seasonal patterns
├─ Load-related issues
├─ Configuration drift
└─ Recurring problems
"""Quality Gate 1: Issue Identified
- [ ] Symptoms clearly documented
- [ ] Issue type classified
- [ ] Scope and impact assessed
- [ ] Recent changes reviewed
- [ ] Similar issues investigated
Phase 2: ISOLATE - Component Isolation
Objective
Isolate the specific component or layer causing the issue.
Steps
2.1 Component-Level Testing
bash
# Test individual components to isolate the problem
claude-code "Help me test each component systematically to isolate the issue"
# Component isolation testing:
"""
Component Isolation Strategy:
1. API Layer Testing:
# Test Claude API directly
curl -X POST https://api.anthropic.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 100,
"messages": [{"role": "user", "content": "Test message"}]
}'
2. Application Layer Testing:
# Test application endpoints
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "test", "session_id": "test123"}'
3. Database Layer Testing:
# Test database connectivity
psql $DATABASE_URL -c "SELECT 1;"
4. Cache Layer Testing:
# Test Redis connectivity
redis-cli -u $REDIS_URL ping
5. External Services Testing:
# Test external API endpoints
curl -v https://external-api.example.com/health
Testing Matrix:
│ Working │ Failed │ Notes
─────────────────────┼─────────┼────────┼──────
Claude API │ ✓ │ │
Application API │ │ ✗ │ 500 errors
Database │ ✓ │ │
Redis Cache │ ✓ │ │
External APIs │ ✓ │ │
Conclusion: Issue isolated to Application API layer
"""2.2 Network and Connectivity Analysis
bash
# Analyze network connectivity and routing
claude-code "Diagnose network connectivity issues in the AI application stack"
# Network diagnostics:
"""
Network Diagnostics Checklist:
1. Basic Connectivity:
# Test internet connectivity
ping 8.8.8.8
# Test DNS resolution
nslookup api.anthropic.com
# Test Claude API endpoint
curl -I https://api.anthropic.com/v1/messages
2. SSL/TLS Verification:
# Check certificate validity
openssl s_client -connect api.anthropic.com:443 -servername api.anthropic.com
3. Firewall and Security:
# Check outbound connections
telnet api.anthropic.com 443
# Check proxy settings
echo $HTTP_PROXY $HTTPS_PROXY
4. Load Balancer Health:
# Check load balancer status
curl -v http://load-balancer/health
5. Service Mesh (if applicable):
# Check service mesh configuration
kubectl get virtualservice
kubectl get destinationrule
Common Network Issues:
├─ DNS resolution failures
├─ SSL certificate problems
├─ Firewall blocking requests
├─ Proxy configuration issues
├─ Load balancer misconfig
└─ Service mesh routing errors
"""2.3 Authentication and Authorization Testing
bash
# Verify authentication and authorization
claude-code "Test authentication and authorization in the AI application"
# Auth diagnostics:
"""
Authentication Diagnostics:
1. API Key Validation:
# Test API key format
echo $ANTHROPIC_API_KEY | wc -c # Should be proper length
# Test API key validity
curl -H "x-api-key: $ANTHROPIC_API_KEY" \
https://api.anthropic.com/v1/messages \
-d '{"model": "claude-3-5-sonnet-20241022", "max_tokens": 10, "messages": [{"role": "user", "content": "hi"}]}'
2. Application Authentication:
# Test JWT token validation
jwt decode $JWT_TOKEN
# Test session management
curl -b session_cookie=value http://app/protected-endpoint
3. Service-to-Service Auth:
# Test OAuth flows
curl -X POST auth-server/oauth/token \
-d "grant_type=client_credentials&client_id=xxx&client_secret=yyy"
4. Database Authentication:
# Test database connection with credentials
pg_isready -h db-host -U username
Authentication Issue Patterns:
├─ Expired API keys
├─ Invalid JWT tokens
├─ Session timeout
├─ Misconfigured OAuth
├─ Database auth failures
└─ Rate limiting (401/429 errors)
"""2.4 Data Flow Tracing
bash
# Trace data flow through the system
claude-code "Help me trace data flow to find where the issue occurs"
# Data flow tracing:
"""
Data Flow Tracing:
1. Request Tracing:
# Add trace IDs to requests
curl -H "X-Trace-ID: debug-123" http://app/api/chat
# Follow logs with trace ID
grep "debug-123" /var/log/app/*.log
2. Database Query Tracing:
# Enable query logging
SET log_statement = 'all';
# Monitor slow queries
SELECT query, mean_time, calls FROM pg_stat_statements
WHERE mean_time > 1000;
3. Cache Operation Tracing:
# Monitor Redis operations
redis-cli MONITOR
4. API Call Tracing:
# Log outbound API calls
tcpdump -i any -s 0 -A 'host api.anthropic.com'
5. Application Flow Tracing:
# Add debug logging at key points
logger.debug(f"Processing request: {request_id}")
logger.debug(f"Calling Claude API with prompt: {prompt[:100]}...")
logger.debug(f"Received response length: {len(response)}")
Data Flow Visualization:
User Request → Load Balancer → App Server → Cache Check → DB Query → Claude API → Response Processing → User Response
↑ ✓ ✗ ✓ ✓ ? ? ✗
Issue identified at App Server level
"""Quality Gate 2: Component Isolated
- [ ] Faulty component identified
- [ ] Network connectivity verified
- [ ] Authentication validated
- [ ] Data flow traced
- [ ] Root cause hypotheses formed
Phase 3: DIAGNOSE - Deep Analysis
Objective
Perform detailed analysis to understand the exact cause and mechanism of the issue.
Steps
3.1 Log Analysis and Pattern Recognition
bash
# Analyze logs for patterns and root cause indicators
claude-code "Analyze these logs to identify patterns and root causes"
# Log analysis methodology:
"""
Log Analysis Workflow:
1. Gather Relevant Logs:
# Application logs
tail -f /var/log/app/application.log | grep ERROR
# System logs
journalctl -u app-service --since "1 hour ago"
# Claude API interaction logs
grep "api.anthropic.com" /var/log/app/api.log
2. Pattern Analysis:
# Error frequency analysis
grep ERROR /var/log/app/*.log | cut -d' ' -f1-3 | sort | uniq -c
# Response time patterns
awk '/response_time/ {print $NF}' api.log | sort -n
# Memory usage trends
grep "memory_usage" system.log | tail -100
3. Error Correlation:
# Time-based correlation
grep "2024-12-01 14:" /var/log/app/*.log | grep -E "(ERROR|WARN|FATAL)"
# User-based correlation
grep "user_id:12345" /var/log/app/*.log
4. Key Metrics Extraction:
Error Rate: 15% (150/1000 requests)
Response Time P95: 8.5s (normally 2.3s)
Memory Usage: 85% (normally 45%)
Claude API Errors: 12% (rate limit hits)
5. Log Pattern Examples:
[ERROR] 2024-12-01 14:23:15 - Claude API timeout after 30s
[ERROR] 2024-12-01 14:23:16 - Database connection pool exhausted
[WARN] 2024-12-01 14:23:17 - High memory usage: 82%
[ERROR] 2024-12-01 14:23:18 - Failed to process user request: timeout
Pattern Identified: Connection pool exhaustion causing timeouts
"""3.2 Performance Profiling
bash
# Profile application performance to identify bottlenecks
claude-code "Profile the application performance to find bottlenecks"
# Performance profiling:
"""
Performance Profiling Strategy:
1. CPU Profiling:
# Use built-in profilers
python -m cProfile -o profile.out app.py
# Analyze profile results
python -c "import pstats; pstats.Stats('profile.out').sort_stats('tottime').print_stats(10)"
2. Memory Profiling:
# Memory usage analysis
pip install memory-profiler
python -m memory_profiler app.py
# Memory leak detection
valgrind --tool=massif --stacks=yes python app.py
3. Database Performance:
# Query analysis
EXPLAIN ANALYZE SELECT * FROM users WHERE active = true;
# Connection pool monitoring
SELECT count(*) as connections FROM pg_stat_activity;
4. API Response Time Analysis:
# Response time breakdown
Time Breakdown (avg response):
├─ Request parsing: 50ms
├─ Authentication: 120ms
├─ Database query: 450ms ← Bottleneck
├─ Claude API call: 1200ms
├─ Response formatting: 80ms
└─ Total: 1900ms
5. Resource Utilization:
# System resources
top -p $(pgrep python)
iostat -x 1
netstat -an | grep ESTABLISHED | wc -l
Performance Issues Identified:
├─ Database queries taking 450ms (should be <100ms)
├─ Connection pool exhausted during peak load
├─ Memory leaks in request processing
└─ Inefficient Claude API prompt construction
"""3.3 Claude-Specific Debugging
bash
# Debug Claude API interactions and model behavior
claude-code "Debug Claude-specific issues in the application"
# Claude debugging techniques:
"""
Claude API Debugging:
1. Request/Response Analysis:
# Log full Claude interactions
logger.debug(f"Claude Request: {json.dumps(request_payload, indent=2)}")
logger.debug(f"Claude Response: {response.text}")
logger.debug(f"Response Time: {response.elapsed.total_seconds()}s")
2. Model Behavior Analysis:
# Test prompt consistency
for i in range(5):
response = claude_api.complete("What is 2+2?")
print(f"Attempt {i}: {response}")
# Analyze response variations
responses = [claude_api.complete(prompt) for _ in range(10)]
analyze_response_consistency(responses)
3. Token Usage Monitoring:
# Track token consumption
input_tokens = len(tokenizer.encode(prompt))
output_tokens = response['usage']['output_tokens']
total_cost = calculate_cost(input_tokens, output_tokens)
logger.info(f"Tokens: {input_tokens} in, {output_tokens} out, Cost: ${total_cost}")
4. Rate Limiting Analysis:
# Monitor rate limit headers
rate_limit_remaining = response.headers.get('anthropic-ratelimit-requests-remaining')
rate_limit_reset = response.headers.get('anthropic-ratelimit-requests-reset')
if int(rate_limit_remaining) < 10:
logger.warning(f"Rate limit approaching: {rate_limit_remaining} remaining")
5. Prompt Engineering Issues:
# Analyze prompt effectiveness
prompt_metrics = {
'length': len(prompt),
'complexity': calculate_complexity(prompt),
'context_tokens': len(tokenizer.encode(context)),
'success_rate': calculate_success_rate(prompt, test_cases)
}
Common Claude Issues:
├─ Prompt too long (>100k tokens)
├─ Rate limiting (requests/minute exceeded)
├─ Model hallucination/inconsistency
├─ Context window overflow
├─ Malformed API requests
└─ Network timeouts to Claude API
Debugging Output Example:
Claude Request Analysis:
├─ Prompt length: 2,450 tokens
├─ Context length: 8,200 tokens
├─ Total input: 10,650 tokens ← Near context limit
├─ Expected output: ~500 tokens
├─ Rate limit remaining: 45 requests
├─ Response time: 3.2s
└─ Success: True
Issue: Context approaching limit causing slower responses
"""3.4 Reproduce the Issue
bash
# Create reproducible test cases for the issue
claude-code "Create reproducible test cases to demonstrate this issue"
# Issue reproduction methodology:
"""
Issue Reproduction Strategy:
1. Minimal Reproduction Case:
# Create simplest possible reproduction
def reproduce_issue():
# Minimal setup
client = claude_client()
# Exact conditions that cause the issue
prompt = "..." # The problematic prompt
context = {...} # The problematic context
try:
response = client.complete(prompt, context)
print(f"Response: {response}")
except Exception as e:
print(f"Error reproduced: {e}")
return True
return False
2. Environment Recreation:
# Docker container for consistent reproduction
FROM python:3.11
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY reproduce_issue.py .
CMD ["python", "reproduce_issue.py"]
3. Data-Driven Reproduction:
# Use actual data that caused the issue
test_cases = [
{
'input': 'problematic_input_1',
'context': {'user_id': 12345},
'expected_error': 'TimeoutError'
},
{
'input': 'problematic_input_2',
'context': {'user_id': 67890},
'expected_error': 'ConnectionError'
}
]
for test_case in test_cases:
result = reproduce_issue(test_case['input'], test_case['context'])
assert result['error_type'] == test_case['expected_error']
4. Load-Based Reproduction:
# Reproduce under load conditions
import concurrent.futures
import time
def load_test():
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
futures = [executor.submit(reproduce_issue) for _ in range(100)]
results = [f.result() for f in futures]
error_rate = sum(results) / len(results)
print(f"Error rate under load: {error_rate:.2%}")
5. Reproduction Success Criteria:
✓ Issue reproduces consistently (>90% of attempts)
✓ Reproduction time: <2 minutes
✓ Minimal dependencies required
✓ Clear error output/symptoms
✓ Environment-independent (works on different machines)
Reproduction Result:
Issue reproduced successfully:
├─ Occurs when context > 8000 tokens
├─ Combined with specific prompt patterns
├─ Under concurrent load >10 requests/sec
├─ Error: "Context length exceeded"
└─ Success rate: 95% reproduction
"""Quality Gate 3: Issue Diagnosed
- [ ] Root cause identified through logs
- [ ] Performance bottlenecks profiled
- [ ] Claude-specific issues analyzed
- [ ] Issue reproduction achieved
- [ ] Fix approach determined
Phase 4: RESOLVE - Fix Implementation
Objective
Implement and validate the fix for the identified issue.
Steps
4.1 Design the Fix Strategy
bash
# Design comprehensive fix strategy
claude-code "Design a comprehensive fix strategy for this issue"
# Fix strategy framework:
"""
Fix Strategy Design:
Root Cause: Database connection pool exhaustion causing Claude API timeouts
Fix Strategy Options:
Option 1: Immediate (Hot Fix)
├─ Increase database connection pool size
├─ Add connection pool monitoring
├─ Implement circuit breaker for Claude API
├─ Timeline: 2 hours
├─ Risk: Medium (configuration change)
└─ Impact: Resolves immediate symptoms
Option 2: Short-term (Tactical Fix)
├─ Optimize database queries
├─ Implement request queuing
├─ Add Claude API retry logic with exponential backoff
├─ Timeline: 1-2 days
├─ Risk: Low (code changes in non-critical path)
└─ Impact: Improves system resilience
Option 3: Long-term (Strategic Fix)
├─ Implement async request processing
├─ Add Redis caching for Claude responses
├─ Database query optimization and indexing
├─ Horizontal scaling of application servers
├─ Timeline: 1-2 weeks
├─ Risk: High (architecture changes)
└─ Impact: Prevents future occurrences
Recommended Approach:
1. Implement Option 1 immediately (2h)
2. Implement Option 2 in next sprint (1-2 days)
3. Plan Option 3 for next quarter
Fix Implementation Plan:
Phase 1 (Immediate):
├─ Increase connection pool: 10 → 25
├─ Add health check endpoint
├─ Implement Claude API circuit breaker
└─ Deploy with rollback plan
Phase 2 (Short-term):
├─ Add query optimization
├─ Implement retry logic
├─ Add monitoring dashboards
└─ Load testing validation
Phase 3 (Long-term):
├─ Async processing architecture
├─ Comprehensive caching strategy
├─ Auto-scaling implementation
└─ Performance benchmark establishment
"""4.2 Implement the Fix
bash
# Implement the chosen fix
claude-code "Implement the fix for this AI application issue"
# Fix implementation:
"""
Fix Implementation Code:
1. Database Connection Pool Fix:
# config/database.py
DATABASE_CONFIG = {
'host': os.getenv('DB_HOST'),
'port': int(os.getenv('DB_PORT', 5432)),
'database': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD'),
'min_connections': 5,
'max_connections': 25, # Increased from 10
'connection_timeout': 30,
'idle_timeout': 300,
'max_lifetime': 3600
}
2. Circuit Breaker for Claude API:
# utils/circuit_breaker.py
import time
from enum import Enum
class CircuitState(Enum):
CLOSED = "closed"
OPEN = "open"
HALF_OPEN = "half_open"
class ClaudeAPICircuitBreaker:
def __init__(self, failure_threshold=5, recovery_timeout=60):
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.failure_count = 0
self.last_failure_time = None
self.state = CircuitState.CLOSED
async def call_claude_api(self, func, *args, **kwargs):
if self.state == CircuitState.OPEN:
if time.time() - self.last_failure_time > self.recovery_timeout:
self.state = CircuitState.HALF_OPEN
else:
raise Exception("Circuit breaker is OPEN - Claude API unavailable")
try:
result = await func(*args, **kwargs)
if self.state == CircuitState.HALF_OPEN:
self.state = CircuitState.CLOSED
self.failure_count = 0
return result
except Exception as e:
self.failure_count += 1
self.last_failure_time = time.time()
if self.failure_count >= self.failure_threshold:
self.state = CircuitState.OPEN
logger.error(f"Circuit breaker opened due to {self.failure_count} failures")
raise e
3. Retry Logic with Exponential Backoff:
# utils/retry.py
import asyncio
import random
from typing import Callable, Any
async def retry_with_exponential_backoff(
func: Callable,
max_retries: int = 3,
base_delay: float = 1.0,
max_delay: float = 60.0,
jitter: bool = True
) -> Any:
for attempt in range(max_retries + 1):
try:
return await func()
except Exception as e:
if attempt == max_retries:
raise e
# Calculate delay with exponential backoff
delay = min(base_delay * (2 ** attempt), max_delay)
# Add jitter to prevent thundering herd
if jitter:
delay = delay + random.uniform(0, delay * 0.1)
logger.warning(f"Attempt {attempt + 1} failed: {e}. Retrying in {delay:.2f}s")
await asyncio.sleep(delay)
4. Enhanced Claude API Client:
# clients/claude_client.py
class ResilientClaudeClient:
def __init__(self, api_key: str):
self.api_key = api_key
self.circuit_breaker = ClaudeAPICircuitBreaker()
self.client = anthropic.AsyncAnthropic(api_key=api_key)
async def complete_with_resilience(self, prompt: str, **kwargs) -> str:
async def _make_request():
return await self.client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=kwargs.get('max_tokens', 1000),
messages=[{"role": "user", "content": prompt}]
)
# Apply circuit breaker and retry logic
result = await self.circuit_breaker.call_claude_api(
retry_with_exponential_backoff, _make_request
)
return result.content[0].text
5. Health Check Endpoint:
# api/health.py
@app.get("/health")
async def health_check():
health_status = {
"status": "healthy",
"timestamp": datetime.utcnow().isoformat(),
"checks": {}
}
# Database health
try:
await database.execute("SELECT 1")
health_status["checks"]["database"] = "healthy"
except Exception as e:
health_status["checks"]["database"] = f"unhealthy: {e}"
health_status["status"] = "unhealthy"
# Claude API health
try:
test_response = await claude_client.complete_with_resilience("test")
health_status["checks"]["claude_api"] = "healthy"
except Exception as e:
health_status["checks"]["claude_api"] = f"degraded: {e}"
health_status["status"] = "degraded"
# Connection pool health
pool_status = get_connection_pool_status()
health_status["checks"]["connection_pool"] = {
"active_connections": pool_status["active"],
"idle_connections": pool_status["idle"],
"max_connections": pool_status["max"],
"utilization": f"{pool_status['utilization']:.1%}"
}
return health_status
6. Deployment Script:
#!/bin/bash
# deploy_fix.sh
set -e
echo "Starting deployment of database connection fix..."
# Backup current configuration
kubectl get configmap app-config -o yaml > backup-config.yaml
# Update configuration
kubectl patch configmap app-config --patch '
data:
DB_MAX_CONNECTIONS: "25"
CIRCUIT_BREAKER_ENABLED: "true"
RETRY_MAX_ATTEMPTS: "3"
'
# Rolling update deployment
kubectl rollout restart deployment/app-server
# Wait for deployment
kubectl rollout status deployment/app-server --timeout=300s
# Verify health
sleep 30
health_response=$(curl -f http://app.example.com/health)
echo "Health check: $health_response"
echo "Deployment completed successfully"
"""4.3 Test the Fix
bash
# Comprehensively test the implemented fix
claude-code "Create comprehensive tests to validate the fix"
# Fix validation testing:
"""
Fix Validation Test Suite:
1. Unit Tests for New Components:
# test_circuit_breaker.py
import pytest
import asyncio
from unittest.mock import AsyncMock
from utils.circuit_breaker import ClaudeAPICircuitBreaker
@pytest.mark.asyncio
async def test_circuit_breaker_closed_state():
cb = ClaudeAPICircuitBreaker(failure_threshold=2)
mock_func = AsyncMock(return_value="success")
result = await cb.call_claude_api(mock_func)
assert result == "success"
assert cb.state == CircuitState.CLOSED
@pytest.mark.asyncio
async def test_circuit_breaker_opens_after_failures():
cb = ClaudeAPICircuitBreaker(failure_threshold=2)
mock_func = AsyncMock(side_effect=Exception("API Error"))
# First failure
with pytest.raises(Exception):
await cb.call_claude_api(mock_func)
assert cb.state == CircuitState.CLOSED
# Second failure - should open circuit
with pytest.raises(Exception):
await cb.call_claude_api(mock_func)
assert cb.state == CircuitState.OPEN
2. Integration Tests:
# test_claude_integration.py
@pytest.mark.asyncio
async def test_claude_api_resilience():
client = ResilientClaudeClient(api_key=test_api_key)
# Test successful request
response = await client.complete_with_resilience("What is 2+2?")
assert "4" in response
# Test retry behavior (mock failures)
with patch.object(client.client.messages, 'create') as mock_create:
mock_create.side_effect = [
Exception("Timeout"),
Exception("Timeout"),
MockResponse("4") # Success on third attempt
]
response = await client.complete_with_resilience("What is 2+2?")
assert "4" in response
assert mock_create.call_count == 3
3. Load Testing:
# load_test.py
import asyncio
import aiohttp
import time
async def load_test_fix():
concurrent_requests = 50
total_requests = 500
async with aiohttp.ClientSession() as session:
start_time = time.time()
async def make_request():
try:
async with session.post(
'http://localhost:8000/api/chat',
json={'message': 'test', 'session_id': 'load_test'}
) as response:
return response.status == 200
except Exception:
return False
# Execute load test
semaphore = asyncio.Semaphore(concurrent_requests)
async def limited_request():
async with semaphore:
return await make_request()
tasks = [limited_request() for _ in range(total_requests)]
results = await asyncio.gather(*tasks)
end_time = time.time()
# Analyze results
success_count = sum(results)
success_rate = success_count / total_requests
duration = end_time - start_time
rps = total_requests / duration
print(f"Load Test Results:")
print(f"Success Rate: {success_rate:.2%}")
print(f"Requests/sec: {rps:.1f}")
print(f"Duration: {duration:.2f}s")
# Validate fix effectiveness
assert success_rate > 0.95, f"Success rate too low: {success_rate:.2%}"
assert rps > 10, f"Throughput too low: {rps:.1f} RPS"
4. End-to-End Testing:
# test_e2e_fix.py
async def test_original_issue_resolved():
"""Test that the original issue is resolved"""
# Recreate original failure conditions
# High concurrent load + complex prompts + database queries
tasks = []
for i in range(20): # Original failure load
task = asyncio.create_task(
simulate_original_request(f"Complex request {i}")
)
tasks.append(task)
# All should succeed now
results = await asyncio.gather(*tasks, return_exceptions=True)
failures = [r for r in results if isinstance(r, Exception)]
success_rate = (len(results) - len(failures)) / len(results)
print(f"E2E Test - Success Rate: {success_rate:.2%}")
print(f"E2E Test - Failures: {len(failures)}")
# With fix, should have >95% success rate
assert success_rate > 0.95
5. Performance Regression Testing:
# test_performance.py
async def test_performance_not_degraded():
"""Ensure fix doesn't degrade performance"""
baseline_response_time = 2.0 # seconds
start_time = time.time()
response = await make_test_request()
response_time = time.time() - start_time
print(f"Response time: {response_time:.2f}s (baseline: {baseline_response_time}s)")
# Response time should not increase significantly
assert response_time < baseline_response_time * 1.2 # Allow 20% degradation max
Test Results Summary:
✓ Unit tests: 15/15 passing
✓ Integration tests: 8/8 passing
✓ Load test: 98.4% success rate (was 15%)
✓ E2E test: 96.8% success rate (was 10%)
✓ Performance: 1.8s avg (was 2.1s)
✓ No regressions detected
"""4.4 Deploy and Monitor
bash
# Deploy the fix with comprehensive monitoring
claude-code "Deploy the fix and set up monitoring to track its effectiveness"
# Deployment and monitoring setup:
"""
Deployment and Monitoring Strategy:
1. Phased Deployment:
# Stage 1: Deploy to 10% of traffic
kubectl patch deployment app-server -p '
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
'
# Canary deployment
kubectl set env deployment/app-server CANARY_ENABLED=true CANARY_PERCENTAGE=10
# Monitor for 30 minutes
# Stage 2: Deploy to 50% if metrics are good
# Stage 3: Full deployment
2. Monitoring Dashboard Setup:
# prometheus/alerts.yml
groups:
- name: ai_app_alerts
rules:
- alert: HighErrorRate
expr: rate(http_requests_total{status=~"5.."}[5m]) > 0.05
for: 2m
labels:
severity: warning
annotations:
summary: "High error rate detected"
- alert: ClaudeAPICircuitBreakerOpen
expr: circuit_breaker_state{service="claude_api"} == 1
for: 1m
labels:
severity: critical
annotations:
summary: "Claude API circuit breaker is open"
- alert: DatabaseConnectionPoolExhaustion
expr: db_connection_pool_utilization > 0.9
for: 5m
labels:
severity: warning
annotations:
summary: "Database connection pool near exhaustion"
3. Real-time Monitoring Queries:
# Error rate monitoring
rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m])
# Response time monitoring
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))
# Circuit breaker status
circuit_breaker_state{service="claude_api"}
# Database connection pool utilization
db_connection_pool_active / db_connection_pool_max
4. Automated Rollback Conditions:
# rollback.sh
#!/bin/bash
ERROR_RATE_THRESHOLD=0.05
RESPONSE_TIME_THRESHOLD=5.0
# Check error rate
error_rate=$(prometheus_query "rate(http_requests_total{status=~\"5..\"}[5m])")
if (( $(echo "$error_rate > $ERROR_RATE_THRESHOLD" | bc -l) )); then
echo "Error rate too high: $error_rate"
kubectl rollout undo deployment/app-server
exit 1
fi
# Check response time
p95_response_time=$(prometheus_query "histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))")
if (( $(echo "$p95_response_time > $RESPONSE_TIME_THRESHOLD" | bc -l) )); then
echo "Response time too high: $p95_response_time"
kubectl rollout undo deployment/app-server
exit 1
fi
echo "Metrics within acceptable range"
5. Success Metrics Tracking:
Pre-Fix Metrics (Baseline):
├─ Error Rate: 15%
├─ P95 Response Time: 8.5s
├─ Circuit Breaker Opens: 50/hour
├─ Database Pool Exhaustion: 25/hour
└─ Customer Satisfaction: 2.1/5
Post-Fix Metrics (Target):
├─ Error Rate: <2%
├─ P95 Response Time: <3s
├─ Circuit Breaker Opens: <5/hour
├─ Database Pool Exhaustion: <1/hour
└─ Customer Satisfaction: >4.0/5
Actual Results (1 hour post-deployment):
├─ Error Rate: 1.2% ✓
├─ P95 Response Time: 2.1s ✓
├─ Circuit Breaker Opens: 2/hour ✓
├─ Database Pool Exhaustion: 0/hour ✓
└─ Customer Satisfaction: 4.2/5 ✓
6. Communication Plan:
# status_update.md
## Incident Resolution Update
**Status**: RESOLVED
**Time**: 2024-12-01 16:30 UTC
**Duration**: 4 hours 15 minutes
### Issue Summary
Database connection pool exhaustion causing Claude API timeouts
### Resolution
- Increased database connection pool size (10→25)
- Implemented Claude API circuit breaker
- Added retry logic with exponential backoff
- Enhanced monitoring and alerting
### Metrics Improvement
- Error rate: 15% → 1.2%
- Response time: 8.5s → 2.1s
- System stability: Significantly improved
### Next Steps
- Continue monitoring for 24h
- Performance optimization in next sprint
- Post-incident review scheduled
"""Quality Gate 4: Fix Deployed
- [ ] Fix strategy designed and approved
- [ ] Code implemented and tested
- [ ] Fix validation tests passing
- [ ] Deployment successful
- [ ] Monitoring confirms resolution
Phase 5: PREVENT - Future Prevention
Objective
Implement measures to prevent similar issues from recurring.
Steps
5.1 Root Cause Analysis Documentation
bash
# Document comprehensive root cause analysis
claude-code "Create a thorough post-incident review and root cause analysis"
# Post-incident review:
"""
Post-Incident Review: Database Connection Pool Exhaustion
Incident ID: INC-2024-1201-001
Date: December 1, 2024
Duration: 4 hours 15 minutes
Severity: High
Timeline:
10:15 UTC - First alerts received (high error rate)
10:30 UTC - Incident declared, team mobilized
11:45 UTC - Root cause identified (DB pool exhaustion)
13:00 UTC - Fix implemented and deployed
14:30 UTC - Full resolution confirmed
Root Cause Analysis:
Primary Cause:
├─ Database connection pool configured too small (10 connections)
├─ Application not releasing connections properly under load
├─ No circuit breaker for external API calls
└─ Cascading failure from Claude API timeouts
Contributing Factors:
├─ Lack of connection pool monitoring
├─ Insufficient load testing with realistic conditions
├─ Missing retry logic for transient failures
├─ No degraded service mode for high load
└─ Alerting thresholds set too high
Why It Wasn't Caught Earlier:
├─ Development environment had different load patterns
├─ Load testing didn't simulate concurrent Claude API calls
├─ Connection pool exhaustion alerts were not configured
├─ Monitoring focused on application metrics, not infrastructure
└─ Circuit breaker pattern not implemented
Impact Assessment:
├─ User Impact: 2,500 users affected, 15% error rate
├─ Business Impact: $12,000 estimated revenue loss
├─ Reputation Impact: 43 negative reviews/support tickets
├─ Engineering Impact: 16 person-hours incident response
└─ Customer Success Impact: 150+ support inquiries
Lessons Learned:
1. Infrastructure monitoring is as important as application monitoring
2. Load testing must include realistic external API patterns
3. Circuit breaker patterns are essential for external dependencies
4. Connection pools need proper sizing and monitoring
5. Graceful degradation modes improve user experience
Prevention Measures Implemented:
1. Enhanced Monitoring:
├─ Database connection pool utilization alerts
├─ Claude API response time tracking
├─ Circuit breaker status monitoring
└─ Comprehensive infrastructure dashboards
2. Resilience Patterns:
├─ Circuit breaker for all external APIs
├─ Retry logic with exponential backoff
├─ Connection pool health checks
└─ Graceful degradation modes
3. Testing Improvements:
├─ Load testing with external API simulation
├─ Chaos engineering practices
├─ Infrastructure failure simulation
└─ Performance regression testing
4. Process Improvements:
├─ Infrastructure review in code reviews
├─ Mandatory load testing for releases
├─ Regular infrastructure health checks
└─ Incident response runbook updates
"""5.2 Implement Preventive Monitoring
bash
# Set up comprehensive preventive monitoring
claude-code "Implement monitoring and alerting to prevent similar issues"
# Preventive monitoring setup:
"""
Comprehensive Preventive Monitoring:
1. Infrastructure Health Monitoring:
# Database Connection Pool
- Metric: db_connection_pool_utilization
- Alert: >80% for 5 minutes
- Action: Scale pool or investigate connection leaks
# Memory Usage
- Metric: container_memory_usage_percentage
- Alert: >85% for 2 minutes
- Action: Memory leak investigation
# CPU Usage
- Metric: container_cpu_usage_percentage
- Alert: >90% for 5 minutes
- Action: Performance optimization needed
2. Application Performance Monitoring:
# Response Time Degradation
- Metric: http_request_duration_p95
- Alert: >3s for 2 minutes
- Action: Performance investigation
# Error Rate Spike
- Metric: http_requests_error_rate
- Alert: >5% for 1 minute
- Action: Immediate investigation
# Claude API Health
- Metric: claude_api_success_rate
- Alert: <95% for 3 minutes
- Action: Circuit breaker check, API status verification
3. Business Logic Monitoring:
# Conversation Success Rate
- Metric: conversation_completion_rate
- Alert: <90% for 5 minutes
- Action: AI model performance investigation
# User Satisfaction Tracking
- Metric: average_user_rating
- Alert: <3.5/5 for 1 hour
- Action: User experience review
4. Predictive Monitoring:
# Trend Analysis
WITH hourly_metrics AS (
SELECT
date_trunc('hour', timestamp) as hour,
avg(response_time) as avg_response_time,
avg(error_rate) as avg_error_rate,
avg(db_connections) as avg_db_connections
FROM metrics
WHERE timestamp > NOW() - INTERVAL '7 days'
GROUP BY hour
),
trend_analysis AS (
SELECT
hour,
avg_response_time,
avg_response_time - LAG(avg_response_time) OVER (ORDER BY hour) as response_time_trend,
avg_db_connections,
avg_db_connections - LAG(avg_db_connections) OVER (ORDER BY hour) as db_trend
FROM hourly_metrics
)
SELECT * FROM trend_analysis
WHERE response_time_trend > 0.5 OR db_trend > 2;
# Anomaly Detection
- Use machine learning to detect unusual patterns
- Alert on deviations from normal baseline
- Predictive scaling based on trends
5. Monitoring Dashboard:
dashboard.yaml:
- title: "AI Application Health"
panels:
- title: "System Health"
metrics:
- database_health
- claude_api_health
- application_health
- infrastructure_health
- title: "Performance"
metrics:
- response_time_p95
- throughput_rps
- error_rate
- user_satisfaction
- title: "Resource Utilization"
metrics:
- cpu_usage
- memory_usage
- disk_usage
- network_usage
- title: "Business Metrics"
metrics:
- conversations_per_hour
- resolution_rate
- escalation_rate
- cost_per_conversation
6. Automated Response Actions:
# Auto-scaling rules
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: app-server
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
# Circuit breaker automation
if circuit_breaker_open:
enable_degraded_mode()
notify_oncall_team()
scale_up_backup_services()
# Connection pool auto-scaling
if db_pool_utilization > 0.8:
increase_pool_size(factor=1.5)
log_connection_analytics()
schedule_connection_audit()
"""5.3 Establish Chaos Engineering
bash
# Implement chaos engineering practices
claude-code "Set up chaos engineering to proactively find weaknesses"
# Chaos engineering implementation:
"""
Chaos Engineering Framework:
1. Chaos Testing Scenarios:
Scenario 1: Database Connection Failure
- Kill random database connections
- Simulate connection pool exhaustion
- Test application resilience and recovery
Scenario 2: Claude API Degradation
- Inject latency into Claude API calls
- Simulate rate limiting responses
- Test circuit breaker behavior
Scenario 3: Memory Pressure
- Consume available memory gradually
- Simulate memory leaks
- Test garbage collection and recovery
Scenario 4: Network Partitioning
- Block network traffic between services
- Simulate partial network failures
- Test service mesh resilience
2. Chaos Engineering Tools Setup:
# Chaos Monkey for Kubernetes
apiVersion: v1
kind: ConfigMap
metadata:
name: chaoskube-config
data:
config.yaml: |
interval: 10m
dryRun: false
metrics: true
excludedPods:
- kube-system
annotations:
chaos.alpha.kubernetes.io/enabled: "true"
# Litmus Chaos Experiments
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosExperiment
metadata:
name: pod-delete
spec:
definition:
scope: Namespaced
permissions:
- apiGroups: [""]
resources: ["pods"]
verbs: ["create","delete","get","list","patch","update"]
image: "litmuschaos/go-runner:latest"
command:
- go
- -c
- ./experiments -name pod-delete
3. Automated Chaos Testing Pipeline:
# chaos_test_pipeline.py
import asyncio
import random
from datetime import datetime, timedelta
class ChaosTestingPipeline:
def __init__(self):
self.scenarios = [
self.database_chaos,
self.api_chaos,
self.memory_chaos,
self.network_chaos
]
async def run_daily_chaos(self):
"""Run daily chaos engineering tests"""
# Select random scenario
scenario = random.choice(self.scenarios)
# Pre-test health check
baseline_health = await self.check_system_health()
print(f"Starting chaos test: {scenario.__name__}")
print(f"Baseline health: {baseline_health}")
try:
# Run chaos scenario
await scenario()
# Monitor recovery
recovery_time = await self.monitor_recovery()
# Validate system health
post_test_health = await self.check_system_health()
# Record results
await self.record_chaos_results({
'scenario': scenario.__name__,
'baseline_health': baseline_health,
'recovery_time': recovery_time,
'post_test_health': post_test_health,
'success': post_test_health['overall'] > 0.9
})
except Exception as e:
print(f"Chaos test failed: {e}")
await self.emergency_recovery()
async def database_chaos(self):
"""Simulate database connection issues"""
# Kill random DB connections
await self.execute_chaos_command(
"kubectl exec db-pod -- pkill -f 'postgres.*idle'"
)
# Wait for impact
await asyncio.sleep(30)
# Restore connections
await self.execute_chaos_command(
"kubectl rollout restart deployment/app-server"
)
async def api_chaos(self):
"""Simulate Claude API issues"""
# Inject network latency
await self.execute_chaos_command(
"tc qdisc add dev eth0 root netem delay 5000ms"
)
await asyncio.sleep(60)
# Remove latency
await self.execute_chaos_command(
"tc qdisc del dev eth0 root"
)
4. Continuous Resilience Testing:
# Weekly Chaos Schedule
monday: Database connection chaos
tuesday: API latency injection
wednesday: Memory pressure testing
thursday: Network partitioning
friday: Combined stress testing
saturday: Recovery time testing
sunday: Baseline performance testing
# Automated Chaos Results Analysis
def analyze_chaos_results(results):
patterns = {
'recovery_time_trend': analyze_recovery_trends(results),
'failure_patterns': identify_failure_patterns(results),
'resilience_score': calculate_resilience_score(results),
'improvement_areas': suggest_improvements(results)
}
return patterns
5. Game Days - Planned Chaos Events:
# Monthly Game Day Schedule
First Friday: Database disaster simulation
Second Friday: API partner outage simulation
Third Friday: Infrastructure failure simulation
Fourth Friday: Security incident simulation
Game Day Checklist:
□ Stakeholder notification sent
□ Monitoring dashboards prepared
□ Rollback procedures verified
□ Team communication channels ready
□ Success criteria defined
□ Post-game retrospective scheduled
6. Chaos Engineering Metrics:
Resilience Metrics:
├─ Mean Time to Recovery (MTTR): Target <5 minutes
├─ Failure Detection Time: Target <2 minutes
├─ System Recovery Success Rate: Target >98%
├─ Blast Radius Containment: Target <10% users affected
└─ Automated Recovery Success: Target >80%
Chaos Testing KPIs:
├─ Tests per week: Target 5
├─ Scenarios covered: Target 100% per month
├─ Recovery automation: Target 90%
├─ False positive rate: Target <5%
└─ System resilience score: Target >9/10
"""5.4 Update Development Practices
bash
# Update development practices to prevent similar issues
claude-code "Update development practices and processes to prevent similar issues"
# Development practice updates:
"""
Updated Development Practices:
1. Code Review Checklist Updates:
Infrastructure Review Checklist:
□ Connection pool sizing appropriate for expected load
□ Circuit breakers implemented for external dependencies
□ Retry logic with exponential backoff included
□ Health checks and readiness probes configured
□ Resource limits and requests specified
□ Monitoring and alerting configured
□ Graceful degradation paths implemented
□ Error handling covers all failure modes
AI-Specific Review Checklist:
□ Prompt token limits considered and handled
□ Claude API rate limiting accounted for
□ Response time expectations realistic
□ Token usage optimization implemented
□ Context management strategy defined
□ Fallback responses for API failures
□ Cost monitoring and limits configured
□ Model behavior edge cases handled
2. Definition of Done Updates:
Technical Requirements:
□ Unit tests written and passing (>90% coverage)
□ Integration tests include failure scenarios
□ Load testing completed with realistic traffic
□ Infrastructure monitoring configured
□ Performance benchmarks established
□ Security scan completed and passed
□ Documentation updated (architecture, runbooks)
□ Rollback procedure tested
AI-Specific Requirements:
□ Prompt effectiveness validated
□ Token usage benchmarked
□ Model response consistency tested
□ Error handling for API failures verified
□ Cost impact analyzed and approved
□ User experience tested with degraded service
□ Monitoring for AI-specific metrics configured
□ Fallback behavior validated
3. Testing Strategy Updates:
# test_strategy.py
class EnhancedTestingStrategy:
def __init__(self):
self.test_types = {
'unit': UnitTestSuite(),
'integration': IntegrationTestSuite(),
'load': LoadTestSuite(),
'chaos': ChaosTestSuite(),
'resilience': ResilienceTestSuite(),
'ai_behavior': AIBehaviorTestSuite()
}
async def run_comprehensive_testing(self):
"""Run all test suites with infrastructure focus"""
results = {}
# Unit tests - foundation
results['unit'] = await self.test_types['unit'].run()
# Integration tests - with failure injection
results['integration'] = await self.test_types['integration'].run_with_failures()
# Load tests - with realistic Claude API patterns
results['load'] = await self.test_types['load'].run_realistic_load()
# Chaos tests - infrastructure resilience
results['chaos'] = await self.test_types['chaos'].run_infrastructure_chaos()
# AI behavior tests - model consistency and errors
results['ai_behavior'] = await self.test_types['ai_behavior'].run_model_tests()
return self.analyze_results(results)
4. Deployment Pipeline Updates:
# .github/workflows/deploy.yml
name: Enhanced Deployment Pipeline
on:
push:
branches: [main]
jobs:
infrastructure-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Infrastructure Validation
run: |
# Validate resource configurations
python scripts/validate_resources.py
# Check connection pool sizing
python scripts/validate_db_config.py
# Validate monitoring configuration
python scripts/validate_monitoring.py
load-testing:
runs-on: ubuntu-latest
needs: infrastructure-tests
steps:
- name: Load Testing with AI Patterns
run: |
# Start test environment
docker-compose -f docker-compose.test.yml up -d
# Run load tests with Claude API simulation
python scripts/load_test_with_ai.py
# Validate performance thresholds
python scripts/validate_performance.py
chaos-testing:
runs-on: ubuntu-latest
needs: load-testing
steps:
- name: Chaos Engineering Tests
run: |
# Run infrastructure chaos tests
python scripts/chaos_test.py --mode=infrastructure
# Validate recovery times
python scripts/validate_recovery.py
deploy:
needs: [infrastructure-tests, load-testing, chaos-testing]
steps:
- name: Deploy with Monitoring
run: |
# Deploy with enhanced monitoring
kubectl apply -f k8s/monitoring/
kubectl apply -f k8s/app/
# Wait for health checks
scripts/wait_for_health.sh
# Validate deployment metrics
python scripts/validate_deployment.py
5. Monitoring Integration in Development:
# Local development monitoring
docker-compose.dev.yml:
version: '3.8'
services:
app:
build: .
environment:
- MONITORING_ENABLED=true
- LOG_LEVEL=DEBUG
prometheus:
image: prom/prometheus
ports: ["9090:9090"]
grafana:
image: grafana/grafana
ports: ["3000:3000"]
jaeger:
image: jaegertracing/all-in-one
ports: ["16686:16686"]
# Development monitoring script
#!/bin/bash
# scripts/dev_monitoring.sh
echo "Starting development monitoring stack..."
# Start monitoring services
docker-compose -f docker-compose.dev.yml up -d prometheus grafana jaeger
# Wait for services
sleep 30
# Import dashboards
curl -X POST http://admin:admin@localhost:3000/api/dashboards/db \
-H "Content-Type: application/json" \
-d @monitoring/dashboards/ai-app-dashboard.json
echo "Monitoring available at:"
echo " Prometheus: http://localhost:9090"
echo " Grafana: http://localhost:3000 (admin/admin)"
echo " Jaeger: http://localhost:16686"
6. Knowledge Sharing Updates:
# Team Knowledge Base Updates
docs/runbooks/database_issues.md:
- Connection pool exhaustion symptoms
- Investigation procedures
- Recovery steps
- Prevention measures
docs/runbooks/claude_api_issues.md:
- Rate limiting handling
- Circuit breaker management
- Prompt optimization
- Cost monitoring
docs/architecture/resilience_patterns.md:
- Circuit breaker implementation
- Retry strategies
- Graceful degradation
- Monitoring best practices
# Regular Training Schedule
- Monthly: Infrastructure resilience training
- Quarterly: Chaos engineering workshops
- Bi-annually: Incident response drills
- Annually: Architecture review sessions
7. Incident Response Updates:
# Enhanced incident response process
incident_response_v2.md:
Severity Levels:
├─ P0: System down, data loss risk
├─ P1: Major functionality impacted
├─ P2: Degraded service, workaround available
├─ P3: Minor issues, scheduled fix acceptable
Response Times:
├─ P0: 15 minutes
├─ P1: 1 hour
├─ P2: 4 hours
├─ P3: Next business day
Enhanced Escalation:
├─ Automatic escalation if no progress in 30 minutes
├─ Infrastructure team inclusion for database/network issues
├─ AI team inclusion for Claude API issues
├─ Business stakeholder notification for P0/P1 issues
Post-Incident Requirements:
├─ Root cause analysis within 48 hours
├─ Prevention measures implemented within 1 week
├─ Team retrospective within 3 days
├─ Knowledge base updates within 1 week
"""Quality Gate 5: Prevention Implemented
- [ ] Root cause analysis completed and documented
- [ ] Preventive monitoring implemented
- [ ] Chaos engineering framework established
- [ ] Development practices updated
- [ ] Team training completed
Common AI Application Issues
Claude API Issues
bash
# Common Claude API problems and solutions
"""
Common Claude API Issues:
1. Rate Limiting (429 errors):
- Symptoms: Frequent 429 responses
- Causes: Exceeding requests per minute/hour limits
- Solutions: Implement rate limiting, request queuing, exponential backoff
2. Token Limit Exceeded:
- Symptoms: Context length errors
- Causes: Prompts or context too large
- Solutions: Context truncation, prompt optimization, chunking
3. API Timeouts:
- Symptoms: Request timeouts, no response
- Causes: Network issues, large prompts, high load
- Solutions: Timeout adjustment, retry logic, circuit breakers
4. Inconsistent Responses:
- Symptoms: Varying outputs for same inputs
- Causes: Model non-determinism, prompt ambiguity
- Solutions: Temperature adjustment, prompt engineering, multiple samples
5. High Costs:
- Symptoms: Unexpected API billing
- Causes: Large token usage, inefficient prompts
- Solutions: Token monitoring, prompt optimization, caching
"""Integration Issues
bash
# Common integration problems
"""
Integration Issues:
1. Authentication Failures:
- API key issues
- Token expiration
- Permission problems
2. Data Format Mismatches:
- JSON schema differences
- Encoding problems
- Type conversion errors
3. Network Connectivity:
- DNS resolution failures
- Firewall blocking
- SSL/TLS issues
4. Service Dependencies:
- Database unavailability
- Cache service failures
- External API outages
"""Success Metrics
Debugging Effectiveness
| Metric | Target | Measurement |
|---|---|---|
| Time to Resolution | <2 hours | Incident start to resolution |
| Root Cause Accuracy | >90% | Verified root causes |
| Recurrence Rate | <10% | Same issue within 30 days |
| Prevention Success | >80% | Issues caught before production |
System Resilience
| Metric | Target | Tracking |
|---|---|---|
| MTTR | <15 minutes | Recovery time tracking |
| MTTD | <5 minutes | Detection time monitoring |
| Availability | >99.9% | Uptime monitoring |
| Error Rate | <1% | Error tracking dashboards |
Best Practices Summary
Do's ✅
- Start with systematic problem identification
- Use structured debugging methodologies
- Implement comprehensive monitoring
- Create reproducible test cases
- Document all findings and solutions
- Focus on prevention, not just resolution
- Establish chaos engineering practices
- Update development processes based on learnings
Don'ts ❌
- Don't skip root cause analysis
- Don't implement fixes without testing
- Don't ignore similar historical issues
- Don't rush to production without validation
- Don't forget to update monitoring
- Don't skip team knowledge sharing
- Don't overlook prevention measures
- Don't blame individuals for systemic issues
See Also
- Workflow-006: Building AI Agents
- SOP-012: Error Handling
- SOP-014: Monitoring & Logging
- SOP-011: Rate Limiting
Next Workflow: Try Production Deployment to safely deploy your AI applications to production