Skip to content

[Feature]: Filter the Usage report by API key (and by unattributed admission kind) #7289

Closed as not planned

Description

@nordz0r

Area

Dashboard

What are you trying to accomplish?

Operators running one OpenCodex instance for several clients (Codex CLI, Claude Code, Cursor, OMP, Hermes, OpenClaw, third-party agents, plain curl) need to attribute spend and traffic to the credential that produced it. The Usage page already filters by time range and by client surface (codex / claude / grok), but there is no way to answer "how many tokens did API key ocx_data_ab12cd34 spend this week, and on which models".

The backend already records and accepts everything needed: usage.jsonl rows carry apiKeyId and admissionKind (src/usage/log.ts), GET /api/usage?apiKeyId=... filters on it (src/server/management/logs-usage-routes.ts), ocx usage --api-key-id exposes it on the CLI, and GET /api/keys already returns a per-key requests7d / totalRequests / lastUsedAt rollup. Only the Dashboard Usage page never exposes the filter.

What prevents this today?

gui/src/pages/Usage.tsx renders exactly two filter controls: the range segmented control and the surface segmented control (UsageFilters, surface state).

The only apiKeyId the page ever sends is the connected client's own key — if (connected && scope === "machine" && apiKeyId) query.set("apiKeyId", apiKeyId) — which is a self-scope, not a filter: on a standalone hub the parameter is never sent at all, and on a connected client it cannot be changed to another key.

Consequences:

  • On a hub serving several clients, per-key cost and token attribution requires the CLI (ocx usage --api-key-id <id>) or hand-editing a URL, because the Dashboard has no control for it.
  • The API Keys page shows a 7-day request count per key but offers no drill-down into that key's usage report.
  • admissionKind: "environment" and "loopback" traffic is invisible as a category: those rows carry no apiKeyId by design (admissionFields() in src/server/auth-cors.ts), so "which part of my spend is unattributed?" cannot be answered from the UI.

What should OpenCodex do?

Add an API-key filter to the Usage page, alongside the existing range and surface controls:

  1. Key selector. A dropdown listing the configured keys from GET /api/keys (id, name, masked prefix) plus explicit entries for the two unattributed admission kinds: environment and loopback. Default stays "All keys", so the current report is unchanged until the operator picks one.
  2. Server-side filtering, not client-side. The selection is sent as the existing apiKeyId query parameter on GET /api/usage, so the filtered read keeps using the full-scan singleflight path that already exists for filtered requests and never enters the range:surface cache. For the unattributed kinds, extend the parameter with an explicit admission-kind value (for example apiKeyId=@environment) rather than inventing a sentinel key id — apiKeyId is a lookup key and a synthetic value would collide with a hand-edited config entry.
  3. Honest echo and empty state. The existing filter echo ({ apiKeyId, matched, ... }) should drive a visible chip naming the active key, and an empty result must read as "no readable rows for this key in this range" rather than as a zero-spend key. usageIncomplete must keep its current meaning.
  4. Connected-client behavior. On a connected client, "this machine's key" remains the only selectable key (the hub's /api/hub/usage route pins the caller's own identity and rejects any other), so the control should render read-only there and explain why, instead of offering a selection the hub will refuse.
  5. Entry point from the API Keys page. The per-key usage column should link into Usage with that key preselected, so the existing requests7d number has somewhere to lead.

Example usage or interface

Dashboard, Usage → Report:

Range:  [Today] [7d] [30d] [All]
Client: [All] [Codex] [Claude] [Grok]
Key:    [All keys ▾]   ← new
          All keys
          ci-agent           ocx_data_ab12cd34…   1,204 req
          laptop-codex       ocx_data_77ff01ab…     318 req
          unattributed (environment)
          unattributed (loopback)

Equivalent CLI calls that already work today and define the expected numbers:

ocx usage --range 7d --api-key-id ci-agent
ocx usage --range 7d --surface codex --api-key-id ci-agent

The Dashboard filter must return the same totals as these two commands for the same window.

Alternatives or workarounds

  • ocx usage --api-key-id <id> on the hub host. Correct, but terminal-only and unavailable to an operator working in the Dashboard.
  • Reading usage.jsonl directly or building the URL by hand (/api/usage?range=7d&apiKeyId=...). Works, but nothing in the UI hints the parameter exists, and the cache-key and echo behavior are easy to get wrong by hand.
  • Per-key isolation via separate OpenCodex instances with their own OPENCODEX_HOME. That is the workaround described in the closed per-key spend-scope request (feat(spend): per-API-key spend scope (spend.apiKey) and opt-in keys on loopback #6156, closed as not-planned because it was not filed through a template); it multiplies ledgers and processes instead of answering the attribution question in one place.

Additional context

Checks

  • I searched existing issues and documentation.
  • This request describes a concrete OpenCodex workflow rather than merely naming a desired technology.
  • I removed secrets and personal data.

Activity

  1. added
    enhancement
    gui
    on Oct 17, 2026
  2. Ingwannu commented on Oct 18, 2026

    @Ingwannu
    Owner

    This needs a small UI/API design before implementation. Reuse the existing API-key attribution/filtering contract and preserve the connected caller's server-enforced key scope; a dashboard selector must not widen it. Keep “all permitted keys”, one concrete key, and unattributed admission kinds as distinct typed states, not string sentinels that can collide with real key IDs. Specify deleted/renamed keys, mixed attributed/unattributed records, combined date/provider/model filters, CSV/export parity, and connected versus standalone behavior. Please attach the proposed interaction and empty/loading/error states for owner review. No data-plane or identity migration is needed for the initial filter.

  3. added
    needs-design
    on Oct 18, 2026
  4. lidge-jun commented on Oct 22, 2026

    @lidge-jun
    Owner

    Thank you for the detailed proposal. We're closing this as not planned: it is outside the current priorities, and keeping it open would suggest work that isn't scheduled. The discussion stays here for reference. If you'd like to pursue it, please reopen this or open a new issue with a narrow, opt-in first slice.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementguineeds-design

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions