tokoscope 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.
- tokoscope-0.1.0/LICENSE +21 -0
- tokoscope-0.1.0/PKG-INFO +153 -0
- tokoscope-0.1.0/README.md +125 -0
- tokoscope-0.1.0/pyproject.toml +41 -0
- tokoscope-0.1.0/setup.cfg +4 -0
- tokoscope-0.1.0/tokoscope/__init__.py +4 -0
- tokoscope-0.1.0/tokoscope/tracker.py +24 -0
- tokoscope-0.1.0/tokoscope/wrapper.py +121 -0
- tokoscope-0.1.0/tokoscope.egg-info/PKG-INFO +153 -0
- tokoscope-0.1.0/tokoscope.egg-info/SOURCES.txt +11 -0
- tokoscope-0.1.0/tokoscope.egg-info/dependency_links.txt +1 -0
- tokoscope-0.1.0/tokoscope.egg-info/requires.txt +1 -0
- tokoscope-0.1.0/tokoscope.egg-info/top_level.txt +1 -0
tokoscope-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tokoscope
|
|
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.
|
tokoscope-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tokoscope
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Audit, compress and monitor your LLM token usage in 2 lines of code
|
|
5
|
+
Author-email: Tokoscope <hello@tokoscope.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://tokoscope.com
|
|
8
|
+
Project-URL: Documentation, https://app.tokoscope.com/onboarding
|
|
9
|
+
Project-URL: Repository, https://github.com/tokoscope/sdk
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/tokoscope/sdk/issues
|
|
11
|
+
Keywords: llm,openai,anthropic,tokens,optimization,cost,monitoring,prompt,compression,ai,token-usage,llm-cost,observability,gpt,claude
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Requires-Python: >=3.8
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests>=2.28.0
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# Tokoscope Python SDK
|
|
30
|
+
|
|
31
|
+
> Audit, compress, and monitor your LLM token usage in 2 lines of code.
|
|
32
|
+
|
|
33
|
+
[](https://pypi.org/project/tokoscope/)
|
|
34
|
+
[](https://pypi.org/project/tokoscope/)
|
|
35
|
+
[](https://opensource.org/licenses/MIT)
|
|
36
|
+
[](https://www.python.org/)
|
|
37
|
+
|
|
38
|
+
Tokoscope sits between your app and any LLM API. It tracks every call, scores your prompts for waste, compresses bloated inputs automatically, and shows you exactly where your token budget is going.
|
|
39
|
+
|
|
40
|
+
**Works with OpenAI and Anthropic out of the box.**
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install tokoscope
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Quick start
|
|
53
|
+
|
|
54
|
+
### OpenAI
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from openai import OpenAI
|
|
58
|
+
from tokoscope import wrap
|
|
59
|
+
|
|
60
|
+
client = wrap(OpenAI(), api_key="ts_live_...") # get your key at app.tokoscope.com
|
|
61
|
+
|
|
62
|
+
response = client.chat.completions.create(
|
|
63
|
+
model="gpt-4o",
|
|
64
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
print(response.choices[0].message.content)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Anthropic
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from anthropic import Anthropic
|
|
74
|
+
from tokoscope import wrap
|
|
75
|
+
|
|
76
|
+
client = wrap(Anthropic(), api_key="ts_live_...")
|
|
77
|
+
|
|
78
|
+
response = client.messages.create(
|
|
79
|
+
model="claude-sonnet-4-6",
|
|
80
|
+
max_tokens=1024,
|
|
81
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
print(response.content[0].text)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
That's it. Every API call is now tracked automatically.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Real example
|
|
92
|
+
|
|
93
|
+
**Original prompt:** 113 tokens
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
Please note that it is very important that you make sure to respond
|
|
97
|
+
to my question. As an AI, I want you to please make sure that you
|
|
98
|
+
understand that I need you to help me...
|
|
99
|
+
What is the capital of France?
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Tokoscope compressed:** 8 tokens
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
What is the capital of France? Answer concisely.
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Result:** 90% token reduction. Same answer.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## What you get
|
|
113
|
+
|
|
114
|
+
Once integrated, your [Tokoscope dashboard](https://app.tokoscope.com) shows:
|
|
115
|
+
|
|
116
|
+
- **Token usage** broken down by model, endpoint, and provider
|
|
117
|
+
- **Cost per request** calculated automatically using current pricing
|
|
118
|
+
- **Waste score** for every prompt
|
|
119
|
+
- **Auto-compression** — prompts with high waste scores are automatically rewritten
|
|
120
|
+
- **Budget alerts** — get notified before costs spike
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Supported providers
|
|
125
|
+
|
|
126
|
+
| Provider | Status |
|
|
127
|
+
|---|---|
|
|
128
|
+
| OpenAI | ✅ Supported |
|
|
129
|
+
| Anthropic | ✅ Supported |
|
|
130
|
+
| Gemini | 🔜 Coming soon |
|
|
131
|
+
| Mistral | 🔜 Coming soon |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Requirements
|
|
136
|
+
|
|
137
|
+
- Python 3.8+
|
|
138
|
+
- `requests` library (installed automatically)
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Links
|
|
149
|
+
|
|
150
|
+
- [Website](https://tokoscope.com)
|
|
151
|
+
- [Dashboard](https://app.tokoscope.com)
|
|
152
|
+
- [JavaScript SDK](https://www.npmjs.com/package/tokoscope)
|
|
153
|
+
- [Contact](mailto:hello@tokoscope.com)
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# Tokoscope Python SDK
|
|
2
|
+
|
|
3
|
+
> Audit, compress, and monitor your LLM token usage in 2 lines of code.
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/tokoscope/)
|
|
6
|
+
[](https://pypi.org/project/tokoscope/)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://www.python.org/)
|
|
9
|
+
|
|
10
|
+
Tokoscope sits between your app and any LLM API. It tracks every call, scores your prompts for waste, compresses bloated inputs automatically, and shows you exactly where your token budget is going.
|
|
11
|
+
|
|
12
|
+
**Works with OpenAI and Anthropic out of the box.**
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install tokoscope
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Quick start
|
|
25
|
+
|
|
26
|
+
### OpenAI
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from openai import OpenAI
|
|
30
|
+
from tokoscope import wrap
|
|
31
|
+
|
|
32
|
+
client = wrap(OpenAI(), api_key="ts_live_...") # get your key at app.tokoscope.com
|
|
33
|
+
|
|
34
|
+
response = client.chat.completions.create(
|
|
35
|
+
model="gpt-4o",
|
|
36
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
print(response.choices[0].message.content)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Anthropic
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from anthropic import Anthropic
|
|
46
|
+
from tokoscope import wrap
|
|
47
|
+
|
|
48
|
+
client = wrap(Anthropic(), api_key="ts_live_...")
|
|
49
|
+
|
|
50
|
+
response = client.messages.create(
|
|
51
|
+
model="claude-sonnet-4-6",
|
|
52
|
+
max_tokens=1024,
|
|
53
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
print(response.content[0].text)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
That's it. Every API call is now tracked automatically.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Real example
|
|
64
|
+
|
|
65
|
+
**Original prompt:** 113 tokens
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
Please note that it is very important that you make sure to respond
|
|
69
|
+
to my question. As an AI, I want you to please make sure that you
|
|
70
|
+
understand that I need you to help me...
|
|
71
|
+
What is the capital of France?
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**Tokoscope compressed:** 8 tokens
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
What is the capital of France? Answer concisely.
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Result:** 90% token reduction. Same answer.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## What you get
|
|
85
|
+
|
|
86
|
+
Once integrated, your [Tokoscope dashboard](https://app.tokoscope.com) shows:
|
|
87
|
+
|
|
88
|
+
- **Token usage** broken down by model, endpoint, and provider
|
|
89
|
+
- **Cost per request** calculated automatically using current pricing
|
|
90
|
+
- **Waste score** for every prompt
|
|
91
|
+
- **Auto-compression** — prompts with high waste scores are automatically rewritten
|
|
92
|
+
- **Budget alerts** — get notified before costs spike
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Supported providers
|
|
97
|
+
|
|
98
|
+
| Provider | Status |
|
|
99
|
+
|---|---|
|
|
100
|
+
| OpenAI | ✅ Supported |
|
|
101
|
+
| Anthropic | ✅ Supported |
|
|
102
|
+
| Gemini | 🔜 Coming soon |
|
|
103
|
+
| Mistral | 🔜 Coming soon |
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Requirements
|
|
108
|
+
|
|
109
|
+
- Python 3.8+
|
|
110
|
+
- `requests` library (installed automatically)
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
MIT
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Links
|
|
121
|
+
|
|
122
|
+
- [Website](https://tokoscope.com)
|
|
123
|
+
- [Dashboard](https://app.tokoscope.com)
|
|
124
|
+
- [JavaScript SDK](https://www.npmjs.com/package/tokoscope)
|
|
125
|
+
- [Contact](mailto:hello@tokoscope.com)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=42", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tokoscope"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Audit, compress and monitor your LLM token usage in 2 lines of code"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
authors = [{ name = "Tokoscope", email = "hello@tokoscope.com" }]
|
|
12
|
+
requires-python = ">=3.8"
|
|
13
|
+
dependencies = ["requests>=2.28.0"]
|
|
14
|
+
keywords = [
|
|
15
|
+
"llm", "openai", "anthropic", "tokens", "optimization",
|
|
16
|
+
"cost", "monitoring", "prompt", "compression", "ai",
|
|
17
|
+
"token-usage", "llm-cost", "observability", "gpt", "claude"
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"License :: OSI Approved :: MIT License",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3.8",
|
|
25
|
+
"Programming Language :: Python :: 3.9",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Programming Language :: Python :: 3.12",
|
|
29
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
30
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://tokoscope.com"
|
|
35
|
+
Documentation = "https://app.tokoscope.com/onboarding"
|
|
36
|
+
Repository = "https://github.com/tokoscope/sdk"
|
|
37
|
+
"Bug Tracker" = "https://github.com/tokoscope/sdk/issues"
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.packages.find]
|
|
40
|
+
where = ["."]
|
|
41
|
+
include = ["tokoscope*"]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
import requests
|
|
3
|
+
|
|
4
|
+
ENDPOINT = "https://us-central1-tokoscope-e8ab2.cloudfunctions.net/trackEvent"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def track(api_key: str, payload: dict) -> None:
|
|
8
|
+
"""Send tracking event to Tokoscope in a background thread."""
|
|
9
|
+
def _send():
|
|
10
|
+
try:
|
|
11
|
+
requests.post(
|
|
12
|
+
ENDPOINT,
|
|
13
|
+
json=payload,
|
|
14
|
+
headers={
|
|
15
|
+
"Content-Type": "application/json",
|
|
16
|
+
"Authorization": f"Bearer {api_key}"
|
|
17
|
+
},
|
|
18
|
+
timeout=5
|
|
19
|
+
)
|
|
20
|
+
except Exception:
|
|
21
|
+
pass # Silent fail — never break the main app
|
|
22
|
+
|
|
23
|
+
thread = threading.Thread(target=_send, daemon=True)
|
|
24
|
+
thread.start()
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from .tracker import track
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class OpenAIWrapper:
|
|
5
|
+
"""Wraps the OpenAI client to track token usage."""
|
|
6
|
+
|
|
7
|
+
def __init__(self, client, api_key: str):
|
|
8
|
+
self._client = client
|
|
9
|
+
self._api_key = api_key
|
|
10
|
+
self.chat = ChatWrapper(client.chat, api_key)
|
|
11
|
+
|
|
12
|
+
def __getattr__(self, name):
|
|
13
|
+
return getattr(self._client, name)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ChatWrapper:
|
|
17
|
+
def __init__(self, chat, api_key: str):
|
|
18
|
+
self._chat = chat
|
|
19
|
+
self._api_key = api_key
|
|
20
|
+
self.completions = CompletionsWrapper(chat.completions, api_key)
|
|
21
|
+
|
|
22
|
+
def __getattr__(self, name):
|
|
23
|
+
return getattr(self._chat, name)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class CompletionsWrapper:
|
|
27
|
+
def __init__(self, completions, api_key: str):
|
|
28
|
+
self._completions = completions
|
|
29
|
+
self._api_key = api_key
|
|
30
|
+
|
|
31
|
+
def create(self, **kwargs):
|
|
32
|
+
result = self._completions.create(**kwargs)
|
|
33
|
+
try:
|
|
34
|
+
messages = kwargs.get("messages", [])
|
|
35
|
+
prompt = " ".join(
|
|
36
|
+
m.get("content", "") if isinstance(m, dict) else str(m)
|
|
37
|
+
for m in messages
|
|
38
|
+
)
|
|
39
|
+
track(self._api_key, {
|
|
40
|
+
"provider": "openai",
|
|
41
|
+
"model": kwargs.get("model", "unknown"),
|
|
42
|
+
"inputTokens": getattr(result.usage, "prompt_tokens", 0),
|
|
43
|
+
"outputTokens": getattr(result.usage, "completion_tokens", 0),
|
|
44
|
+
"prompt": prompt[:500],
|
|
45
|
+
"endpoint": kwargs.get("model", "unknown"),
|
|
46
|
+
})
|
|
47
|
+
except Exception:
|
|
48
|
+
pass
|
|
49
|
+
return result
|
|
50
|
+
|
|
51
|
+
def __getattr__(self, name):
|
|
52
|
+
return getattr(self._completions, name)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class AnthropicWrapper:
|
|
56
|
+
"""Wraps the Anthropic client to track token usage."""
|
|
57
|
+
|
|
58
|
+
def __init__(self, client, api_key: str):
|
|
59
|
+
self._client = client
|
|
60
|
+
self._api_key = api_key
|
|
61
|
+
self.messages = AnthropicMessagesWrapper(client.messages, api_key)
|
|
62
|
+
|
|
63
|
+
def __getattr__(self, name):
|
|
64
|
+
return getattr(self._client, name)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class AnthropicMessagesWrapper:
|
|
68
|
+
def __init__(self, messages, api_key: str):
|
|
69
|
+
self._messages = messages
|
|
70
|
+
self._api_key = api_key
|
|
71
|
+
|
|
72
|
+
def create(self, **kwargs):
|
|
73
|
+
result = self._messages.create(**kwargs)
|
|
74
|
+
try:
|
|
75
|
+
messages = kwargs.get("messages", [])
|
|
76
|
+
prompt = " ".join(
|
|
77
|
+
m.get("content", "") if isinstance(m, dict) else str(m)
|
|
78
|
+
for m in messages
|
|
79
|
+
)
|
|
80
|
+
track(self._api_key, {
|
|
81
|
+
"provider": "anthropic",
|
|
82
|
+
"model": kwargs.get("model", "unknown"),
|
|
83
|
+
"inputTokens": getattr(result.usage, "input_tokens", 0),
|
|
84
|
+
"outputTokens": getattr(result.usage, "output_tokens", 0),
|
|
85
|
+
"prompt": prompt[:500],
|
|
86
|
+
"endpoint": kwargs.get("model", "unknown"),
|
|
87
|
+
})
|
|
88
|
+
except Exception:
|
|
89
|
+
pass
|
|
90
|
+
return result
|
|
91
|
+
|
|
92
|
+
def __getattr__(self, name):
|
|
93
|
+
return getattr(self._messages, name)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def wrap(client, api_key: str):
|
|
97
|
+
"""
|
|
98
|
+
Wrap an OpenAI or Anthropic client to track token usage with Tokoscope.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
client: An OpenAI() or Anthropic() client instance
|
|
102
|
+
api_key: Your Tokoscope API key from app.tokoscope.com/settings
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
Wrapped client with identical interface
|
|
106
|
+
|
|
107
|
+
Example:
|
|
108
|
+
from openai import OpenAI
|
|
109
|
+
from tokoscope import wrap
|
|
110
|
+
|
|
111
|
+
client = wrap(OpenAI(), api_key="ts_live_...")
|
|
112
|
+
"""
|
|
113
|
+
if not api_key:
|
|
114
|
+
raise ValueError("Tokoscope: api_key is required")
|
|
115
|
+
|
|
116
|
+
client_type = type(client).__name__
|
|
117
|
+
|
|
118
|
+
if "Anthropic" in client_type:
|
|
119
|
+
return AnthropicWrapper(client, api_key)
|
|
120
|
+
else:
|
|
121
|
+
return OpenAIWrapper(client, api_key)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tokoscope
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Audit, compress and monitor your LLM token usage in 2 lines of code
|
|
5
|
+
Author-email: Tokoscope <hello@tokoscope.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://tokoscope.com
|
|
8
|
+
Project-URL: Documentation, https://app.tokoscope.com/onboarding
|
|
9
|
+
Project-URL: Repository, https://github.com/tokoscope/sdk
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/tokoscope/sdk/issues
|
|
11
|
+
Keywords: llm,openai,anthropic,tokens,optimization,cost,monitoring,prompt,compression,ai,token-usage,llm-cost,observability,gpt,claude
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Requires-Python: >=3.8
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests>=2.28.0
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# Tokoscope Python SDK
|
|
30
|
+
|
|
31
|
+
> Audit, compress, and monitor your LLM token usage in 2 lines of code.
|
|
32
|
+
|
|
33
|
+
[](https://pypi.org/project/tokoscope/)
|
|
34
|
+
[](https://pypi.org/project/tokoscope/)
|
|
35
|
+
[](https://opensource.org/licenses/MIT)
|
|
36
|
+
[](https://www.python.org/)
|
|
37
|
+
|
|
38
|
+
Tokoscope sits between your app and any LLM API. It tracks every call, scores your prompts for waste, compresses bloated inputs automatically, and shows you exactly where your token budget is going.
|
|
39
|
+
|
|
40
|
+
**Works with OpenAI and Anthropic out of the box.**
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install tokoscope
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Quick start
|
|
53
|
+
|
|
54
|
+
### OpenAI
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from openai import OpenAI
|
|
58
|
+
from tokoscope import wrap
|
|
59
|
+
|
|
60
|
+
client = wrap(OpenAI(), api_key="ts_live_...") # get your key at app.tokoscope.com
|
|
61
|
+
|
|
62
|
+
response = client.chat.completions.create(
|
|
63
|
+
model="gpt-4o",
|
|
64
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
print(response.choices[0].message.content)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Anthropic
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from anthropic import Anthropic
|
|
74
|
+
from tokoscope import wrap
|
|
75
|
+
|
|
76
|
+
client = wrap(Anthropic(), api_key="ts_live_...")
|
|
77
|
+
|
|
78
|
+
response = client.messages.create(
|
|
79
|
+
model="claude-sonnet-4-6",
|
|
80
|
+
max_tokens=1024,
|
|
81
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
print(response.content[0].text)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
That's it. Every API call is now tracked automatically.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Real example
|
|
92
|
+
|
|
93
|
+
**Original prompt:** 113 tokens
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
Please note that it is very important that you make sure to respond
|
|
97
|
+
to my question. As an AI, I want you to please make sure that you
|
|
98
|
+
understand that I need you to help me...
|
|
99
|
+
What is the capital of France?
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Tokoscope compressed:** 8 tokens
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
What is the capital of France? Answer concisely.
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Result:** 90% token reduction. Same answer.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## What you get
|
|
113
|
+
|
|
114
|
+
Once integrated, your [Tokoscope dashboard](https://app.tokoscope.com) shows:
|
|
115
|
+
|
|
116
|
+
- **Token usage** broken down by model, endpoint, and provider
|
|
117
|
+
- **Cost per request** calculated automatically using current pricing
|
|
118
|
+
- **Waste score** for every prompt
|
|
119
|
+
- **Auto-compression** — prompts with high waste scores are automatically rewritten
|
|
120
|
+
- **Budget alerts** — get notified before costs spike
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Supported providers
|
|
125
|
+
|
|
126
|
+
| Provider | Status |
|
|
127
|
+
|---|---|
|
|
128
|
+
| OpenAI | ✅ Supported |
|
|
129
|
+
| Anthropic | ✅ Supported |
|
|
130
|
+
| Gemini | 🔜 Coming soon |
|
|
131
|
+
| Mistral | 🔜 Coming soon |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Requirements
|
|
136
|
+
|
|
137
|
+
- Python 3.8+
|
|
138
|
+
- `requests` library (installed automatically)
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Links
|
|
149
|
+
|
|
150
|
+
- [Website](https://tokoscope.com)
|
|
151
|
+
- [Dashboard](https://app.tokoscope.com)
|
|
152
|
+
- [JavaScript SDK](https://www.npmjs.com/package/tokoscope)
|
|
153
|
+
- [Contact](mailto:hello@tokoscope.com)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
tokoscope/__init__.py
|
|
5
|
+
tokoscope/tracker.py
|
|
6
|
+
tokoscope/wrapper.py
|
|
7
|
+
tokoscope.egg-info/PKG-INFO
|
|
8
|
+
tokoscope.egg-info/SOURCES.txt
|
|
9
|
+
tokoscope.egg-info/dependency_links.txt
|
|
10
|
+
tokoscope.egg-info/requires.txt
|
|
11
|
+
tokoscope.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
requests>=2.28.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tokoscope
|