Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions packages/models/src/models.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getCheapestFromAvailableProviders } from "./get-cheapest-from-available
import { getCheapestModelForProvider } from "./get-cheapest-model-for-provider.js";
import { models } from "./models.js";
import { prepareRequestBody } from "./prepare-request-body.js";
import { getProviderDefinition } from "./providers.js";

import type { ProviderModelMapping } from "./models.js";
import type { BaseMessage, OpenAIRequestBody } from "./types.js";
Expand Down Expand Up @@ -518,15 +519,36 @@ describe("getCheapestFromAvailableProviders", () => {
modelWithDiscountProvider,
);

// Calculate actual prices with discount
const regularPrice =
// Calculate actual effective prices with discount and priority
// The function uses: discountMultiplier = 1 - discount, effectivePrice = totalPrice / priority
const regularProviderDef = getProviderDefinition(
regularProvider.providerId,
);
const discountProviderDef = getProviderDefinition(
discountProvider.providerId,
);
const regularPriority = regularProviderDef?.priority ?? 1;
const discountPriority = discountProviderDef?.priority ?? 1;

const regularBasePrice =
(regularProvider.inputPrice! + regularProvider.outputPrice!) / 2;
const discountPrice =
const regularEffectivePrice =
regularPriority > 0
? regularBasePrice / regularPriority
: regularBasePrice;

const discount = (discountProvider as ProviderModelMapping).discount!;
const discountMultiplier = 1 - discount;
const discountBasePrice =
((discountProvider.inputPrice! + discountProvider.outputPrice!) / 2) *
(discountProvider as ProviderModelMapping).discount!;

// If discount provider is cheaper, it should be selected
if (discountPrice < regularPrice) {
discountMultiplier;
const discountEffectivePrice =
discountPriority > 0
? discountBasePrice / discountPriority
: discountBasePrice;

// The provider with lower effective price should be selected
if (discountEffectivePrice < regularEffectivePrice) {
expect(cheapestProvider?.provider.providerId).toBe(
discountProvider.providerId,
);
Expand Down
9 changes: 9 additions & 0 deletions packages/models/src/models/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const anthropicModels = [
outputPrice: 15.0 / 1e6,
cachedInputPrice: 0.3 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 8192,
reasoning: true,
Expand Down Expand Up @@ -123,6 +124,7 @@ export const anthropicModels = [
inputPrice: 3.0 / 1e6,
outputPrice: 15.0 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 8192,
streaming: true,
Expand Down Expand Up @@ -186,6 +188,7 @@ export const anthropicModels = [
inputPrice: 3.0 / 1e6,
outputPrice: 15.0 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: undefined,
reasoning: true,
Expand Down Expand Up @@ -227,6 +230,7 @@ export const anthropicModels = [
inputPrice: 3.0 / 1e6,
outputPrice: 15.0 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 8192,
reasoning: true,
Expand Down Expand Up @@ -268,6 +272,7 @@ export const anthropicModels = [
inputPrice: 3.0 / 1e6,
outputPrice: 15.0 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 8192,
reasoning: true,
Expand Down Expand Up @@ -357,6 +362,7 @@ export const anthropicModels = [
inputPrice: 15.0 / 1e6,
outputPrice: 75.0 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: undefined,
reasoning: true,
Expand Down Expand Up @@ -397,6 +403,7 @@ export const anthropicModels = [
inputPrice: 15.0 / 1e6,
outputPrice: 75.0 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 32000,
streaming: true,
Expand Down Expand Up @@ -486,6 +493,7 @@ export const anthropicModels = [
outputPrice: 4.0 / 1e6,
cachedInputPrice: 0.08 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 8192,
streaming: true,
Expand Down Expand Up @@ -574,6 +582,7 @@ export const anthropicModels = [
outputPrice: 25.0 / 1e6,
cachedInputPrice: 0.5 / 1e6,
requestPrice: 0,
discount: 0.3,
contextSize: 200000,
maxOutput: 32000,
reasoning: true,
Expand Down