LessTokens Logo
Api Reference
Compression

Documentation

Getting Started

  • Overview
  • Account Setup
  • First API Key
  • First Request
  • Understanding Metrics

API Reference

  • Overview
  • Authentication
  • Compressionnew
  • MCP Servernew
  • Errors

Integrations

  • Node.js / TypeScript
  • Python
  • cURL

Resources

  • Best Practices

Need help?

Join our community or contact support for assistance.

Contact Support →

Compression Endpoint

new

Compress text prompts intelligently to reduce token usage and LLM costs. The compression endpoint optimizes your prompts while preserving semantic meaning.

Endpoint Overview

POST/api/compress

Compress a text prompt using intelligent optimization to reduce token count while maintaining semantic meaning.

Base URL

https://lesstokens.hive-hub.ai

Content-Type

application/json

Authentication

X-API-Key header

Request Format

Example Request
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Your text to compress here..."
  }'

Request Parameters

promptstringrequired

The text content to compress. Can be plain text, code, JSON, or any string format.

Constraints:

  • Minimum: 1 character
  • Maximum: 100,000 characters
  • UTF-8 encoding supported

Example:

"Large language models are powerful but expensive. This API helps reduce costs."

Response Format

A successful compression returns a JSON object with the compressed text and detailed metrics:

Success Response (200 OK)
{
  "compressedPrompt": "LLMs powerful but expensive. API reduces costs.",
  "originalTokens": 28,
  "compressedTokens": 16,
  "compressionRatio": 0.43,
  "savingsPercentage": 42.86,
  "processingTimeMs": 45,
  "id": "req_abc123xyz",
  "timestamp": "2025-01-18T14:30:00Z"
}

Response Fields

compressedPromptstring

The compressed version of your input prompt

originalTokensnumber

Token count of the original prompt

compressedTokensnumber

Token count of the compressed prompt

compressionRationumber

Ratio of compression (0.0-1.0, where 0.5 = 50% compression)

savingsPercentagenumber

Percentage of tokens saved

processingTimeMsnumber

Time taken to process the request in milliseconds

idstring

Unique identifier for this compression request

timestampstring

ISO 8601 timestamp of when the request was processed

Usage Examples

Text Compression

Automatic intelligent compression

Request:

{
  "prompt": "The quick brown fox jumps over the lazy dog. This is a test sentence with some redundancy and unnecessary verbosity."
}

Response:

{
  "compressedPrompt": "Quick brown fox jumps lazy dog. Test sentence.",
  "originalTokens": 28,
  "compressedTokens": 12,
  "compressionRatio": 0.57,
  "savingsPercentage": 57.14,
  "processingTimeMs": 32,
  "id": "req_abc123",
  "timestamp": "2025-01-18T14:30:00Z"
}

Structured Data Compression

Automatic optimization for JSON and structured content

Request:

{
  "prompt": "{\n  \"user\": {\n    \"name\": \"John\",\n    \"age\": 30,\n    \"email\": \"[email protected]\"\n  }\n}"
}

Response:

{
  "compressedPrompt": "{\"user\":{\"name\":\"John\",\"age\":30,\"email\":\"[email protected]\"}}",
  "originalTokens": 47,
  "compressedTokens": 24,
  "compressionRatio": 0.49,
  "savingsPercentage": 48.94,
  "processingTimeMs": 28,
  "id": "req_def456",
  "timestamp": "2025-01-18T14:31:00Z"
}

Code Compression

Automatic optimization for code snippets

Request:

{
  "prompt": "function calculateTotal(a, b) {\n  // Calculate the sum\n  const total = a + b;\n  // Return result\n  return total;\n}"
}

Response:

{
  "compressedPrompt": "function calculateTotal(a,b){const total=a+b;return total;}",
  "originalTokens": 35,
  "compressedTokens": 18,
  "compressionRatio": 0.49,
  "savingsPercentage": 48.57,
  "processingTimeMs": 25,
  "id": "req_ghi789",
  "timestamp": "2025-01-18T14:32:00Z"
}

Error Responses

400

Bad Request

Invalid request body or parameters

Example Response:

{
  "error": "Bad Request",
  "message": "prompt is required and must be a string",
  "code": "VALIDATION_ERROR",
  "field": "prompt"
}
401

Unauthorized

Missing or invalid API key

Example Response:

{
  "error": "Unauthorized",
  "message": "Invalid or missing API key",
  "code": "AUTHENTICATION_REQUIRED"
}
413

Payload Too Large

Prompt exceeds maximum size

Example Response:

{
  "error": "Payload Too Large",
  "message": "Prompt exceeds maximum size of 100KB",
  "code": "PAYLOAD_TOO_LARGE",
  "maxSize": 100000
}
429

Too Many Requests

Rate limit exceeded

Example Response:

{
  "error": "Too Many Requests",
  "message": "Rate limit exceeded. Try again in 3600 seconds",
  "code": "RATE_LIMIT_EXCEEDED",
  "retryAfter": 3600
}
500

Internal Server Error

Server-side error during compression

Example Response:

{
  "error": "Internal Server Error",
  "message": "An error occurred during compression",
  "code": "INTERNAL_ERROR",
  "requestId": "req_xyz789"
}

Best Practices

Trust Automatic Optimization

Our algorithms automatically detect and optimize your content type - no configuration needed

Tip: Structured data (JSON, code) typically achieves 35-50% compression, while prose achieves 20-25%

Monitor Processing Time

Adjust maxTimeMs based on your latency requirements

Tip: Lower maxTimeMs (1000-3000ms) for real-time apps, higher (10000ms+) for batch processing

Validate Compressed Output

Always verify the compressed prompt maintains semantic meaning

Tip: Test with your LLM to ensure quality isn't sacrificed for compression

Performance Tips

Batch Processing

If processing multiple prompts, use concurrent requests with rate limit awareness

Up to 5x faster

Caching

Cache compression results for frequently used prompts

99% latency reduction

Timeout Configuration

Set maxTimeMs appropriately for your use case

Balanced speed/quality

Error Handling

Implement retry logic with exponential backoff

Higher reliability

Next Steps

Ready to start compressing? Try the endpoint or explore error handling: