openclaw - 💡(How to fix) Fix [Bug] Token usage 显示为 0,dashscope-coding-plan API 返回的字段名不匹配 [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#52069Fetched 2026-04-08 01:16:00
View on GitHub
Comments
2
Participants
3
Timeline
2
Reactions
0
Timeline (top)
commented ×2

Code Example

input: response.usage?.input_tokens ?? 0,
output: response.usage?.output_tokens ?? 0,
totalTokens: response.usage?.total_tokens ?? 0

---

{
  "prompt_tokens": 7,
  "completion_tokens": 172,
  "total_tokens": 179
}

---

const usage = normalizeUsage(response.usage);
// normalizeUsage 已支持:prompt_tokens, completion_tokens, input_tokens, output_tokens 等

---

curl https://coding.dashscope.aliyuncs.com/v1/chat/completions \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -d '{"model":"glm-5","messages":[{"role":"user","content":"OK"}]}'
  
# 返回:
# "usage":{"completion_tokens":172,"prompt_tokens":7,"total_tokens":179}
RAW_BUFFERClick to expand / collapse

问题描述

使用 dashscope-coding-plan 提供商时,session status 显示 Context: 0/128k (0%),所有 usage 字段都为 0。

根本原因

API 实际返回了正确的 usage 信息,但 OpenClaw 代码中的字段名不匹配:

OpenClaw 代码期望的字段名(auth-profiles-DDVivXkv.js):

input: response.usage?.input_tokens ?? 0,
output: response.usage?.output_tokens ?? 0,
totalTokens: response.usage?.total_tokens ?? 0

dashscope API 实际返回的字段名

{
  "prompt_tokens": 7,
  "completion_tokens": 172,
  "total_tokens": 179
}

字段名不匹配导致 usage 信息丢失。

建议修复

OpenClaw 已有 normalizeUsage 函数支持多种字段名映射,应该使用它而不是硬编码字段名:

const usage = normalizeUsage(response.usage);
// normalizeUsage 已支持:prompt_tokens, completion_tokens, input_tokens, output_tokens 等

环境信息

验证

直接 curl 调用 API 确认返回了 usage:

curl https://coding.dashscope.aliyuncs.com/v1/chat/completions \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -d '{"model":"glm-5","messages":[{"role":"user","content":"OK"}]}'
  
# 返回:
# "usage":{"completion_tokens":172,"prompt_tokens":7,"total_tokens":179}

extent analysis

Fix Plan

To resolve the issue, we need to update the OpenClaw code to use the normalizeUsage function, which supports multiple field name mappings.

Step-by-Step Solution:

  1. Update the usage fields: Replace the hardcoded field names with the normalizeUsage function.
const usage = normalizeUsage(response.usage);
  1. Use the normalized usage: Update the code to use the normalized usage object.
input: usage.input_tokens ?? 0,
output: usage.output_tokens ?? 0,
totalTokens: usage.total_tokens ?? 0

Example Code:

const response = {
  usage: {
    prompt_tokens: 7,
    completion_tokens: 172,
    total_tokens: 179
  }
};

const usage = normalizeUsage(response.usage);

console.log(usage);
// Output:
// {
//   input_tokens: 7,
//   output_tokens: 172,
//   total_tokens: 179
// }

const input = usage.input_tokens ?? 0;
const output = usage.output_tokens ?? 0;
const totalTokens = usage.total_tokens ?? 0;

console.log(input, output, totalTokens);
// Output: 7 172 179

Verification

To verify the fix, use the curl command provided in the issue body to confirm that the API returns the usage information.

curl https://coding.dashscope.aliyuncs.com/v1/chat/completions \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -d '{"model":"glm-5","messages":[{"role":"user","content":"OK"}]}'

Check that the response includes the usage information with the correct field names.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING