Skip to content

feat(gateway): return actual token usage in /v1/chat/completions#24604

Closed
jogelin wants to merge 2 commits into
openclaw:mainfrom
jogelin:feat/openai-compat-usage-tokens
Closed

feat(gateway): return actual token usage in /v1/chat/completions#24604
jogelin wants to merge 2 commits into
openclaw:mainfrom
jogelin:feat/openai-compat-usage-tokens

Conversation

@jogelin

@jogelin jogelin commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

Problem

The OpenAI-compatible /v1/chat/completions endpoint always returns hardcoded zero token counts:

"usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0 }

This makes it impossible for downstream consumers (pipeline runners, scripts, monitoring tools) to track per-request token consumption without using the full WebSocket session protocol.

Solution

Extract actual token counts from the agent command result (result.meta.agentMeta.usage) and return them in the HTTP response, using the existing normalizeUsage() utility from agents/usage.ts.

Changes

  • src/gateway/openai-http.ts: Add resolveAgentUsage() function that extracts and normalizes token usage from the agent result, replacing the hardcoded zeros.

Impact

  • Non-streaming /v1/chat/completions responses now include actual prompt_tokens and completion_tokens
  • No breaking changes — the response shape is identical, just with real values
  • Streaming responses are unaffected (usage tracking in SSE chunks is a separate concern)

Greptile Summary

Adds actual token usage reporting to the /v1/chat/completions endpoint by extracting usage data from agent command results and normalizing it using the existing normalizeUsage utility.

Changes:

  • Imports normalizeUsage from agents/usage.ts
  • Adds AgentCommandResult type that captures the meta.agentMeta.usage structure
  • Implements resolveAgentUsage() function to extract and format token counts from agent results
  • Replaces hardcoded zeros with resolveAgentUsage(result) in non-streaming responses

Note: Per PR description, streaming responses are intentionally excluded from this change.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The change is straightforward and well-isolated: it extracts existing usage data from agent results using the established normalizeUsage utility, with proper fallback to zero values when data is unavailable. The implementation correctly handles null/undefined values, and only affects non-streaming responses as intended. The type definitions are appropriate and consistent with existing patterns in the codebase.
  • No files require special attention

Last reviewed commit: 25d2208

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels Feb 23, 2026
@jogelin jogelin force-pushed the feat/openai-compat-usage-tokens branch from 25d2208 to 59c0f8c Compare February 23, 2026 16:23
@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label Feb 23, 2026
Previously the OpenAI-compatible chat completions endpoint always
returned usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }.

This extracts actual token counts from the agent command result
(result.meta.agentMeta.usage) and returns them in the response,
using the existing normalizeUsage() utility.

This enables downstream consumers (pipeline runners, scripts) to
track per-request token consumption without needing the full
WebSocket session protocol.
@jogelin jogelin force-pushed the feat/openai-compat-usage-tokens branch from 59c0f8c to f80b3e8 Compare February 23, 2026 16:26
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 1, 2026
@jogelin

jogelin commented Mar 1, 2026

Copy link
Copy Markdown
Contributor Author

This pull request has been automatically marked as stale due to inactivity. Please add updates or it will be closed.

Reopen

@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Mar 3, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added stale Marked as stale due to inactivity and removed stale Marked as stale due to inactivity labels Mar 22, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 30, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime size: S stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant