[Feature Request] Upgrade HTTP compression to Brotli or Zstandard for API requests
[Feature Request] Upgrade HTTP compression to Brotli or Zstandard for API requests
Summary
Claude Code should use modern compression algorithms (Brotli or Zstandard) for HTTP requests to the Anthropic API, replacing the current gzip compression. This would reduce bandwidth and improve performance, especially for large conversation payloads.
Current State
- Response compression: gzip enabled (
content-encoding: gzip) - Request compression: Unclear if enabled; if so, likely gzip
- Payload size: Can reach 500KB+ for long conversations with tool use
Proposed Change
Implement Brotli or Zstandard compression for HTTP request bodies to /v1/messages and /v1/messages/count_tokens endpoints.
Compression Algorithm Comparison
| Algorithm | Compression Ratio | Encode Speed | Decode Speed |
|-----------|------------------|--------------|--------------|
| gzip | 1.0x (baseline) | 1.0x | 1.0x |
| Brotli | 1.15-1.25x | 0.5x | 1.2x |
| Zstandard | 1.10-1.20x | 2.0x | 1.5x |
Source: Cloudflare: Results of experimenting with Brotli for dynamic web content
Why This Matters
1. Bandwidth Reduction:
- Large conversations: 500KB → ~350KB (Brotli) or ~375KB (Zstd)
- Annual bandwidth savings: Significant at scale
2. Latency Improvement:
- Smaller payloads = faster network transfer
- Especially impactful on slower connections
- Estimated improvement: 5-15% for large requests
3. Cost Reduction:
- Lower bandwidth costs for Anthropic
- Faster transfers = lower cloud egress costs
Industry Adoption
Cloudflare
"Brotli compression helps make the Web faster... We saw an improvement in compression ratio of around 15-25% compared to Gzip." — Cloudflare Blog: Brotli Compression
"Brotli compressed data is typically 20-26% smaller than gzip for text content... JavaScript bundles compressed with Brotli are 14% smaller on average than gzip." — Google Web Fundamentals: Optimizing Encoding and Transfer Size
Meta (Facebook)
"Zstandard... achieves compression ratios comparable to the best available compressors while being significantly faster." — Facebook Engineering: Zstandard
Implementation Considerations
Brotli (Recommended)
Pros:
- Best compression ratio (15-25% better than gzip)
- Native browser support (98%+ coverage)
- Standard
Accept-Encoding: brheader - Well-tested in production (Cloudflare, Google, Akamai)
Cons:
- Slower encoding than Zstd (acceptable for client-side)
- Requires
brotlilibrary (Node.js:npm install brotli)
Zstandard (Alternative)
Pros:
- Fastest compression/decompression speed
- Tunable compression levels
- Excellent for server-side processing
Cons:
- Less mature HTTP support (requires custom header)
- May need API server upgrade to support
Content-Encoding: zstd
Proposed Implementation (Brotli)
// Add to API client configuration
import zlib from 'zlib';
const requestBody = JSON.stringify({
model: 'claude-sonnet-4',
messages: [...],
tools: [...]
});
// Compress request body with Brotli
const compressed = zlib.brotliCompressSync(
Buffer.from(requestBody),
{
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 6, // Balance speed vs ratio
}
}
);
// Send with appropriate headers
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'br',
'Accept-Encoding': 'br, gzip',
},
body: compressed,
});
Expected Impact
For a typical long conversation:
- Uncompressed: 500 KB
- gzip: ~150 KB (70% reduction)
- Brotli: ~120 KB (76% reduction) → 20% better than gzip
- Network time saved: ~40ms on 10 Mbps connection
Related Issues
- #12952: Token counting performance degradation (this compression helps marginally)
References
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