@outputai/core 0.15.1-next.755b932.0 → 0.15.1-next.91027b1.0
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.
- package/dist/credentials/index.d.ts +1 -1
- package/dist/credentials/types.d.ts +4 -4
- package/dist/evals/index.d.ts +1 -1
- package/dist/evals/verify.d.ts +4 -3
- package/package.json +5 -5
- package/src/http/fetch/index.d.ts +3 -5
- package/src/http/index.d.ts +1 -0
- package/src/interface/index.d.ts +1 -0
- package/src/llm/index.d.ts +35 -22
- package/src/llm/utils/cost.js +138 -22
- package/src/llm/utils/metering.js +12 -7
- package/src/llm/utils/models_pricing_snapshot.json +168 -48
- package/src/llm/utils/usage.js +12 -12
- package/src/llm/utils/web_search.js +4 -4
- package/src/worker/loader/workflows.js +16 -2
|
@@ -3,4 +3,4 @@ export { setProvider } from './provider_registry.js';
|
|
|
3
3
|
export { encrypt, decrypt, generateKey } from './encryption.js';
|
|
4
4
|
export { InvalidCredentialsKeyError, MalformedCredentialsKeyError, MissingCredentialError, MissingKeyError } from './errors.js';
|
|
5
5
|
export { getNestedValue, resolveCredentialsPath, resolveKeyPath, resolveKeyEnvVar, resolveWorkflowCredentialsPath, resolveWorkflowKeyPath, resolveWorkflowKeyEnvVar } from './paths.js';
|
|
6
|
-
export type { CredentialsProvider } from './types.js';
|
|
6
|
+
export type { CredentialsGlobalContext, CredentialsProvider, CredentialsWorkflowContext } from './types.js';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface CredentialsGlobalContext {
|
|
2
2
|
environment: string | undefined;
|
|
3
3
|
}
|
|
4
|
-
export interface
|
|
4
|
+
export interface CredentialsWorkflowContext {
|
|
5
5
|
workflowName: string;
|
|
6
6
|
workflowDir: string | undefined;
|
|
7
7
|
environment?: string | undefined;
|
|
8
8
|
}
|
|
9
9
|
export interface CredentialsProvider {
|
|
10
|
-
loadGlobal(context:
|
|
11
|
-
loadForWorkflow(context:
|
|
10
|
+
loadGlobal(context: CredentialsGlobalContext): Record<string, unknown>;
|
|
11
|
+
loadForWorkflow(context: CredentialsWorkflowContext): Record<string, unknown> | null;
|
|
12
12
|
}
|
package/dist/evals/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export { evalWorkflow } from './eval_workflow.js';
|
|
|
2
2
|
export type { EvalDefinition, EvalWorkflowConfig, EvalWorkflowInput, EvalWorkflowFn, EvalWorkflowOptions } from './eval_workflow.js';
|
|
3
3
|
export { Verdict } from './verdict.js';
|
|
4
4
|
export { verify } from './verify.js';
|
|
5
|
-
export type { CheckContext } from './verify.js';
|
|
5
|
+
export type { CheckContext, CheckFn, VerifyOptions } from './verify.js';
|
|
6
6
|
export { VERDICT, CRITICALITY, EvalOutputSchema, DatasetSchema } from './schemas.js';
|
|
7
7
|
export type { Verdict as VerdictType, Criticality, InterpretConfig, EvaluatorResult, EvalCase, EvalSummary, EvalOutput, GroundTruth, LastOutput, LastEval, Dataset } from './schemas.js';
|
package/dist/evals/verify.d.ts
CHANGED
|
@@ -7,8 +7,10 @@ export type CheckContext<TInput = unknown, TOutput = unknown> = {
|
|
|
7
7
|
ground_truth: Record<string, unknown>;
|
|
8
8
|
};
|
|
9
9
|
};
|
|
10
|
-
|
|
11
|
-
type
|
|
10
|
+
/** Evaluation function accepted by {@link verify}. */
|
|
11
|
+
export type CheckFn<TInput = unknown, TOutput = unknown> = (ctx: CheckContext<TInput, TOutput>) => EvaluationResult | Promise<EvaluationResult>;
|
|
12
|
+
/** Options accepted by {@link verify}. */
|
|
13
|
+
export type VerifyOptions<I extends z.ZodType = z.ZodType<unknown>, O extends z.ZodType = z.ZodType<unknown>> = {
|
|
12
14
|
name: string;
|
|
13
15
|
input?: I;
|
|
14
16
|
output?: O;
|
|
@@ -38,4 +40,3 @@ export declare const verify: <I extends z.ZodType, O extends z.ZodType>(options:
|
|
|
38
40
|
optin: "optional" | "defaulted";
|
|
39
41
|
};
|
|
40
42
|
} ? k_1 : never]?: T_2[k_1]["_zod"]["input"] | undefined; } : never) extends infer T ? { [K in keyof T]: T[K]; } : never) => Promise<EvaluationResult>;
|
|
41
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@outputai/core",
|
|
3
|
-
"version": "0.15.1-next.
|
|
3
|
+
"version": "0.15.1-next.91027b1.0",
|
|
4
4
|
"description": "The core module of the output framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -76,9 +76,9 @@
|
|
|
76
76
|
"winston": "3.19.0"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
|
-
"zod": ">=4.
|
|
79
|
+
"zod": ">=4.6.6 <5",
|
|
80
80
|
"ky": ">=2 <3",
|
|
81
|
-
"undici": ">=8 <9",
|
|
81
|
+
"undici": ">=8.12.2 <9",
|
|
82
82
|
"ai": ">=7 <8",
|
|
83
83
|
"@ai-sdk/amazon-bedrock": ">=5 <6",
|
|
84
84
|
"@ai-sdk/anthropic": ">=4 <5",
|
|
@@ -109,9 +109,9 @@
|
|
|
109
109
|
},
|
|
110
110
|
"devDependencies": {
|
|
111
111
|
"@types/js-yaml": "4.0.9",
|
|
112
|
-
"zod": "4.6.
|
|
112
|
+
"zod": "4.6.6",
|
|
113
113
|
"ky": "2.0.2",
|
|
114
|
-
"undici": "8.
|
|
114
|
+
"undici": "8.12.3",
|
|
115
115
|
"ai": "7.0.112",
|
|
116
116
|
"@ai-sdk/amazon-bedrock": "5.0.92",
|
|
117
117
|
"@ai-sdk/anthropic": "4.0.61",
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type * as undici from 'undici';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
type OutputFetch = {
|
|
7
|
-
( input: NodeRequestInfo, init?: NodeRequestInit ): Promise<Response>;
|
|
3
|
+
/** Fetch function supporting Node and Undici request types. */
|
|
4
|
+
export type OutputFetch = {
|
|
5
|
+
( input: string | URL | globalThis.Request, init?: globalThis.RequestInit & Pick<undici.RequestInit, 'dispatcher'> ): Promise<Response>;
|
|
8
6
|
( input: undici.RequestInfo, init?: undici.RequestInit ): Promise<Response>;
|
|
9
7
|
};
|
|
10
8
|
|
package/src/http/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export type HttpRequestCostEvent = {
|
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
export { outputFetch } from './fetch/index.js';
|
|
20
|
+
export type { OutputFetch } from './fetch/index.js';
|
|
20
21
|
export { HTTPRequestCount } from './fetch/logger.js';
|
|
21
22
|
export { createKyClient } from './ky/index.js';
|
|
22
23
|
|
package/src/interface/index.d.ts
CHANGED
package/src/llm/index.d.ts
CHANGED
|
@@ -193,12 +193,8 @@ export type Prompt = {
|
|
|
193
193
|
instructions: string | null;
|
|
194
194
|
};
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
type
|
|
198
|
-
type CompatibleToolSchemaKey = 'inputSchema' | 'outputSchema' | 'contextSchema';
|
|
199
|
-
type WithoutToolSchemaBrands<T> = T extends unknown ? {
|
|
200
|
-
[K in keyof T]: K extends CompatibleToolSchemaKey ? unknown : T[K];
|
|
201
|
-
} : never;
|
|
196
|
+
/** Any AI SDK structured output specification. */
|
|
197
|
+
export type AnyAiOutput = AIOutputNamespace.Output<unknown, unknown, unknown>;
|
|
202
198
|
|
|
203
199
|
/**
|
|
204
200
|
* Structurally-compatible AI SDK 7 tool shape.
|
|
@@ -206,12 +202,17 @@ type WithoutToolSchemaBrands<T> = T extends unknown ? {
|
|
|
206
202
|
* This derives from AI SDK's `ToolSet` and widens only schema fields so tools from packages
|
|
207
203
|
* resolved with a different Zod peer instance remain assignable.
|
|
208
204
|
*/
|
|
209
|
-
export type CompatibleTool =
|
|
205
|
+
export type CompatibleTool = ToolSet[string] extends infer Tool ?
|
|
206
|
+
Tool extends unknown ? {
|
|
207
|
+
[K in keyof Tool]: K extends 'inputSchema' | 'outputSchema' | 'contextSchema' ? unknown : Tool[K];
|
|
208
|
+
} : never :
|
|
209
|
+
never;
|
|
210
210
|
|
|
211
211
|
/** AI SDK tools accepted by Output APIs without requiring one exact Zod peer instance. */
|
|
212
212
|
export type CompatibleToolSet = Record<string, CompatibleTool>;
|
|
213
213
|
|
|
214
|
-
|
|
214
|
+
/** Options for an LLM call using a prompt file. */
|
|
215
|
+
export type PromptFileCallOptions = {
|
|
215
216
|
/** Prompt file name (e.g. 'summary@v1') */
|
|
216
217
|
prompt: string;
|
|
217
218
|
/** Variables to interpolate into the prompt file */
|
|
@@ -220,7 +221,8 @@ type PromptFileCallOptions = {
|
|
|
220
221
|
promptDir?: string;
|
|
221
222
|
};
|
|
222
223
|
|
|
223
|
-
|
|
224
|
+
/** Options for an LLM call using a loaded or custom prompt. */
|
|
225
|
+
export type PromptObjectCallOptions = {
|
|
224
226
|
/** Loaded or custom prompt object. */
|
|
225
227
|
prompt: Prompt;
|
|
226
228
|
/** Prompt objects are already rendered and cannot receive interpolation variables. */
|
|
@@ -229,13 +231,16 @@ type PromptObjectCallOptions = {
|
|
|
229
231
|
promptDir?: never;
|
|
230
232
|
};
|
|
231
233
|
|
|
232
|
-
|
|
234
|
+
/** Prompt file or loaded prompt options accepted by LLM calls. */
|
|
235
|
+
export type PromptCallOptions = PromptFileCallOptions | PromptObjectCallOptions;
|
|
233
236
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
+
/** Stop conditions accepted by text generation calls. */
|
|
238
|
+
export type StopWhen<Tools extends ToolSet = ToolSet> =
|
|
239
|
+
StopCondition<NoInfer<Tools>, Record<string, unknown>> |
|
|
240
|
+
Array<StopCondition<NoInfer<Tools>, Record<string, unknown>>>;
|
|
237
241
|
|
|
238
|
-
|
|
242
|
+
/** Shared options accepted by text generation calls. */
|
|
243
|
+
export type TextCallOptions<
|
|
239
244
|
Tools extends ToolSet = ToolSet,
|
|
240
245
|
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
241
246
|
> = PromptCallOptions & {
|
|
@@ -290,7 +295,8 @@ export type GenerateImageInput =
|
|
|
290
295
|
mediaType?: string;
|
|
291
296
|
};
|
|
292
297
|
|
|
293
|
-
|
|
298
|
+
/** Image and mask inputs accepted by image generation calls. */
|
|
299
|
+
export type GenerateImageInputs =
|
|
294
300
|
| {
|
|
295
301
|
/** Runtime image inputs for image-to-image generation */
|
|
296
302
|
images: GenerateImageInput[];
|
|
@@ -346,7 +352,7 @@ export type OutputAgentStreamParameters = OutputAgentGenerateWithStreamingParame
|
|
|
346
352
|
* `ai` does not export a named `Source` type.
|
|
347
353
|
*/
|
|
348
354
|
export type ExtractedSource =
|
|
349
|
-
AIGenerateTextResult<ToolSet,
|
|
355
|
+
AIGenerateTextResult<ToolSet, Record<string, unknown>, AnyAiOutput>['sources'][number];
|
|
350
356
|
|
|
351
357
|
export type LLMGenerationUsageStatus = 'complete' | 'incomplete';
|
|
352
358
|
|
|
@@ -390,11 +396,17 @@ export interface LLMUsageEvent {
|
|
|
390
396
|
readonly tokensUsed: number;
|
|
391
397
|
}
|
|
392
398
|
|
|
393
|
-
export type LLMGenerationCostItemStatus = 'ok' | 'fallback' | 'missing';
|
|
399
|
+
export type LLMGenerationCostItemStatus = 'ok' | 'fallback' | 'uncertain' | 'missing';
|
|
394
400
|
export type LLMGenerationCostStatus = 'precise' | 'imprecise' | 'incomplete';
|
|
395
401
|
export type LLMGenerationCostPricingFreshness = 'live' | 'cached' | 'stale' | 'snapshot';
|
|
396
402
|
|
|
397
|
-
/**
|
|
403
|
+
/** Context-based pricing tier applied to a normalized cost item. */
|
|
404
|
+
export interface LLMGenerationCostTier {
|
|
405
|
+
type: 'context';
|
|
406
|
+
size: number;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Cost calculated for one normalized LLM usage pricing segment. */
|
|
398
410
|
export interface LLMGenerationCostItem {
|
|
399
411
|
group: 'input' | 'output' | 'tools';
|
|
400
412
|
label: string | null;
|
|
@@ -402,6 +414,7 @@ export interface LLMGenerationCostItem {
|
|
|
402
414
|
ppm: number | null;
|
|
403
415
|
total: number | null;
|
|
404
416
|
status: LLMGenerationCostItemStatus;
|
|
417
|
+
tier?: LLMGenerationCostTier;
|
|
405
418
|
}
|
|
406
419
|
|
|
407
420
|
/** Normalized LLM generation cost trace attribute. */
|
|
@@ -430,7 +443,7 @@ export interface LLMGenerationMeteringEvent {
|
|
|
430
443
|
* `onEnd` payload plus `result`, `cost`, and merged `sources`.
|
|
431
444
|
*/
|
|
432
445
|
export type WrappedStreamTextOnEndEvent<Tools extends ToolSet = ToolSet> =
|
|
433
|
-
Parameters<GenerateTextOnEndCallback<Tools,
|
|
446
|
+
Parameters<GenerateTextOnEndCallback<Tools, Record<string, unknown>>>[0] & {
|
|
434
447
|
result: string;
|
|
435
448
|
cost: LLMGenerationCost | null;
|
|
436
449
|
sources: ExtractedSource[];
|
|
@@ -448,7 +461,7 @@ export type WrappedStreamTextOnEndCallback<Tools extends ToolSet = ToolSet> = (
|
|
|
448
461
|
export type GenerateTextResult<
|
|
449
462
|
Tools extends ToolSet = ToolSet,
|
|
450
463
|
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
451
|
-
> = AIGenerateTextResult<Tools,
|
|
464
|
+
> = AIGenerateTextResult<Tools, Record<string, unknown>, OutputSpec> & {
|
|
452
465
|
/** Unified field name alias for 'text' */
|
|
453
466
|
result: string;
|
|
454
467
|
/** Calculated cost for the LLM call; `null` when pricing data is not available */
|
|
@@ -610,7 +623,7 @@ export function streamText<
|
|
|
610
623
|
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
611
624
|
>(
|
|
612
625
|
args: StreamTextParameters<Tools, OutputSpec>
|
|
613
|
-
): AIStreamTextResult<Tools,
|
|
626
|
+
): AIStreamTextResult<Tools, Record<string, unknown>, OutputSpec>;
|
|
614
627
|
|
|
615
628
|
/**
|
|
616
629
|
* Use an image model to generate images from a prompt filename or object.
|
|
@@ -679,6 +692,6 @@ export declare class Agent<
|
|
|
679
692
|
* `onEnd` receives {@link WrappedStreamTextOnEndEvent} (`result`, `cost`, `sources`), matching {@link streamText}.
|
|
680
693
|
*/
|
|
681
694
|
stream( options?: OutputAgentStreamParameters ): Promise<
|
|
682
|
-
AIStreamTextResult<ToolSet,
|
|
695
|
+
AIStreamTextResult<ToolSet, Record<string, unknown>, OutputSpec>
|
|
683
696
|
>;
|
|
684
697
|
}
|
package/src/llm/utils/cost.js
CHANGED
|
@@ -5,12 +5,14 @@ import { resolveWebSearchPpm } from './web_search.js';
|
|
|
5
5
|
import { Logger } from '#runtime_logger';
|
|
6
6
|
import Decimal from 'decimal.js';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// Checks whether a pricing value can be used in cost calculations.
|
|
9
|
+
const isValidNumber = v => Number.isFinite( v ) && v >= 0;
|
|
9
10
|
|
|
10
11
|
export class LLMGenerationCostItem {
|
|
11
12
|
static Status = Object.freeze( {
|
|
12
13
|
OK: 'ok',
|
|
13
14
|
FALLBACK: 'fallback',
|
|
15
|
+
UNCERTAIN: 'uncertain',
|
|
14
16
|
MISSING: 'missing'
|
|
15
17
|
} );
|
|
16
18
|
|
|
@@ -21,13 +23,20 @@ export class LLMGenerationCostItem {
|
|
|
21
23
|
total;
|
|
22
24
|
status;
|
|
23
25
|
|
|
24
|
-
constructor( group, label, amount, ppm, total, status ) {
|
|
26
|
+
constructor( group, label, amount, ppm, total, status, tier = undefined ) {
|
|
25
27
|
this.group = group;
|
|
26
28
|
this.label = label;
|
|
27
29
|
this.amount = amount;
|
|
28
30
|
this.ppm = ppm;
|
|
29
31
|
this.total = total;
|
|
30
32
|
this.status = status;
|
|
33
|
+
if ( tier ) {
|
|
34
|
+
this.tier = { type: tier.type, size: tier.size };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get key() {
|
|
39
|
+
return JSON.stringify( [ this.group, this.label, this.ppm, this.status, this.tier?.type, this.tier?.size ] );
|
|
31
40
|
}
|
|
32
41
|
}
|
|
33
42
|
|
|
@@ -52,47 +61,67 @@ export class LLMGenerationCost extends BaseAttribute {
|
|
|
52
61
|
super( LLMGenerationCost.TYPE );
|
|
53
62
|
this.modelId = modelId;
|
|
54
63
|
this.providerId = providerId;
|
|
55
|
-
|
|
64
|
+
|
|
65
|
+
// group items with the same key, summing amount
|
|
66
|
+
this.items = Map.groupBy( items, e => e.key )
|
|
67
|
+
.values()
|
|
68
|
+
.toArray()
|
|
69
|
+
.map( items => {
|
|
70
|
+
if ( items.length === 1 ) {
|
|
71
|
+
return items[0];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { group, label, ppm, status, tier } = items[0];
|
|
75
|
+
const amount = items.reduce( ( sum, item ) => sum + item.amount, 0 );
|
|
76
|
+
const total = isValidNumber( ppm ) ? Decimal( amount ).div( 1_000_000 ).mul( ppm ).toNumber() : null;
|
|
77
|
+
return new LLMGenerationCostItem( group, label, amount, ppm, total, status, tier );
|
|
78
|
+
} );
|
|
79
|
+
|
|
56
80
|
this.pricingFreshness = pricingFreshness;
|
|
57
81
|
|
|
58
|
-
const meaningfulItems = items.filter( v => v.amount > 0 );
|
|
82
|
+
const meaningfulItems = this.items.filter( v => v.amount > 0 );
|
|
83
|
+
const hasMissingItems = meaningfulItems.some( v => v.status === LLMGenerationCostItem.Status.MISSING );
|
|
84
|
+
const hasFallbackItems = meaningfulItems.some( v => v.status === LLMGenerationCostItem.Status.FALLBACK );
|
|
85
|
+
const hasUncertainItems = meaningfulItems.some( v => v.status === LLMGenerationCostItem.Status.UNCERTAIN );
|
|
59
86
|
|
|
60
|
-
if (
|
|
87
|
+
if ( hasMissingItems || usageStatus === LLMGenerationUsage.Status.INCOMPLETE ) {
|
|
61
88
|
this.status = LLMGenerationCost.Status.INCOMPLETE;
|
|
62
|
-
} else if (
|
|
89
|
+
} else if ( hasFallbackItems || pricingFreshness === Freshness.SNAPSHOT || hasUncertainItems ) {
|
|
63
90
|
this.status = LLMGenerationCost.Status.IMPRECISE;
|
|
64
91
|
} else {
|
|
65
92
|
this.status = LLMGenerationCost.Status.PRECISE;
|
|
66
93
|
}
|
|
94
|
+
|
|
67
95
|
const sumGroup = group => {
|
|
68
|
-
const groupItems = items.filter( p => p.group === group &&
|
|
96
|
+
const groupItems = this.items.filter( p => p.group === group && isValidNumber( p.total ) );
|
|
69
97
|
return groupItems.length > 0 ? groupItems.reduce( ( s, p ) => s.add( p.total ), Decimal( 0 ) ).toNumber() : null;
|
|
70
98
|
};
|
|
71
99
|
this.input = sumGroup( LLMGenerationUsageItem.Group.INPUT );
|
|
72
100
|
this.output = sumGroup( LLMGenerationUsageItem.Group.OUTPUT );
|
|
73
101
|
this.tools = sumGroup( LLMGenerationUsageItem.Group.TOOLS );
|
|
74
|
-
if (
|
|
102
|
+
if ( isValidNumber( this.input ) || isValidNumber( this.output ) || isValidNumber( this.tools ) ) {
|
|
75
103
|
this.total = Decimal( this.input ?? 0 ).add( this.output ?? 0 ).add( this.tools ?? 0 ).toNumber();
|
|
76
104
|
}
|
|
77
105
|
}
|
|
78
106
|
}
|
|
79
107
|
|
|
108
|
+
// Selects the exact rate, then the first available fallback rate.
|
|
80
109
|
const resolveValue = values => {
|
|
81
|
-
if (
|
|
110
|
+
if ( isValidNumber( values[0] ) ) {
|
|
82
111
|
return { ppm: values[0], status: LLMGenerationCostItem.Status.OK };
|
|
83
112
|
}
|
|
84
|
-
const fallback = values.slice( 1 ).find(
|
|
85
|
-
if (
|
|
113
|
+
const fallback = values.slice( 1 ).find( isValidNumber );
|
|
114
|
+
if ( isValidNumber( fallback ) ) {
|
|
86
115
|
return { ppm: fallback, status: LLMGenerationCostItem.Status.FALLBACK };
|
|
87
116
|
}
|
|
88
117
|
return { ppm: null, status: LLMGenerationCostItem.Status.MISSING };
|
|
89
118
|
};
|
|
90
119
|
|
|
91
|
-
|
|
92
|
-
|
|
120
|
+
// Resolves the applicable token or provider-tool rate for one usage item.
|
|
121
|
+
const resolvePrice = ( { providerId, group, label, pricing, isReasoning } ) => {
|
|
93
122
|
// Tool charges are never token-priced: models.dev has no rate for them.
|
|
94
123
|
if ( group === LLMGenerationUsageItem.Group.TOOLS ) {
|
|
95
|
-
return resolveValue( [ resolveWebSearchPpm( providerId, label,
|
|
124
|
+
return resolveValue( [ resolveWebSearchPpm( providerId, label, isReasoning ) ] );
|
|
96
125
|
}
|
|
97
126
|
if ( !pricing ) {
|
|
98
127
|
return resolveValue( [] );
|
|
@@ -114,13 +143,86 @@ const resolvePrice = ( { providerId, group, label, modelPricing } ) => {
|
|
|
114
143
|
return resolveValue( values );
|
|
115
144
|
};
|
|
116
145
|
|
|
146
|
+
// Finds the highest context threshold crossed by this request's input.
|
|
147
|
+
const resolvePricingTier = ( modelPricing, input ) => {
|
|
148
|
+
// DESC sort by size
|
|
149
|
+
const supportedTiers = modelPricing.cost.tiers.filter( t => t?.tier?.type === 'context' && isValidNumber( t.tier.size ) );
|
|
150
|
+
const hasUnsupported = supportedTiers.length !== modelPricing.cost.tiers.length;
|
|
151
|
+
|
|
152
|
+
const sortedTiers = supportedTiers.toSorted( ( t1, t2 ) => t2.tier.size - t1.tier.size );
|
|
153
|
+
const tier = sortedTiers.find( tier => input > tier.tier.size );
|
|
154
|
+
|
|
155
|
+
return { tier, hasUnsupported };
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Calculates the price of each usage item
|
|
159
|
+
const calculateCostItems = ( { usage, pricing, isReasoning, uncertain } ) => {
|
|
160
|
+
const { providerId } = usage;
|
|
161
|
+
return usage.items.map( ( { group, label, amount } ) => {
|
|
162
|
+
const { ppm, status } = resolvePrice( { providerId, isReasoning, group, label, pricing } );
|
|
163
|
+
const total = isValidNumber( ppm ) ? Decimal( amount ).div( 1_000_000 ).mul( ppm ).toNumber() : null;
|
|
164
|
+
|
|
165
|
+
const isTool = group === LLMGenerationUsageItem.Group.TOOLS;
|
|
166
|
+
const isCostOk = status === LLMGenerationCostItem.Status.OK;
|
|
167
|
+
|
|
168
|
+
const finalStatus = !isTool && isCostOk && uncertain ? LLMGenerationCostItem.Status.UNCERTAIN : status;
|
|
169
|
+
|
|
170
|
+
const tier = !isTool && pricing?.tier?.type ? pricing.tier : null;
|
|
171
|
+
return new LLMGenerationCostItem( group, label, amount, ppm, total, finalStatus, tier );
|
|
172
|
+
} );
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// Prices the total usage
|
|
176
|
+
const calculateStandardUsage = ( usage, modelPricing ) => {
|
|
177
|
+
const { reasoning: isReasoning, cost: pricing } = modelPricing ?? {};
|
|
178
|
+
return calculateCostItems( { usage, pricing, isReasoning } );
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/* Prices the tiered usage:
|
|
182
|
+
- Multi-step + any incomplete step: base pricing, uncertain.
|
|
183
|
+
- Known request + no tier + no unsupported tiers: base, precise.
|
|
184
|
+
- Known request + no tier + unsupported tiers: base, uncertain.
|
|
185
|
+
- Known request + tier + no unsupported tiers: tier, precise.
|
|
186
|
+
- Known request + tier + unsupported tiers: tier, uncertain.
|
|
187
|
+
*/
|
|
188
|
+
const calculateTieredUsage = ( totalUsage, stepsUsage, modelPricing ) => {
|
|
189
|
+
const { cost: pricing, reasoning: isReasoning } = modelPricing;
|
|
190
|
+
|
|
191
|
+
const isMultiStep = stepsUsage.length > 1;
|
|
192
|
+
const hasIncompleteSteps = stepsUsage.some( u => u?.status !== LLMGenerationUsage.Status.COMPLETE );
|
|
193
|
+
|
|
194
|
+
// incomplete multi-steps fallback to calculate total usage with NO tiers, since token counts are unreliable
|
|
195
|
+
if ( isMultiStep && hasIncompleteSteps ) {
|
|
196
|
+
return calculateCostItems( { usage: totalUsage, pricing, isReasoning, uncertain: true } );
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return ( isMultiStep ? stepsUsage : [ totalUsage ] ).flatMap( usage => {
|
|
200
|
+
// output usage can't have tier billing
|
|
201
|
+
if ( !isValidNumber( usage.input ) ) {
|
|
202
|
+
return calculateCostItems( { usage, pricing, isReasoning } );
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const pricingTier = resolvePricingTier( modelPricing, usage.input );
|
|
206
|
+
|
|
207
|
+
// if there is a matching tier used, otherwise use base pricing
|
|
208
|
+
const finalPricing = pricingTier.tier ?? pricing;
|
|
209
|
+
|
|
210
|
+
// it will be uncertain if unsupported tiers exists
|
|
211
|
+
return calculateCostItems( { usage, pricing: finalPricing, isReasoning, uncertain: pricingTier.hasUnsupported } );
|
|
212
|
+
} );
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// Returns whether model pricing has tier info or not
|
|
216
|
+
const hasTiers = modelPricing => Array.isArray( modelPricing?.cost?.tiers ) && modelPricing.cost.tiers.length > 0;
|
|
217
|
+
|
|
117
218
|
/**
|
|
118
219
|
* Calculates the cost of an LLM call based on the LLMGenerationUsage.
|
|
119
220
|
*
|
|
120
221
|
* @param {LLMGenerationUsage} usage - Normalized LLM generation usage
|
|
222
|
+
* @param {LLMGenerationUsage[]} stepsUsage - Per-request usage for multi-step calls
|
|
121
223
|
* @returns {Promise<LLMGenerationCost | null>} LLM generation cost with input, output, total and breakdown
|
|
122
224
|
*/
|
|
123
|
-
export const calculateCosts = async usage => {
|
|
225
|
+
export const calculateCosts = async ( usage, stepsUsage ) => {
|
|
124
226
|
const { models, freshness: pricingFreshness } = await fetchModelsPricing();
|
|
125
227
|
|
|
126
228
|
if ( !models ) {
|
|
@@ -135,17 +237,31 @@ export const calculateCosts = async usage => {
|
|
|
135
237
|
Logger.warn( 'Missing pricing reference for model', { namespace: 'LLM', modelId, providerId } );
|
|
136
238
|
}
|
|
137
239
|
|
|
138
|
-
const items =
|
|
139
|
-
const { ppm, status } = resolvePrice( { providerId, modelPricing, group, label } );
|
|
140
|
-
const total = exists( ppm ) ? Decimal( amount ).div( 1_000_000 ).mul( ppm ).toNumber() : null;
|
|
141
|
-
return new LLMGenerationCostItem( group, label, amount, ppm, total, status );
|
|
142
|
-
} );
|
|
240
|
+
const items = hasTiers( modelPricing ) ? calculateTieredUsage( usage, stepsUsage, modelPricing ) : calculateStandardUsage( usage, modelPricing );
|
|
143
241
|
|
|
144
|
-
const unrated = items.find( v =>
|
|
145
|
-
v.group === LLMGenerationUsageItem.Group.TOOLS && v.status === LLMGenerationCostItem.Status.MISSING );
|
|
242
|
+
const unrated = items.find( v => v.group === LLMGenerationUsageItem.Group.TOOLS && v.status === LLMGenerationCostItem.Status.MISSING );
|
|
146
243
|
if ( unrated ) {
|
|
147
244
|
Logger.warn( 'Tool call with no rate for model', { namespace: 'LLM', modelId, providerId, type: unrated.label } );
|
|
148
245
|
}
|
|
149
246
|
|
|
150
247
|
return new LLMGenerationCost( modelId, providerId, items, usage.status, pricingFreshness );
|
|
151
248
|
};
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Calculates a cost using the legacy aggregate pricing behavior.
|
|
252
|
+
*
|
|
253
|
+
* @param {LLMGenerationUsage} usage - Normalized aggregate LLM generation usage
|
|
254
|
+
* @returns {Promise<LLMGenerationCost | null>} Cost compatible with the legacy conversion
|
|
255
|
+
*/
|
|
256
|
+
export const calculateLegacyCosts = async usage => {
|
|
257
|
+
const { models, freshness: pricingFreshness } = await fetchModelsPricing();
|
|
258
|
+
if ( !models ) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const { providerId, modelId } = usage;
|
|
263
|
+
const modelPricing = models.get( `${providerId}/${modelId}` );
|
|
264
|
+
const items = calculateStandardUsage( usage, modelPricing );
|
|
265
|
+
|
|
266
|
+
return new LLMGenerationCost( modelId, providerId, items, usage.status, pricingFreshness );
|
|
267
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseLLMUsage } from './usage.js';
|
|
2
|
-
import { calculateCosts } from './cost.js';
|
|
2
|
+
import { calculateCosts, calculateLegacyCosts } from './cost.js';
|
|
3
3
|
import { stepEventBus } from '#bus';
|
|
4
4
|
import { addEventActionWithContext, EventAction } from '#tracing';
|
|
5
5
|
import { Logger } from '#runtime_logger';
|
|
@@ -26,17 +26,22 @@ export class Metering {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
async #createAttributes( { usage, steps } ) {
|
|
29
|
-
|
|
29
|
+
const totalUsage = parseLLMUsage( { prompt: this.#prompt, usage, steps, toolIdsByName: this.#toolIdsByName } );
|
|
30
|
+
const stepsUsage = steps.map( step => parseLLMUsage( {
|
|
30
31
|
prompt: this.#prompt,
|
|
31
|
-
usage,
|
|
32
|
-
steps,
|
|
32
|
+
usage: step.usage,
|
|
33
|
+
steps: [ step ],
|
|
33
34
|
toolIdsByName: this.#toolIdsByName
|
|
34
|
-
} );
|
|
35
|
+
} ) );
|
|
36
|
+
|
|
37
|
+
this.#attributes.usage = totalUsage;
|
|
35
38
|
if ( this.#attributes.usage ) {
|
|
36
|
-
this.#attributes.cost = await calculateCosts( this.#attributes.usage );
|
|
39
|
+
this.#attributes.cost = await calculateCosts( this.#attributes.usage, stepsUsage );
|
|
40
|
+
|
|
37
41
|
if ( this.#attributes.cost ) {
|
|
38
42
|
// @TEMP Preserve the deprecated event and trace attribute for legacy consumers.
|
|
39
|
-
|
|
43
|
+
const legacyCost = await calculateLegacyCosts( this.#attributes.usage );
|
|
44
|
+
this.#attributes.legacy = convertCostToLegacy( legacyCost );
|
|
40
45
|
}
|
|
41
46
|
}
|
|
42
47
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"_meta": {
|
|
3
|
-
"createdAt": "2026-10-
|
|
3
|
+
"createdAt": "2026-10-30T10:59:23.371Z"
|
|
4
4
|
},
|
|
5
5
|
"amazon-bedrock": {
|
|
6
6
|
"models": {
|
|
@@ -325,6 +325,15 @@
|
|
|
325
325
|
},
|
|
326
326
|
"reasoning": true
|
|
327
327
|
},
|
|
328
|
+
"au.anthropic.claude-sonnet-5-5": {
|
|
329
|
+
"cost": {
|
|
330
|
+
"input": 2.2,
|
|
331
|
+
"output": 11,
|
|
332
|
+
"cache_read": 0.11,
|
|
333
|
+
"cache_write": 2.75
|
|
334
|
+
},
|
|
335
|
+
"reasoning": true
|
|
336
|
+
},
|
|
328
337
|
"ca.amazon.nova-lite-v1:0": {
|
|
329
338
|
"cost": {
|
|
330
339
|
"input": 0.064,
|
|
@@ -890,6 +899,33 @@
|
|
|
890
899
|
},
|
|
891
900
|
"reasoning": true
|
|
892
901
|
},
|
|
902
|
+
"global.openai.gpt-6.1-terra": {
|
|
903
|
+
"cost": {
|
|
904
|
+
"input": 1,
|
|
905
|
+
"output": 5,
|
|
906
|
+
"cache_read": 0.05,
|
|
907
|
+
"cache_write": 1.25,
|
|
908
|
+
"tiers": [
|
|
909
|
+
{
|
|
910
|
+
"input": 2,
|
|
911
|
+
"output": 7.5,
|
|
912
|
+
"cache_read": 0.1,
|
|
913
|
+
"cache_write": 2.5,
|
|
914
|
+
"tier": {
|
|
915
|
+
"type": "context",
|
|
916
|
+
"size": 272000
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
],
|
|
920
|
+
"context_over_200k": {
|
|
921
|
+
"input": 2,
|
|
922
|
+
"output": 7.5,
|
|
923
|
+
"cache_read": 0.1,
|
|
924
|
+
"cache_write": 2.5
|
|
925
|
+
}
|
|
926
|
+
},
|
|
927
|
+
"reasoning": true
|
|
928
|
+
},
|
|
893
929
|
"global.xai.grok-4.6": {
|
|
894
930
|
"cost": {
|
|
895
931
|
"input": 2,
|
|
@@ -1506,6 +1542,33 @@
|
|
|
1506
1542
|
},
|
|
1507
1543
|
"reasoning": true
|
|
1508
1544
|
},
|
|
1545
|
+
"openai.gpt-6.1-terra": {
|
|
1546
|
+
"cost": {
|
|
1547
|
+
"input": 1.1,
|
|
1548
|
+
"output": 5.5,
|
|
1549
|
+
"cache_read": 0.055,
|
|
1550
|
+
"cache_write": 1.375,
|
|
1551
|
+
"tiers": [
|
|
1552
|
+
{
|
|
1553
|
+
"input": 2.2,
|
|
1554
|
+
"output": 8.25,
|
|
1555
|
+
"cache_read": 0.11,
|
|
1556
|
+
"cache_write": 2.75,
|
|
1557
|
+
"tier": {
|
|
1558
|
+
"type": "context",
|
|
1559
|
+
"size": 272000
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
],
|
|
1563
|
+
"context_over_200k": {
|
|
1564
|
+
"input": 2.2,
|
|
1565
|
+
"output": 8.25,
|
|
1566
|
+
"cache_read": 0.11,
|
|
1567
|
+
"cache_write": 2.75
|
|
1568
|
+
}
|
|
1569
|
+
},
|
|
1570
|
+
"reasoning": true
|
|
1571
|
+
},
|
|
1509
1572
|
"openai.gpt-oss-120b": {
|
|
1510
1573
|
"cost": {
|
|
1511
1574
|
"input": 0.15,
|
|
@@ -2059,6 +2122,33 @@
|
|
|
2059
2122
|
},
|
|
2060
2123
|
"reasoning": true
|
|
2061
2124
|
},
|
|
2125
|
+
"us.openai.gpt-6.1-terra": {
|
|
2126
|
+
"cost": {
|
|
2127
|
+
"input": 1.1,
|
|
2128
|
+
"output": 5.5,
|
|
2129
|
+
"cache_read": 0.055,
|
|
2130
|
+
"cache_write": 1.375,
|
|
2131
|
+
"tiers": [
|
|
2132
|
+
{
|
|
2133
|
+
"input": 2.2,
|
|
2134
|
+
"output": 8.25,
|
|
2135
|
+
"cache_read": 0.11,
|
|
2136
|
+
"cache_write": 2.75,
|
|
2137
|
+
"tier": {
|
|
2138
|
+
"type": "context",
|
|
2139
|
+
"size": 272000
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
],
|
|
2143
|
+
"context_over_200k": {
|
|
2144
|
+
"input": 2.2,
|
|
2145
|
+
"output": 8.25,
|
|
2146
|
+
"cache_read": 0.11,
|
|
2147
|
+
"cache_write": 2.75
|
|
2148
|
+
}
|
|
2149
|
+
},
|
|
2150
|
+
"reasoning": true
|
|
2151
|
+
},
|
|
2062
2152
|
"us.writer.palmyra-x4-v1:0": {
|
|
2063
2153
|
"cost": {
|
|
2064
2154
|
"input": 2.5,
|
|
@@ -2379,15 +2469,6 @@
|
|
|
2379
2469
|
},
|
|
2380
2470
|
"reasoning": true
|
|
2381
2471
|
},
|
|
2382
|
-
"claude-opus-4-1": {
|
|
2383
|
-
"cost": {
|
|
2384
|
-
"input": 15,
|
|
2385
|
-
"output": 75,
|
|
2386
|
-
"cache_read": 1.5,
|
|
2387
|
-
"cache_write": 18.75
|
|
2388
|
-
},
|
|
2389
|
-
"reasoning": true
|
|
2390
|
-
},
|
|
2391
2472
|
"claude-opus-4-5": {
|
|
2392
2473
|
"cost": {
|
|
2393
2474
|
"input": 5,
|
|
@@ -2557,13 +2638,6 @@
|
|
|
2557
2638
|
},
|
|
2558
2639
|
"reasoning": false
|
|
2559
2640
|
},
|
|
2560
|
-
"deepseek-r1": {
|
|
2561
|
-
"cost": {
|
|
2562
|
-
"input": 1.35,
|
|
2563
|
-
"output": 5.4
|
|
2564
|
-
},
|
|
2565
|
-
"reasoning": true
|
|
2566
|
-
},
|
|
2567
2641
|
"deepseek-v3.2": {
|
|
2568
2642
|
"cost": {
|
|
2569
2643
|
"input": 0.58,
|
|
@@ -2592,6 +2666,14 @@
|
|
|
2592
2666
|
},
|
|
2593
2667
|
"reasoning": true
|
|
2594
2668
|
},
|
|
2669
|
+
"deepseek-v4.1-flash": {
|
|
2670
|
+
"cost": {
|
|
2671
|
+
"input": 0.3,
|
|
2672
|
+
"output": 1.2,
|
|
2673
|
+
"cache_read": 0.006
|
|
2674
|
+
},
|
|
2675
|
+
"reasoning": true
|
|
2676
|
+
},
|
|
2595
2677
|
"gpt-3.5-turbo-0125": {
|
|
2596
2678
|
"cost": {
|
|
2597
2679
|
"input": 0.5,
|
|
@@ -2671,7 +2753,7 @@
|
|
|
2671
2753
|
"cost": {
|
|
2672
2754
|
"input": 1.25,
|
|
2673
2755
|
"output": 10,
|
|
2674
|
-
"cache_read": 0.
|
|
2756
|
+
"cache_read": 0.125
|
|
2675
2757
|
},
|
|
2676
2758
|
"reasoning": true
|
|
2677
2759
|
},
|
|
@@ -2679,7 +2761,7 @@
|
|
|
2679
2761
|
"cost": {
|
|
2680
2762
|
"input": 1.25,
|
|
2681
2763
|
"output": 10,
|
|
2682
|
-
"cache_read": 0.
|
|
2764
|
+
"cache_read": 0.125
|
|
2683
2765
|
},
|
|
2684
2766
|
"reasoning": true
|
|
2685
2767
|
},
|
|
@@ -2687,7 +2769,7 @@
|
|
|
2687
2769
|
"cost": {
|
|
2688
2770
|
"input": 0.25,
|
|
2689
2771
|
"output": 2,
|
|
2690
|
-
"cache_read": 0.
|
|
2772
|
+
"cache_read": 0.025
|
|
2691
2773
|
},
|
|
2692
2774
|
"reasoning": true
|
|
2693
2775
|
},
|
|
@@ -2695,7 +2777,7 @@
|
|
|
2695
2777
|
"cost": {
|
|
2696
2778
|
"input": 0.05,
|
|
2697
2779
|
"output": 0.4,
|
|
2698
|
-
"cache_read": 0.
|
|
2780
|
+
"cache_read": 0.005
|
|
2699
2781
|
},
|
|
2700
2782
|
"reasoning": true
|
|
2701
2783
|
},
|
|
@@ -2742,7 +2824,7 @@
|
|
|
2742
2824
|
"cost": {
|
|
2743
2825
|
"input": 1.75,
|
|
2744
2826
|
"output": 14,
|
|
2745
|
-
"cache_read": 0.
|
|
2827
|
+
"cache_read": 0.175
|
|
2746
2828
|
},
|
|
2747
2829
|
"reasoning": true
|
|
2748
2830
|
},
|
|
@@ -2878,14 +2960,14 @@
|
|
|
2878
2960
|
"cost": {
|
|
2879
2961
|
"input": 4,
|
|
2880
2962
|
"output": 20,
|
|
2881
|
-
"cache_read": 0.
|
|
2882
|
-
"cache_write":
|
|
2963
|
+
"cache_read": 0.4,
|
|
2964
|
+
"cache_write": 5,
|
|
2883
2965
|
"tiers": [
|
|
2884
2966
|
{
|
|
2885
|
-
"input":
|
|
2886
|
-
"output":
|
|
2887
|
-
"cache_read":
|
|
2888
|
-
"cache_write":
|
|
2967
|
+
"input": 8,
|
|
2968
|
+
"output": 30,
|
|
2969
|
+
"cache_read": 0.8,
|
|
2970
|
+
"cache_write": 10,
|
|
2889
2971
|
"tier": {
|
|
2890
2972
|
"type": "context",
|
|
2891
2973
|
"size": 272000
|
|
@@ -2893,10 +2975,10 @@
|
|
|
2893
2975
|
}
|
|
2894
2976
|
],
|
|
2895
2977
|
"context_over_200k": {
|
|
2896
|
-
"input":
|
|
2897
|
-
"output":
|
|
2898
|
-
"cache_read":
|
|
2899
|
-
"cache_write":
|
|
2978
|
+
"input": 8,
|
|
2979
|
+
"output": 30,
|
|
2980
|
+
"cache_read": 0.8,
|
|
2981
|
+
"cache_write": 10
|
|
2900
2982
|
}
|
|
2901
2983
|
},
|
|
2902
2984
|
"reasoning": true
|
|
@@ -3036,6 +3118,33 @@
|
|
|
3036
3118
|
},
|
|
3037
3119
|
"reasoning": true
|
|
3038
3120
|
},
|
|
3121
|
+
"gpt-6.1-terra": {
|
|
3122
|
+
"cost": {
|
|
3123
|
+
"input": 1,
|
|
3124
|
+
"output": 5,
|
|
3125
|
+
"cache_read": 0.05,
|
|
3126
|
+
"cache_write": 1.25,
|
|
3127
|
+
"tiers": [
|
|
3128
|
+
{
|
|
3129
|
+
"input": 2,
|
|
3130
|
+
"output": 7.5,
|
|
3131
|
+
"cache_read": 0.1,
|
|
3132
|
+
"cache_write": 2.5,
|
|
3133
|
+
"tier": {
|
|
3134
|
+
"type": "context",
|
|
3135
|
+
"size": 272000
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
],
|
|
3139
|
+
"context_over_200k": {
|
|
3140
|
+
"input": 2,
|
|
3141
|
+
"output": 7.5,
|
|
3142
|
+
"cache_read": 0.1,
|
|
3143
|
+
"cache_write": 2.5
|
|
3144
|
+
}
|
|
3145
|
+
},
|
|
3146
|
+
"reasoning": true
|
|
3147
|
+
},
|
|
3039
3148
|
"gpt-chat-latest": {
|
|
3040
3149
|
"cost": {
|
|
3041
3150
|
"input": 5,
|
|
@@ -4071,14 +4180,6 @@
|
|
|
4071
4180
|
},
|
|
4072
4181
|
"reasoning": true
|
|
4073
4182
|
},
|
|
4074
|
-
"gpt-5.2-chat-latest": {
|
|
4075
|
-
"cost": {
|
|
4076
|
-
"input": 1.75,
|
|
4077
|
-
"output": 14,
|
|
4078
|
-
"cache_read": 0.175
|
|
4079
|
-
},
|
|
4080
|
-
"reasoning": false
|
|
4081
|
-
},
|
|
4082
4183
|
"gpt-5.2-pro": {
|
|
4083
4184
|
"cost": {
|
|
4084
4185
|
"input": 21,
|
|
@@ -4086,14 +4187,6 @@
|
|
|
4086
4187
|
},
|
|
4087
4188
|
"reasoning": true
|
|
4088
4189
|
},
|
|
4089
|
-
"gpt-5.3-chat-latest": {
|
|
4090
|
-
"cost": {
|
|
4091
|
-
"input": 1.75,
|
|
4092
|
-
"output": 14,
|
|
4093
|
-
"cache_read": 0.175
|
|
4094
|
-
},
|
|
4095
|
-
"reasoning": false
|
|
4096
|
-
},
|
|
4097
4190
|
"gpt-5.3-codex": {
|
|
4098
4191
|
"cost": {
|
|
4099
4192
|
"input": 1.75,
|
|
@@ -4432,6 +4525,33 @@
|
|
|
4432
4525
|
},
|
|
4433
4526
|
"reasoning": true
|
|
4434
4527
|
},
|
|
4528
|
+
"gpt-6.1-terra": {
|
|
4529
|
+
"cost": {
|
|
4530
|
+
"input": 1,
|
|
4531
|
+
"output": 5,
|
|
4532
|
+
"cache_read": 0.05,
|
|
4533
|
+
"cache_write": 1.25,
|
|
4534
|
+
"tiers": [
|
|
4535
|
+
{
|
|
4536
|
+
"input": 2,
|
|
4537
|
+
"output": 7.5,
|
|
4538
|
+
"cache_read": 0.1,
|
|
4539
|
+
"cache_write": 2.5,
|
|
4540
|
+
"tier": {
|
|
4541
|
+
"type": "context",
|
|
4542
|
+
"size": 272000
|
|
4543
|
+
}
|
|
4544
|
+
}
|
|
4545
|
+
],
|
|
4546
|
+
"context_over_200k": {
|
|
4547
|
+
"input": 2,
|
|
4548
|
+
"output": 7.5,
|
|
4549
|
+
"cache_read": 0.1,
|
|
4550
|
+
"cache_write": 2.5
|
|
4551
|
+
}
|
|
4552
|
+
},
|
|
4553
|
+
"reasoning": true
|
|
4554
|
+
},
|
|
4435
4555
|
"gpt-daybreak-blue-latest": {
|
|
4436
4556
|
"cost": {
|
|
4437
4557
|
"input": 4,
|
package/src/llm/utils/usage.js
CHANGED
|
@@ -3,9 +3,9 @@ import { BaseAttribute } from '#trace_attribute';
|
|
|
3
3
|
import { extractUsageFromSteps } from './usage_tools.js';
|
|
4
4
|
import { parseWebSearchUsage } from './web_search.js';
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const isValidTokenCount = v => Number.isSafeInteger( v ) && v >= 0;
|
|
7
7
|
|
|
8
|
-
const safeSum = ( ...values ) => values.filter(
|
|
8
|
+
const safeSum = ( ...values ) => values.filter( isValidTokenCount ).reduce( ( t, v ) => t + v, 0 );
|
|
9
9
|
|
|
10
10
|
export class LLMGenerationUsageItem {
|
|
11
11
|
static Group = { INPUT: 'input', OUTPUT: 'output', TOOLS: 'tools' };
|
|
@@ -51,10 +51,10 @@ export class LLMGenerationUsage extends BaseAttribute {
|
|
|
51
51
|
this.output = outputItems.reduce( ( s, p ) => s.add( p.amount ), Decimal( 0 ) ).toNumber();
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
if (
|
|
54
|
+
if ( isValidTokenCount( this.input ) || isValidTokenCount( this.output ) ) {
|
|
55
55
|
this.total = ( this.input ?? 0 ) + ( this.output ?? 0 );
|
|
56
56
|
}
|
|
57
|
-
this.status = LLMGenerationUsage.Status[
|
|
57
|
+
this.status = LLMGenerationUsage.Status[isValidTokenCount( this.input ) && isValidTokenCount( this.output ) ? 'COMPLETE' : 'INCOMPLETE'];
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
@@ -65,7 +65,7 @@ export class LLMGenerationUsage extends BaseAttribute {
|
|
|
65
65
|
* every token count undefined, and the steps are then the only record of what was already spent.
|
|
66
66
|
*/
|
|
67
67
|
const resolveUsage = ( { usage, steps } ) => {
|
|
68
|
-
if ( usage && (
|
|
68
|
+
if ( usage && ( isValidTokenCount( usage.inputTokens ) || isValidTokenCount( usage.outputTokens ) ) ) {
|
|
69
69
|
return usage;
|
|
70
70
|
}
|
|
71
71
|
return Array.isArray( steps ) && steps.length > 0 ? extractUsageFromSteps( steps ) : null;
|
|
@@ -98,19 +98,19 @@ export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], too
|
|
|
98
98
|
|
|
99
99
|
const items = [];
|
|
100
100
|
|
|
101
|
-
if (
|
|
101
|
+
if ( isValidTokenCount( inputTokens ) ) {
|
|
102
102
|
// the sum of the components must be equal to the tokens
|
|
103
103
|
const useDetails = inputTokens > 0 && safeSum( noCacheTokens, cacheReadTokens, cacheWriteTokens ) === inputTokens;
|
|
104
104
|
const group = LLMGenerationUsageItem.Group.INPUT;
|
|
105
105
|
|
|
106
106
|
if ( useDetails ) {
|
|
107
|
-
if (
|
|
107
|
+
if ( isValidTokenCount( noCacheTokens ) ) {
|
|
108
108
|
items.push( new LLMGenerationUsageItem( group, 'no_cache', noCacheTokens ) );
|
|
109
109
|
}
|
|
110
|
-
if (
|
|
110
|
+
if ( isValidTokenCount( cacheReadTokens ) ) {
|
|
111
111
|
items.push( new LLMGenerationUsageItem( group, 'cache_read', cacheReadTokens ) );
|
|
112
112
|
}
|
|
113
|
-
if (
|
|
113
|
+
if ( isValidTokenCount( cacheWriteTokens ) ) {
|
|
114
114
|
items.push( new LLMGenerationUsageItem( group, 'cache_write', cacheWriteTokens ) );
|
|
115
115
|
}
|
|
116
116
|
} else {
|
|
@@ -118,16 +118,16 @@ export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], too
|
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
if (
|
|
121
|
+
if ( isValidTokenCount( outputTokens ) ) {
|
|
122
122
|
// the sum of the components must be equal to the tokens
|
|
123
123
|
const useDetails = outputTokens > 0 && safeSum( textTokens, reasoningTokens ) === outputTokens;
|
|
124
124
|
const group = LLMGenerationUsageItem.Group.OUTPUT;
|
|
125
125
|
|
|
126
126
|
if ( useDetails ) {
|
|
127
|
-
if (
|
|
127
|
+
if ( isValidTokenCount( textTokens ) ) {
|
|
128
128
|
items.push( new LLMGenerationUsageItem( group, 'text', textTokens ) );
|
|
129
129
|
}
|
|
130
|
-
if (
|
|
130
|
+
if ( isValidTokenCount( reasoningTokens ) ) {
|
|
131
131
|
items.push( new LLMGenerationUsageItem( group, 'reasoning', reasoningTokens ) );
|
|
132
132
|
}
|
|
133
133
|
} else {
|
|
@@ -120,10 +120,10 @@ export const parseWebSearchUsage = ( providerId, modelId, step, toolIdsByName )
|
|
|
120
120
|
*
|
|
121
121
|
* @param {string} providerId
|
|
122
122
|
* @param {string} label
|
|
123
|
-
* @param {
|
|
123
|
+
* @param {boolean} isReasoning - Whether the used model had reasoning support
|
|
124
124
|
* @returns {number | null} Price per million requests, queries, or prompts
|
|
125
125
|
*/
|
|
126
|
-
export const resolveWebSearchPpm = ( providerId, label,
|
|
126
|
+
export const resolveWebSearchPpm = ( providerId, label, isReasoning ) => {
|
|
127
127
|
if ( providerId === 'google-vertex' ) {
|
|
128
128
|
if ( label === Label.GROUNDING_QUERY ) {
|
|
129
129
|
return Price.GOOGLE_GROUNDING_QUERY;
|
|
@@ -138,10 +138,10 @@ export const resolveWebSearchPpm = ( providerId, label, modelPricing ) => {
|
|
|
138
138
|
if ( label === Label.WEB_SEARCH ) {
|
|
139
139
|
return Price.OPENAI_WEB_SEARCH;
|
|
140
140
|
}
|
|
141
|
-
if ( label === Label.WEB_SEARCH_PREVIEW &&
|
|
141
|
+
if ( label === Label.WEB_SEARCH_PREVIEW && isReasoning === false ) {
|
|
142
142
|
return Price.OPENAI_WEB_SEARCH_PREVIEW;
|
|
143
143
|
}
|
|
144
|
-
if ( label === Label.WEB_SEARCH_PREVIEW &&
|
|
144
|
+
if ( label === Label.WEB_SEARCH_PREVIEW && isReasoning === true ) {
|
|
145
145
|
return Price.OPENAI_WEB_SEARCH_PREVIEW_REASONING;
|
|
146
146
|
}
|
|
147
147
|
return null;
|
|
@@ -19,8 +19,22 @@ const createWorkflowsEntrypoint = workflows => {
|
|
|
19
19
|
aliases.map( alias => ( { name: alias, path } ) )
|
|
20
20
|
);
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
// A static re-export makes every cached workflow evaluate the whole catalog: Temporal's reusable VM
|
|
23
|
+
// keeps each module loaded during an activation in that workflow's own cache. A lazy export loads
|
|
24
|
+
// only the invoked workflow; eager mode keeps it in the same bundle. Workflow type resolution is by
|
|
25
|
+
// export name, and the metadata the framework reads is read in Node, never from the bundle.
|
|
26
|
+
const lazyExports = [ ...workflows, ...aliasExports ].map( ( { name, path } ) => `\
|
|
27
|
+
async function _${name}( ...args ) {
|
|
28
|
+
const { default: workflow } = await import( /* webpackMode: "eager" */ '${path}' );
|
|
29
|
+
if ( typeof workflow !== 'function' ) {
|
|
30
|
+
throw new TypeError( 'Workflow "${name}" must be exported as default.' );
|
|
31
|
+
}
|
|
32
|
+
return workflow( ...args );
|
|
33
|
+
}
|
|
34
|
+
export { _${name} as ${name} };` );
|
|
35
|
+
|
|
36
|
+
// The catalog stays static: it is the framework's own workflow, small, and always loaded.
|
|
37
|
+
const content = [ ...lazyExports, `export { default as ${catalog.name} } from '${catalog.path}';` ].join( EOL );
|
|
24
38
|
|
|
25
39
|
return writeFileInTempDir( content, WORKFLOWS_INDEX_FILENAME );
|
|
26
40
|
};
|