@opencode/ai 0.0.0-dev-21765 → 0.0.0-dev-21766

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.
@@ -996,6 +996,7 @@ const mapUsage = (usage, providerMetadataKey) => {
996
996
  cacheWriteInputTokens: cacheWrite,
997
997
  reasoningTokens: ProviderShared.sumTokens(...iterations.map((item) => item.output_tokens_details?.thinking_tokens)),
998
998
  totalTokens: ProviderShared.totalTokens(inputTokens, outputTokens, undefined),
999
+ ...ProviderShared.reportedBilling(usage),
999
1000
  providerMetadata: { [providerMetadataKey]: usage },
1000
1001
  });
1001
1002
  };
@@ -1024,6 +1025,8 @@ const mergeUsage = (left, right, providerMetadataKey) => {
1024
1025
  cacheWriteInputTokens,
1025
1026
  reasoningTokens,
1026
1027
  totalTokens: ProviderShared.totalTokens(inputTokens, outputTokens, undefined),
1028
+ cost: right.cost ?? left.cost,
1029
+ allowance: right.allowance ?? left.allowance,
1027
1030
  providerMetadata: {
1028
1031
  [providerMetadataKey]: mergeJsonRecords(left.providerMetadata?.[providerMetadataKey], right.providerMetadata?.[providerMetadataKey]) ??
1029
1032
  {},
@@ -105,6 +105,10 @@ declare const GeminiBody: Schema.Struct<{
105
105
  }>;
106
106
  export type GeminiBody = Schema.Schema.Type<typeof GeminiBody>;
107
107
  declare const GeminiUsage: Schema.Struct<{
108
+ readonly cost: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Finite>, never>>;
109
+ readonly opencode: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Struct<{
110
+ readonly allowance: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Finite>, never>>;
111
+ }>>, never>>;
108
112
  readonly cachedContentTokenCount: Schema.optional<Schema.NullOr<Schema.Number>>;
109
113
  readonly thoughtsTokenCount: Schema.optional<Schema.NullOr<Schema.Number>>;
110
114
  readonly promptTokenCount: Schema.optional<Schema.NullOr<Schema.Number>>;
@@ -259,6 +263,10 @@ export declare const protocol: Protocol<{
259
263
  readonly safetyRatings?: unknown;
260
264
  } | null | undefined;
261
265
  readonly usageMetadata?: {
266
+ readonly cost?: number | undefined;
267
+ readonly opencode?: {
268
+ readonly allowance?: number | undefined;
269
+ } | undefined;
262
270
  readonly cachedContentTokenCount?: number | null | undefined;
263
271
  readonly thoughtsTokenCount?: number | null | undefined;
264
272
  readonly promptTokenCount?: number | null | undefined;
@@ -286,6 +294,10 @@ export declare const protocol: Protocol<{
286
294
  readonly safetyRatings?: unknown;
287
295
  } | null | undefined;
288
296
  readonly usageMetadata?: {
297
+ readonly cost?: number | undefined;
298
+ readonly opencode?: {
299
+ readonly allowance?: number | undefined;
300
+ } | undefined;
289
301
  readonly cachedContentTokenCount?: number | null | undefined;
290
302
  readonly thoughtsTokenCount?: number | null | undefined;
291
303
  readonly promptTokenCount?: number | null | undefined;
@@ -171,6 +171,7 @@ const GeminiUsage = Schema.Struct({
171
171
  promptTokenCount: optionalNull(Schema.Number),
172
172
  candidatesTokenCount: optionalNull(Schema.Number),
173
173
  totalTokenCount: optionalNull(Schema.Number),
174
+ ...ProviderShared.ReportedBilling.fields,
174
175
  });
175
176
  const GeminiCandidate = Schema.Struct({
176
177
  content: optionalNull(GeminiResponseContent),
@@ -431,6 +432,8 @@ const mapUsage = (usage, metadataKey, metadata = usage ?? {}) => {
431
432
  cacheReadInputTokens: cached,
432
433
  reasoningTokens: thoughts,
433
434
  totalTokens: ProviderShared.totalTokens(promptTokens, outputTokens, usage.totalTokenCount ?? undefined),
435
+ cost: usage.cost,
436
+ allowance: usage.opencode?.allowance,
434
437
  providerMetadata: providerMetadata(metadataKey, metadata),
435
438
  });
436
439
  };
@@ -444,6 +447,9 @@ const mapContinuedUsage = (requests, metadataKey) => {
444
447
  cachedContentTokenCount: requests[0].cachedContentTokenCount,
445
448
  candidatesTokenCount: sum("candidatesTokenCount"),
446
449
  thoughtsTokenCount: sum("thoughtsTokenCount"),
450
+ // Every continued request is billed on its own.
451
+ cost: sum("cost"),
452
+ opencode: { allowance: ProviderShared.sumTokens(...requests.map((usage) => usage.opencode?.allowance)) },
447
453
  }, metadataKey, {
448
454
  promptTokenCount: sum("promptTokenCount"),
449
455
  cachedContentTokenCount: sum("cachedContentTokenCount"),
@@ -644,6 +644,7 @@ export const mapUsage = (usage, providerMetadataKey) => {
644
644
  cacheWriteInputTokens: cacheWrite,
645
645
  reasoningTokens: reasoning,
646
646
  totalTokens: ProviderShared.totalTokens(usage.input_tokens, usage.output_tokens, usage.total_tokens),
647
+ ...ProviderShared.reportedBilling(usage),
647
648
  providerMetadata: { [providerMetadataKey]: usage },
648
649
  });
649
650
  };
@@ -709,6 +709,7 @@ const mapUsage = (usage, providerMetadataKey) => {
709
709
  cacheWriteInputTokens: cacheWrite,
710
710
  reasoningTokens: reasoning,
711
711
  totalTokens: ProviderShared.totalTokens(input, output, usage.total_tokens ?? undefined),
712
+ ...ProviderShared.reportedBilling(usage),
712
713
  providerMetadata: { [providerMetadataKey]: usage },
713
714
  });
714
715
  };
@@ -13,6 +13,21 @@ export declare const optionalNull: <const S extends Schema.Top>(schema: S) => Sc
13
13
  export declare const optionalNullish: <const S extends Schema.Top>(schema: S) => Schema.decodeTo<Schema.optional<Schema.toType<S>>, Schema.optional<Schema.NullOr<S>>, never, never>;
14
14
  /** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
15
15
  export declare const lenient: <const S extends Schema.Top>(schema: S) => Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<S>, S["DecodingServices"]>>;
16
+ /**
17
+ * Billing an endpoint reports beside its token usage. `cost` is the USD it charged (OpenRouter, LiteLLM, OpenCode
18
+ * Zen); OpenCode Go reports the subscription allowance a request used as `opencode.allowance`, also in USD.
19
+ */
20
+ export declare const ReportedBilling: Schema.Struct<{
21
+ readonly cost: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Finite>, never>>;
22
+ readonly opencode: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Struct<{
23
+ readonly allowance: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Finite>, never>>;
24
+ }>>, never>>;
25
+ }>;
26
+ /** Reads `ReportedBilling` from a raw usage object, which provider metadata keeps as the endpoint sent it. */
27
+ export declare const reportedBilling: (usage: unknown) => {} | {
28
+ cost: number | undefined;
29
+ allowance: number | undefined;
30
+ };
16
31
  /**
17
32
  * Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
18
33
  * keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
@@ -17,6 +17,21 @@ export const optionalNullish = (schema) => Schema.optional(Schema.NullOr(schema)
17
17
  })));
18
18
  /** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
19
19
  export const lenient = (schema) => Schema.optionalKey(Schema.UndefinedOr(schema).pipe(Schema.catchDecoding(() => Effect.succeed(Option.some(undefined)))));
20
+ /** A billed amount in USD; a malformed or negative one decodes as absent. */
21
+ const billedAmount = lenient(Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)));
22
+ /**
23
+ * Billing an endpoint reports beside its token usage. `cost` is the USD it charged (OpenRouter, LiteLLM, OpenCode
24
+ * Zen); OpenCode Go reports the subscription allowance a request used as `opencode.allowance`, also in USD.
25
+ */
26
+ export const ReportedBilling = Schema.Struct({
27
+ cost: billedAmount,
28
+ opencode: lenient(Schema.Struct({ allowance: billedAmount })),
29
+ });
30
+ /** Reads `ReportedBilling` from a raw usage object, which provider metadata keeps as the endpoint sent it. */
31
+ export const reportedBilling = (usage) => Option.match(Schema.decodeUnknownOption(ReportedBilling)(usage), {
32
+ onNone: () => ({}),
33
+ onSome: (billing) => ({ cost: billing.cost, allowance: billing.opencode?.allowance }),
34
+ });
20
35
  /**
21
36
  * Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
22
37
  * keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
@@ -15,6 +15,14 @@ declare const Usage_base: Schema.Class<Usage, Schema.Struct<{
15
15
  readonly cacheWriteInputTokens: Schema.optional<Schema.Number>;
16
16
  readonly reasoningTokens: Schema.optional<Schema.Number>;
17
17
  readonly totalTokens: Schema.optional<Schema.Number>;
18
+ /**
19
+ * USD the endpoint says it charged for this request, as OpenRouter, LiteLLM and OpenCode Zen report it in
20
+ * `usage.cost`. `0` is authoritative: the request was free or a subscription covered it. Absent, callers price the
21
+ * tokens themselves.
22
+ */
23
+ readonly cost: Schema.optional<Schema.Number>;
24
+ /** USD of subscription allowance the request used, as OpenCode Go reports it in `usage.opencode.allowance`. */
25
+ readonly allowance: Schema.optional<Schema.Number>;
18
26
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
19
27
  }>, {}>;
