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.1.tar.gz (17.8 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.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tokenc-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a59d4b81120358d1bafebff13b46aa517dd3bb22f10a005ffdb18bbc73fb6b64
MD5 0e50afa2950a5074755ad5fc35855be8
BLAKE2b-256 6401eb2aa0f866187d03b36858e917e85909f3c9e99444eefbea81fe740281d4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tokenc-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a7d3088f24fa7af49cb227db8caa7ca98d8d19386c817869ce60d3151422211e
MD5 cf82d1db218711be8ceb4c8e8e32c3d9
BLAKE2b-256 e0105d1ecb0c13c4ca13d9c872f2e4df9eae92be7862614252c5d998ebd8d94b

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