Auto-track AI SDK token usage with cost calculation and statistics
Project description
tokenetc
自动追踪 OpenAI / Anthropic / Gemini SDK 的 token 用量,支持成本计算与多渠道统计。
安装
pip install -e .
使用
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.0.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.0-py3-none-any.whl
(11.8 kB
view details)
File details
Details for the file tokenetc-0.1.0.tar.gz.
File metadata
- Download URL: tokenetc-0.1.0.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 |
35698e28326cfdc787254edc8dbe10de625047525352210c21eb08998572d547
|
|
| MD5 |
a4ad6a3af5f723ee74405bd971964337
|
|
| BLAKE2b-256 |
1e32fc1805ebfcd43914e33f464811bf1579082bff9a67c7896ae6d5c891d810
|
File details
Details for the file tokenetc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tokenetc-0.1.0-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 |
4cc2233ac02af3c8cdc930ce24e944c8ec73517454b61f7722028e3284c2faae
|
|
| MD5 |
7c7ae9094267ee912ed15c9fdefbe267
|
|
| BLAKE2b-256 |
ad2bfde7967821d33173853f168af7a7b84f307b322bff130777a2f9fe27a171
|