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.1.tar.gz
(11.0 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.1-py3-none-any.whl
(11.8 kB
view details)
File details
Details for the file tokenetc-0.1.1.tar.gz.
File metadata
- Download URL: tokenetc-0.1.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0e023a223d6b75f470dc9613dce7a255ccae5bc312a6d2decc35a4c1a61fb39
|
|
| MD5 |
203fe94d80343e2e2bb152d0b5e13aae
|
|
| BLAKE2b-256 |
e7ac224ac48bb40b923ef8617f5827c9c2192a7d2f3756e8033b28c99e6f9fc0
|
File details
Details for the file tokenetc-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tokenetc-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.8 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 |
01cbd09674bc089e2775c02d9a5cf5965752979bd0bc8ecae5395af97d19c2d6
|
|
| MD5 |
348f3bafe24b2a29f7c6c0c5d5bcf676
|
|
| BLAKE2b-256 |
cc8232b2dd096e5cf3589ee179fb72e6897835ce55c05119bce8211e8815b277
|