Auto-track AI SDK token usage with cost calculation and statistics
Project description
tokenetc
自动追踪 OpenAI / Anthropic / Gemini SDK 的 token 用量,支持成本计算与多渠道统计。
安装
pip install tokenetc
使用
import tokenetc
# 一行 patch,自动追踪所有已安装 SDK 的调用(同步 + 异步)
tokenetc.patch()
# 之后正常使用 openai / anthropic / gemini,用量自动记录
import openai
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "hello"}],
)
# 手动录入(Cursor / 网页版等无法自动追踪的渠道)
tokenetc.record(
channel="cursor",
model="claude-sonnet-4",
input_tokens=3000,
output_tokens=500,
)
# 计算费用
cost = tokenetc.cost("gpt-4o", input_tokens=1000, output_tokens=500)
print(f"${cost:.6f}") # $0.007500
# 查询统计
stats = tokenetc.stats(days=7)
print(stats["total_tokens"])
print(stats["cost_usd"])
print(stats["by_channel"])
print(stats["by_model"])
# 查看所有支持的模型价格
tokenetc.list_models()
数据存储
数据保存在 ~/.tokenetc/data.db(SQLite)。
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
tokenetc-0.1.2.tar.gz
(11.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
tokenetc-0.1.2-py3-none-any.whl
(12.4 kB
view details)
File details
Details for the file tokenetc-0.1.2.tar.gz.
File metadata
- Download URL: tokenetc-0.1.2.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
590b9dc716dd5b3e15b3af1425a44bde4a2b96f8f1406f8602fab9e0d050532b
|
|
| MD5 |
3e1493b2cbc947d0193174f3318f5e51
|
|
| BLAKE2b-256 |
1ffd46fba98617aa983cf7d66653e4536c98e2404c197eb6d063394b7d135be9
|
File details
Details for the file tokenetc-0.1.2-py3-none-any.whl.
File metadata
- Download URL: tokenetc-0.1.2-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c01acd8abc23bac46dda46f34cef035df941d0c2671bb118c2da8ba54aa509a9
|
|
| MD5 |
a7aff63160f3a4ca437cae997056ea31
|
|
| BLAKE2b-256 |
476dd43bf3843ac7e50049a953fd7489d2c0cd12aeb9e0e95ece7342bf20751b
|