Skip to main content

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.pygen_data.pydsh-llm-costpricing.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

dsh_llm_cost-0.2.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dsh_llm_cost-0.2.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file dsh_llm_cost-0.2.0.tar.gz.

File metadata

  • Download URL: dsh_llm_cost-0.2.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for dsh_llm_cost-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a7a7aec964296823035849dff5dde53c044cf3aa29b0b367eb59e4139988cabd
MD5 a635d9c2360f6c2c2d7103d8b69b83dc
BLAKE2b-256 ac284b097ef357c5999a2880e2bd6ac2cd216a35fed72087955c140572fc7eeb

See more details on using hashes here.

File details

Details for the file dsh_llm_cost-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dsh_llm_cost-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for dsh_llm_cost-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc817b8070c542245aeccfef31cbba5bdc1d9d79a544e38ee8beb170f3105b48
MD5 47af12127014ef4f4df1c2780b2df83d
BLAKE2b-256 c62994970188ab275d06628c134ee6f6fbee42944f2fefbae2e279656e5a4da6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page