Performance Optimization
Optimize your Claro integration to minimize latency, reduce costs, and improve user experience. This guide covers caching, connection pooling, batch operations, and monitoring.Caching Strategies
Why Cache Prompts?
Prompts don’t change frequently, making them ideal for caching:- Reduced latency - Serve from cache instead of API
- Lower costs - Fewer API calls
- Better reliability - Work offline with cached prompts
- Improved UX - Faster response times
Cache prompts aggressively, but ensure you have a strategy to invalidate the cache when prompts are updated.
In-Memory Caching
Simple caching for single-server applications:Redis Caching
For distributed systems, use Redis:Cache Invalidation Strategies
- Time-Based (TTL)
- Version-Based
- Webhook-Based
- Manual
Automatically expire after a time periodPros:
- Simple to implement
- No manual invalidation needed
- Works well for stable prompts
- May serve stale data
- Can’t force updates immediately
Connection Pooling
HTTP Connection Reuse
The Claro SDK uses connection pooling by default, but you can optimize it:Singleton Pattern
Reuse a single client instance across your application:FastAPI Dependency Injection
For web applications, use dependency injection:Batch Operations
Fetching Multiple Prompts
Load multiple prompts in parallel:Pagination Best Practices
When listing prompts, use efficient pagination:Monitoring and Metrics
Request Timing
Track API performance:Performance Metrics
Track key performance indicators:Integration with Monitoring Tools
- Prometheus
- DataDog
- CloudWatch
Export metrics to Prometheus
Troubleshooting Slow Requests
Common Performance Issues
Cold Start Latency
Cold Start Latency
Problem: First request is slowCause: Initial connection setup, DNS resolutionSolution:
Network Latency
Network Latency
Problem: Slow response times from APICause: Geographic distance from API serversSolution:
- Use caching aggressively (TTL of 5-10 minutes)
- Fetch prompts at application startup
- Consider edge caching with CloudFlare or CDN
Large Prompt Content
Large Prompt Content
Problem: Prompts with large context files are slowCause: Transferring large files over networkSolution:
Too Many Sequential Requests
Too Many Sequential Requests
Problem: Loading many prompts takes too longCause: Sequential API calls add upSolution:
Use concurrent fetching (see Batch Operations section above)
Rate Limiting
Rate Limiting
Problem: Requests throttled due to rate limitsCause: Exceeding API rate limitsSolution:
Best Practices
Cache Aggressively
Cache Aggressively
Prompts are relatively static - cache them:
- Use 5-10 minute TTL for frequently accessed prompts
- Use version-based cache keys to avoid stale data
- Implement cache warming for critical prompts
Batch When Possible
Batch When Possible
Reduce API calls by batching:
- Fetch multiple prompts concurrently
- Use pagination with maximum page size
- Pre-load prompts at startup for critical paths
Monitor Performance
Monitor Performance
Track metrics to identify issues:
- Log slow requests (>1 second)
- Monitor error rates
- Track cache hit rates
- Set up alerts for anomalies
Optimize for Common Cases
Optimize for Common Cases
Optimize hot paths:
- Cache most frequently used prompts
- Pre-fetch prompts for common workflows
- Use CDN for static prompt content
Handle Failures Gracefully
Handle Failures Gracefully
Plan for API unavailability:
- Implement fallback prompts
- Cache prompts locally as backup
- Return graceful errors to users
- Retry with exponential backoff
Performance Checklist
Before deploying to production:- Caching implemented with appropriate TTL
- Connection pooling configured
- Metrics and monitoring in place
- Slow request alerts configured
- Rate limit handling implemented
- Fallback strategy for API failures
- Performance tested under load
- Critical prompts pre-loaded at startup
Next Steps
Advanced Patterns
See production-ready optimization examples
Error Handling
Handle API errors gracefully
Testing
Performance test your integration
Security
Secure your cached data