Create

Sign in to ReadmeX

or

How we handle your data: Privacy Policy

ReadmeX

Your AI reading companion

What would you like to catch up on?

Pick a briefing, or ask your own question.

Your community and people briefings stay personal to you.

Story

Turn Token Logs into Per-Request AI Cost Metrics

AI summary

掘金 · 人工智能 presents an open-source cost_model.py script that combines structured token logs with a configurable model price table to calculate total, per-request, and per-model costs. The tutorial also covers budget limits, CI-compatible exit codes, model- and feature-level attribution, and warnings for models left unpriced. Its sample output uses synthetic logs and a configurable price table, while a separate real-link test reports roughly 0.001 yuan per request with DeepSeek-tier models.

In-depth insight

Per-request cost attribution provides a practical basis for pricing AI features, comparing optimization work, detecting spending increases, and enforcing budgets in production.

系列《AI 应用生产化手册》第 14 篇(共 30 篇)|配套开源项目:github.com/ChenYingbo/… 从本篇进入模块四:成本工程与 AI 网关——成本算得清,才谈得上优化(C02/C03)和采购治理(C04/C05)

一、先看问题:三个答不上来的问题

AI 应用上线后,老板/财务问的第一批问题,往往一个都答不上来:

  • "这个月花了多少钱?"——只能翻云账单猜,猜完了也不知道花在哪个功能上。
  • "每个请求平均多少钱?"——不知道就没法定价、没法判断某个功能"值不值得上"。
  • "成本突然涨了,涨在哪?"——没有按模型/功能拆解,只能等月底对账,涨完才发现。

解法:成本模型——按模型价格表,从日志里的 token 用量算出每次请求、每个模型的成本,并对预算做告警。

核心认知:成本不是"月底账单",是"实时可观测的指标"。只有拿到「每请求成本」,你才能做三件事:定价(值不值)、优化(C02/C03 省在哪)、控制(预算告警在哪断)。

#ai-prod-demo#cost_model.py#DeepSeek#GPT-5-mini

Original · 6 min readRead the original
Group chat

No comments yet. Start the conversation.