Skip to content

gateway: log one usage line per model call (model, tokens, hashed key id; no text) - #31

Merged
vadymhimself merged 2 commits into
release/2.65.0-vadymhimself.15from
gauge/usage-log
Nov 15, 2026
Merged

vadymhimself merged 2 commits into
release/2.65.0-vadymhimself.15from
gauge/usage-log

Conversation

@vadymhimself

Copy link
Copy Markdown
Collaborator

Why

The gateway logs no model, tokens or caller per call, so per-key usage (for example the share of calls and tokens on each model) can't be read from the process log. The fork already holds that on its own final request record and usage ledger, but only in the gateway's memory and local disk.

What

  • src/lib/usage-line-log.ts (new): one line per finished request: [usage] status=<http> provider="<id>" model="<id>" in=<n> out=<n> cached=<n> key=<8-hex> ms=<n>.
    • key is the first 8 hex of sha256 of the configured key's record id, never the secret. Environment or loopback admission has no id and logs -.
    • Ids are redacted, stripped of control characters, cut to 80 characters and quoted. A pooled provider's account suffix is dropped, so the line names no account.
    • Only the named fields of the record are read: no message text, body, header, secret or email. Missing counts log as -.
    • Written only when OPENCODEX_AGENTRADAR=1, the same condition as the [ar-acct] line, so a self-hosted gateway gains no new output. It never throws.
  • src/server/request-log.ts: one call in addRequestLog, the single sink every finished request row passes through, on the same record the usage ledger persists. Local refusals (413, 429) also log, with counts -.
  • structure/transports/byte-accounting.md: documents the line.
  • Version 2.65.0-vadymhimself.25 through release-version-sources sync (all 4 sources).

Tests

  • tests/lib/call-usage-line-log.test.ts (new, 8): fields and format; prompt text, secret and authorization header never appear; ids with newlines or 200 characters stay on one bounded line; account suffix dropped; off without AgentRadar mode; missing counts log -. Driven from the fork's own request record, so no provider payload is imitated.
  • Run alone and green: the new test, request-log-served-model, file-size ratchet, release-version-sources, test-layout, test-layout-tooling, privacy-scan; scoped type check of the changed sources clean.

Eval added/updated: a finished request through the request log writes one usage line with model, token counts and a hashed key id, and no prompt text or secret appears in it.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

✅ Deterministic PR hygiene checks passed.

… id; no text)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vadymhimself

Copy link
Copy Markdown
Collaborator Author

independent-qa (Mars): GREEN at b8acc2a48a673c8416ccf722b869ab2290733126 (security side: what goes into logs)

(Reviewed at 6cd770b. This head is a rebase onto main, which now has #30. All 10 of this PR's file blobs are identical to 6cd770b.)

What I checked:

  • Only named fields are read. usageLogLine reads status, provider, model, three token counts, apiKeyId and durationMs from the fork's own final request record. It reads no text, body, header or secret. The hook is the one call in addRequestLog (request-log.ts:612), after retention, on the same record the usage ledger stores.
  • Key hash. It is the first 8 hex of sha256 of the configured key's record id, and - for environment or loopback admission. OcxApiKeyEntry keeps id and the secret key as separate fields, and apiKeyId is set only for configured admission (request-log.ts:137-140). So the input is the record id, never the key. The hash is unsalted, which is fine here: the record id is not a secret, and the hash is only meant to be matched against a list of ids. Even if someone hand-edited a config to make the id equal the key, only 32 bits of its sha256 would show.
  • No account named. baseProviderLabel strips the p pool labels, and ACCOUNT_SUFFIX_RE strips the o (OAuth) and k (API-key) labels, which covers every account label shape in account-label.ts. The test covers all three.
  • One bounded line. Provider and model go through sanitizeLogMetadataString: control and line-separator characters are removed, the shared secret redactor runs, the value is cut to 80 characters, and it is JSON-quoted. That covers a client-supplied model id too. Counts and the duration print only as finite, non-negative numbers, and the status only as an integer, otherwise -.
  • Only on AgentRadar gateways. The line is written only when OPENCODEX_AGENTRADAR=1, the same switch the [ar-acct] line uses, so a self-hosted gateway prints nothing new. A test covers it.
  • Safe sink. It is one synchronous console.info in a try with no buffer, so it can't throw into the request or grow memory.

Before merge:

  1. CI must be green at this sha. When I looked, gates had failed and the four test shards and both macOS jobs were still running. A fix push comes back to me, and a test-only or layout-only change is a quick re-stamp.

Not blocking:

  • One more test row: a provider label holding an sk- style token, to show the redactor runs on that field too. Today only newline and length are tested for ids.
  • Mapping hashes to lanes: when Liza's side sends the key id per agent, have the ids only, never the keys. Keep the mapping out of the log itself.

A new push voids this GREEN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the chore label Nov 15, 2026
@vadymhimself

Copy link
Copy Markdown
Collaborator Author

independent-qa (Mars): GREEN at 93d7d480eeca0b78358826ce44ba5ba1ee39dc12 (re-stamp; one test-only commit on my GREEN at b8acc2a)

The commit changes only tests/lib/call-usage-line-log.test.ts:

  • the fake auth header now uses the privacy scan's allowed sentinel shape;
  • a new row checks that a key-shaped token inside a provider label is redacted and the line keeps its shape.

No product file changed, so my review stands. Merge once CI is green at this sha. A new push voids this GREEN.

@vadymhimself
vadymhimself merged commit 8ca6f61 into release/2.65.0-vadymhimself.15 Nov 15, 2026
60 of 62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants