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
Compress LLM prompts to reduce costs and latency. 100K tokens compressed in ~85ms.
Install
pip install tokenc
Usage
from tokenc import TokenClient
client = TokenClient(api_key="your-api-key")
result = client.compress_input(
input="Your long prompt here...",
model="bear-1.2", # or "bear-1.1", "bear-1", etc.
aggressiveness=0.5 # 0.1 = light, 0.5 = balanced, 0.9 = aggressive
)
print(result.output) # compressed text
print(result.tokens_saved) # tokens removed
print(result.compression_ratio) # e.g. 1.8x
Protected Content
Wrap text in <ttc_safe> tags to exclude it from compression:
result = client.compress_input(
input="Compress this but <ttc_safe>keep this exactly as is</ttc_safe>.",
model="bear-1.2",
aggressiveness=0.7
)
Context Manager
with TokenClient(api_key="your-api-key") as client:
result = client.compress_input(input="Your text...", model="bear-1.2", aggressiveness=0.5)
Performance
Requests are gzip-compressed and use HTTP keep-alive automatically.
| Input Size | E2E Latency | Throughput |
|---|---|---|
| 10K tokens | 38ms | 198K tok/s |
| 100K tokens | 85ms | 975K tok/s |
| 1M tokens | 542ms | 1.5M tok/s |
Error Handling
from tokenc import TokenClient, AuthenticationError, RateLimitError, APIError
try:
result = client.compress_input(input="Your text...", model="bear-1.2")
except AuthenticationError:
print("Invalid API key")
except RateLimitError:
print("Rate limit exceeded")
except APIError as e:
print(f"API error: {e}")
Links
- Get API keys: https://thetokencompany.com
- Issues: https://github.com/TheTokenCompany/tokenc-python-sdk/issues
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-1.0.0.tar.gz.
File metadata
- Download URL: tokenc-1.0.0.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06697738afbb6dfdec2e0053b8e7e17a12eb19f7c0ef0c511fd7cc8f618360d9
|
|
| MD5 |
866f851df55edd77fc77539552f3ce2b
|
|
| BLAKE2b-256 |
3b04b1e139f569779f9e92ef11da466f8bb8bd113708222375d781a0d4252553
|
File details
Details for the file tokenc-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tokenc-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.1 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 |
bd43f41a8de4baf19d0d7a4360c395cbf42354c99d932a51de80b9918f0136a1
|
|
| MD5 |
2cbb60030e32121ccb4aa834941c7f2e
|
|
| BLAKE2b-256 |
e728a750e26387228e83c1b250dc66b990ad1d962d4fab4dea3d78e118a96ce5
|