openclaw - ✅(Solved) Fix [Bug]: openai-completions adapter does not extract usage information from response [2 pull requests, 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#54420Fetched 2026-04-08 01:27:48
View on GitHub
Comments
2
Participants
3
Timeline
10
Reactions
0
Author
Timeline (top)
commented ×2cross-referenced ×2labeled ×2referenced ×2

Title: openai-completions adapter does not extract usage information from response

Body:

Root Cause

Title: openai-completions adapter does not extract usage information from response

Body:

PR fix notes

PR #54516: fix: extract usage data from openai-completions adapter response (#54420)

Description (problem / solution / changelog)

Summary

  • Fixes #54420
  • The openai-completions adapter was not extracting usage.prompt_tokens and usage.completion_tokens from non-streaming responses
  • Maps provider usage fields to OpenAI's internal format (input/output tokens)

Test plan

  • Use openai-completions adapter with a provider that returns usage data (e.g. vLLM)
  • Verify session files record correct token counts instead of all zeros

Changed files

  • src/gateway/openai-http.ts (modified, +25/-1)

PR #54624: fix(openai-completions): extract usage tokens from non-streaming response

Description (problem / solution / changelog)

Summary

Fixes #54420.

The openai-completions adapter hardcoded usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } in non-streaming responses, ignoring actual token counts returned by the provider (e.g. vLLM).

Root Cause

In src/gateway/openai-http.ts, the non-streaming response builder always returned zeroed usage fields instead of reading from result.meta.agentMeta.usage.

Fix

Added extractUsage() that reads result.meta.agentMeta.usage (same shape used by openresponses-http.ts) and maps internal normalized fields (input, output, total) to OpenAI format (prompt_tokens, completion_tokens, total_tokens). Falls back to zeros when usage metadata is absent.

Tests

Added two test cases:

  • Verifies usage tokens are correctly extracted when meta.agentMeta.usage is present
  • Verifies graceful fallback to zeros when meta is absent

Changed files

  • src/gateway/openai-http.test.ts (modified, +52/-0)
  • src/gateway/openai-http.ts (modified, +27/-1)

Code Example

{
  "id": "chatcmpl-a2b283107539c1c4",
  "object": "chat.completion",
  "model": "glm-5",
  "choices": [...],
  "usage": {
    "prompt_tokens": 8,
    "completion_tokens": 10,
    "total_tokens": 18
  }
}

---

{
  "usage": {
    "input": 0,
    "output": 0,
    "cacheRead": 0,
    "cacheWrite": 0,
    "totalTokens": 0
  }
}

---
RAW_BUFFERClick to expand / collapse

Bug type

Regression (worked before, now fails)

Summary

Title: openai-completions adapter does not extract usage information from response

Body:

Environment

  • OpenClaw version: 2026.3.23-2
  • Backend: vLLM (OpenAI-compatible API)
  • Config: blockStreamingDefault: "on" (non-streaming mode)
  • Adapter: openai-completions

Problem

vLLM correctly returns usage information in non-streaming responses:

{
  "id": "chatcmpl-a2b283107539c1c4",
  "object": "chat.completion",
  "model": "glm-5",
  "choices": [...],
  "usage": {
    "prompt_tokens": 8,
    "completion_tokens": 10,
    "total_tokens": 18
  }
}

However, OpenClaw session files record all zeros:

{
  "usage": {
    "input": 0,
    "output": 0,
    "cacheRead": 0,
    "cacheWrite": 0,
    "totalTokens": 0
  }
}

Expected Behavior

The openai-completions adapter should correctly extract usage.prompt_tokens and usage.completion_tokens from the OpenAI-compatible response and map them to the internal usage fields.

Additional Notes

  • This affects token usage statistics in /status and session analytics
  • The issue was confirmed with both streaming (requires stream_options.include_usage: true) and non-streaming modes
  • vLLM is a common OpenAI-compatible backend, so this likely affects many users

提交地址: https://github.com/openclaw/openclaw/issues/new

复制上面的内容提交即可。

Steps to reproduce

openai-completions adapter does not extract usage information from response

Expected behavior

The openai-completions adapter should correctly extract usage.prompt_tokens and usage.completion_tokens from the OpenAI-compatible response and map them to the internal usage fields.

Actual behavior

openai-completions adapter does not extract usage information from response

OpenClaw version

2026.3.22

Operating system

Ubuntu 24

Install method

No response

Model

GLM-5

Provider / routing chain

custom providers -> vllm -> GLM-5

Additional provider/model setup details

No response

Logs, screenshots, and evidence

Impact and severity

No response

Additional information

No response

extent analysis

Fix Plan

To fix the issue with the openai-completions adapter not extracting usage information from the response, follow these steps:

  • Update the openai-completions adapter to correctly map the usage.prompt_tokens and usage.completion_tokens from the OpenAI-compatible response to the internal usage fields.
  • Modify the adapter to handle both streaming and non-streaming modes.

Example code changes:

# In openai-completions adapter
def extract_usage(response):
    usage = response.get('usage', {})
    prompt_tokens = usage.get('prompt_tokens', 0)
    completion_tokens = usage.get('completion_tokens', 0)
    total_tokens = usage.get('total_tokens', 0)
    
    # Map to internal usage fields
    internal_usage = {
        'input': prompt_tokens,
        'output': completion_tokens,
        'cacheRead': 0,  # Not provided by OpenAI API
        'cacheWrite': 0,  # Not provided by OpenAI API
        'totalTokens': total_tokens
    }
    return internal_usage

def process_response(response):
    # ... existing code ...
    usage = extract_usage(response)
    # ... existing code ...
    return usage

Verification

To verify that the fix worked:

  • Test the openai-completions adapter with both streaming and non-streaming modes.
  • Check that the usage information is correctly extracted and mapped to the internal usage fields.
  • Verify that the token usage statistics in /status and session analytics are accurate.

Extra Tips

  • Make sure to update the adapter to handle any potential errors or edge cases.
  • Consider adding logging or debugging statements to help diagnose any issues that may arise.
  • Review the OpenAI API documentation to ensure that the adapter is correctly implementing the usage extraction logic.

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…

FAQ

Expected behavior

The openai-completions adapter should correctly extract usage.prompt_tokens and usage.completion_tokens from the OpenAI-compatible response and map them to the internal usage fields.

Still need to ship something?

×6

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

Back to top recommendations

TRENDING

openclaw - ✅(Solved) Fix [Bug]: openai-completions adapter does not extract usage information from response [2 pull requests, 2 comments, 3 participants]