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"}],
)
# 查询统计(默认最近 1 天)
stats = tokenetc.stats(days=7)
print(stats["total_tokens"]) # 总 token 数
print(stats["cost_usd"]) # 估算费用(USD)
print(stats["by_channel"]) # 按渠道(openai / anthropic / ollama …)
print(stats["by_model"]) # 按模型
print(stats["by_tag"]) # 按项目标签
多脚本 / 多项目区分
同一台机器上所有脚本共用同一个本地数据库。使用 tag 参数区分不同脚本或项目:
# script_a.py
tokenetc.patch(tag="data-pipeline")
# notebook_b.ipynb
tokenetc.patch(tag="research")
查询时按标签过滤或汇总:
# 只看某个项目
tokenetc.stats(days=7, tag="data-pipeline")
# 所有项目汇总,by_tag 自动分组
s = tokenetc.stats(days=30)
print(s["by_tag"])
# {
# "data-pipeline": {"total_tokens": 50000, "cost_usd": 0.12, ...},
# "research": {"total_tokens": 20000, "cost_usd": 0.05, ...},
# "(untagged)": {"total_tokens": 5000, "cost_usd": 0.01, ...}
# }
手动录入
Cursor、网页版 ChatGPT / Gemini 等无法自动追踪的渠道,可手动录入:
tokenetc.record(
channel="cursor",
model="claude-sonnet-4",
tag="work", # 可选标签
input_tokens=3000,
output_tokens=500,
)
成本计算
cost = tokenetc.cost("gpt-4o", input_tokens=1000, output_tokens=500)
print(f"${cost:.6f}") # $0.007500
# 查看所有支持的模型价格
tokenetc.list_models()
统计查询参数
tokenetc.stats(
days=7, # 最近 N 天(与 start/end 互斥)
channel="openai", # 过滤渠道(可选)
model="gpt-4o", # 过滤模型(可选)
tag="research", # 过滤标签/项目(可选)
)
返回结构包含:total_tokens、input_tokens、output_tokens、cost_usd、count、by_channel、by_model、by_tag、daily、period。
跳过某个 SDK
tokenetc.patch(gemini=False) # 不追踪 Gemini
tokenetc.patch(openai=False) # 不追踪 OpenAI
数据存储
数据保存在 ~/.tokenetc/data.db(SQLite,本地只读,无需联网)。
自定义路径:
TOKENETC_DB=~/my_project/tokens.db python my_script.py
支持的 SDK
| SDK | 版本要求 | 同步 | 异步 | 流式 |
|---|---|---|---|---|
| openai | >= 1.0 | ✅ | ✅ | ✅ |
| anthropic | >= 0.20 | ✅ | ✅ | ✅ |
| google-generativeai | >= 0.5 | ✅ | ✅ | — |
本地模型(Ollama 等,base_url 含 localhost)自动识别为零成本。
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
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
File details
Details for the file tokenetc-0.1.3.tar.gz.
File metadata
- Download URL: tokenetc-0.1.3.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
755d4b583d88d4832cb036976c84ada52a6e1bb53557431e48fb244994339867
|
|
| MD5 |
9116eab5cba3fd7fcbcd8cf6e3b6c53c
|
|
| BLAKE2b-256 |
6fb2761907d155644ba3196b37d5c89712cdbac3b130ac31228e94fc066ae005
|
File details
Details for the file tokenetc-0.1.3-py3-none-any.whl.
File metadata
- Download URL: tokenetc-0.1.3-py3-none-any.whl
- Upload date:
- Size: 13.3 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 |
db08e305d0720a6edfff8df2d32307d5d4bf67f1de5997095e84dd89ed85539d
|
|
| MD5 |
245c472221598a0be69f4b37f0985181
|
|
| BLAKE2b-256 |
419b7314d50695c949ae3e0310b3b9f855b9ca9424ae6e6df34358f92caa3845
|