tokenator 0.1.10__tar.gz → 0.1.11__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: tokenator
3
- Version: 0.1.10
3
+ Version: 0.1.11
4
4
  Summary: Token usage tracking wrapper for LLMs
5
5
  License: MIT
6
6
  Author: Ujjwal Maheshwari
@@ -20,12 +20,12 @@ Requires-Dist: requests (>=2.32.3,<3.0.0)
20
20
  Requires-Dist: sqlalchemy (>=2.0.0,<3.0.0)
21
21
  Description-Content-Type: text/markdown
22
22
 
23
- # Tokenator : Easiest way to track and analyze LLM token usage and cost
23
+ # Tokenator : Track and analyze LLM token usage and cost
24
24
 
25
25
  Have you ever wondered about :
26
26
  - How many tokens does your AI agent consume?
27
27
  - How much does it cost to do run a complex AI workflow with multiple LLM providers?
28
- - How much money did I spent today on development?
28
+ - How much money/tokens did you spend today on developing with LLMs?
29
29
 
30
30
  Afraid not, tokenator is here! With tokenator's easy to use API, you can start tracking LLM usage in a matter of minutes.
31
31
 
@@ -57,6 +57,9 @@ response = client.chat.completions.create(
57
57
  )
58
58
  ```
59
59
 
60
+ Works with AsyncOpenAI and `streaming=True` as well!
61
+ Note : When streaming, don't forget to add `stream_options={"include_usage": True}` to the `create()` call!
62
+
60
63
  ### Cost Analysis
61
64
 
62
65
  ```python
@@ -120,6 +123,56 @@ print(cost.last_hour().model_dump_json(indent=4))
120
123
  - Minimal memory footprint
121
124
  - Minimal latency footprint
122
125
 
126
+ ### Anthropic
127
+
128
+ ```python
129
+ from anthropic import Anthropic, AsyncAnthropic
130
+ from tokenator import tokenator_anthropic
131
+
132
+ anthropic_client = AsyncAnthropic(api_key="your-api-key")
133
+
134
+ # Wrap it with Tokenator
135
+ client = tokenator_anthropic(anthropic_client)
136
+
137
+ # Use it exactly like the Anthropic client
138
+ response = await client.messages.create(
139
+ model="claude-3-5-haiku-20241022",
140
+ messages=[{"role": "user", "content": "hello how are you"}],
141
+ max_tokens=20,
142
+ )
143
+
144
+ print(response)
145
+
146
+ print(usage.last_execution().model_dump_json(indent=4))
147
+ """
148
+ {
149
+ "total_cost": 0.0001,
150
+ "total_tokens": 23,
151
+ "prompt_tokens": 10,
152
+ "completion_tokens": 13,
153
+ "providers": [
154
+ {
155
+ "total_cost": 0.0001,
156
+ "total_tokens": 23,
157
+ "prompt_tokens": 10,
158
+ "completion_tokens": 13,
159
+ "provider": "anthropic",
160
+ "models": [
161
+ {
162
+ "total_cost": 0.0004,
163
+ "total_tokens": 79,
164
+ "prompt_tokens": 52,
165
+ "completion_tokens": 27,
166
+ "model": "claude-3-5-haiku-20241022"
167
+ }
168
+ ]
169
+ }
170
+ ]
171
+ }
172
+ """
173
+ ```
174
+ ---
175
+
123
176
  Most importantly, none of your data is ever sent to any server.
124
177
 
125
178
  ## License
@@ -1,9 +1,9 @@
1
- # Tokenator : Easiest way to track and analyze LLM token usage and cost
1
+ # Tokenator : Track and analyze LLM token usage and cost
2
2
 
3
3
  Have you ever wondered about :
4
4
  - How many tokens does your AI agent consume?
5
5
  - How much does it cost to do run a complex AI workflow with multiple LLM providers?
