Skip to main content
The Swarms API uses intelligent response compression to significantly reduce payload sizes and improve response times. Responses are automatically compressed when beneficial, reducing bandwidth usage and speeding up data transfer. The API implements a dynamic compression middleware that automatically compresses HTTP responses based on:
  • Response size: Only compresses bodies larger than 500 bytes and no larger than 8 MiB
  • Client support: Negotiates the algorithm via the Accept-Encoding header (including q-values)
  • Compression efficiency: Only applies compression if it actually reduces the response size

Supported Compression Algorithms

The server prefers LZ4 for speed and falls back to GZip for universal compatibility:
Standard HTTP clients advertise gzip (not lz4) by default, so in practice most responses are GZip-compressed. LZ4 is only used when a client explicitly sends lz4 in Accept-Encoding and can decode LZ4 frame data.

How It Works

Automatic Compression

The compression middleware automatically:
  1. Parses client preferences: Reads the Accept-Encoding header, ordering encodings by their q-values. An encoding with q=0 is explicitly excluded; a * wildcard means any encoding is acceptable
  2. Selects the algorithm: Walks the client’s encodings in preference order and picks the first one the server supports. If only * matches, GZip is used, since every client can decode it
  3. Checks response size: Only compresses bodies larger than 500 bytes and no larger than 8 MiB
  4. Validates compression: Only applies if the compressed size is smaller than the original
  5. Sets headers: Sets Content-Encoding, updates Content-Length to the compressed size, and appends Accept-Encoding to the Vary header

Compression Flow

Responses That Are Never Compressed

The middleware passes these responses through unmodified:
  • Streaming responses, including Server-Sent Events (text/event-stream) — events must be flushed immediately, not buffered
  • Already-encoded responses — anything that already has a Content-Encoding header is never re-compressed
  • Small responses — bodies of 500 bytes or less
  • Large responses — bodies over 8 MiB (bounds server buffering memory)
  • Responses where compression doesn’t help — if the compressed body isn’t smaller, the original is returned

Client Preferences

The API respects client compression preferences through the Accept-Encoding header, including q-values:
In this example the client prefers LZ4, accepts GZip at lower priority, and explicitly refuses Brotli (q=0). The middleware:
  • Honors q-value ordering when choosing among supported methods
  • Treats q=0 as “do not use this encoding”
  • Treats * as “any encoding” and responds with GZip, since every client can decode it
  • Returns the response uncompressed if no supported encoding is acceptable

Benefits

Performance Improvements

Automatic Optimization

  • No configuration required: Compression is automatic and transparent
  • Smart fallback: Automatically uses the best available method
  • Size validation: Only compresses when it actually helps
  • Header management: Properly sets compression headers for client compatibility

Usage

Standard HTTP Requests

Compression works automatically with all API endpoints. No special configuration is needed — standard clients already send Accept-Encoding: gzip and decompress transparently:

Client Libraries

Most HTTP clients automatically request and decompress GZip. Only opt into LZ4 if your client can decode LZ4 frame data:

Response Headers

Compressed responses include these headers:
  • Content-Encoding: Indicates the compression method used (gzip or lz4)
  • Content-Length: Updated to the compressed size (smaller than the original)
  • Vary: Accept-Encoding is appended so caches store separate variants per encoding

Compression Statistics

The middleware logs compression details (at debug level) for monitoring:
  • Original response size
  • Compressed response size
  • Algorithm used

Best Practices

1. Include Accept-Encoding Header

Make sure your client sends an Accept-Encoding header (most do by default):
Only add lz4 if your client decodes LZ4 frames — e.g. Accept-Encoding: lz4, gzip;q=0.8.

2. Let Clients Handle Decompression

Most HTTP clients automatically decompress responses. Don’t manually decompress unless necessary.

3. Monitor Response Sizes

Large responses benefit most from compression. The API automatically handles this optimization.

4. Use Modern Clients

Modern HTTP clients (requests, fetch, axios) automatically handle compression and decompression.

Technical Details

Size Thresholds

  • Minimum (500 bytes): Bodies of 500 bytes or less are not compressed — compression overhead exceeds the benefit for tiny payloads, where network latency is the bottleneck anyway
  • Maximum (8 MiB): Bodies larger than 8 MiB are passed through uncompressed to bound the memory used for buffering responses on the server

Compression Method Selection

The middleware selects the compression method as follows:
  1. Client preference match: Walks the client’s Accept-Encoding entries in q-value order and uses the first method the server supports (q=0 entries are skipped entirely)
  2. Wildcard support: If the client accepts *, GZip is used, since every client can decode it
  3. No compression: Returns the response uncompressed if no acceptable method is supported

Error Handling

If compression fails:
  • Original uncompressed response is returned
  • No error is raised to the client
  • Compression failure is logged for monitoring
  • Client receives valid response regardless

Compatibility

Browser Support

All modern browsers support GZip compression automatically. LZ4 is not natively supported by browsers or most HTTP libraries — only request it if you decode it yourself.

API Clients

Summary

The Swarms API’s automatic response compression:
  • Reduces bandwidth usage by compressing responses
  • Improves response times through smaller payloads
  • Works transparently with no configuration needed
  • Supports LZ4 and GZip, preferring LZ4 for speed when the client accepts it
  • Respects client preferences via the Accept-Encoding header, including q-values (q=0 excludes an encoding; * accepts any)
  • Only compresses when it helps — bodies between 500 bytes and 8 MiB, and only if the result is actually smaller
  • Never touches streaming (SSE) or already-encoded responses
  • Compatible with all clients that support standard HTTP compression
Compression is automatic and requires no configuration. Simply make API requests as normal, and responses will be compressed when beneficial.