20
28
  /**
@@ -61,6 +61,14 @@ export class Usage extends Schema.Class("AI.Usage")({
61
61
  cacheWriteInputTokens: Schema.optional(Schema.Number),
62
62
  reasoningTokens: Schema.optional(Schema.Number),
63
63
  totalTokens: Schema.optional(Schema.Number),
64
+ /**
65
+ * USD the endpoint says it charged for this request, as OpenRouter, LiteLLM and OpenCode Zen report it in
66
+ * `usage.cost`. `0` is authoritative: the request was free or a subscription covered it. Absent, callers price the
67
+ * tokens themselves.
68
+ */
69
+ cost: Schema.optional(Schema.Number),
70
+ /** USD of subscription allowance the request used, as OpenCode Go reports it in `usage.opencode.allowance`. */
71
+ allowance: Schema.optional(Schema.Number),
64
72
  providerMetadata: Schema.optional(ProviderMetadata),
65
73
  }) {
66
74
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
- "version": "0.0.0-dev-21765",
3
+ "version": "0.0.0-dev-21766",
4
4
  "name": "@opencode/ai",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "devDependencies": {
36
36
  "@clack/prompts": "1.0.0-alpha.1",
37
37
  "@effect/platform-node": "4.0.1",
38
- "@opencode/http-recorder": "0.0.0-dev-21765",
38
+ "@opencode/http-recorder": "0.0.0-dev-21766",
39
39
  "@tsconfig/bun": "1.0.9",
40
40
  "@types/bun": "1.4.3",
41
41
  "@typescript/native": "npm:typescript@7.1.0-beta",
@@ -45,7 +45,7 @@
45
45
  "@aws-sdk/credential-providers": "3.1057.0",
46
46
  "@smithy/eventstream-codec": "4.2.14",
47
47
  "@smithy/util-utf8": "4.2.2",
48
- "@opencode/schema": "0.0.0-dev-21765",
48
+ "@opencode/schema": "0.0.0-dev-21766",
49
49
  "aws4fetch": "1.0.20",
50
50
  "effect": "4.0.1",
51
51
  "google-auth-library": "10.5.0"