Skip to main content

Python SDK for The Token Company — compress LLM prompts to reduce costs and latency

Project description

The Token Company Python SDK

Compress LLM prompts to reduce costs and latency. 100K tokens compressed in ~85ms.

CI PyPI version Python versions License: MIT

Docs · Website · Dashboard · Node.js SDK

Install

pip install the-token-company

Quick start

from thetokencompany import TheTokenCompany

client = TheTokenCompany(api_key="ttc-...")
result = client.compress("Your long prompt text here...", model="bear-2")

print(result.output)           # compressed text
print(result.tokens_saved)     # tokens removed
print(result.compression_ratio)  # e.g. 1.8

SDK wrappers

Drop-in wrappers that auto-compress all non-assistant messages before sending to your LLM. Assistant messages pass through unchanged so the provider's KV cache stays warm.

OpenAI / OpenRouter

from openai import OpenAI
from thetokencompany.openai import with_compression

client = with_compression(OpenAI(), compression_api_key="ttc-...")

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant..."},
        {"role": "user", "content": "Summarize these results..."},
    ],
)

Works with AsyncOpenAI too — the wrapper detects async automatically.

Anthropic

from anthropic import Anthropic
from thetokencompany.anthropic import with_compression

client = with_compression(Anthropic(), compression_api_key="ttc-...")

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a helpful assistant...",
    messages=[{"role": "user", "content": "Summarize these results..."}],
)

Both messages and the system parameter are compressed.

Async

from thetokencompany import AsyncTheTokenCompany

async with AsyncTheTokenCompany(api_key="ttc-...") as client:
    result = await client.compress("Your long prompt text...")

Models

Model Description
bear-2 Latest, recommended
bear-1.2 Previous generation

Aggressiveness

Control compression intensity with aggressiveness (0.0 – 1.0, default 0.5):

result = client.compress(text, model="bear-2", aggressiveness=0.8)

App ID

Tag compression requests with an application identifier for usage tracking:

# Set on the client — applies to all requests
client = TheTokenCompany(api_key="ttc-...", app_id="my-chatbot")

# Or per-request (overrides the client-level value)
result = client.compress(text, model="bear-2", app_id="my-chatbot")

Also supported in wrappers:

client = with_compression(OpenAI(), compression_api_key="ttc-...", app_id="my-chatbot")

Gzip

Enable gzip compression of request payloads for better performance on large inputs (up to 2.2x faster on 1M+ tokens):

client = TheTokenCompany(api_key="ttc-...", gzip=True)

Protect text from compression

Use protect() to wrap content in <ttc_safe> tags — protected text passes through unchanged:

from thetokencompany import protect

prompt = f"{protect('system:')} You are a helpful assistant.\n{protect('user:')} Hello!"
result = client.compress(prompt, model="bear-2")

Response

CompressResponse fields:

Field Type Description
output str Compressed text
output_tokens int Token count after compression
input_tokens int Token count before compression
tokens_saved int Tokens removed
compression_ratio float Ratio (e.g. 1.8x)

License

MIT

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

the_token_company-0.3.2.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

the_token_company-0.3.2-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file the_token_company-0.3.2.tar.gz.

File metadata

  • Download URL: the_token_company-0.3.2.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for the_token_company-0.3.2.tar.gz
Algorithm Hash digest
SHA256 b3b9b4df95c1d31d3c51c4615813e1c5a6ef1f4bc5d2d5979d9ec7fdf6cf23f4
MD5 374ee98310f7ad98ef3ad01314437fac
BLAKE2b-256 429b2b9e1caa71f967073e0bd797b0b515db5220d046b88ceaca7d0157eb3627

See more details on using hashes here.

File details

Details for the file the_token_company-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for the_token_company-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1923732e188033d01e6135a97891eaf38fbf4f62ed78b9736f2ef7465ca42bc5
MD5 3b34bab452c7b58263ccb10ffcbfda43
BLAKE2b-256 e4d312a0cdfa2e7f476b0f1dd023158032e54a26bcd03042d36417a693629236

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