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 authenticationbase_url(str, optional): Base URL for the APItimeout(int, optional): Request timeout in seconds
compress_input(...) -> CompressResponse
Compress text input for optimized LLM inference.
Parameters:
input(str): The text to compressmodel(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 outputmin_output_tokens(int, optional): Minimum token count for outputcompression_settings(CompressionSettings, optional): Custom settings object
Returns:
CompressResponse: Object containing compressed output and metadata
Raises:
AuthenticationError: Invalid API keyInvalidRequestError: Invalid request parametersRateLimitError: Rate limit exceededAPIError: Other API errors
CompressionSettings
Dataclass for compression configuration.
Attributes:
aggressiveness(float): Compression intensity 0.0-1.0max_output_tokens(int | None): Optional maximum output tokensmin_output_tokens(int | None): Optional minimum output tokens
CompressResponse
Dataclass for compression results.
Attributes:
output(str): The compressed textoutput_tokens(int): Token count of compressed outputoriginal_input_tokens(int): Token count of original inputcompression_time(float): Processing duration in seconds
Properties:
tokens_saved(int): Number of tokens savedcompression_ratio(float): Ratio of original to compressed tokenscompression_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:
- GitHub Issues: https://github.com/yourusername/tokenc/issues
- Documentation: https://thetokencompany.com/docs
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a59d4b81120358d1bafebff13b46aa517dd3bb22f10a005ffdb18bbc73fb6b64
|
|
| MD5 |
0e50afa2950a5074755ad5fc35855be8
|
|
| BLAKE2b-256 |
6401eb2aa0f866187d03b36858e917e85909f3c9e99444eefbea81fe740281d4
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7d3088f24fa7af49cb227db8caa7ca98d8d19386c817869ce60d3151422211e
|
|
| MD5 |
cf82d1db218711be8ceb4c8e8e32c3d9
|
|
| BLAKE2b-256 |
e0105d1ecb0c13c4ca13d9c872f2e4df9eae92be7862614252c5d998ebd8d94b
|