ai-vercel-gateway: usage.cost leaves out gateway surcharges, and the BYOK credential type is not exposed
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 55/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
Start with packages/ai-vercel-gateway/src/utils/gateway-cost.ts and the gateway response metadata described in the issue. Trace how withGatewayCost sets usage.cost and how providerUsageDetails is assembled, then check the related tests. Done when usage exposes the gateway-debited amount, surcharge information, and credential type as described.
索引モデルが issue の本文から書いたものです。
説明
What happens
Follow-up to #1845 (fixed in #1873, thank you). withGatewayCost copies provider_metadata.gateway.cost to usage.cost and documents it as the amount debited:
When the gateway adds a surcharge (Custom Reporting user / tags writes, Zero Data Retention), cost is the inference charge only. The amount debited is gatewayCost, which is cost + surchargeCost. Vercel's generation lookup docs define gateway_cost as "Total amount debited from your AI Gateway balance for this generation", and total_cost as including "any surcharges (for example, Zero Data Retention or Custom Reporting writes)".
The response also says which credential served the request (routing.modelAttempts[].providerAttempts[].credentialType: byok or system). That is not carried over either, so a consumer cannot tell whether cost: 0 means a BYOK request, which the provider bills at marketCost. We use costDetails.upstreamCost ?? cost for that today.
Repro
@tanstack/ai 1.0.0, @tanstack/ai-vercel-gateway 1.0.0, server-side chat() on Node 24
provider_metadata.gateway from POST https://ai-gateway.vercel.sh/v1/responses with max_output_tokens: 16:
| Model (credential) | providerOptions.gateway |
cost |
marketCost |
surchargeCost |
gatewayCost |
|---|---|---|---|---|---|
openai/gpt-4.1-nano (system) |
none | 0.0000019 | 0.0000019 | 0 | 0.0000019 |
openai/gpt-4.1-nano (system) |
user + tags |
0.0000019 | 0.0000019 | 0.00015 | 0.0001519 |
anthropic/claude-sonnet-5.5 (byok) |
none | 0 | 0.00008 | 0 | 0 |
anthropic/claude-sonnet-5.5 (byok) |
user + tags |
0 | 0.00008 | 0.00015 | 0.00015 |
The same user + tags requests through the adapter:
let usage
await chat({
adapter: createVercelGatewayText(model, key),
messages: [{ role: 'user', content: 'Reply with OK.' }],
threadId: 'repro',
stream: false,
modelOptions: { max_output_tokens: 16, gateway: { user: 'u1', tags: ['t1'] } },
middleware: [{ name: 'usage', onFinish: (_ctx, info) => { usage = info.usage } }],
})
| Model | usage.cost |
usage.costDetails |
usage.providerUsageDetails |
GET /v1/generation for that id |
|---|---|---|---|---|
openai/gpt-4.1-nano |
0.0000019 | { upstreamCost: 0.0000019 } |
generationId, finalProvider: 'azure' |
gateway_cost 0.0001519, surcharge_cost 0.00015, is_byok false |
anthropic/claude-sonnet-5.5 |
0 | { upstreamCost: 0.00008 } |
generationId, finalProvider: 'anthropic' |
gateway_cost 0.00015, surcharge_cost 0.00015, is_byok true |
Expected
usage.cost is what the gateway debits (gatewayCost), or the surcharge is available next to it (for example surchargeCost and gatewayCost in providerUsageDetails). The credential type is carried in providerUsageDetails as well.
Why
Per-run cost in our analytics should match the gateway bill. For BYOK runs we report the provider's list price instead, which only works when we can tell BYOK from system credits.
- 主要言語
- TypeScript
- スター
- 3.2k
- フォーク
- 372
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 229
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TanStack/ai のほかの issue
-
openai-base: a Responses max_output_tokens cutoff ends as RUN_ERROR without usage instead of finishReason 'length'対応中かも @pcbeingused333 が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 58/100
メンテナーはふだん 1 日以内に返信
-
ai-gemini: native image generation throws "returned no id" when modelOptions.store is false対応中かも @pcbeingused333 が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 57/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
Shell: serve the home app's Cookie Policy on a hard reload対応中かも @2001anshkaushik が今日担当しました。 オープンfeature module:ai
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
rocketride-org/rocketride-server#2899 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
wazuh/wazuh-dashboard-plugins#9551 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
gofish-graphics/gofish-graphics#1252 ·
メンテナーはふだん 1 日以内に返信
-
optimization P3 size:XS
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
josephfung/curia#2366 ·
メンテナーはふだん 1 日以内に返信
-
type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
nightgauge/nightgauge#2688 ·
メンテナーはふだん 1 日以内に返信