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
Metadata
Release files for dsh-llm-cost 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dsh_llm_cost-0.2.1.tar.gz | 9.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dsh_llm_cost-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.0 kB
Release files / dsh_llm_cost-0.2.1.tar.gz
| Download URL | dsh_llm_cost-0.2.1.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e8256fdf8ffd79032d65b64227b62131e0e08da6e65b59252a400b3f6bcb2bd
|
|
BLAKE2b-256 checksum How to use checksums |
7021115475db01f9f2c66cb14142f0ba9199440951cbe2f9b0430aeef74f9808
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.11
|
Release files / dsh_llm_cost-0.2.1-py3-none-any.whl
| Download URL | dsh_llm_cost-0.2.1-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bf9538ee04bb296a940863650eb95237a05beae6d102579195a8e53e4a92a8a5
|
|
BLAKE2b-256 checksum How to use checksums |
a5f83cceb6596b5b8a220fe1d94ab157209575e9eac90906ed9697dffb61a2ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.11
|