@mupt-ai/dari-gateway 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,8 @@ import { isRecord } from "@mupt-ai/dari-router/policy-engine";
2
2
  import { gatewayFor } from "../models/gateways.js";
3
3
  import { providerFailureFromCaught } from "../runtime/provider_stream_outcome.js";
4
4
  // Gateways bill per call and say what they billed: OpenRouter as `usage.cost`
5
- // on the final chunk, Vercel AI Gateway as `providerMetadata.gateway.cost`.
5
+ // on the final chunk (plus the provider's charge on a BYOK call), Vercel AI
6
+ // Gateway as `providerMetadata.gateway.cost`.
6
7
  // Pi prices usage from model rates and drops both, so the amount is read off
7
8
  // the raw response bytes and replaces the priced total.
8
9
  export function reportsProviderCost(model) {
@@ -63,7 +64,21 @@ export function reportedCost(body) {
63
64
  const gateway = isRecord(body.providerMetadata) && isRecord(body.providerMetadata.gateway)
64
65
  ? body.providerMetadata.gateway
65
66
  : undefined;
66
- return usd(isRecord(body.usage) ? body.usage.cost : undefined) ?? usd(gateway?.cost);
67
+ return openRouterCost(body.usage) ?? usd(gateway?.cost);
68
+ }
69
+ // On a BYOK call OpenRouter bills the provider's charge to the org's own key:
70
+ // `usage.cost` is only its fee (often 0), and the charge is in
71
+ // `cost_details.upstream_inference_cost`.
72
+ function openRouterCost(usage) {
73
+ if (!isRecord(usage))
74
+ return undefined;
75
+ const cost = usd(usage.cost);
76
+ if (cost === undefined || usage.is_byok !== true)
77
+ return cost;
78
+ const upstream = isRecord(usage.cost_details)
79
+ ? usd(usage.cost_details.upstream_inference_cost)
80
+ : undefined;
81
+ return cost + (upstream ?? 0);
67
82
  }
68
83
  function usd(value) {
69
84
  const amount = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mupt-ai/dari-gateway",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Self-hostable LLM gateway for coding agents and APIs: one endpoint for Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses, with subscription pooling, failover, and prompt-cache-aware routing.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -5,7 +5,8 @@ import { providerFailureFromCaught } from "../runtime/provider_stream_outcome.js
5
5
  import type { ProviderFetch } from "./response_capture.js";
6
6
 
7
7
  // Gateways bill per call and say what they billed: OpenRouter as `usage.cost`
8
- // on the final chunk, Vercel AI Gateway as `providerMetadata.gateway.cost`.
8
+ // on the final chunk (plus the provider's charge on a BYOK call), Vercel AI
9
+ // Gateway as `providerMetadata.gateway.cost`.
9
10
  // Pi prices usage from model rates and drops both, so the amount is read off
10
11
  // the raw response bytes and replaces the priced total.
11
12
 
@@ -84,7 +85,20 @@ export function reportedCost(body: unknown): number | undefined {
84
85
  const gateway = isRecord(body.providerMetadata) && isRecord(body.providerMetadata.gateway)
85
86
  ? body.providerMetadata.gateway
86
87
  : undefined;
87
- return usd(isRecord(body.usage) ? body.usage.cost : undefined) ?? usd(gateway?.cost);
88
+ return openRouterCost(body.usage) ?? usd(gateway?.cost);
89
+ }
90
+
91
+ // On a BYOK call OpenRouter bills the provider's charge to the org's own key:
92
+ // `usage.cost` is only its fee (often 0), and the charge is in
93
+ // `cost_details.upstream_inference_cost`.
94
+ function openRouterCost(usage: unknown): number | undefined {
95
+ if (!isRecord(usage)) return undefined;
96
+ const cost = usd(usage.cost);
97
+ if (cost === undefined || usage.is_byok !== true) return cost;
98
+ const upstream = isRecord(usage.cost_details)
99
+ ? usd(usage.cost_details.upstream_inference_cost)
100
+ : undefined;
101
+ return cost + (upstream ?? 0);
88
102
  }
89
103
 
90
104
  function usd(value: unknown): number | undefined {