tokenc 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tokenc-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Token Company
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include LICENSE
3
+ include requirements.txt
4
+ recursive-include tokenc *.py
5
+ recursive-exclude * __pycache__
6
+ recursive-exclude * *.py[co]
tokenc-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,272 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenc
3
+ Version: 0.1.0
4
+ Summary: Python SDK for The Token Company API - Compress LLM inputs to reduce costs
5
+ Home-page: https://github.com/yourusername/tokenc
6
+ Author: The Token Company
7
+ Author-email: The Token Company <support@thetokencompany.com>
8
+ License: MIT
9
+ Project-URL: Homepage, https://thetokencompany.com
10
+ Project-URL: Documentation, https://thetokencompany.com/docs
11
+ Project-URL: Repository, https://github.com/yourusername/tokenc
12
+ Project-URL: Bug Tracker, https://github.com/yourusername/tokenc/issues
13
+ Keywords: llm,compression,tokens,ai,api,cost-optimization
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.7
20
+ Classifier: Programming Language :: Python :: 3.8
21
+ Classifier: Programming Language :: Python :: 3.9
22
+ Classifier: Programming Language :: Python :: 3.10
23
+ Classifier: Programming Language :: Python :: 3.11
24
+ Classifier: Programming Language :: Python :: 3.12
25
+ Requires-Python: >=3.7
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: requests>=2.25.0
29
+ Provides-Extra: dev
30
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
31
+ Requires-Dist: pytest-cov>=3.0.0; extra == "dev"
32
+ Requires-Dist: black>=22.0.0; extra == "dev"
33
+ Requires-Dist: mypy>=0.950; extra == "dev"
34
+ Requires-Dist: flake8>=4.0.0; extra == "dev"
35
+ Dynamic: author
36
+ Dynamic: home-page
37
+ Dynamic: license-file
38
+ Dynamic: requires-python
39
+
40
+ # tokenc - Python SDK for The Token Company
41
+
42
+ A Python client library for compressing LLM inputs to reduce token usage, lower costs, and speed up AI applications.
43
+
44
+ ## Installation
45
+
46
+ ```bash
47
+ pip install tokenc
48
+ ```
49
+
50
+ ## Quick Start
51
+
52
+ ```python
53
+ from tokenc import TokenClient
54
+
55
+ # Initialize the client
56
+ client = TokenClient(api_key="your-api-key")
57
+
58
+ # Compress text
59
+ response = client.compress_input(
60
+ input="Your long text here that needs compression...",
61
+ aggressiveness=0.5
62
+ )
63
+
64
+ print(f"Compressed text: {response.output}")
65
+ print(f"Original tokens: {response.original_input_tokens}")
66
+ print(f"Compressed tokens: {response.output_tokens}")
67
+ print(f"Tokens saved: {response.tokens_saved}")
68
+ print(f"Compression ratio: {response.compression_ratio:.2f}x")
69
+ ```
70
+
71
+ ## Features
72
+
73
+ - 🚀 Simple and intuitive API
74
+ - 🎯 Type-safe with dataclasses
75
+ - 🔧 Flexible compression settings
76
+ - 📊 Built-in compression metrics
77
+ - ⚡ Context manager support
78
+ - 🛡️ Comprehensive error handling
79
+
80
+ ## Usage Examples
81
+
82
+ ### Basic Compression
83
+
84
+ ```python
85
+ from tokenc import TokenClient
86
+
87
+ client = TokenClient(api_key="your-api-key")
88
+
89
+ response = client.compress_input(
90
+ input="This is a long text that contains lots of unnecessary filler words and redundant information that can be compressed.",
91
+ aggressiveness=0.5
92
+ )
93
+
94
+ print(response.output)
95
+ ```
96
+
97
+ ### Advanced Compression with Settings
98
+
99
+ ```python
100
+ from tokenc import TokenClient, CompressionSettings
101
+
102
+ client = TokenClient(api_key="your-api-key")
103
+
104
+ # Create custom compression settings
105
+ settings = CompressionSettings(
106
+ aggressiveness=0.7,
107
+ max_output_tokens=100,
108
+ min_output_tokens=50
109
+ )
110
+
111
+ response = client.compress_input(
112
+ input="Your text here...",
113
+ compression_settings=settings
114
+ )
115
+
116
+ print(f"Compression percentage: {response.compression_percentage:.1f}%")
117
+ ```
118
+
119
+ ### Using as Context Manager
120
+
121
+ ```python
122
+ from tokenc import TokenClient
123
+
124
+ with TokenClient(api_key="your-api-key") as client:
125
+ response = client.compress_input(
126
+ input="Your text here...",
127
+ aggressiveness=0.6
128
+ )
129
+ print(response.output)
130
+ # Session automatically closed
131
+ ```
132
+
133
+ ### Different Compression Levels
134
+
135
+ ```python
136
+ from tokenc import TokenClient
137
+
138
+ client = TokenClient(api_key="your-api-key")
139
+
140
+ text = "Your long text here..."
141
+
142
+ # Light compression - preserve most content
143
+ light = client.compress_input(input=text, aggressiveness=0.2)
144
+
145
+ # Moderate compression - balanced approach
146
+ moderate = client.compress_input(input=text, aggressiveness=0.5)
147
+
148
+ # Aggressive compression - maximum savings
149
+ aggressive = client.compress_input(input=text, aggressiveness=0.8)
150
+ ```
151
+
152
+ ## Compression Aggressiveness Levels
153
+
154
+ - **0.1-0.3**: Light compression - Removes obvious filler words
155
+ - **0.4-0.6**: Moderate compression - Balanced approach (recommended)
156
+ - **0.7-0.9**: Aggressive compression - Maximum cost savings
157
+
158
+ ## API Reference
159
+
160
+ ### TokenClient
161
+
162
+ #### `__init__(api_key: str, base_url: str = ..., timeout: int = 30)`
163
+
164
+ Initialize the Token Company API client.
165
+
166
+ **Parameters:**
167
+ - `api_key` (str): Your API key for authentication
168
+ - `base_url` (str, optional): Base URL for the API
169
+ - `timeout` (int, optional): Request timeout in seconds
170
+
171
+ #### `compress_input(...) -> CompressResponse`
172
+
173
+ Compress text input for optimized LLM inference.
174
+
175
+ **Parameters:**
176
+ - `input` (str): The text to compress
177
+ - `model` (str, optional): Model to use (default: "bear-1")
178
+ - `aggressiveness` (float, optional): Compression intensity 0.0-1.0 (default: 0.5)
179
+ - `max_output_tokens` (int, optional): Maximum token count for output
180
+ - `min_output_tokens` (int, optional): Minimum token count for output
181
+ - `compression_settings` (CompressionSettings, optional): Custom settings object
182
+
183
+ **Returns:**
184
+ - `CompressResponse`: Object containing compressed output and metadata
185
+
186
+ **Raises:**
187
+ - `AuthenticationError`: Invalid API key
188
+ - `InvalidRequestError`: Invalid request parameters
189
+ - `RateLimitError`: Rate limit exceeded
190
+ - `APIError`: Other API errors
191
+
192
+ ### CompressionSettings
193
+
194
+ Dataclass for compression configuration.
195
+
196
+ **Attributes:**
197
+ - `aggressiveness` (float): Compression intensity 0.0-1.0
198
+ - `max_output_tokens` (int | None): Optional maximum output tokens
199
+ - `min_output_tokens` (int | None): Optional minimum output tokens
200
+
201
+ ### CompressResponse
202
+
203
+ Dataclass for compression results.
204
+
205
+ **Attributes:**
206
+ - `output` (str): The compressed text
207
+ - `output_tokens` (int): Token count of compressed output
208
+ - `original_input_tokens` (int): Token count of original input
209
+ - `compression_time` (float): Processing duration in seconds
210
+
211
+ **Properties:**
212
+ - `tokens_saved` (int): Number of tokens saved
213
+ - `compression_ratio` (float): Ratio of original to compressed tokens
214
+ - `compression_percentage` (float): Percentage reduction in tokens
215
+
216
+ ## Error Handling
217
+
218
+ ```python
219
+ from tokenc import TokenClient, AuthenticationError, InvalidRequestError, RateLimitError, APIError
220
+
221
+ client = TokenClient(api_key="your-api-key")
222
+
223
+ try:
224
+ response = client.compress_input(input="Your text...")
225
+ except AuthenticationError:
226
+ print("Invalid API key")
227
+ except InvalidRequestError as e:
228
+ print(f"Invalid request: {e}")
229
+ except RateLimitError:
230
+ print("Rate limit exceeded, please wait")
231
+ except APIError as e:
232
+ print(f"API error: {e}")
233
+ ```
234
+
235
+ ## Development
236
+
237
+ ### Setup Development Environment
238
+
239
+ ```bash
240
+ # Clone the repository
241
+ git clone https://github.com/yourusername/tokenc.git
242
+ cd tokenc
243
+
244
+ # Install in development mode
245
+ pip install -e ".[dev]"
246
+ ```
247
+
248
+ ### Running Tests
249
+
250
+ ```bash
251
+ pytest tests/
252
+ ```
253
+
254
+ ### Code Formatting
255
+
256
+ ```bash
257
+ black tokenc/
258
+ ```
259
+
260
+ ## License
261
+
262
+ MIT License - see LICENSE file for details
263
+
264
+ ## Support
265
+
266
+ For issues and questions:
267
+ - GitHub Issues: https://github.com/yourusername/tokenc/issues
268
+ - Documentation: https://thetokencompany.com/docs
269
+
270
+ ## Contributing
271
+
272
+ Contributions are welcome! Please feel free to submit a Pull Request.
tokenc-0.1.0/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # tokenc - Python SDK for The Token Company
2
+
3
+ A Python client library for compressing LLM inputs to reduce token usage, lower costs, and speed up AI applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install tokenc
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```python
14
+ from tokenc import TokenClient
15
+
16
+ # Initialize the client
17
+ client = TokenClient(api_key="your-api-key")
18
+
19
+ # Compress text
20
+ response = client.compress_input(
21
+ input="Your long text here that needs compression...",
22
+ aggressiveness=0.5
23
+ )
24
+
25
+ print(f"Compressed text: {response.output}")
26
+ print(f"Original tokens: {response.original_input_tokens}")
27
+ print(f"Compressed tokens: {response.output_tokens}")
28
+ print(f"Tokens saved: {response.tokens_saved}")
29
+ print(f"Compression ratio: {response.compression_ratio:.2f}x")
30
+ ```
31
+
32
+ ## Features
33
+
34
+ - 🚀 Simple and intuitive API
35
+ - 🎯 Type-safe with dataclasses
36
+ - 🔧 Flexible compression settings
37
+ - 📊 Built-in compression metrics
38
+ - ⚡ Context manager support
39
+ - 🛡️ Comprehensive error handling
40
+
41
+ ## Usage Examples
42
+
43
+ ### Basic Compression
44
+
45
+ ```python
46
+ from tokenc import TokenClient
47
+
48
+ client = TokenClient(api_key="your-api-key")
49
+
50
+ response = client.compress_input(
51
+ input="This is a long text that contains lots of unnecessary filler words and redundant information that can be compressed.",
52
+ aggressiveness=0.5
53
+ )
54
+
55
+ print(response.output)
56
+ ```
57
+
58
+ ### Advanced Compression with Settings
59
+
60
+ ```python
61
+ from tokenc import TokenClient, CompressionSettings
62
+
63
+ client = TokenClient(api_key="your-api-key")
64
+
65
+ # Create custom compression settings
66
+ settings = CompressionSettings(
67
+ aggressiveness=0.7,
68
+ max_output_tokens=100,
69
+ min_output_tokens=50
70
+ )
71
+
72
+ response = client.compress_input(
73
+ input="Your text here...",
74
+ compression_settings=settings
75
+ )
76
+
77
+ print(f"Compression percentage: {response.compression_percentage:.1f}%")
78
+ ```
79
+
80
+ ### Using as Context Manager
81
+
82
+ ```python
83
+ from tokenc import TokenClient
84
+
85
+ with TokenClient(api_key="your-api-key") as client:
86
+ response = client.compress_input(
87
+ input="Your text here...",
88
+ aggressiveness=0.6
89
+ )
90
+ print(response.output)
91
+ # Session automatically closed
92
+ ```
93
+
94
+ ### Different Compression Levels
95
+
96
+ ```python
97
+ from tokenc import TokenClient
98
+
99
+ client = TokenClient(api_key="your-api-key")
100
+
101
+ text = "Your long text here..."
102
+
103
+ # Light compression - preserve most content
104
+ light = client.compress_input(input=text, aggressiveness=0.2)
105
+
106
+ # Moderate compression - balanced approach
107
+ moderate = client.compress_input(input=text, aggressiveness=0.5)
108
+
109
+ # Aggressive compression - maximum savings
110
+ aggressive = client.compress_input(input=text, aggressiveness=0.8)
111
+ ```
112
+
113
+ ## Compression Aggressiveness Levels
114
+
115
+ - **0.1-0.3**: Light compression - Removes obvious filler words
116
+ - **0.4-0.6**: Moderate compression - Balanced approach (recommended)
117
+ - **0.7-0.9**: Aggressive compression - Maximum cost savings
118
+
119
+ ## API Reference
120
+
121
+ ### TokenClient
122
+
123
+ #### `__init__(api_key: str, base_url: str = ..., timeout: int = 30)`
124
+
125
+ Initialize the Token Company API client.
126
+
127
+ **Parameters:**
128
+ - `api_key` (str): Your API key for authentication
129
+ - `base_url` (str, optional): Base URL for the API
130
+ - `timeout` (int, optional): Request timeout in seconds
131
+
132
+ #### `compress_input(...) -> CompressResponse`
133
+
134
+ Compress text input for optimized LLM inference.
135
+
136
+ **Parameters:**
137
+ - `input` (str): The text to compress
138
+ - `model` (str, optional): Model to use (default: "bear-1")
139
+ - `aggressiveness` (float, optional): Compression intensity 0.0-1.0 (default: 0.5)
140
+ - `max_output_tokens` (int, optional): Maximum token count for output
141
+ - `min_output_tokens` (int, optional): Minimum token count for output
142
+ - `compression_settings` (CompressionSettings, optional): Custom settings object
143
+
144
+ **Returns:**
145
+ - `CompressResponse`: Object containing compressed output and metadata
146
+
147
+ **Raises:**
148
+ - `AuthenticationError`: Invalid API key
149
+ - `InvalidRequestError`: Invalid request parameters
150
+ - `RateLimitError`: Rate limit exceeded
151
+ - `APIError`: Other API errors
152
+
153
+ ### CompressionSettings
154
+
155
+ Dataclass for compression configuration.
156
+
157
+ **Attributes:**
158
+ - `aggressiveness` (float): Compression intensity 0.0-1.0
159
+ - `max_output_tokens` (int | None): Optional maximum output tokens
160
+ - `min_output_tokens` (int | None): Optional minimum output tokens
161
+
162
+ ### CompressResponse
163
+
164
+ Dataclass for compression results.
165
+
166
+ **Attributes:**
167
+ - `output` (str): The compressed text
168
+ - `output_tokens` (int): Token count of compressed output
169
+ - `original_input_tokens` (int): Token count of original input
170
+ - `compression_time` (float): Processing duration in seconds
171
+
172
+ **Properties:**
173
+ - `tokens_saved` (int): Number of tokens saved
174
+ - `compression_ratio` (float): Ratio of original to compressed tokens
175
+ - `compression_percentage` (float): Percentage reduction in tokens
176
+
177
+ ## Error Handling
178
+
179
+ ```python
180
+ from tokenc import TokenClient, AuthenticationError, InvalidRequestError, RateLimitError, APIError
181
+
182
+ client = TokenClient(api_key="your-api-key")
183
+
184
+ try:
185
+ response = client.compress_input(input="Your text...")
186
+ except AuthenticationError:
187
+ print("Invalid API key")
188
+ except InvalidRequestError as e:
189
+ print(f"Invalid request: {e}")
190
+ except RateLimitError:
191
+ print("Rate limit exceeded, please wait")
192
+ except APIError as e:
193
+ print(f"API error: {e}")
194
+ ```
195
+
196
+ ## Development
197
+
198
+ ### Setup Development Environment
199
+
200
+ ```bash
201
+ # Clone the repository
202
+ git clone https://github.com/yourusername/tokenc.git
203
+ cd tokenc
204
+
205
+ # Install in development mode
206
+ pip install -e ".[dev]"
207
+ ```
208
+
209
+ ### Running Tests
210
+
211
+ ```bash
212
+ pytest tests/
213
+ ```
214
+
215
+ ### Code Formatting
216
+
217
+ ```bash
218
+ black tokenc/
219
+ ```
220
+
221
+ ## License
222
+
223
+ MIT License - see LICENSE file for details
224
+
225
+ ## Support
226
+
227
+ For issues and questions:
228
+ - GitHub Issues: https://github.com/yourusername/tokenc/issues
229
+ - Documentation: https://thetokencompany.com/docs
230
+
231
+ ## Contributing
232
+
233
+ Contributions are welcome! Please feel free to submit a Pull Request.
@@ -0,0 +1,51 @@
1
+ [build-system]
2
+ requires = ["setuptools>=45", "wheel", "setuptools_scm[toml]>=6.2"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tokenc"
7
+ version = "0.1.0"
8
+ description = "Python SDK for The Token Company API - Compress LLM inputs to reduce costs"
9
+ readme = "README.md"
10
+ authors = [
11
+ {name = "The Token Company", email = "support@thetokencompany.com"}
12
+ ]
13
+ license = {text = "MIT"}
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "Topic :: Software Development :: Libraries :: Python Modules",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.7",
21
+ "Programming Language :: Python :: 3.8",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ ]
27
+ keywords = ["llm", "compression", "tokens", "ai", "api", "cost-optimization"]
28
+ requires-python = ">=3.7"
29
+ dependencies = [
30
+ "requests>=2.25.0",
31
+ ]
32
+
33
+ [project.optional-dependencies]
34
+ dev = [
35
+ "pytest>=7.0.0",
36
+ "pytest-cov>=3.0.0",
37
+ "black>=22.0.0",
38
+ "mypy>=0.950",
39
+ "flake8>=4.0.0",
40
+ ]
41
+
42
+ [project.urls]
43
+ Homepage = "https://thetokencompany.com"
44
+ Documentation = "https://thetokencompany.com/docs"
45
+ Repository = "https://github.com/yourusername/tokenc"
46
+ "Bug Tracker" = "https://github.com/yourusername/tokenc/issues"
47
+
48
+ [tool.setuptools.packages.find]
49
+ where = ["."]
50
+ include = ["tokenc*"]
51
+ exclude = ["tests*", "examples*"]
@@ -0,0 +1 @@
1
+ requests>=2.25.0
tokenc-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
tokenc-0.1.0/setup.py ADDED
@@ -0,0 +1,44 @@
1
+ """
2
+ Setup configuration for tokenc package
3
+ """
4
+
5
+ from setuptools import setup, find_packages
6
+
7
+ with open("README.md", "r", encoding="utf-8") as fh:
8
+ long_description = fh.read()
9
+
10
+ setup(
11
+ name="tokenc",
12
+ version="0.1.0",
13
+ author="The Token Company",
14
+ description="Python SDK for The Token Company API - Compress LLM inputs to reduce costs",
15
+ long_description=long_description,
16
+ long_description_content_type="text/markdown",
17
+ url="https://github.com/yourusername/tokenc",
18
+ packages=find_packages(),
19
+ classifiers=[
20
+ "Development Status :: 3 - Alpha",
21
+ "Intended Audience :: Developers",
22
+ "Topic :: Software Development :: Libraries :: Python Modules",
23
+ "License :: OSI Approved :: MIT License",
24
+ "Programming Language :: Python :: 3",
25
+ "Programming Language :: Python :: 3.7",
26
+ "Programming Language :: Python :: 3.8",
27
+ "Programming Language :: Python :: 3.9",
28
+ "Programming Language :: Python :: 3.10",
29
+ "Programming Language :: Python :: 3.11",
30
+ ],
31
+ python_requires=">=3.7",
32
+ install_requires=[
33
+ "requests>=2.25.0",
34
+ ],
35
+ extras_require={
36
+ "dev": [
37
+ "pytest>=7.0.0",
38
+ "pytest-cov>=3.0.0",
39
+ "black>=22.0.0",
40
+ "mypy>=0.950",
41
+ "flake8>=4.0.0",
42
+ ],
43
+ },
44
+ )
@@ -0,0 +1,36 @@
1
+ """
2
+ tokenc - Python SDK for The Token Company API
3
+
4
+ A Python client library for compressing LLM inputs to reduce token usage,
5
+ lower costs, and speed up AI applications.
6
+ """
7
+
8
+ from tokenc.client import TokenClient
9
+ from tokenc.types import (
10
+ CompressionSettings,
11
+ CompressRequest,
12
+ CompressResponse,
13
+ )
14
+ from tokenc.errors import (
15
+ TokenCError,
16
+ AuthenticationError,
17
+ InvalidRequestError,
18
+ APIError,
19
+ RateLimitError,
20
+ )
21
+ from tokenc.constants import Model
22
+
23
+ __version__ = "0.1.0"
24
+
25
+ __all__ = [
26
+ "TokenClient",
27
+ "CompressionSettings",
28
+ "CompressRequest",
29
+ "CompressResponse",
30
+ "TokenCError",
31
+ "AuthenticationError",
32
+ "InvalidRequestError",
33
+ "APIError",
34
+ "RateLimitError",
35
+ "Model",
36
+ ]
@@ -0,0 +1,161 @@
1
+ """
2
+ Token Company API Client
3
+ """
4
+
5
+ from typing import Optional
6
+ import requests
7
+
8
+ from tokenc.constants import API_BASE_URL, DEFAULT_TIMEOUT, Model
9
+ from tokenc.types import CompressionSettings, CompressResponse
10
+ from tokenc.errors import (
11
+ AuthenticationError,
12
+ InvalidRequestError,
13
+ APIError,
14
+ RateLimitError,
15
+ )
16
+
17
+
18
+ class TokenClient:
19
+ """
20
+ Client for interacting with The Token Company API.
21
+
22
+ Example:
23
+ >>> from tokenc import TokenClient
24
+ >>> client = TokenClient(api_key="your-api-key")
25
+ >>> response = client.compress_input(
26
+ ... input="Your long text here...",
27
+ ... aggressiveness=0.5
28
+ ... )
29
+ >>> print(response.output)
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ api_key: str,
35
+ base_url: str = API_BASE_URL,
36
+ timeout: int = DEFAULT_TIMEOUT,
37
+ ):
38
+ """
39
+ Initialize the Token Company API client.
40
+
41
+ Args:
42
+ api_key: Your API key for authentication
43
+ base_url: Base URL for the API (default: https://api.thetokencompany.com)
44
+ timeout: Request timeout in seconds (default: 30)
45
+ """
46
+ self.api_key = api_key
47
+ self.base_url = base_url.rstrip("/")
48
+ self.timeout = timeout
49
+ self._session = self._create_session()
50
+
51
+ def _create_session(self) -> requests.Session:
52
+ """Create a requests session with default headers."""
53
+ session = requests.Session()
54
+ session.headers.update({
55
+ "Authorization": f"Bearer {self.api_key}",
56
+ "Content-Type": "application/json",
57
+ })
58
+ return session
59
+
60
+ def _handle_error(self, response: requests.Response) -> None:
61
+ """Handle API error responses."""
62
+ if response.status_code == 401:
63
+ raise AuthenticationError("Invalid API key")
64
+ elif response.status_code == 400:
65
+ error_msg = response.json().get("error", "Invalid request")
66
+ raise InvalidRequestError(error_msg)
67
+ elif response.status_code == 429:
68
+ raise RateLimitError("Rate limit exceeded")
69
+ elif response.status_code >= 500:
70
+ raise APIError(f"Server error: {response.status_code}")
71
+ else:
72
+ raise APIError(f"API error: {response.status_code}")
73
+
74
+ def compress_input(
75
+ self,
76
+ input: str,
77
+ model: str = Model.BEAR_1,
78
+ aggressiveness: float = 0.5,
79
+ max_output_tokens: Optional[int] = None,
80
+ min_output_tokens: Optional[int] = None,
81
+ compression_settings: Optional[CompressionSettings] = None,
82
+ ) -> CompressResponse:
83
+ """
84
+ Compress text input for optimized LLM inference.
85
+
86
+ Args:
87
+ input: The text to compress
88
+ model: Model to use for compression (default: "bear-1")
89
+ aggressiveness: Compression intensity from 0.0 to 1.0 (default: 0.5)
90
+ - 0.1-0.3: Light compression (obvious filler removal)
91
+ - 0.4-0.6: Moderate compression (balanced approach)
92
+ - 0.7-0.9: Aggressive compression (maximum cost savings)
93
+ max_output_tokens: Optional maximum token count for output
94
+ min_output_tokens: Optional minimum token count for output
95
+ compression_settings: Optional CompressionSettings object. If provided,
96
+ individual parameters (aggressiveness, max_output_tokens,
97
+ min_output_tokens) are ignored.
98
+
99
+ Returns:
100
+ CompressResponse containing the compressed output and metadata
101
+
102
+ Raises:
103
+ AuthenticationError: If API key is invalid
104
+ InvalidRequestError: If request parameters are invalid
105
+ RateLimitError: If rate limit is exceeded
106
+ APIError: For other API errors
107
+
108
+ Example:
109
+ >>> response = client.compress_input(
110
+ ... input="This is a very long text that needs compression...",
111
+ ... aggressiveness=0.7,
112
+ ... max_output_tokens=100
113
+ ... )
114
+ >>> print(f"Compressed: {response.output}")
115
+ >>> print(f"Saved {response.original_input_tokens - response.output_tokens} tokens")
116
+ """
117
+ # Build compression settings
118
+ if compression_settings is None:
119
+ compression_settings = CompressionSettings(
120
+ aggressiveness=aggressiveness,
121
+ max_output_tokens=max_output_tokens,
122
+ min_output_tokens=min_output_tokens,
123
+ )
124
+
125
+ # Build request payload
126
+ payload = {
127
+ "model": model,
128
+ "input": input,
129
+ "compression_settings": compression_settings.to_dict(),
130
+ }
131
+
132
+ # Make API request
133
+ try:
134
+ response = self._session.post(
135
+ f"{self.base_url}/v1/compress",
136
+ json=payload,
137
+ timeout=self.timeout,
138
+ )
139
+
140
+ if not response.ok:
141
+ self._handle_error(response)
142
+
143
+ data = response.json()
144
+ return CompressResponse.from_dict(data)
145
+
146
+ except requests.exceptions.Timeout:
147
+ raise APIError("Request timeout")
148
+ except requests.exceptions.RequestException as e:
149
+ raise APIError(f"Request failed: {str(e)}")
150
+
151
+ def close(self) -> None:
152
+ """Close the HTTP session."""
153
+ self._session.close()
154
+
155
+ def __enter__(self):
156
+ """Context manager entry."""
157
+ return self
158
+
159
+ def __exit__(self, exc_type, exc_val, exc_tb):
160
+ """Context manager exit."""
161
+ self.close()
@@ -0,0 +1,12 @@
1
+ """
2
+ Constants for The Token Company API
3
+ """
4
+
5
+
6
+ class Model:
7
+ """Available compression models."""
8
+ BEAR_1 = "bear-1"
9
+
10
+
11
+ API_BASE_URL = "https://api.thetokencompany.com"
12
+ DEFAULT_TIMEOUT = 30
@@ -0,0 +1,28 @@
1
+ """
2
+ Custom exceptions for The Token Company API
3
+ """
4
+
5
+
6
+ class TokenCError(Exception):
7
+ """Base exception for all Token Company API errors."""
8
+ pass
9
+
10
+
11
+ class AuthenticationError(TokenCError):
12
+ """Raised when API authentication fails."""
13
+ pass
14
+
15
+
16
+ class InvalidRequestError(TokenCError):
17
+ """Raised when the request parameters are invalid."""
18
+ pass
19
+
20
+
21
+ class APIError(TokenCError):
22
+ """Raised when the API returns an error."""
23
+ pass
24
+
25
+
26
+ class RateLimitError(TokenCError):
27
+ """Raised when the API rate limit is exceeded."""
28
+ pass
@@ -0,0 +1,116 @@
1
+ """
2
+ Type definitions for The Token Company API
3
+ """
4
+
5
+ from typing import Optional
6
+ from dataclasses import dataclass
7
+
8
+
9
+ @dataclass
10
+ class CompressionSettings:
11
+ """
12
+ Settings for text compression.
13
+
14
+ Attributes:
15
+ aggressiveness: Compression intensity from 0.0 to 1.0 (default: 0.5)
16
+ max_output_tokens: Optional maximum token count for output
17
+ min_output_tokens: Optional minimum token count for output
18
+ """
19
+ aggressiveness: float = 0.5
20
+ max_output_tokens: Optional[int] = None
21
+ min_output_tokens: Optional[int] = None
22
+
23
+ def __post_init__(self):
24
+ """Validate compression settings."""
25
+ if not 0.0 <= self.aggressiveness <= 1.0:
26
+ raise ValueError("aggressiveness must be between 0.0 and 1.0")
27
+
28
+ if self.max_output_tokens is not None and self.max_output_tokens < 1:
29
+ raise ValueError("max_output_tokens must be positive")
30
+
31
+ if self.min_output_tokens is not None and self.min_output_tokens < 1:
32
+ raise ValueError("min_output_tokens must be positive")
33
+
34
+ if (
35
+ self.max_output_tokens is not None
36
+ and self.min_output_tokens is not None
37
+ and self.min_output_tokens > self.max_output_tokens
38
+ ):
39
+ raise ValueError("min_output_tokens cannot exceed max_output_tokens")
40
+
41
+ def to_dict(self) -> dict:
42
+ """Convert to dictionary format for API requests."""
43
+ return {
44
+ "aggressiveness": self.aggressiveness,
45
+ "max_output_tokens": self.max_output_tokens,
46
+ "min_output_tokens": self.min_output_tokens,
47
+ }
48
+
49
+
50
+ @dataclass
51
+ class CompressRequest:
52
+ """
53
+ Request for text compression.
54
+
55
+ Attributes:
56
+ model: Model to use for compression
57
+ input: The text to compress
58
+ compression_settings: Settings for compression
59
+ """
60
+ model: str
61
+ input: str
62
+ compression_settings: CompressionSettings
63
+
64
+ def to_dict(self) -> dict:
65
+ """Convert to dictionary format for API requests."""
66
+ return {
67
+ "model": self.model,
68
+ "input": self.input,
69
+ "compression_settings": self.compression_settings.to_dict(),
70
+ }
71
+
72
+
73
+ @dataclass
74
+ class CompressResponse:
75
+ """
76
+ Response from text compression.
77
+
78
+ Attributes:
79
+ output: The compressed text
80
+ output_tokens: Token count of the compressed output
81
+ original_input_tokens: Token count of the original input
82
+ compression_time: Processing duration in seconds
83
+ """
84
+ output: str
85
+ output_tokens: int
86
+ original_input_tokens: int
87
+ compression_time: float
88
+
89
+ @classmethod
90
+ def from_dict(cls, data: dict) -> "CompressResponse":
91
+ """Create from API response dictionary."""
92
+ return cls(
93
+ output=data["output"],
94
+ output_tokens=data["output_tokens"],
95
+ original_input_tokens=data["original_input_tokens"],
96
+ compression_time=data["compression_time"],
97
+ )
98
+
99
+ @property
100
+ def tokens_saved(self) -> int:
101
+ """Calculate the number of tokens saved by compression."""
102
+ return self.original_input_tokens - self.output_tokens
103
+
104
+ @property
105
+ def compression_ratio(self) -> float:
106
+ """Calculate the compression ratio (original/compressed)."""
107
+ if self.output_tokens == 0:
108
+ return 0.0
109
+ return self.original_input_tokens / self.output_tokens
110
+
111
+ @property
112
+ def compression_percentage(self) -> float:
113
+ """Calculate the percentage reduction in tokens."""
114
+ if self.original_input_tokens == 0:
115
+ return 0.0
116
+ return (self.tokens_saved / self.original_input_tokens) * 100
@@ -0,0 +1,272 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenc
3
+ Version: 0.1.0
4
+ Summary: Python SDK for The Token Company API - Compress LLM inputs to reduce costs
5
+ Home-page: https://github.com/yourusername/tokenc
6
+ Author: The Token Company
7
+ Author-email: The Token Company <support@thetokencompany.com>
8
+ License: MIT
9
+ Project-URL: Homepage, https://thetokencompany.com
10
+ Project-URL: Documentation, https://thetokencompany.com/docs
11
+ Project-URL: Repository, https://github.com/yourusername/tokenc
12
+ Project-URL: Bug Tracker, https://github.com/yourusername/tokenc/issues
13
+ Keywords: llm,compression,tokens,ai,api,cost-optimization
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.7
20
+ Classifier: Programming Language :: Python :: 3.8
21
+ Classifier: Programming Language :: Python :: 3.9
22
+ Classifier: Programming Language :: Python :: 3.10
23
+ Classifier: Programming Language :: Python :: 3.11
24
+ Classifier: Programming Language :: Python :: 3.12
25
+ Requires-Python: >=3.7
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: requests>=2.25.0
29
+ Provides-Extra: dev
30
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
31
+ Requires-Dist: pytest-cov>=3.0.0; extra == "dev"
32
+ Requires-Dist: black>=22.0.0; extra == "dev"
33
+ Requires-Dist: mypy>=0.950; extra == "dev"
34
+ Requires-Dist: flake8>=4.0.0; extra == "dev"
35
+ Dynamic: author
36
+ Dynamic: home-page
37
+ Dynamic: license-file
38
+ Dynamic: requires-python
39
+
40
+ # tokenc - Python SDK for The Token Company
41
+
42
+ A Python client library for compressing LLM inputs to reduce token usage, lower costs, and speed up AI applications.
43
+
44
+ ## Installation
45
+
46
+ ```bash
47
+ pip install tokenc
48
+ ```
49
+
50
+ ## Quick Start
51
+
52
+ ```python
53
+ from tokenc import TokenClient
54
+
55
+ # Initialize the client
56
+ client = TokenClient(api_key="your-api-key")
57
+
58
+ # Compress text
59
+ response = client.compress_input(
60
+ input="Your long text here that needs compression...",
61
+ aggressiveness=0.5
62
+ )
63
+
64
+ print(f"Compressed text: {response.output}")
65
+ print(f"Original tokens: {response.original_input_tokens}")
66
+ print(f"Compressed tokens: {response.output_tokens}")
67
+ print(f"Tokens saved: {response.tokens_saved}")
68
+ print(f"Compression ratio: {response.compression_ratio:.2f}x")
69
+ ```
70
+
71
+ ## Features
72
+
73
+ - 🚀 Simple and intuitive API
74
+ - 🎯 Type-safe with dataclasses
75
+ - 🔧 Flexible compression settings
76
+ - 📊 Built-in compression metrics
77
+ - ⚡ Context manager support
78
+ - 🛡️ Comprehensive error handling
79
+
80
+ ## Usage Examples
81
+
82
+ ### Basic Compression
83
+
84
+ ```python
85
+ from tokenc import TokenClient
86
+
87
+ client = TokenClient(api_key="your-api-key")
88
+
89
+ response = client.compress_input(
90
+ input="This is a long text that contains lots of unnecessary filler words and redundant information that can be compressed.",
91
+ aggressiveness=0.5
92
+ )
93
+
94
+ print(response.output)
95
+ ```
96
+
97
+ ### Advanced Compression with Settings
98
+
99
+ ```python
100
+ from tokenc import TokenClient, CompressionSettings
101
+
102
+ client = TokenClient(api_key="your-api-key")
103
+
104
+ # Create custom compression settings
105
+ settings = CompressionSettings(
106
+ aggressiveness=0.7,
107
+ max_output_tokens=100,
108
+ min_output_tokens=50
109
+ )
110
+
111
+ response = client.compress_input(
112
+ input="Your text here...",
113
+ compression_settings=settings
114
+ )
115
+
116
+ print(f"Compression percentage: {response.compression_percentage:.1f}%")
117
+ ```
118
+
119
+ ### Using as Context Manager
120
+
121
+ ```python
122
+ from tokenc import TokenClient
123
+
124
+ with TokenClient(api_key="your-api-key") as client:
125
+ response = client.compress_input(
126
+ input="Your text here...",
127
+ aggressiveness=0.6
128
+ )
129
+ print(response.output)
130
+ # Session automatically closed
131
+ ```
132
+
133
+ ### Different Compression Levels
134
+
135
+ ```python
136
+ from tokenc import TokenClient
137
+
138
+ client = TokenClient(api_key="your-api-key")
139
+
140
+ text = "Your long text here..."
141
+
142
+ # Light compression - preserve most content
143
+ light = client.compress_input(input=text, aggressiveness=0.2)
144
+
145
+ # Moderate compression - balanced approach
146
+ moderate = client.compress_input(input=text, aggressiveness=0.5)
147
+
148
+ # Aggressive compression - maximum savings
149
+ aggressive = client.compress_input(input=text, aggressiveness=0.8)
150
+ ```
151
+
152
+ ## Compression Aggressiveness Levels
153
+
154
+ - **0.1-0.3**: Light compression - Removes obvious filler words
155
+ - **0.4-0.6**: Moderate compression - Balanced approach (recommended)
156
+ - **0.7-0.9**: Aggressive compression - Maximum cost savings
157
+
158
+ ## API Reference
159
+
160
+ ### TokenClient
161
+
162
+ #### `__init__(api_key: str, base_url: str = ..., timeout: int = 30)`
163
+
164
+ Initialize the Token Company API client.
165
+
166
+ **Parameters:**
167
+ - `api_key` (str): Your API key for authentication
168
+ - `base_url` (str, optional): Base URL for the API
169
+ - `timeout` (int, optional): Request timeout in seconds
170
+
171
+ #### `compress_input(...) -> CompressResponse`
172
+
173
+ Compress text input for optimized LLM inference.
174
+
175
+ **Parameters:**
176
+ - `input` (str): The text to compress
177
+ - `model` (str, optional): Model to use (default: "bear-1")
178
+ - `aggressiveness` (float, optional): Compression intensity 0.0-1.0 (default: 0.5)
179
+ - `max_output_tokens` (int, optional): Maximum token count for output
180
+ - `min_output_tokens` (int, optional): Minimum token count for output
181
+ - `compression_settings` (CompressionSettings, optional): Custom settings object
182
+
183
+ **Returns:**
184
+ - `CompressResponse`: Object containing compressed output and metadata
185
+
186
+ **Raises:**
187
+ - `AuthenticationError`: Invalid API key
188
+ - `InvalidRequestError`: Invalid request parameters
189
+ - `RateLimitError`: Rate limit exceeded
190
+ - `APIError`: Other API errors
191
+
192
+ ### CompressionSettings
193
+
194
+ Dataclass for compression configuration.
195
+
196
+ **Attributes:**
197
+ - `aggressiveness` (float): Compression intensity 0.0-1.0
198
+ - `max_output_tokens` (int | None): Optional maximum output tokens
199
+ - `min_output_tokens` (int | None): Optional minimum output tokens
200
+
201
+ ### CompressResponse
202
+
203
+ Dataclass for compression results.
204
+
205
+ **Attributes:**
206
+ - `output` (str): The compressed text
207
+ - `output_tokens` (int): Token count of compressed output
208
+ - `original_input_tokens` (int): Token count of original input
209
+ - `compression_time` (float): Processing duration in seconds
210
+
211
+ **Properties:**
212
+ - `tokens_saved` (int): Number of tokens saved
213
+ - `compression_ratio` (float): Ratio of original to compressed tokens
214
+ - `compression_percentage` (float): Percentage reduction in tokens
215
+
216
+ ## Error Handling
217
+
218
+ ```python
219
+ from tokenc import TokenClient, AuthenticationError, InvalidRequestError, RateLimitError, APIError
220
+
221
+ client = TokenClient(api_key="your-api-key")
222
+
223
+ try:
224
+ response = client.compress_input(input="Your text...")
225
+ except AuthenticationError:
226
+ print("Invalid API key")
227
+ except InvalidRequestError as e:
228
+ print(f"Invalid request: {e}")
229
+ except RateLimitError:
230
+ print("Rate limit exceeded, please wait")
231
+ except APIError as e:
232
+ print(f"API error: {e}")
233
+ ```
234
+
235
+ ## Development
236
+
237
+ ### Setup Development Environment
238
+
239
+ ```bash
240
+ # Clone the repository
241
+ git clone https://github.com/yourusername/tokenc.git
242
+ cd tokenc
243
+
244
+ # Install in development mode
245
+ pip install -e ".[dev]"
246
+ ```
247
+
248
+ ### Running Tests
249
+
250
+ ```bash
251
+ pytest tests/
252
+ ```
253
+
254
+ ### Code Formatting
255
+
256
+ ```bash
257
+ black tokenc/
258
+ ```
259
+
260
+ ## License
261
+
262
+ MIT License - see LICENSE file for details
263
+
264
+ ## Support
265
+
266
+ For issues and questions:
267
+ - GitHub Issues: https://github.com/yourusername/tokenc/issues
268
+ - Documentation: https://thetokencompany.com/docs
269
+
270
+ ## Contributing
271
+
272
+ Contributions are welcome! Please feel free to submit a Pull Request.
@@ -0,0 +1,16 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ requirements.txt
6
+ setup.py
7
+ tokenc/__init__.py
8
+ tokenc/client.py
9
+ tokenc/constants.py
10
+ tokenc/errors.py
11
+ tokenc/types.py
12
+ tokenc.egg-info/PKG-INFO
13
+ tokenc.egg-info/SOURCES.txt
14
+ tokenc.egg-info/dependency_links.txt
15
+ tokenc.egg-info/requires.txt
16
+ tokenc.egg-info/top_level.txt
@@ -0,0 +1,8 @@
1
+ requests>=2.25.0
2
+
3
+ [dev]
4
+ pytest>=7.0.0
5
+ pytest-cov>=3.0.0
6
+ black>=22.0.0
7
+ mypy>=0.950
8
+ flake8>=4.0.0
@@ -0,0 +1 @@
1
+ tokenc