LessTokens Logo
Api Reference
Errors

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 →

Error Reference

Complete reference for all API error codes, their causes, and solutions. Learn how to handle errors gracefully in your applications.

Error Response Format

All API errors follow a consistent JSON structure for easy parsing and handling:

Error Response Structure
{
  "error": "Error Title",
  "message": "Detailed error description",
  "code": "ERROR_CODE",
  "requestId": "req_abc123xyz",
  "timestamp": "2025-01-18T14:30:00Z",
  "details": {
    // Additional context-specific information
  }
}

HTTP Status Codes

4xx Client Errors

Errors caused by invalid requests from the client

400

Bad Request

Invalid request body, parameters, or formatting

Common Causes:

  • Missing required field (prompt)
  • Invalid JSON format
  • Invalid parameter type
  • Prompt exceeds character limit

Solution:

Verify request body matches API specification. Check prompt is a string and within size limits.

Example Response:

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

Unauthorized

Missing or invalid API key

Common Causes:

  • No X-API-Key header provided
  • Invalid API key format
  • Revoked or expired API key
  • API key does not exist

Solution:

Verify API key in dashboard and ensure X-API-Key header is included with valid key.

Example Response:

{
  "error": "Unauthorized",
  "message": "Invalid or missing API key",
  "code": "AUTHENTICATION_REQUIRED",
  "requestId": "req_abc123"
}
403

Forbidden

Valid key but insufficient permissions or quota

Common Causes:

  • Rate limit exceeded
  • API key lacks required permissions
  • Account suspended
  • Quota exhausted

Solution:

Check rate limits and account status in dashboard. Wait before retrying or upgrade plan.

Example Response:

{
  "error": "Forbidden",
  "message": "Rate limit exceeded. Retry after 3600 seconds",
  "code": "RATE_LIMIT_EXCEEDED",
  "retryAfter": 3600,
  "requestId": "req_abc123"
}
413

Payload Too Large

Request body exceeds maximum size

Common Causes:

  • Prompt exceeds 100KB limit
  • Request headers too large

Solution:

Reduce prompt size or split into multiple requests. Maximum prompt size is 100,000 characters.

Example Response:

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

Too Many Requests

Rate limit exceeded for your tier

Common Causes:

  • Too many requests in time window
  • Burst limit exceeded
  • Daily quota reached

Solution:

Implement exponential backoff and retry logic. Check X-RateLimit-* headers for limits.

Example Response:

{
  "error": "Too Many Requests",
  "message": "Rate limit exceeded. Try again in 3600 seconds",
  "code": "RATE_LIMIT_EXCEEDED",
  "retryAfter": 3600,
  "limit": 100,
  "remaining": 0,
  "reset": 1705590000,
  "requestId": "req_abc123"
}

5xx Server Errors

Errors caused by server-side issues

500

Internal Server Error

Unexpected server-side error during processing

Common Causes:

  • Compression algorithm failure
  • Database connection error
  • Internal service timeout
  • Unexpected exception

Solution:

Retry request with exponential backoff. If persists, contact support with requestId.

Example Response:

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

Service Unavailable

Service temporarily unavailable

Common Causes:

  • Scheduled maintenance
  • System overload
  • Service deployment

Solution:

Wait and retry. Check status page for maintenance schedules.

Example Response:

{
  "error": "Service Unavailable",
  "message": "Service temporarily unavailable. Try again later",
  "code": "SERVICE_UNAVAILABLE",
  "retryAfter": 60,
  "requestId": "req_abc123"
}
504

Gateway Timeout

Request timeout during processing

Common Causes:

  • Compression took longer than maxTimeMs
  • Network issues
  • Upstream service timeout

Solution:

Increase maxTimeMs option or reduce prompt size. Retry with smaller payload.

Example Response:

{
  "error": "Gateway Timeout",
  "message": "Request timeout after 30000ms",
  "code": "GATEWAY_TIMEOUT",
  "timeout": 30000,
  "requestId": "req_abc123"
}

Error Codes Reference

Application-specific error codes returned in the code field:

AUTHENTICATION_REQUIRED
401

API key missing or invalid

VALIDATION_ERROR
400

Request validation failed

RATE_LIMIT_EXCEEDED
429

Too many requests

PAYLOAD_TOO_LARGE
413

Request exceeds size limit

INTERNAL_ERROR
500

Server-side error

SERVICE_UNAVAILABLE
503

Service temporarily down

GATEWAY_TIMEOUT
504

Request processing timeout

COMPRESSION_FAILED
500

Compression algorithm failed

Error Handling Best Practices

Retry Logic with Exponential Backoff

retry-logic.ts
async function compressWithRetry(
  prompt: string,
  maxRetries = 3
): Promise<CompressResponse> {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      return await compress(prompt);
    } catch (error: any) {
      const isRetryable = [429, 500, 503, 504].includes(error.status);
      const isLastAttempt = attempt === maxRetries - 1;

      if (!isRetryable || isLastAttempt) {
        throw error;
      }

      // Exponential backoff: 1s, 2s, 4s, 8s...
      const delay = Math.pow(2, attempt) * 1000;
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  }
}

Handling Rate Limits

rate-limit-handler.ts
async function handleRateLimit(error: any): Promise<void> {
  if (error.code === 'RATE_LIMIT_EXCEEDED') {
    const retryAfter = error.retryAfter || 60;
    
    console.log(`Rate limit hit. Retrying in ${retryAfter} seconds`);
    
    // Wait for retry period
    await new Promise(resolve => 
      setTimeout(resolve, retryAfter * 1000)
    );
    
    // Check rate limit headers
    const remaining = error.remaining || 0;
    const reset = error.reset || Date.now() + retryAfter * 1000;
    
    console.log(`Rate limit resets at: ${new Date(reset * 1000)}`);
  }
}

Error Classification

error-classifier.ts
function classifyError(error: any): 'client' | 'server' | 'network' {
  const status = error.status;
  
  if (status >= 400 && status < 500) {
    return 'client'; // Fix in your code
  }
  
  if (status >= 500) {
    return 'server'; // Retry or contact support
  }
  
  return 'network'; // Network/connection issue
}

function shouldRetry(error: any): boolean {
  const retryableStatuses = [429, 500, 502, 503, 504];
  const retryableCodes = [
    'RATE_LIMIT_EXCEEDED',
    'INTERNAL_ERROR',
    'SERVICE_UNAVAILABLE',
    'GATEWAY_TIMEOUT'
  ];
  
  return retryableStatuses.includes(error.status) ||
         retryableCodes.includes(error.code);
}

Best Practices

Log Request IDs

Always log requestId from errors for debugging and support

Implement Timeouts

Set request timeouts to prevent hanging connections

Use Retry Logic

Implement exponential backoff for transient errors

Handle Gracefully

Show user-friendly messages, log technical details

Rate Limit Headers

All responses include rate limit information in headers:

X-RateLimit-Limit

Maximum requests allowed per hour

100
X-RateLimit-Remaining

Requests remaining in current window

75
X-RateLimit-Reset

Unix timestamp when limit resets

1705590000
Retry-After

Seconds to wait before retrying (only on 429/503)

3600