From 71626468e2cc701410801db31c5879ce6dfdb59f Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 28 May 2026 16:57:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B1=95=E5=B9=B3=E5=B5=8C=E5=A5=97usag?= =?UTF-8?q?e=E6=95=B0=E6=8D=AE=E4=BB=A5=E6=94=AF=E6=8C=81pricing=E5=BC=95?= =?UTF-8?q?=E6=93=8E=E7=82=B9=E5=8F=B7=E8=B7=AF=E5=BE=84=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:pricing引擎的config_data.get(k)是平面dict查找, 当k='prompt_tokens_details.cached_tokens'时无法从嵌套结构取值。 在llm_charging()中将prompt_tokens_details和completion_tokens_details 的子键展平为顶层key(如'prompt_tokens_details.cached_tokens')。 --- llmage/accounting.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/llmage/accounting.py b/llmage/accounting.py index c34d42b..6935a23 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -16,6 +16,13 @@ async def llm_charging(ppid, llmusage): usages = llmusage.usages if isinstance(usages, str): usages = json.loads(usages) + # 展平嵌套dict,使 prompt_tokens_details.cached_tokens 等点号路径 + # 可以作为顶层key被pricing引擎的 config_data.get(k) 取到 + for nested_key in ('prompt_tokens_details', 'completion_tokens_details'): + nested = usages.get(nested_key) + if isinstance(nested, dict): + for k, v in nested.items(): + usages[f'{nested_key}.{k}'] = v prices = await env.buffered_charging(ppid, usages) if prices is None: e = Exception(f'{ppid=}, {usages=}{llmusage.id=} env.buffered_charging() return None')