Skip to main content

Python SDK for The Token Company API - Compress LLM inputs to reduce costs

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

tokenc - Python SDK for The Token Company

A Python client library for compressing LLM inputs to reduce token usage, lower costs, and speed up AI applications.

Installation

pip install tokenc

Quick Start

from tokenc import TokenClient

# Initialize the client
client = TokenClient(api_key="your-api-key")

# Compress text
response = client.compress_input(
    input="Your long text here that needs compression...",
    aggressiveness=0.5
)

print(f"Compressed text: {response.output}")
print(f"Original tokens: {response.original_input_tokens}")
print(f"Compressed tokens: {response.output_tokens}")
print(f"Tokens saved: {response.tokens_saved}")
print(f"Compression ratio: {response.compression_ratio:.2f}x")

Features

  • 🚀 Simple and intuitive API
  • 🎯 Type-safe with dataclasses
  • 🔧 Flexible compression settings
  • 📊 Built-in compression metrics
  • ⚡ Context manager support
  • 🛡️ Comprehensive error handling

Usage Examples

Basic Compression

from tokenc import TokenClient

client = TokenClient(api_key="your-api-key")

response = client.compress_input(
    input="This is a long text that contains lots of unnecessary filler words and redundant information that can be compressed.",
    aggressiveness=0.5
)

print(response.output)

Advanced Compression with Settings

from tokenc import TokenClient, CompressionSettings

client = TokenClient(api_key="your-api-key")

# Create custom compression settings
settings = CompressionSettings(
    aggressiveness=0.7,
    max_output_tokens=100,
    min_output_tokens=50
)

response = client.compress_input(
    input="Your text here...",
    compression_settings=settings
)

print(f"Compression percentage: {response.compression_percentage:.1f}%")

Using as Context Manager

from tokenc import TokenClient

with TokenClient(api_key="your-api-key") as client:
    response = client.compress_input(
        input="Your text here...",
        aggressiveness=0.6
    )
    print(response.output)
# Session automatically closed

Different Compression Levels

from tokenc import TokenClient

client = TokenClient(api_key="your-api-key")

text = "Your long text here..."

# Light compression - preserve most content
light = client.compress_input(input=text, aggressiveness=0.2)

# Moderate compression - balanced approach
moderate = client.compress_input(input=text, aggressiveness=0.5)

# Aggressive compression - maximum savings
aggressive = client.compress_input(input=text, aggressiveness=0.8)

Compression Aggressiveness Levels

  • 0.1-0.3: Light compression - Removes obvious filler words
  • 0.4-0.6: Moderate compression - Balanced approach (recommended)
  • 0.7-0.9: Aggressive compression - Maximum cost savings

API Reference

TokenClient

__init__(api_key: str, base_url: str = ..., timeout: int = 30)

Initialize the Token Company API client.

Parameters:

  • api_key (str): Your API key for authentication
  • base_url (str, optional): Base URL for the API
  • timeout (int, optional): Request timeout in seconds

compress_input(...) -> CompressResponse

Compress text input for optimized LLM inference.

Parameters:

  • input (str): The text to compress
  • model (str, optional): Model to use (default: "bear-1")
  • aggressiveness (float, optional): Compression intensity 0.0-1.0 (default: 0.5)
  • max_output_tokens (int, optional): Maximum token count for output
  • min_output_tokens (int, optional): Minimum token count for output
  • compression_settings (CompressionSettings, optional): Custom settings object

Returns:

  • CompressResponse: Object containing compressed output and metadata

Raises:

  • AuthenticationError: Invalid API key
  • InvalidRequestError: Invalid request parameters
  • RateLimitError: Rate limit exceeded
  • APIError: Other API errors

CompressionSettings

Dataclass for compression configuration.

Attributes:

  • aggressiveness (float): Compression intensity 0.0-1.0
  • max_output_tokens (int | None): Optional maximum output tokens
  • min_output_tokens (int | None): Optional minimum output tokens

CompressResponse

Dataclass for compression results.

Attributes:

  • output (str): The compressed text
  • output_tokens (int): Token count of compressed output
  • original_input_tokens (int): Token count of original input
  • compression_time (float): Processing duration in seconds

Properties:

  • tokens_saved (int): Number of tokens saved
  • compression_ratio (float): Ratio of original to compressed tokens
  • compression_percentage (float): Percentage reduction in tokens

Error Handling

from tokenc import TokenClient, AuthenticationError, InvalidRequestError, RateLimitError, APIError

client = TokenClient(api_key="your-api-key")

try:
    response = client.compress_input(input="Your text...")
except AuthenticationError:
    print("Invalid API key")
except InvalidRequestError as e:
    print(f"Invalid request: {e}")
except RateLimitError:
    print("Rate limit exceeded, please wait")
except APIError as e:
    print(f"API error: {e}")

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/yourusername/tokenc.git
cd tokenc

# Install in development mode
pip install -e ".[dev]"

Running Tests

pytest tests/

Code Formatting

black tokenc/

License

MIT License - see LICENSE file for details

Support

For issues and questions:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokenc-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokenc-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file tokenc-0.1.0.tar.gz.

File metadata

  • Download URL: tokenc-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for tokenc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5414040ae7970333578ab2f1d01374de2bef59e7bcdae155de1d4c4518952741
MD5 07172ca7a05722ea4cff78e460b6f7e7
BLAKE2b-256 89022a5efb3a91ae53cd9d12b2c1e217e05f8aefb84faa4f6641f946191d0ec4

See more details on using hashes here.

File details

Details for the file tokenc-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tokenc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for tokenc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 037f1ed44b60b662dc852c6fa0e80a8cdc184209cc9d35f2e3d4b4ea56fb63b6
MD5 d44a89d004c325ddaa8243c80024e035
BLAKE2b-256 661f01135ba6f4e0a5d21eaa1a1693622de22ef707d56570bdd7b871119e187c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page