Metadata-Version: 2.4
Name: tokenetc
Version: 0.1.2
Summary: Auto-track AI SDK token usage with cost calculation and statistics
Author: likun.pm
License-Expression: MIT
Project-URL: Homepage, https://github.com/likunpm/tokenetc
Project-URL: Repository, https://github.com/likunpm/tokenetc
Keywords: openai,anthropic,gemini,token,cost,tracking,llm,ai
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"

# tokenetc

自动追踪 OpenAI / Anthropic / Gemini SDK 的 token 用量，支持成本计算与多渠道统计。

## 安装

```bash
pip install tokenetc
```

## 使用

```python
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）。
