openai-usage 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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 AllenChou
|
|
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,50 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: openai-usage
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple Library for OpenAI Usage
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Allen Chou
|
|
7
|
+
Author-email: f1470891079@gmail.com
|
|
8
|
+
Requires-Python: >=3.11,<4
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Requires-Dist: dictpress (>=0.3.0)
|
|
15
|
+
Requires-Dist: openai (>=1,<2)
|
|
16
|
+
Requires-Dist: openai-agents (>=0.1.0,<1.0.0)
|
|
17
|
+
Requires-Dist: pydantic (>=2)
|
|
18
|
+
Requires-Dist: str-or-none
|
|
19
|
+
Project-URL: Homepage, https://github.com/allen2c/openai-usage
|
|
20
|
+
Project-URL: PyPI, https://pypi.org/project/openai-usage/
|
|
21
|
+
Project-URL: Repository, https://github.com/allen2c/openai-usage
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# openai-usage
|
|
25
|
+
|
|
26
|
+
[](https://pypi.org/project/openai-usage/)
|
|
27
|
+
|
|
28
|
+
Utilities to track OpenAI API usage.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install openai-usage
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from openai_usage import Usage
|
|
40
|
+
|
|
41
|
+
# Track usage manually
|
|
42
|
+
usage = Usage()
|
|
43
|
+
usage.add(Usage(requests=1, input_tokens=10, output_tokens=20, total_tokens=30))
|
|
44
|
+
|
|
45
|
+
# Create from OpenAI object
|
|
46
|
+
from openai.types.completion_usage import CompletionUsage
|
|
47
|
+
openai_usage = CompletionUsage(prompt_tokens=10, completion_tokens=20, total_tokens=30)
|
|
48
|
+
usage = Usage.from_openai(openai_usage)
|
|
49
|
+
```
|
|
50
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# openai-usage
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/openai-usage/)
|
|
4
|
+
|
|
5
|
+
Utilities to track OpenAI API usage.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install openai-usage
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from openai_usage import Usage
|
|
17
|
+
|
|
18
|
+
# Track usage manually
|
|
19
|
+
usage = Usage()
|
|
20
|
+
usage.add(Usage(requests=1, input_tokens=10, output_tokens=20, total_tokens=30))
|
|
21
|
+
|
|
22
|
+
# Create from OpenAI object
|
|
23
|
+
from openai.types.completion_usage import CompletionUsage
|
|
24
|
+
openai_usage = CompletionUsage(prompt_tokens=10, completion_tokens=20, total_tokens=30)
|
|
25
|
+
usage = Usage.from_openai(openai_usage)
|
|
26
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# openai_usage/__init__.py
|
|
2
|
+
"""OpenAI usage tracking utilities.
|
|
3
|
+
|
|
4
|
+
Simple models for tracking and aggregating OpenAI API usage data.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import pathlib
|
|
8
|
+
|
|
9
|
+
import agents
|
|
10
|
+
import pydantic
|
|
11
|
+
from openai.types.completion_usage import CompletionUsage
|
|
12
|
+
from openai.types.responses.response_usage import (
|
|
13
|
+
InputTokensDetails,
|
|
14
|
+
OutputTokensDetails,
|
|
15
|
+
ResponseUsage,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__version__ = pathlib.Path(__file__).parent.joinpath("VERSION").read_text().strip()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Usage(pydantic.BaseModel):
|
|
22
|
+
"""Usage statistics for OpenAI API calls.
|
|
23
|
+
|
|
24
|
+
Tracks token counts and request metrics across API interactions.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
requests: int = 0
|
|
28
|
+
input_tokens: int = 0
|
|
29
|
+
input_tokens_details: InputTokensDetails = pydantic.Field(
|
|
30
|
+
default_factory=lambda: InputTokensDetails(cached_tokens=0)
|
|
31
|
+
)
|
|
32
|
+
output_tokens: int = 0
|
|
33
|
+
output_tokens_details: OutputTokensDetails = pydantic.Field(
|
|
34
|
+
default_factory=lambda: OutputTokensDetails(reasoning_tokens=0)
|
|
35
|
+
)
|
|
36
|
+
total_tokens: int = 0
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def from_openai(
|
|
40
|
+
cls,
|
|
41
|
+
openai_usage: (
|
|
42
|
+
ResponseUsage | agents.RunContextWrapper | agents.Usage | CompletionUsage
|
|
43
|
+
),
|
|
44
|
+
*,
|
|
45
|
+
inplace: bool = False,
|
|
46
|
+
) -> "Usage":
|
|
47
|
+
"""Create Usage from OpenAI usage objects.
|
|
48
|
+
|
|
49
|
+
Supports ResponseUsage, RunContextWrapper, agents.Usage, and CompletionUsage types.
|
|
50
|
+
Returns new instance unless inplace=True.
|
|
51
|
+
""" # noqa: E501
|
|
52
|
+
|
|
53
|
+
if isinstance(openai_usage, ResponseUsage):
|
|
54
|
+
usage = cls(
|
|
55
|
+
requests=1,
|
|
56
|
+
input_tokens=openai_usage.input_tokens,
|
|
57
|
+
input_tokens_details=openai_usage.input_tokens_details,
|
|
58
|
+
output_tokens=openai_usage.output_tokens,
|
|
59
|
+
output_tokens_details=openai_usage.output_tokens_details,
|
|
60
|
+
total_tokens=openai_usage.total_tokens,
|
|
61
|
+
)
|
|
62
|
+
elif isinstance(openai_usage, agents.RunContextWrapper):
|
|
63
|
+
usage = cls(
|
|
64
|
+
requests=1,
|
|
65
|
+
input_tokens=openai_usage.usage.input_tokens,
|
|
66
|
+
input_tokens_details=openai_usage.usage.input_tokens_details,
|
|
67
|
+
output_tokens=openai_usage.usage.output_tokens,
|
|
68
|
+
output_tokens_details=openai_usage.usage.output_tokens_details,
|
|
69
|
+
total_tokens=openai_usage.usage.total_tokens,
|
|
70
|
+
)
|
|
71
|
+
elif isinstance(openai_usage, agents.Usage):
|
|
72
|
+
usage = cls(
|
|
73
|
+
requests=1,
|
|
74
|
+
input_tokens=openai_usage.input_tokens,
|
|
75
|
+
input_tokens_details=openai_usage.input_tokens_details,
|
|
76
|
+
output_tokens=openai_usage.output_tokens,
|
|
77
|
+
output_tokens_details=openai_usage.output_tokens_details,
|
|
78
|
+
total_tokens=openai_usage.total_tokens,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
elif isinstance(openai_usage, CompletionUsage):
|
|
82
|
+
usage = cls(
|
|
83
|
+
requests=1,
|
|
84
|
+
input_tokens=openai_usage.prompt_tokens,
|
|
85
|
+
input_tokens_details=InputTokensDetails(
|
|
86
|
+
cached_tokens=(
|
|
87
|
+
openai_usage.prompt_tokens_details.cached_tokens or 0
|
|
88
|
+
if openai_usage.prompt_tokens_details
|
|
89
|
+
else 0
|
|
90
|
+
)
|
|
91
|
+
),
|
|
92
|
+
output_tokens=openai_usage.completion_tokens,
|
|
93
|
+
output_tokens_details=OutputTokensDetails(
|
|
94
|
+
reasoning_tokens=(
|
|
95
|
+
openai_usage.completion_tokens_details.reasoning_tokens or 0
|
|
96
|
+
if openai_usage.completion_tokens_details
|
|
97
|
+
else 0
|
|
98
|
+
)
|
|
99
|
+
),
|
|
100
|
+
total_tokens=openai_usage.total_tokens,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
else:
|
|
104
|
+
raise ValueError(f"Unsupported usage type: {type(openai_usage)}")
|
|
105
|
+
|
|
106
|
+
if inplace:
|
|
107
|
+
return usage
|
|
108
|
+
else:
|
|
109
|
+
return cls.model_validate_json(usage.model_dump_json())
|
|
110
|
+
|
|
111
|
+
def add(self, other: "Usage") -> None:
|
|
112
|
+
"""Add usage from another Usage instance.
|
|
113
|
+
|
|
114
|
+
Accumulates all metrics including token counts and request totals.
|
|
115
|
+
"""
|
|
116
|
+
self.requests += other.requests if other.requests else 0
|
|
117
|
+
self.input_tokens += other.input_tokens if other.input_tokens else 0
|
|
118
|
+
self.output_tokens += other.output_tokens if other.output_tokens else 0
|
|
119
|
+
self.total_tokens += other.total_tokens if other.total_tokens else 0
|
|
120
|
+
self.input_tokens_details = InputTokensDetails(
|
|
121
|
+
cached_tokens=self.input_tokens_details.cached_tokens
|
|
122
|
+
+ other.input_tokens_details.cached_tokens
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
self.output_tokens_details = OutputTokensDetails(
|
|
126
|
+
reasoning_tokens=self.output_tokens_details.reasoning_tokens
|
|
127
|
+
+ other.output_tokens_details.reasoning_tokens
|
|
128
|
+
)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
authors = [{ name = "Allen Chou", email = "f1470891079@gmail.com" }]
|
|
3
|
+
dependencies = [
|
|
4
|
+
"dictpress (>=0.3.0)",
|
|
5
|
+
"openai (>=1,<2)",
|
|
6
|
+
"openai-agents (>=0.1.0,<1.0.0)",
|
|
7
|
+
"pydantic (>=2)",
|
|
8
|
+
"str-or-none",
|
|
9
|
+
]
|
|
10
|
+
description = "Simple Library for OpenAI Usage"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
name = "openai-usage"
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.11,<4"
|
|
15
|
+
version = "0.1.0"
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/allen2c/openai-usage"
|
|
19
|
+
"PyPI" = "https://pypi.org/project/openai-usage/"
|
|
20
|
+
Repository = "https://github.com/allen2c/openai-usage"
|
|
21
|
+
|
|
22
|
+
[tool.poetry]
|
|
23
|
+
packages = [{ include = "openai_usage" }]
|
|
24
|
+
|
|
25
|
+
[tool.poetry.extras]
|
|
26
|
+
all = []
|
|
27
|
+
|
|
28
|
+
[tool.poetry.group.dev.dependencies]
|
|
29
|
+
black = { extras = ["jupyter"], version = "*" }
|
|
30
|
+
codepress = "*"
|
|
31
|
+
isort = "*"
|
|
32
|
+
poetry-plugin-export = "*"
|
|
33
|
+
pytest = "*"
|
|
34
|
+
pytest-asyncio = "*"
|
|
35
|
+
pytest-cov = "*"
|
|
36
|
+
pytest-env = "*"
|
|
37
|
+
pytest-xdist = "*"
|
|
38
|
+
rich = "*"
|
|
39
|
+
rich-color-support = "*"
|
|
40
|
+
setuptools = "*"
|
|
41
|
+
twine = "*"
|
|
42
|
+
|
|
43
|
+
[tool.isort]
|
|
44
|
+
profile = "black"
|
|
45
|
+
|
|
46
|
+
[tool.flake8]
|
|
47
|
+
ignore = ["E203", "E704", "W503"]
|
|
48
|
+
max-line-length = 88
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
env = ["ENVIRONMENT=test", "PYTEST_IS_RUNNING=true"]
|
|
52
|
+
|
|
53
|
+
[build-system]
|
|
54
|
+
build-backend = "poetry.core.masonry.api"
|
|
55
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|