Skip to main content

Overview

Shannon implements a multi-layered caching system for LLM responses, providing significant cost savings and latency reduction for repeated or similar queries. The caching system supports both in-memory and Redis backends.

Architecture

LLM Response Caching

Cache Backends

In-Memory Cache (Default)

The default caching backend uses an LRU (Least Recently Used) dictionary with automatic eviction. Features:
  • Zero external dependencies
  • Fast lookup (O(1) average)
  • Automatic eviction when capacity reached
  • Hit rate tracking
Limitations:
  • Not shared across instances
  • Lost on service restart

Redis Cache

For production deployments, Redis provides distributed caching across multiple instances. Features:
  • Distributed across all LLM service instances
  • Persistent storage
  • Automatic TTL expiration
  • High availability with Redis Sentinel/Cluster
Configuration:

Configuration

Global Settings

Configure caching in config/models.yaml:

Per-Request Override

Override cache behavior on individual requests:

Cache Key Generation

Cache keys are generated deterministically from request parameters:

Included Parameters

  • Message content and roles
  • Model tier and specific model
  • Temperature and max_tokens
  • Function definitions
  • Random seed

Excluded Parameters

  • Streaming flag (streaming not cached)
  • Session/task IDs (cache is request-based)

Caching Rules

When Responses Are Cached

Responses are cached when:
  • Caching is enabled globally
  • Request is non-streaming
  • Response has non-empty content OR has function_call
  • Finish reason is not “length” (truncated) or “content_filter”
  • For JSON mode: content is valid JSON object

When Responses Are NOT Cached

The following responses are never cached to ensure quality:
  • Streaming responses
  • Truncated responses (finish_reason: “length”)
  • Content-filtered responses
  • Empty responses without function calls
  • Invalid JSON in strict JSON mode

Cache Validation

Before serving cached responses, Shannon validates:
  1. Finish Reason Check: Skips truncated or filtered responses
  2. JSON Mode Validation: Ensures valid JSON object for JSON mode
  3. Content Presence: Requires non-empty content or function_call

Environment Variables

Performance Impact

Latency Reduction

Cost Savings

Cache hits eliminate LLM provider costs entirely:
  • Typical hit rates: 20-40% for diverse workloads
  • High hit rates: 60-80% for repetitive queries
  • Potential cost reduction: 20-80% depending on workload

Monitoring

Cache Metrics

The LLM service exposes cache metrics:

API Response Fields

Best Practices

Maximize Cache Hits

  1. Normalize prompts: Consistent formatting improves hit rates
  2. Use deterministic seeds: Set seed for reproducible outputs
  3. Standardize temperatures: Use consistent temperature values
  4. Reuse system prompts: Keep system messages consistent

Cache Key Strategy

Redis Configuration

For production:

Troubleshooting

Low Hit Rate

  • Check prompt normalization
  • Verify temperature consistency
  • Review cache TTL settings
  • Monitor cache eviction rate

Cache Not Working

  1. Verify prompt_cache.enabled: true in config
  2. Check Redis connection (if using Redis)
  3. Ensure requests are non-streaming
  4. Verify responses are not being filtered

Memory Issues

  • Reduce max_cache_size_mb
  • Use Redis for large-scale deployments
  • Implement cache partitioning by tenant

Next Steps

Model Selection

Configure model tiers and routing

Cost Control

Understand budget management