llm-cost
LLM API 定价表 + 成本估算,作为
dsh-llm-cost(DeepSeek Harness 插件)与 Python 智能体系统的单一事实来源。
把定价数据(llm_models.toml [pricing_v2])和成本计算逻辑从大项目里解耦出来,让 Python 侧和 DSH 侧对「每个请求花了多少钱」得到完全一致的数字。
安装
pip install dsh-llm-cost
用法
from dsh_llm_cost import estimate_cost, list_models, resolve_pricing
# 一次请求的成本(input/output/cache 分桶)
est = estimate_cost(
"deepseek-v4-flash", "deepseek",
input_tokens=1000, output_tokens=500, cache_read_tokens=2000,
)
print(est.total_usd) # 0.003xxxx
# 未知模型 → None(绝不静默显示 $0,避免「未知」被误读成「免费」)
assert estimate_cost("made-up-model", "openai", input_tokens=10, output_tokens=10) is None
# 匹配阶梯 + 完整决议
r = resolve_pricing("gpt-5.4-mini-20250601", "openai")
print(r.match) # 'substring'
print(list_models()) # 所有模型 id
匹配阶梯(对齐 models.py:get_pricing)
0. provider === "ollama" → 免费 (0)
1. 模型 id 精确匹配
2. 最长 key 子串匹配(防 "gpt-5.4" 吞掉 "gpt-5.4-mini")
3. 未知 → None(显示 "unknown",不显示 $0.00)
成本公式(与 dsh-llm-cost 插件一致):
total = input_tokens/1e6 * input_per_m
+ cache_read_tokens/1e6 * cache_read_per_m
+ cache_write_tokens/1e6 * cache_write_per_m
+ output_tokens/1e6 * output_per_m
input_tokens 是不含缓存的输入(DeepSeek 适配器已把缓存命中从 prompt_tokens 里减掉);reasoning 已含在 output_tokens 内;batch/storage/1h 维度 v1 不计入显示值。
数据
src/dsh_llm_cost/_data.py 由 gen_data.py 从 dsh-llm-cost 的 pricing.json 生成。单一事实来源是 llm_models.toml [pricing_v2],链路:
llm_models.toml --(dsh-llm-cost: npm run gen)--> pricing.json --(gen_data.py)--> _data.py
重新生成:
python gen_data.py ../dsh-llm-cost/pricing.json
开发
python -m pytest # 跑测试
python -m build # 打包 sdist + wheel
python -m twine upload dist/* # 发布(需 PyPI token)
License
MIT © 2026 Chen (Jarry) Pan
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 dsh_llm_cost-0.1.0.tar.gz.
File metadata
- Download URL: dsh_llm_cost-0.1.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0aeecf5820cf4faa6756fc13ce829b7949b21237b71af3b0437270d81d0b371f
|
|
| MD5 |
ef6e73dc3eef4c25b0d82e1d85c03dd8
|
|
| BLAKE2b-256 |
86bb74d2d595281a53d08597da189d226e788305f8ae5882e8c53b7c21398fba
|
File details
Details for the file dsh_llm_cost-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dsh_llm_cost-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8a26862147c405702fc37f842df5d944a8e02110614dcb251eaf91c61991133
|
|
| MD5 |
894c08a583bdef9af6806d6e410e82cf
|
|
| BLAKE2b-256 |
9453f38dcbd54af4d29521fb62ffb2f36d01ea9e743739bdc490b1158dcebf6f
|