6
- - How much money did I spent today on development?
6
+ - How much money/tokens did you spend today on developing with LLMs?
7
7
 
8
8
  Afraid not, tokenator is here! With tokenator's easy to use API, you can start tracking LLM usage in a matter of minutes.
9
9
 
@@ -35,6 +35,9 @@ response = client.chat.completions.create(
35
35
  )
36
36
  ```
37
37
 
38
+ Works with AsyncOpenAI and `streaming=True` as well!
39
+ Note : When streaming, don't forget to add `stream_options={"include_usage": True}` to the `create()` call!
40
+
38
41
  ### Cost Analysis
39
42
 
40
43
  ```python
@@ -98,6 +101,56 @@ print(cost.last_hour().model_dump_json(indent=4))
98
101
  - Minimal memory footprint
99
102
  - Minimal latency footprint
100
103
 
104
+ ### Anthropic
105
+
106
+ ```python
107
+ from anthropic import Anthropic, AsyncAnthropic
108
+ from tokenator import tokenator_anthropic
109
+
110
+ anthropic_client = AsyncAnthropic(api_key="your-api-key")
111
+
112
+ # Wrap it with Tokenator
113
+ client = tokenator_anthropic(anthropic_client)
114
+
115
+ # Use it exactly like the Anthropic client
116
+ response = await client.messages.create(
117
+ model="claude-3-5-haiku-20241022",
118
+ messages=[{"role": "user", "content": "hello how are you"}],
119
+ max_tokens=20,
120
+ )
121
+
122
+ print(response)
123
+
124
+ print(usage.last_execution().model_dump_json(indent=4))
125
+ """
126
+ {
127
+ "total_cost": 0.0001,
128
+ "total_tokens": 23,
129
+ "prompt_tokens": 10,
130
+ "completion_tokens": 13,
131
+ "providers": [
132
+ {
133
+ "total_cost": 0.0001,
134
+ "total_tokens": 23,
135
+ "prompt_tokens": 10,
136
+ "completion_tokens": 13,
137
+ "provider": "anthropic",
138
+ "models": [
139
+ {
140
+ "total_cost": 0.0004,
141
+ "total_tokens": 79,
142
+ "prompt_tokens": 52,
143
+ "completion_tokens": 27,
144
+ "model": "claude-3-5-haiku-20241022"
145
+ }
146
+ ]
147
+ }
148
+ ]
149
+ }
150
+ """
151
+ ```
152
+ ---
153
+
101
154
  Most importantly, none of your data is ever sent to any server.
102
155
 
103
156
  ## License
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "tokenator"
3
- version = "0.1.10"
3
+ version = "0.1.11"
4
4
  description = "Token usage tracking wrapper for LLMs"
5
5
  authors = ["Ujjwal Maheshwari <your.email@example.com>"]
6
6
  readme = "README.md"
@@ -71,7 +71,6 @@ def _create_usage_callback(execution_id, log_usage_fn):
71
71
  usage_data.usage.prompt_tokens += chunk.message.usage.input_tokens
72
72
  usage_data.usage.completion_tokens += chunk.message.usage.output_tokens
73
73
  elif isinstance(chunk, RawMessageDeltaEvent):
74
- usage_data.usage.prompt_tokens += chunk.usage.input_tokens
75
74
  usage_data.usage.completion_tokens += chunk.usage.output_tokens
76
75
 
77
76
  usage_data.usage.total_tokens = usage_data.usage.prompt_tokens + usage_data.usage.completion_tokens
@@ -47,7 +47,7 @@ class BaseWrapper:
47
47
  total_tokens=token_usage_stats.usage.total_tokens,
48
48
  )
49
49
  session.add(token_usage)
50
- logger.info(
50
+ logger.debug(
51
51
  "Logged token usage: model=%s, total_tokens=%d",
52
52
  token_usage_stats.model,
53
53
  token_usage_stats.usage.total_tokens,
File without changes