Skip to main content

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

Automatically expire after a time periodPros:
  • Simple to implement
  • No manual invalidation needed
  • Works well for stable prompts
Cons:
  • 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:
Be mindful of rate limits when making concurrent requests. Start with a conservative max_workers value (3-5) and monitor for rate limit errors.

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

Export metrics to Prometheus

Troubleshooting Slow Requests

Common Performance Issues

Problem: First request is slowCause: Initial connection setup, DNS resolutionSolution:
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
Problem: Prompts with large context files are slowCause: Transferring large files over networkSolution:
Problem: Loading many prompts takes too longCause: Sequential API calls add upSolution: Use concurrent fetching (see Batch Operations section above)
Problem: Requests throttled due to rate limitsCause: Exceeding API rate limitsSolution:

Best Practices

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
Reduce API calls by batching:
  • Fetch multiple prompts concurrently
  • Use pagination with maximum page size
  • Pre-load prompts at startup for critical paths
Track metrics to identify issues:
  • Log slow requests (>1 second)
  • Monitor error rates
  • Track cache hit rates
  • Set up alerts for anomalies
Optimize hot paths:
  • Cache most frequently used prompts
  • Pre-fetch prompts for common workflows
  • Use CDN for static prompt content
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