Skip to content

feat(ai-gateway): log Vercel upstream provider id as upstream_id - #4828

Merged
chrarnoldus merged 2 commits into
mainfrom
similar-gourd
Jul 28, 2026
Merged

feat(ai-gateway): log Vercel upstream provider id as upstream_id#4828
chrarnoldus merged 2 commits into
mainfrom
similar-gourd

Conversation

@chrarnoldus

Copy link
Copy Markdown
Contributor

Problem

The Vercel AI Gateway reports the upstream provider's own id in
provider_metadata.gateway.routing.modelAttempts[].providerAttempts[].providerRequestId
(and providerResponseId), e.g. Fireworks' chatcmpl-ccebc94c526d4f8797cfe00023478a9a.

microdollar_usage_metadata.upstream_id was only ever populated from the
OpenRouter-style GET /generation?id=… lookup, and that lookup is skipped for
healthy Vercel requests (they already report cost and finalProvider), so the
upstream provider id was never logged.

Change

  • VercelProviderAttempt now models providerRequestId / providerResponseId.
  • New extractVercelUpstreamId() in processUsage.shared.ts, sharing the
    serving-attempt selection with extractVercelIsByok() via
    findVercelServingProviderAttempt().
  • Wired into all six Vercel parse paths that previously hardcoded
    upstream_id: null: chat_completions (stream + JSON), messages (stream +
    JSON), responses (stream + JSON).
  • processTokenData no longer clobbers a response-derived upstream_id with a
    null from the /generation merge.

Verification

  • jest src/lib/ai-gateway/processUsage — 74 passed, including three new
    chat_completions tests (streamed id, missing id stays null, non-streamed id).
  • The vercel-messages/vercel-responses approval fixtures already contained
    providerRequestId; their approved output now shows the real upstream ids
    instead of null.
  • tsgo --noEmit, pnpm lint, pnpm format.

The Vercel AI Gateway reports the upstream provider's own request/response id
in provider_metadata.gateway.routing.modelAttempts[].providerAttempts[]. Until
now upstream_id was only populated from an OpenRouter-style /generation lookup,
which is skipped for healthy Vercel requests, so microdollar_usage_metadata
never recorded it.

Extract it in all chat_completions, messages and responses parse paths, using
the same serving-attempt selection as the BYOK extraction.
@chrarnoldus chrarnoldus self-assigned this Jul 28, 2026
@kilo-code-bot

kilo-code-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

Reviewed the Vercel AI Gateway upstream_id extraction change; the attempt-selection logic, all six wired call sites, and the processTokenData merge fix are consistent and covered by new/updated tests with no issues found in the changed code.

Files Reviewed (9 files)
  • apps/web/src/lib/ai-gateway/processUsage.messages.ts
  • apps/web/src/lib/ai-gateway/processUsage.responses.ts
  • apps/web/src/lib/ai-gateway/processUsage.shared.ts
  • apps/web/src/lib/ai-gateway/processUsage.test.ts
  • apps/web/src/lib/ai-gateway/processUsage.ts
  • apps/web/src/lib/ai-gateway/processUsage.types.ts
  • apps/web/src/tests/sample/vercel-messages.log.resp.sse.approved.json
  • apps/web/src/tests/sample/vercel-responses.log.resp.json.approved.json
  • apps/web/src/tests/sample/vercel-responses.log.resp.sse.approved.json

Reviewed by claude-sonnet-5 · Input: 20 · Output: 4.8K · Cached: 413.9K

Review guidance: REVIEW.md from base branch main

@chrarnoldus
chrarnoldus merged commit 22d842c into main Jul 28, 2026
14 checks passed
@chrarnoldus
chrarnoldus deleted the similar-gourd branch July 28, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants