[GH-ISSUE #23926] feat: Track token usage analytics for API-key requests without chat_id #35641

Closed
opened 2026-04-25 09:48:14 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @pvyswiss on GitHub (Apr 21, 2026).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/23926

Feature Request

The /admin/analytics dashboard shows zero token usage for models used exclusively via API clients (OpenCode Desktop, Continue.dev, curl). Models used from the Web UI browser show correct analytics.

Related to: #23917 (Bug 6)

Root Cause

The analytics pipeline requires event_emitter to be non-None, which requires chat_id, session_id, and message_id. API clients don't send these -- they manage their own conversation state.

When event_emitter is None, the streaming handler falls to a simple passthrough branch with NO usage tracking and NO DB writes. The usage data IS present in the SSE stream but nobody reads it.

Path chat_id event_emitter Usage Tracked DB Write
Web UI browser Set by frontend Non-None Yes Yes
API client None None No No

Suggested Fix

In the passthrough branch, extract usage from the final SSE chunk and write to DB:

else:
    async def stream_wrapper(original_generator, events):
        usage = None
        async for data in original_generator:
            if isinstance(data, str) and data.startswith('data: '):
                try:
                    chunk = json.loads(data[6:])
                    if chunk.get('usage'):
                        usage = normalize_usage(chunk['usage'])
                except (json.JSONDecodeError, ValueError):
                    pass
            yield data
        if usage and metadata.get('user_id'):
            # Write usage to DB even without chat_id
            ...

Impact

  • Web UI: None (already has full analytics)
  • API clients: Token usage tracked for cost monitoring

File

backend/open_webui/utils/middleware.py

Originally created by @pvyswiss on GitHub (Apr 21, 2026). Original GitHub issue: https://github.com/open-webui/open-webui/issues/23926 ## Feature Request The `/admin/analytics` dashboard shows zero token usage for models used exclusively via API clients (OpenCode Desktop, Continue.dev, curl). Models used from the Web UI browser show correct analytics. Related to: #23917 (Bug 6) ## Root Cause The analytics pipeline requires `event_emitter` to be non-None, which requires `chat_id`, `session_id`, and `message_id`. API clients don't send these -- they manage their own conversation state. When `event_emitter` is None, the streaming handler falls to a simple passthrough branch with NO usage tracking and NO DB writes. The usage data IS present in the SSE stream but nobody reads it. | Path | chat_id | event_emitter | Usage Tracked | DB Write | |------|---------|---------------|---------------|----------| | Web UI browser | Set by frontend | Non-None | Yes | Yes | | API client | None | None | No | No | ## Suggested Fix In the passthrough branch, extract usage from the final SSE chunk and write to DB: ```python else: async def stream_wrapper(original_generator, events): usage = None async for data in original_generator: if isinstance(data, str) and data.startswith('data: '): try: chunk = json.loads(data[6:]) if chunk.get('usage'): usage = normalize_usage(chunk['usage']) except (json.JSONDecodeError, ValueError): pass yield data if usage and metadata.get('user_id'): # Write usage to DB even without chat_id ... ``` ## Impact - **Web UI**: None (already has full analytics) - **API clients**: Token usage tracked for cost monitoring ## File `backend/open_webui/utils/middleware.py`
Author
Owner
<!-- gh-comment-id:4295600568 --> @gaby commented on GitHub (Apr 22, 2026): Duplicate of https://github.com/open-webui/open-webui/issues/21675?issue=open-webui%7Copen-webui%7C21652 Related to https://github.com/open-webui/open-webui/issues/21675?issue=open-webui%7Copen-webui%7C21675
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#35641