mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-08 02:24:24 +01:00
🤖 feat: Claude Opus 4.6 - 1M Context, Premium Pricing, Adaptive Thinking (#11670)
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
* feat: Implement new features for Claude Opus 4.6 model - Added support for tiered pricing based on input token count for the Claude Opus 4.6 model. - Updated token value calculations to include inputTokenCount for accurate pricing. - Enhanced transaction handling to apply premium rates when input tokens exceed defined thresholds. - Introduced comprehensive tests to validate pricing logic for both standard and premium rates across various scenarios. - Updated related utility functions and models to accommodate new pricing structure. This change improves the flexibility and accuracy of token pricing for the Claude Opus 4.6 model, ensuring users are charged appropriately based on their usage. * feat: Add effort field to conversation and preset schemas - Introduced a new optional `effort` field of type `String` in both the `IPreset` and `IConversation` interfaces. - Updated the `conversationPreset` schema to include the `effort` field, enhancing the data structure for better context management. * chore: Clean up unused variable and comments in initialize function * chore: update dependencies and SDK versions - Updated @anthropic-ai/sdk to version 0.73.0 in package.json and overrides. - Updated @anthropic-ai/vertex-sdk to version 0.14.3 in packages/api/package.json. - Updated @librechat/agents to version 3.1.34 in packages/api/package.json. - Refactored imports in packages/api/src/endpoints/anthropic/vertex.ts for consistency. * chore: remove postcss-loader from dependencies * feat: Bedrock model support for adaptive thinking configuration - Updated .env.example to include new Bedrock model IDs for Claude Opus 4.6. - Refactored bedrockInputParser to support adaptive thinking for Opus models, allowing for dynamic thinking configurations. - Introduced a new function to check model compatibility with adaptive thinking. - Added an optional `effort` field to the input schemas and updated related configurations. - Enhanced tests to validate the new adaptive thinking logic and model configurations. * feat: Add tests for Opus 4.6 adaptive thinking configuration * feat: Update model references for Opus 4.6 by removing version suffix * feat: Update @librechat/agents to version 3.1.35 in package.json and package-lock.json * chore: @librechat/agents to version 3.1.36 in package.json and package-lock.json * feat: Normalize inputTokenCount for spendTokens and enhance transaction handling - Introduced normalization for promptTokens to ensure inputTokenCount does not go negative. - Updated transaction logic to reflect normalized inputTokenCount in pricing calculations. - Added comprehensive tests to validate the new normalization logic and its impact on transaction rates for both standard and premium models. - Refactored related functions to improve clarity and maintainability of token value calculations. * chore: Simplify adaptive thinking configuration in helpers.ts - Removed unnecessary type casting for the thinking property in updatedOptions. - Ensured that adaptive thinking is directly assigned when conditions are met, improving code clarity. * refactor: Replace hard-coded token values with dynamic retrieval from maxTokensMap in model tests * fix: Ensure non-negative token values in spendTokens calculations - Updated token value retrieval to use Math.max for prompt and completion tokens, preventing negative values. - Enhanced clarity in token calculations for both prompt and completion transactions. * test: Add test for normalization of negative structured token values in spendStructuredTokens - Implemented a test to ensure that negative structured token values are normalized to zero during token spending. - Verified that the transaction rates remain consistent with the expected standard values after normalization. * refactor: Bedrock model support for adaptive thinking and context handling - Added tests for various alternate naming conventions of Claude models to validate adaptive thinking and context support. - Refactored `supportsAdaptiveThinking` and `supportsContext1m` functions to utilize new parsing methods for model version extraction. - Updated `bedrockInputParser` to handle effort configurations more effectively and strip unnecessary fields for non-adaptive models. - Improved handling of anthropic model configurations in the input parser. * fix: Improve token value retrieval in getMultiplier function - Updated the token value retrieval logic to use optional chaining for better safety against undefined values. - Added a test case to ensure that the function returns the default rate when the provided valueKey does not exist in tokenValues.
This commit is contained in:
parent
1d5f2eb04b
commit
41e2348d47
32 changed files with 2902 additions and 1087 deletions
|
|
@ -138,11 +138,10 @@ const updateBalance = async ({ user, incrementValue, setValues }) => {
|
|||
|
||||
/** Method to calculate and set the tokenValue for a transaction */
|
||||
function calculateTokenValue(txn) {
|
||||
if (!txn.valueKey || !txn.tokenType) {
|
||||
txn.tokenValue = txn.rawAmount;
|
||||
}
|
||||
const { valueKey, tokenType, model, endpointTokenConfig } = txn;
|
||||
const multiplier = Math.abs(getMultiplier({ valueKey, tokenType, model, endpointTokenConfig }));
|
||||
const { valueKey, tokenType, model, endpointTokenConfig, inputTokenCount } = txn;
|
||||
const multiplier = Math.abs(
|
||||
getMultiplier({ valueKey, tokenType, model, endpointTokenConfig, inputTokenCount }),
|
||||
);
|
||||
txn.rate = multiplier;
|
||||
txn.tokenValue = txn.rawAmount * multiplier;
|
||||
if (txn.context && txn.tokenType === 'completion' && txn.context === 'incomplete') {
|
||||
|
|
@ -166,6 +165,7 @@ async function createAutoRefillTransaction(txData) {
|
|||
}
|
||||
const transaction = new Transaction(txData);
|
||||
transaction.endpointTokenConfig = txData.endpointTokenConfig;
|
||||
transaction.inputTokenCount = txData.inputTokenCount;
|
||||
calculateTokenValue(transaction);
|
||||
await transaction.save();
|
||||
|
||||
|
|
@ -200,6 +200,7 @@ async function createTransaction(_txData) {
|
|||
|
||||
const transaction = new Transaction(txData);
|
||||
transaction.endpointTokenConfig = txData.endpointTokenConfig;
|
||||
transaction.inputTokenCount = txData.inputTokenCount;
|
||||
calculateTokenValue(transaction);
|
||||
|
||||
await transaction.save();
|
||||
|
|
@ -231,10 +232,9 @@ async function createStructuredTransaction(_txData) {
|
|||
return;
|
||||
}
|
||||
|
||||
const transaction = new Transaction({
|
||||
...txData,
|
||||
endpointTokenConfig: txData.endpointTokenConfig,
|
||||
});
|
||||
const transaction = new Transaction(txData);
|
||||
transaction.endpointTokenConfig = txData.endpointTokenConfig;
|
||||
transaction.inputTokenCount = txData.inputTokenCount;
|
||||
|
||||
calculateStructuredTokenValue(transaction);
|
||||
|
||||
|
|
@ -266,10 +266,15 @@ function calculateStructuredTokenValue(txn) {
|
|||
return;
|
||||
}
|
||||
|
||||
const { model, endpointTokenConfig } = txn;
|
||||
const { model, endpointTokenConfig, inputTokenCount } = txn;
|
||||
|
||||
if (txn.tokenType === 'prompt') {
|
||||
const inputMultiplier = getMultiplier({ tokenType: 'prompt', model, endpointTokenConfig });
|
||||
const inputMultiplier = getMultiplier({
|
||||
tokenType: 'prompt',
|
||||
model,
|
||||
endpointTokenConfig,
|
||||
inputTokenCount,
|
||||
});
|
||||
const writeMultiplier =
|
||||
getCacheMultiplier({ cacheType: 'write', model, endpointTokenConfig }) ?? inputMultiplier;
|
||||
const readMultiplier =
|
||||
|
|
@ -304,7 +309,12 @@ function calculateStructuredTokenValue(txn) {
|
|||
|
||||
txn.rawAmount = -totalPromptTokens;
|
||||
} else if (txn.tokenType === 'completion') {
|
||||
const multiplier = getMultiplier({ tokenType: txn.tokenType, model, endpointTokenConfig });
|
||||
const multiplier = getMultiplier({
|
||||
tokenType: txn.tokenType,
|
||||
model,
|
||||
endpointTokenConfig,
|
||||
inputTokenCount,
|
||||
});
|
||||
txn.rate = Math.abs(multiplier);
|
||||
txn.tokenValue = -Math.abs(txn.rawAmount) * multiplier;
|
||||
txn.rawAmount = -Math.abs(txn.rawAmount);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
const mongoose = require('mongoose');
|
||||
const { MongoMemoryServer } = require('mongodb-memory-server');
|
||||
const { spendTokens, spendStructuredTokens } = require('./spendTokens');
|
||||
const { getMultiplier, getCacheMultiplier } = require('./tx');
|
||||
const { getMultiplier, getCacheMultiplier, premiumTokenValues, tokenValues } = require('./tx');
|
||||
const { createTransaction, createStructuredTransaction } = require('./Transaction');
|
||||
const { Balance, Transaction } = require('~/db/models');
|
||||
|
||||
|
|
@ -564,3 +564,291 @@ describe('Transactions Config Tests', () => {
|
|||
expect(balance.tokenCredits).toBe(initialBalance);
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateTokenValue Edge Cases', () => {
|
||||
test('should derive multiplier from model when valueKey is not provided', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'gpt-4';
|
||||
const promptTokens = 1000;
|
||||
|
||||
const result = await createTransaction({
|
||||
user: userId,
|
||||
conversationId: 'test-no-valuekey',
|
||||
model,
|
||||
tokenType: 'prompt',
|
||||
rawAmount: -promptTokens,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
});
|
||||
|
||||
const expectedRate = getMultiplier({ model, tokenType: 'prompt' });
|
||||
expect(result.rate).toBe(expectedRate);
|
||||
|
||||
const tx = await Transaction.findOne({ user: userId });
|
||||
expect(tx.tokenValue).toBe(-promptTokens * expectedRate);
|
||||
expect(tx.rate).toBe(expectedRate);
|
||||
});
|
||||
|
||||
test('should derive valueKey and apply correct rate for an unknown model with tokenType', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
await createTransaction({
|
||||
user: userId,
|
||||
conversationId: 'test-unknown-model',
|
||||
model: 'some-unrecognized-model-xyz',
|
||||
tokenType: 'prompt',
|
||||
rawAmount: -500,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
});
|
||||
|
||||
const tx = await Transaction.findOne({ user: userId });
|
||||
expect(tx.rate).toBeDefined();
|
||||
expect(tx.rate).toBeGreaterThan(0);
|
||||
expect(tx.tokenValue).toBe(tx.rawAmount * tx.rate);
|
||||
});
|
||||
|
||||
test('should correctly apply model-derived multiplier without valueKey for completion', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const completionTokens = 500;
|
||||
|
||||
const result = await createTransaction({
|
||||
user: userId,
|
||||
conversationId: 'test-completion-no-valuekey',
|
||||
model,
|
||||
tokenType: 'completion',
|
||||
rawAmount: -completionTokens,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
});
|
||||
|
||||
const expectedRate = getMultiplier({ model, tokenType: 'completion' });
|
||||
expect(expectedRate).toBe(tokenValues[model].completion);
|
||||
expect(result.rate).toBe(expectedRate);
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(
|
||||
initialBalance - completionTokens * expectedRate,
|
||||
0,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Premium Token Pricing Integration Tests', () => {
|
||||
test('spendTokens should apply standard pricing when prompt tokens are below premium threshold', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const promptTokens = 100000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-premium-below',
|
||||
model,
|
||||
context: 'test',
|
||||
endpointTokenConfig: null,
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const standardPromptRate = tokenValues[model].prompt;
|
||||
const standardCompletionRate = tokenValues[model].completion;
|
||||
const expectedCost =
|
||||
promptTokens * standardPromptRate + completionTokens * standardCompletionRate;
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
|
||||
test('spendTokens should apply premium pricing when prompt tokens exceed premium threshold', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const promptTokens = 250000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-premium-above',
|
||||
model,
|
||||
context: 'test',
|
||||
endpointTokenConfig: null,
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const premiumPromptRate = premiumTokenValues[model].prompt;
|
||||
const premiumCompletionRate = premiumTokenValues[model].completion;
|
||||
const expectedCost =
|
||||
promptTokens * premiumPromptRate + completionTokens * premiumCompletionRate;
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
|
||||
test('spendTokens should apply standard pricing at exactly the premium threshold', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const promptTokens = premiumTokenValues[model].threshold;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-premium-exact',
|
||||
model,
|
||||
context: 'test',
|
||||
endpointTokenConfig: null,
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const standardPromptRate = tokenValues[model].prompt;
|
||||
const standardCompletionRate = tokenValues[model].completion;
|
||||
const expectedCost =
|
||||
promptTokens * standardPromptRate + completionTokens * standardCompletionRate;
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
|
||||
test('spendStructuredTokens should apply premium pricing when total input tokens exceed threshold', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-structured-premium',
|
||||
model,
|
||||
context: 'message',
|
||||
endpointTokenConfig: null,
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
const tokenUsage = {
|
||||
promptTokens: {
|
||||
input: 200000,
|
||||
write: 10000,
|
||||
read: 5000,
|
||||
},
|
||||
completionTokens: 1000,
|
||||
};
|
||||
|
||||
const totalInput =
|
||||
tokenUsage.promptTokens.input + tokenUsage.promptTokens.write + tokenUsage.promptTokens.read;
|
||||
|
||||
await spendStructuredTokens(txData, tokenUsage);
|
||||
|
||||
const premiumPromptRate = premiumTokenValues[model].prompt;
|
||||
const premiumCompletionRate = premiumTokenValues[model].completion;
|
||||
const writeMultiplier = getCacheMultiplier({ model, cacheType: 'write' });
|
||||
const readMultiplier = getCacheMultiplier({ model, cacheType: 'read' });
|
||||
|
||||
const expectedPromptCost =
|
||||
tokenUsage.promptTokens.input * premiumPromptRate +
|
||||
tokenUsage.promptTokens.write * writeMultiplier +
|
||||
tokenUsage.promptTokens.read * readMultiplier;
|
||||
const expectedCompletionCost = tokenUsage.completionTokens * premiumCompletionRate;
|
||||
const expectedTotalCost = expectedPromptCost + expectedCompletionCost;
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(totalInput).toBeGreaterThan(premiumTokenValues[model].threshold);
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(initialBalance - expectedTotalCost, 0);
|
||||
});
|
||||
|
||||
test('spendStructuredTokens should apply standard pricing when total input tokens are below threshold', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-structured-standard',
|
||||
model,
|
||||
context: 'message',
|
||||
endpointTokenConfig: null,
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
const tokenUsage = {
|
||||
promptTokens: {
|
||||
input: 50000,
|
||||
write: 10000,
|
||||
read: 5000,
|
||||
},
|
||||
completionTokens: 1000,
|
||||
};
|
||||
|
||||
const totalInput =
|
||||
tokenUsage.promptTokens.input + tokenUsage.promptTokens.write + tokenUsage.promptTokens.read;
|
||||
|
||||
await spendStructuredTokens(txData, tokenUsage);
|
||||
|
||||
const standardPromptRate = tokenValues[model].prompt;
|
||||
const standardCompletionRate = tokenValues[model].completion;
|
||||
const writeMultiplier = getCacheMultiplier({ model, cacheType: 'write' });
|
||||
const readMultiplier = getCacheMultiplier({ model, cacheType: 'read' });
|
||||
|
||||
const expectedPromptCost =
|
||||
tokenUsage.promptTokens.input * standardPromptRate +
|
||||
tokenUsage.promptTokens.write * writeMultiplier +
|
||||
tokenUsage.promptTokens.read * readMultiplier;
|
||||
const expectedCompletionCost = tokenUsage.completionTokens * standardCompletionRate;
|
||||
const expectedTotalCost = expectedPromptCost + expectedCompletionCost;
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(totalInput).toBeLessThanOrEqual(premiumTokenValues[model].threshold);
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(initialBalance - expectedTotalCost, 0);
|
||||
});
|
||||
|
||||
test('non-premium models should not be affected by inputTokenCount regardless of prompt size', async () => {
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({ user: userId, tokenCredits: initialBalance });
|
||||
|
||||
const model = 'claude-opus-4-5';
|
||||
const promptTokens = 300000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-no-premium',
|
||||
model,
|
||||
context: 'test',
|
||||
endpointTokenConfig: null,
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const standardPromptRate = getMultiplier({ model, tokenType: 'prompt' });
|
||||
const standardCompletionRate = getMultiplier({ model, tokenType: 'completion' });
|
||||
const expectedCost =
|
||||
promptTokens * standardPromptRate + completionTokens * standardCompletionRate;
|
||||
|
||||
const updatedBalance = await Balance.findOne({ user: userId });
|
||||
expect(updatedBalance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -24,12 +24,14 @@ const spendTokens = async (txData, tokenUsage) => {
|
|||
},
|
||||
);
|
||||
let prompt, completion;
|
||||
const normalizedPromptTokens = Math.max(promptTokens ?? 0, 0);
|
||||
try {
|
||||
if (promptTokens !== undefined) {
|
||||
prompt = await createTransaction({
|
||||
...txData,
|
||||
tokenType: 'prompt',
|
||||
rawAmount: promptTokens === 0 ? 0 : -Math.max(promptTokens, 0),
|
||||
rawAmount: promptTokens === 0 ? 0 : -normalizedPromptTokens,
|
||||
inputTokenCount: normalizedPromptTokens,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -38,6 +40,7 @@ const spendTokens = async (txData, tokenUsage) => {
|
|||
...txData,
|
||||
tokenType: 'completion',
|
||||
rawAmount: completionTokens === 0 ? 0 : -Math.max(completionTokens, 0),
|
||||
inputTokenCount: normalizedPromptTokens,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -87,21 +90,31 @@ const spendStructuredTokens = async (txData, tokenUsage) => {
|
|||
let prompt, completion;
|
||||
try {
|
||||
if (promptTokens) {
|
||||
const { input = 0, write = 0, read = 0 } = promptTokens;
|
||||
const input = Math.max(promptTokens.input ?? 0, 0);
|
||||
const write = Math.max(promptTokens.write ?? 0, 0);
|
||||
const read = Math.max(promptTokens.read ?? 0, 0);
|
||||
const totalInputTokens = input + write + read;
|
||||
prompt = await createStructuredTransaction({
|
||||
...txData,
|
||||
tokenType: 'prompt',
|
||||
inputTokens: -input,
|
||||
writeTokens: -write,
|
||||
readTokens: -read,
|
||||
inputTokenCount: totalInputTokens,
|
||||
});
|
||||
}
|
||||
|
||||
if (completionTokens) {
|
||||
const totalInputTokens = promptTokens
|
||||
? Math.max(promptTokens.input ?? 0, 0) +
|
||||
Math.max(promptTokens.write ?? 0, 0) +
|
||||
Math.max(promptTokens.read ?? 0, 0)
|
||||
: undefined;
|
||||
completion = await createTransaction({
|
||||
...txData,
|
||||
tokenType: 'completion',
|
||||
rawAmount: -completionTokens,
|
||||
rawAmount: -Math.max(completionTokens, 0),
|
||||
inputTokenCount: totalInputTokens,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
const mongoose = require('mongoose');
|
||||
const { MongoMemoryServer } = require('mongodb-memory-server');
|
||||
const { spendTokens, spendStructuredTokens } = require('./spendTokens');
|
||||
const { createTransaction, createAutoRefillTransaction } = require('./Transaction');
|
||||
const { tokenValues, premiumTokenValues, getCacheMultiplier } = require('./tx');
|
||||
const { spendTokens, spendStructuredTokens } = require('./spendTokens');
|
||||
|
||||
require('~/db/models');
|
||||
|
||||
|
|
@ -734,4 +735,328 @@ describe('spendTokens', () => {
|
|||
expect(balance).toBeDefined();
|
||||
expect(balance.tokenCredits).toBeLessThan(10000); // Balance should be reduced
|
||||
});
|
||||
|
||||
describe('premium token pricing', () => {
|
||||
it('should charge standard rates for claude-opus-4-6 when prompt tokens are below threshold', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const promptTokens = 100000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-standard-pricing',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const expectedCost =
|
||||
promptTokens * tokenValues[model].prompt + completionTokens * tokenValues[model].completion;
|
||||
|
||||
const balance = await Balance.findOne({ user: userId });
|
||||
expect(balance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
|
||||
it('should charge premium rates for claude-opus-4-6 when prompt tokens exceed threshold', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const promptTokens = 250000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-premium-pricing',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const expectedCost =
|
||||
promptTokens * premiumTokenValues[model].prompt +
|
||||
completionTokens * premiumTokenValues[model].completion;
|
||||
|
||||
const balance = await Balance.findOne({ user: userId });
|
||||
expect(balance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
|
||||
it('should charge premium rates for both prompt and completion in structured tokens when above threshold', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-structured-premium',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
const tokenUsage = {
|
||||
promptTokens: {
|
||||
input: 200000,
|
||||
write: 10000,
|
||||
read: 5000,
|
||||
},
|
||||
completionTokens: 1000,
|
||||
};
|
||||
|
||||
const result = await spendStructuredTokens(txData, tokenUsage);
|
||||
|
||||
const premiumPromptRate = premiumTokenValues[model].prompt;
|
||||
const premiumCompletionRate = premiumTokenValues[model].completion;
|
||||
const writeRate = getCacheMultiplier({ model, cacheType: 'write' });
|
||||
const readRate = getCacheMultiplier({ model, cacheType: 'read' });
|
||||
|
||||
const expectedPromptCost =
|
||||
tokenUsage.promptTokens.input * premiumPromptRate +
|
||||
tokenUsage.promptTokens.write * writeRate +
|
||||
tokenUsage.promptTokens.read * readRate;
|
||||
const expectedCompletionCost = tokenUsage.completionTokens * premiumCompletionRate;
|
||||
|
||||
expect(result.prompt.prompt).toBeCloseTo(-expectedPromptCost, 0);
|
||||
expect(result.completion.completion).toBeCloseTo(-expectedCompletionCost, 0);
|
||||
});
|
||||
|
||||
it('should charge standard rates for structured tokens when below threshold', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-structured-standard',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
const tokenUsage = {
|
||||
promptTokens: {
|
||||
input: 50000,
|
||||
write: 10000,
|
||||
read: 5000,
|
||||
},
|
||||
completionTokens: 1000,
|
||||
};
|
||||
|
||||
const result = await spendStructuredTokens(txData, tokenUsage);
|
||||
|
||||
const standardPromptRate = tokenValues[model].prompt;
|
||||
const standardCompletionRate = tokenValues[model].completion;
|
||||
const writeRate = getCacheMultiplier({ model, cacheType: 'write' });
|
||||
const readRate = getCacheMultiplier({ model, cacheType: 'read' });
|
||||
|
||||
const expectedPromptCost =
|
||||
tokenUsage.promptTokens.input * standardPromptRate +
|
||||
tokenUsage.promptTokens.write * writeRate +
|
||||
tokenUsage.promptTokens.read * readRate;
|
||||
const expectedCompletionCost = tokenUsage.completionTokens * standardCompletionRate;
|
||||
|
||||
expect(result.prompt.prompt).toBeCloseTo(-expectedPromptCost, 0);
|
||||
expect(result.completion.completion).toBeCloseTo(-expectedCompletionCost, 0);
|
||||
});
|
||||
|
||||
it('should not apply premium pricing to non-premium models regardless of prompt size', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-5';
|
||||
const promptTokens = 300000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-no-premium',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const expectedCost =
|
||||
promptTokens * tokenValues[model].prompt + completionTokens * tokenValues[model].completion;
|
||||
|
||||
const balance = await Balance.findOne({ user: userId });
|
||||
expect(balance.tokenCredits).toBeCloseTo(initialBalance - expectedCost, 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('inputTokenCount Normalization', () => {
|
||||
it('should normalize negative promptTokens to zero for inputTokenCount', async () => {
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: 100000000,
|
||||
});
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-negative-prompt',
|
||||
model: 'claude-opus-4-6',
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens: -500, completionTokens: 100 });
|
||||
|
||||
const transactions = await Transaction.find({ user: userId }).sort({ tokenType: 1 });
|
||||
|
||||
const completionTx = transactions.find((t) => t.tokenType === 'completion');
|
||||
const promptTx = transactions.find((t) => t.tokenType === 'prompt');
|
||||
|
||||
expect(Math.abs(promptTx.rawAmount)).toBe(0);
|
||||
expect(completionTx.rawAmount).toBe(-100);
|
||||
|
||||
const standardCompletionRate = tokenValues['claude-opus-4-6'].completion;
|
||||
expect(completionTx.rate).toBe(standardCompletionRate);
|
||||
});
|
||||
|
||||
it('should use normalized inputTokenCount for premium threshold check on completion', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const promptTokens = 250000;
|
||||
const completionTokens = 500;
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-normalized-premium',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens, completionTokens });
|
||||
|
||||
const transactions = await Transaction.find({ user: userId }).sort({ tokenType: 1 });
|
||||
const completionTx = transactions.find((t) => t.tokenType === 'completion');
|
||||
const promptTx = transactions.find((t) => t.tokenType === 'prompt');
|
||||
|
||||
const premiumPromptRate = premiumTokenValues[model].prompt;
|
||||
const premiumCompletionRate = premiumTokenValues[model].completion;
|
||||
expect(promptTx.rate).toBe(premiumPromptRate);
|
||||
expect(completionTx.rate).toBe(premiumCompletionRate);
|
||||
});
|
||||
|
||||
it('should keep inputTokenCount as zero when promptTokens is zero', async () => {
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: 100000000,
|
||||
});
|
||||
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-zero-prompt',
|
||||
model: 'claude-opus-4-6',
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens: 0, completionTokens: 100 });
|
||||
|
||||
const transactions = await Transaction.find({ user: userId }).sort({ tokenType: 1 });
|
||||
const completionTx = transactions.find((t) => t.tokenType === 'completion');
|
||||
const promptTx = transactions.find((t) => t.tokenType === 'prompt');
|
||||
|
||||
expect(Math.abs(promptTx.rawAmount)).toBe(0);
|
||||
|
||||
const standardCompletionRate = tokenValues['claude-opus-4-6'].completion;
|
||||
expect(completionTx.rate).toBe(standardCompletionRate);
|
||||
});
|
||||
|
||||
it('should not trigger premium pricing with negative promptTokens on premium model', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-negative-no-premium',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
await spendTokens(txData, { promptTokens: -300000, completionTokens: 500 });
|
||||
|
||||
const transactions = await Transaction.find({ user: userId }).sort({ tokenType: 1 });
|
||||
const completionTx = transactions.find((t) => t.tokenType === 'completion');
|
||||
|
||||
const standardCompletionRate = tokenValues[model].completion;
|
||||
expect(completionTx.rate).toBe(standardCompletionRate);
|
||||
});
|
||||
|
||||
it('should normalize negative structured token values to zero in spendStructuredTokens', async () => {
|
||||
const initialBalance = 100000000;
|
||||
await Balance.create({
|
||||
user: userId,
|
||||
tokenCredits: initialBalance,
|
||||
});
|
||||
|
||||
const model = 'claude-opus-4-6';
|
||||
const txData = {
|
||||
user: userId,
|
||||
conversationId: 'test-negative-structured',
|
||||
model,
|
||||
context: 'test',
|
||||
balance: { enabled: true },
|
||||
};
|
||||
|
||||
const tokenUsage = {
|
||||
promptTokens: { input: -100, write: 50, read: -30 },
|
||||
completionTokens: -200,
|
||||
};
|
||||
|
||||
await spendStructuredTokens(txData, tokenUsage);
|
||||
|
||||
const transactions = await Transaction.find({
|
||||
user: userId,
|
||||
conversationId: 'test-negative-structured',
|
||||
}).sort({ tokenType: 1 });
|
||||
|
||||
const completionTx = transactions.find((t) => t.tokenType === 'completion');
|
||||
const promptTx = transactions.find((t) => t.tokenType === 'prompt');
|
||||
|
||||
expect(Math.abs(promptTx.inputTokens)).toBe(0);
|
||||
expect(promptTx.writeTokens).toBe(-50);
|
||||
expect(Math.abs(promptTx.readTokens)).toBe(0);
|
||||
|
||||
expect(Math.abs(completionTx.rawAmount)).toBe(0);
|
||||
|
||||
const standardRate = tokenValues[model].completion;
|
||||
expect(completionTx.rate).toBe(standardRate);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -174,6 +174,7 @@ const tokenValues = Object.assign(
|
|||
'claude-haiku-4-5': { prompt: 1, completion: 5 },
|
||||
'claude-opus-4': { prompt: 15, completion: 75 },
|
||||
'claude-opus-4-5': { prompt: 5, completion: 25 },
|
||||
'claude-opus-4-6': { prompt: 5, completion: 25 },
|
||||
'claude-sonnet-4': { prompt: 3, completion: 15 },
|
||||
'command-r': { prompt: 0.5, completion: 1.5 },
|
||||
'command-r-plus': { prompt: 3, completion: 15 },
|
||||
|
|
@ -310,6 +311,7 @@ const cacheTokenValues = {
|
|||
'claude-sonnet-4': { write: 3.75, read: 0.3 },
|
||||
'claude-opus-4': { write: 18.75, read: 1.5 },
|
||||
'claude-opus-4-5': { write: 6.25, read: 0.5 },
|
||||
'claude-opus-4-6': { write: 6.25, read: 0.5 },
|
||||
// DeepSeek models - cache hit: $0.028/1M, cache miss: $0.28/1M
|
||||
deepseek: { write: 0.28, read: 0.028 },
|
||||
'deepseek-chat': { write: 0.28, read: 0.028 },
|
||||
|
|
@ -328,6 +330,15 @@ const cacheTokenValues = {
|
|||
'kimi-k2-thinking-turbo': { write: 1.15, read: 0.15 },
|
||||
};
|
||||
|
||||
/**
|
||||
* Premium (tiered) pricing for models whose rates change based on prompt size.
|
||||
* Each entry specifies the token threshold and the rates that apply above it.
|
||||
* @type {Object.<string, {threshold: number, prompt: number, completion: number}>}
|
||||
*/
|
||||
const premiumTokenValues = {
|
||||
'claude-opus-4-6': { threshold: 200000, prompt: 10, completion: 37.5 },
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the key associated with a given model name.
|
||||
*
|
||||
|
|
@ -384,15 +395,27 @@ const getValueKey = (model, endpoint) => {
|
|||
* @param {string} [params.model] - The model name to derive the value key from if not provided.
|
||||
* @param {string} [params.endpoint] - The endpoint name to derive the value key from if not provided.
|
||||
* @param {EndpointTokenConfig} [params.endpointTokenConfig] - The token configuration for the endpoint.
|
||||
* @param {number} [params.inputTokenCount] - Total input token count for tiered pricing.
|
||||
* @returns {number} The multiplier for the given parameters, or a default value if not found.
|
||||
*/
|
||||
const getMultiplier = ({ valueKey, tokenType, model, endpoint, endpointTokenConfig }) => {
|
||||
const getMultiplier = ({
|
||||
model,
|
||||
valueKey,
|
||||
endpoint,
|
||||
tokenType,
|
||||
inputTokenCount,
|
||||
endpointTokenConfig,
|
||||
}) => {
|
||||
if (endpointTokenConfig) {
|
||||
return endpointTokenConfig?.[model]?.[tokenType] ?? defaultRate;
|
||||
}
|
||||
|
||||
if (valueKey && tokenType) {
|
||||
return tokenValues[valueKey][tokenType] ?? defaultRate;
|
||||
const premiumRate = getPremiumRate(valueKey, tokenType, inputTokenCount);
|
||||
if (premiumRate != null) {
|
||||
return premiumRate;
|
||||
}
|
||||
return tokenValues[valueKey]?.[tokenType] ?? defaultRate;
|
||||
}
|
||||
|
||||
if (!tokenType || !model) {
|
||||
|
|
@ -404,10 +427,33 @@ const getMultiplier = ({ valueKey, tokenType, model, endpoint, endpointTokenConf
|
|||
return defaultRate;
|
||||
}
|
||||
|
||||
// If we got this far, and values[tokenType] is undefined somehow, return a rough average of default multipliers
|
||||
const premiumRate = getPremiumRate(valueKey, tokenType, inputTokenCount);
|
||||
if (premiumRate != null) {
|
||||
return premiumRate;
|
||||
}
|
||||
|
||||
return tokenValues[valueKey]?.[tokenType] ?? defaultRate;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if premium (tiered) pricing applies and returns the premium rate.
|
||||
* Each model defines its own threshold in `premiumTokenValues`.
|
||||
* @param {string} valueKey
|
||||
* @param {string} tokenType
|
||||
* @param {number} [inputTokenCount]
|
||||
* @returns {number|null}
|
||||
*/
|
||||
const getPremiumRate = (valueKey, tokenType, inputTokenCount) => {
|
||||
if (inputTokenCount == null) {
|
||||
return null;
|
||||
}
|
||||
const premiumEntry = premiumTokenValues[valueKey];
|
||||
if (!premiumEntry || inputTokenCount <= premiumEntry.threshold) {
|
||||
return null;
|
||||
}
|
||||
return premiumEntry[tokenType] ?? null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the cache multiplier for a given value key and token type. If no value key is provided,
|
||||
* it attempts to derive it from the model name.
|
||||
|
|
@ -444,8 +490,10 @@ const getCacheMultiplier = ({ valueKey, cacheType, model, endpoint, endpointToke
|
|||
|
||||
module.exports = {
|
||||
tokenValues,
|
||||
premiumTokenValues,
|
||||
getValueKey,
|
||||
getMultiplier,
|
||||
getPremiumRate,
|
||||
getCacheMultiplier,
|
||||
defaultRate,
|
||||
cacheTokenValues,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
/** Note: No hard-coded values should be used in this file. */
|
||||
const { maxTokensMap } = require('@librechat/api');
|
||||
const { EModelEndpoint } = require('librechat-data-provider');
|
||||
const {
|
||||
|
|
@ -5,8 +6,10 @@ const {
|
|||
tokenValues,
|
||||
getValueKey,
|
||||
getMultiplier,
|
||||
getPremiumRate,
|
||||
cacheTokenValues,
|
||||
getCacheMultiplier,
|
||||
premiumTokenValues,
|
||||
} = require('./tx');
|
||||
|
||||
describe('getValueKey', () => {
|
||||
|
|
@ -239,6 +242,15 @@ describe('getMultiplier', () => {
|
|||
expect(getMultiplier({ valueKey: '8k', tokenType: 'unknownType' })).toBe(defaultRate);
|
||||
});
|
||||
|
||||
it('should return defaultRate if valueKey does not exist in tokenValues', () => {
|
||||
expect(getMultiplier({ valueKey: 'non-existent-model', tokenType: 'prompt' })).toBe(
|
||||
defaultRate,
|
||||
);
|
||||
expect(getMultiplier({ valueKey: 'non-existent-model', tokenType: 'completion' })).toBe(
|
||||
defaultRate,
|
||||
);
|
||||
});
|
||||
|
||||
it('should derive the valueKey from the model if not provided', () => {
|
||||
expect(getMultiplier({ tokenType: 'prompt', model: 'gpt-4-some-other-info' })).toBe(
|
||||
tokenValues['8k'].prompt,
|
||||
|
|
@ -334,8 +346,6 @@ describe('getMultiplier', () => {
|
|||
expect(getMultiplier({ model: 'openai/gpt-5.1', tokenType: 'prompt' })).toBe(
|
||||
tokenValues['gpt-5.1'].prompt,
|
||||
);
|
||||
expect(tokenValues['gpt-5.1'].prompt).toBe(1.25);
|
||||
expect(tokenValues['gpt-5.1'].completion).toBe(10);
|
||||
});
|
||||
|
||||
it('should return the correct multiplier for gpt-5.2', () => {
|
||||
|
|
@ -348,8 +358,6 @@ describe('getMultiplier', () => {
|
|||
expect(getMultiplier({ model: 'openai/gpt-5.2', tokenType: 'prompt' })).toBe(
|
||||
tokenValues['gpt-5.2'].prompt,
|
||||
);
|
||||
expect(tokenValues['gpt-5.2'].prompt).toBe(1.75);
|
||||
expect(tokenValues['gpt-5.2'].completion).toBe(14);
|
||||
});
|
||||
|
||||
it('should return the correct multiplier for gpt-4o', () => {
|
||||
|
|
@ -815,8 +823,6 @@ describe('Deepseek Model Tests', () => {
|
|||
expect(getMultiplier({ model: 'deepseek-chat', tokenType: 'completion' })).toBe(
|
||||
tokenValues['deepseek-chat'].completion,
|
||||
);
|
||||
expect(tokenValues['deepseek-chat'].prompt).toBe(0.28);
|
||||
expect(tokenValues['deepseek-chat'].completion).toBe(0.42);
|
||||
});
|
||||
|
||||
it('should return correct pricing for deepseek-reasoner', () => {
|
||||
|
|
@ -826,8 +832,6 @@ describe('Deepseek Model Tests', () => {
|
|||
expect(getMultiplier({ model: 'deepseek-reasoner', tokenType: 'completion' })).toBe(
|
||||
tokenValues['deepseek-reasoner'].completion,
|
||||
);
|
||||
expect(tokenValues['deepseek-reasoner'].prompt).toBe(0.28);
|
||||
expect(tokenValues['deepseek-reasoner'].completion).toBe(0.42);
|
||||
});
|
||||
|
||||
it('should handle DeepSeek model name variations with provider prefixes', () => {
|
||||
|
|
@ -840,8 +844,8 @@ describe('Deepseek Model Tests', () => {
|
|||
modelVariations.forEach((model) => {
|
||||
const promptMultiplier = getMultiplier({ model, tokenType: 'prompt' });
|
||||
const completionMultiplier = getMultiplier({ model, tokenType: 'completion' });
|
||||
expect(promptMultiplier).toBe(0.28);
|
||||
expect(completionMultiplier).toBe(0.42);
|
||||
expect(promptMultiplier).toBe(tokenValues['deepseek-chat'].prompt);
|
||||
expect(completionMultiplier).toBe(tokenValues['deepseek-chat'].completion);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -860,13 +864,13 @@ describe('Deepseek Model Tests', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it('should return correct cache pricing values for DeepSeek models', () => {
|
||||
expect(cacheTokenValues['deepseek-chat'].write).toBe(0.28);
|
||||
expect(cacheTokenValues['deepseek-chat'].read).toBe(0.028);
|
||||
expect(cacheTokenValues['deepseek-reasoner'].write).toBe(0.28);
|
||||
expect(cacheTokenValues['deepseek-reasoner'].read).toBe(0.028);
|
||||
expect(cacheTokenValues['deepseek'].write).toBe(0.28);
|
||||
expect(cacheTokenValues['deepseek'].read).toBe(0.028);
|
||||
it('should have consistent cache pricing across DeepSeek model variants', () => {
|
||||
expect(cacheTokenValues['deepseek'].write).toBe(cacheTokenValues['deepseek-chat'].write);
|
||||
expect(cacheTokenValues['deepseek'].read).toBe(cacheTokenValues['deepseek-chat'].read);
|
||||
expect(cacheTokenValues['deepseek-reasoner'].write).toBe(
|
||||
cacheTokenValues['deepseek-chat'].write,
|
||||
);
|
||||
expect(cacheTokenValues['deepseek-reasoner'].read).toBe(cacheTokenValues['deepseek-chat'].read);
|
||||
});
|
||||
|
||||
it('should handle DeepSeek cache multipliers with model variations', () => {
|
||||
|
|
@ -875,8 +879,8 @@ describe('Deepseek Model Tests', () => {
|
|||
modelVariations.forEach((model) => {
|
||||
const writeMultiplier = getCacheMultiplier({ model, cacheType: 'write' });
|
||||
const readMultiplier = getCacheMultiplier({ model, cacheType: 'read' });
|
||||
expect(writeMultiplier).toBe(0.28);
|
||||
expect(readMultiplier).toBe(0.028);
|
||||
expect(writeMultiplier).toBe(cacheTokenValues['deepseek-chat'].write);
|
||||
expect(readMultiplier).toBe(cacheTokenValues['deepseek-chat'].read);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1876,6 +1880,201 @@ describe('Claude Model Tests', () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return correct prompt and completion rates for Claude Opus 4.6', () => {
|
||||
expect(getMultiplier({ model: 'claude-opus-4-6', tokenType: 'prompt' })).toBe(
|
||||
tokenValues['claude-opus-4-6'].prompt,
|
||||
);
|
||||
expect(getMultiplier({ model: 'claude-opus-4-6', tokenType: 'completion' })).toBe(
|
||||
tokenValues['claude-opus-4-6'].completion,
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle Claude Opus 4.6 model name variations', () => {
|
||||
const modelVariations = [
|
||||
'claude-opus-4-6',
|
||||
'claude-opus-4-6-20250801',
|
||||
'claude-opus-4-6-latest',
|
||||
'anthropic/claude-opus-4-6',
|
||||
'claude-opus-4-6/anthropic',
|
||||
'claude-opus-4-6-preview',
|
||||
];
|
||||
|
||||
modelVariations.forEach((model) => {
|
||||
const valueKey = getValueKey(model);
|
||||
expect(valueKey).toBe('claude-opus-4-6');
|
||||
expect(getMultiplier({ model, tokenType: 'prompt' })).toBe(
|
||||
tokenValues['claude-opus-4-6'].prompt,
|
||||
);
|
||||
expect(getMultiplier({ model, tokenType: 'completion' })).toBe(
|
||||
tokenValues['claude-opus-4-6'].completion,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return correct cache rates for Claude Opus 4.6', () => {
|
||||
expect(getCacheMultiplier({ model: 'claude-opus-4-6', cacheType: 'write' })).toBe(
|
||||
cacheTokenValues['claude-opus-4-6'].write,
|
||||
);
|
||||
expect(getCacheMultiplier({ model: 'claude-opus-4-6', cacheType: 'read' })).toBe(
|
||||
cacheTokenValues['claude-opus-4-6'].read,
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle Claude Opus 4.6 cache rates with model name variations', () => {
|
||||
const modelVariations = [
|
||||
'claude-opus-4-6',
|
||||
'claude-opus-4-6-20250801',
|
||||
'claude-opus-4-6-latest',
|
||||
'anthropic/claude-opus-4-6',
|
||||
'claude-opus-4-6/anthropic',
|
||||
'claude-opus-4-6-preview',
|
||||
];
|
||||
|
||||
modelVariations.forEach((model) => {
|
||||
expect(getCacheMultiplier({ model, cacheType: 'write' })).toBe(
|
||||
cacheTokenValues['claude-opus-4-6'].write,
|
||||
);
|
||||
expect(getCacheMultiplier({ model, cacheType: 'read' })).toBe(
|
||||
cacheTokenValues['claude-opus-4-6'].read,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Premium Token Pricing', () => {
|
||||
const premiumModel = 'claude-opus-4-6';
|
||||
const premiumEntry = premiumTokenValues[premiumModel];
|
||||
const { threshold } = premiumEntry;
|
||||
const belowThreshold = threshold - 1;
|
||||
const aboveThreshold = threshold + 1;
|
||||
const wellAboveThreshold = threshold * 2;
|
||||
|
||||
it('should have premium pricing defined for claude-opus-4-6', () => {
|
||||
expect(premiumEntry).toBeDefined();
|
||||
expect(premiumEntry.threshold).toBeDefined();
|
||||
expect(premiumEntry.prompt).toBeDefined();
|
||||
expect(premiumEntry.completion).toBeDefined();
|
||||
expect(premiumEntry.prompt).toBeGreaterThan(tokenValues[premiumModel].prompt);
|
||||
expect(premiumEntry.completion).toBeGreaterThan(tokenValues[premiumModel].completion);
|
||||
});
|
||||
|
||||
it('should return null from getPremiumRate when inputTokenCount is below threshold', () => {
|
||||
expect(getPremiumRate(premiumModel, 'prompt', belowThreshold)).toBeNull();
|
||||
expect(getPremiumRate(premiumModel, 'completion', belowThreshold)).toBeNull();
|
||||
expect(getPremiumRate(premiumModel, 'prompt', threshold)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return premium rate from getPremiumRate when inputTokenCount exceeds threshold', () => {
|
||||
expect(getPremiumRate(premiumModel, 'prompt', aboveThreshold)).toBe(premiumEntry.prompt);
|
||||
expect(getPremiumRate(premiumModel, 'completion', aboveThreshold)).toBe(
|
||||
premiumEntry.completion,
|
||||
);
|
||||
expect(getPremiumRate(premiumModel, 'prompt', wellAboveThreshold)).toBe(premiumEntry.prompt);
|
||||
});
|
||||
|
||||
it('should return null from getPremiumRate when inputTokenCount is undefined or null', () => {
|
||||
expect(getPremiumRate(premiumModel, 'prompt', undefined)).toBeNull();
|
||||
expect(getPremiumRate(premiumModel, 'prompt', null)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null from getPremiumRate for models without premium pricing', () => {
|
||||
expect(getPremiumRate('claude-opus-4-5', 'prompt', wellAboveThreshold)).toBeNull();
|
||||
expect(getPremiumRate('claude-sonnet-4', 'prompt', wellAboveThreshold)).toBeNull();
|
||||
expect(getPremiumRate('gpt-4o', 'prompt', wellAboveThreshold)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return standard rate from getMultiplier when inputTokenCount is below threshold', () => {
|
||||
expect(
|
||||
getMultiplier({
|
||||
model: premiumModel,
|
||||
tokenType: 'prompt',
|
||||
inputTokenCount: belowThreshold,
|
||||
}),
|
||||
).toBe(tokenValues[premiumModel].prompt);
|
||||
expect(
|
||||
getMultiplier({
|
||||
model: premiumModel,
|
||||
tokenType: 'completion',
|
||||
inputTokenCount: belowThreshold,
|
||||
}),
|
||||
).toBe(tokenValues[premiumModel].completion);
|
||||
});
|
||||
|
||||
it('should return premium rate from getMultiplier when inputTokenCount exceeds threshold', () => {
|
||||
expect(
|
||||
getMultiplier({
|
||||
model: premiumModel,
|
||||
tokenType: 'prompt',
|
||||
inputTokenCount: aboveThreshold,
|
||||
}),
|
||||
).toBe(premiumEntry.prompt);
|
||||
expect(
|
||||
getMultiplier({
|
||||
model: premiumModel,
|
||||
tokenType: 'completion',
|
||||
inputTokenCount: aboveThreshold,
|
||||
}),
|
||||
).toBe(premiumEntry.completion);
|
||||
});
|
||||
|
||||
it('should return standard rate from getMultiplier when inputTokenCount is exactly at threshold', () => {
|
||||
expect(
|
||||
getMultiplier({ model: premiumModel, tokenType: 'prompt', inputTokenCount: threshold }),
|
||||
).toBe(tokenValues[premiumModel].prompt);
|
||||
});
|
||||
|
||||
it('should return premium rate from getMultiplier when inputTokenCount is one above threshold', () => {
|
||||
expect(
|
||||
getMultiplier({ model: premiumModel, tokenType: 'prompt', inputTokenCount: aboveThreshold }),
|
||||
).toBe(premiumEntry.prompt);
|
||||
});
|
||||
|
||||
it('should not apply premium pricing to models without premium entries', () => {
|
||||
expect(
|
||||
getMultiplier({
|
||||
model: 'claude-opus-4-5',
|
||||
tokenType: 'prompt',
|
||||
inputTokenCount: wellAboveThreshold,
|
||||
}),
|
||||
).toBe(tokenValues['claude-opus-4-5'].prompt);
|
||||
expect(
|
||||
getMultiplier({
|
||||
model: 'claude-sonnet-4',
|
||||
tokenType: 'prompt',
|
||||
inputTokenCount: wellAboveThreshold,
|
||||
}),
|
||||
).toBe(tokenValues['claude-sonnet-4'].prompt);
|
||||
});
|
||||
|
||||
it('should use standard rate when inputTokenCount is not provided', () => {
|
||||
expect(getMultiplier({ model: premiumModel, tokenType: 'prompt' })).toBe(
|
||||
tokenValues[premiumModel].prompt,
|
||||
);
|
||||
expect(getMultiplier({ model: premiumModel, tokenType: 'completion' })).toBe(
|
||||
tokenValues[premiumModel].completion,
|
||||
);
|
||||
});
|
||||
|
||||
it('should apply premium pricing through getMultiplier with valueKey path', () => {
|
||||
const valueKey = getValueKey(premiumModel);
|
||||
expect(getMultiplier({ valueKey, tokenType: 'prompt', inputTokenCount: aboveThreshold })).toBe(
|
||||
premiumEntry.prompt,
|
||||
);
|
||||
expect(
|
||||
getMultiplier({ valueKey, tokenType: 'completion', inputTokenCount: aboveThreshold }),
|
||||
).toBe(premiumEntry.completion);
|
||||
});
|
||||
|
||||
it('should apply standard pricing through getMultiplier with valueKey path when below threshold', () => {
|
||||
const valueKey = getValueKey(premiumModel);
|
||||
expect(getMultiplier({ valueKey, tokenType: 'prompt', inputTokenCount: belowThreshold })).toBe(
|
||||
tokenValues[premiumModel].prompt,
|
||||
);
|
||||
expect(
|
||||
getMultiplier({ valueKey, tokenType: 'completion', inputTokenCount: belowThreshold }),
|
||||
).toBe(tokenValues[premiumModel].completion);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokens.ts and tx.js sync validation', () => {
|
||||
|
|
|
|||
|
|
@ -34,8 +34,7 @@
|
|||
},
|
||||
"homepage": "https://librechat.ai",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.71.0",
|
||||
"@anthropic-ai/vertex-sdk": "^0.14.0",
|
||||
"@anthropic-ai/vertex-sdk": "^0.14.3",
|
||||
"@aws-sdk/client-bedrock-runtime": "^3.980.0",
|
||||
"@aws-sdk/client-s3": "^3.980.0",
|
||||
"@aws-sdk/s3-request-presigner": "^3.758.0",
|
||||
|
|
@ -45,7 +44,7 @@
|
|||
"@google/genai": "^1.19.0",
|
||||
"@keyv/redis": "^4.3.3",
|
||||
"@langchain/core": "^0.3.80",
|
||||
"@librechat/agents": "^3.1.33",
|
||||
"@librechat/agents": "^3.1.36",
|
||||
"@librechat/api": "*",
|
||||
"@librechat/data-schemas": "*",
|
||||
"@microsoft/microsoft-graph-client": "^3.0.7",
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
/** Note: No hard-coded values should be used in this file. */
|
||||
const { EModelEndpoint } = require('librechat-data-provider');
|
||||
const {
|
||||
maxTokensMap,
|
||||
|
|
@ -626,41 +627,45 @@ describe('matchModelName', () => {
|
|||
describe('Meta Models Tests', () => {
|
||||
describe('getModelMaxTokens', () => {
|
||||
test('should return correct tokens for LLaMa 2 models', () => {
|
||||
expect(getModelMaxTokens('llama2')).toBe(4000);
|
||||
expect(getModelMaxTokens('llama2.70b')).toBe(4000);
|
||||
expect(getModelMaxTokens('llama2-13b')).toBe(4000);
|
||||
expect(getModelMaxTokens('llama2-70b')).toBe(4000);
|
||||
const llama2Tokens = maxTokensMap[EModelEndpoint.openAI]['llama2'];
|
||||
expect(getModelMaxTokens('llama2')).toBe(llama2Tokens);
|
||||
expect(getModelMaxTokens('llama2.70b')).toBe(llama2Tokens);
|
||||
expect(getModelMaxTokens('llama2-13b')).toBe(llama2Tokens);
|
||||
expect(getModelMaxTokens('llama2-70b')).toBe(llama2Tokens);
|
||||
});
|
||||
|
||||
test('should return correct tokens for LLaMa 3 models', () => {
|
||||
expect(getModelMaxTokens('llama3')).toBe(8000);
|
||||
expect(getModelMaxTokens('llama3.8b')).toBe(8000);
|
||||
expect(getModelMaxTokens('llama3.70b')).toBe(8000);
|
||||
expect(getModelMaxTokens('llama3-8b')).toBe(8000);
|
||||
expect(getModelMaxTokens('llama3-70b')).toBe(8000);
|
||||
const llama3Tokens = maxTokensMap[EModelEndpoint.openAI]['llama3'];
|
||||
expect(getModelMaxTokens('llama3')).toBe(llama3Tokens);
|
||||
expect(getModelMaxTokens('llama3.8b')).toBe(llama3Tokens);
|
||||
expect(getModelMaxTokens('llama3.70b')).toBe(llama3Tokens);
|
||||
expect(getModelMaxTokens('llama3-8b')).toBe(llama3Tokens);
|
||||
expect(getModelMaxTokens('llama3-70b')).toBe(llama3Tokens);
|
||||
});
|
||||
|
||||
test('should return correct tokens for LLaMa 3.1 models', () => {
|
||||
expect(getModelMaxTokens('llama3.1:8b')).toBe(127500);
|
||||
expect(getModelMaxTokens('llama3.1:70b')).toBe(127500);
|
||||
expect(getModelMaxTokens('llama3.1:405b')).toBe(127500);
|
||||
expect(getModelMaxTokens('llama3-1-8b')).toBe(127500);
|
||||
expect(getModelMaxTokens('llama3-1-70b')).toBe(127500);
|
||||
expect(getModelMaxTokens('llama3-1-405b')).toBe(127500);
|
||||
const llama31Tokens = maxTokensMap[EModelEndpoint.openAI]['llama3.1:8b'];
|
||||
expect(getModelMaxTokens('llama3.1:8b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('llama3.1:70b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('llama3.1:405b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('llama3-1-8b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('llama3-1-70b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('llama3-1-405b')).toBe(llama31Tokens);
|
||||
});
|
||||
|
||||
test('should handle partial matches for Meta models', () => {
|
||||
// Test with full model names
|
||||
expect(getModelMaxTokens('meta/llama3.1:405b')).toBe(127500);
|
||||
expect(getModelMaxTokens('meta/llama3.1:70b')).toBe(127500);
|
||||
expect(getModelMaxTokens('meta/llama3.1:8b')).toBe(127500);
|
||||
expect(getModelMaxTokens('meta/llama3-1-8b')).toBe(127500);
|
||||
const llama31Tokens = maxTokensMap[EModelEndpoint.openAI]['llama3.1:8b'];
|
||||
const llama3Tokens = maxTokensMap[EModelEndpoint.openAI]['llama3'];
|
||||
const llama2Tokens = maxTokensMap[EModelEndpoint.openAI]['llama2'];
|
||||
expect(getModelMaxTokens('meta/llama3.1:405b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('meta/llama3.1:70b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('meta/llama3.1:8b')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('meta/llama3-1-8b')).toBe(llama31Tokens);
|
||||
|
||||
// Test base versions
|
||||
expect(getModelMaxTokens('meta/llama3.1')).toBe(127500);
|
||||
expect(getModelMaxTokens('meta/llama3-1')).toBe(127500);
|
||||
expect(getModelMaxTokens('meta/llama3')).toBe(8000);
|
||||
expect(getModelMaxTokens('meta/llama2')).toBe(4000);
|
||||
expect(getModelMaxTokens('meta/llama3.1')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('meta/llama3-1')).toBe(llama31Tokens);
|
||||
expect(getModelMaxTokens('meta/llama3')).toBe(llama3Tokens);
|
||||
expect(getModelMaxTokens('meta/llama2')).toBe(llama2Tokens);
|
||||
});
|
||||
|
||||
test('should match Deepseek model variations', () => {
|
||||
|
|
@ -678,18 +683,33 @@ describe('Meta Models Tests', () => {
|
|||
);
|
||||
});
|
||||
|
||||
test('should return 128000 context tokens for all DeepSeek models', () => {
|
||||
expect(getModelMaxTokens('deepseek-chat')).toBe(128000);
|
||||
expect(getModelMaxTokens('deepseek-reasoner')).toBe(128000);
|
||||
expect(getModelMaxTokens('deepseek-r1')).toBe(128000);
|
||||
expect(getModelMaxTokens('deepseek-v3')).toBe(128000);
|
||||
expect(getModelMaxTokens('deepseek.r1')).toBe(128000);
|
||||
test('should return correct context tokens for all DeepSeek models', () => {
|
||||
const deepseekChatTokens = maxTokensMap[EModelEndpoint.openAI]['deepseek-chat'];
|
||||
expect(getModelMaxTokens('deepseek-chat')).toBe(deepseekChatTokens);
|
||||
expect(getModelMaxTokens('deepseek-reasoner')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek-reasoner'],
|
||||
);
|
||||
expect(getModelMaxTokens('deepseek-r1')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek-r1'],
|
||||
);
|
||||
expect(getModelMaxTokens('deepseek-v3')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek'],
|
||||
);
|
||||
expect(getModelMaxTokens('deepseek.r1')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek.r1'],
|
||||
);
|
||||
});
|
||||
|
||||
test('should handle DeepSeek models with provider prefixes', () => {
|
||||
expect(getModelMaxTokens('deepseek/deepseek-chat')).toBe(128000);
|
||||
expect(getModelMaxTokens('openrouter/deepseek-reasoner')).toBe(128000);
|
||||
expect(getModelMaxTokens('openai/deepseek-v3')).toBe(128000);
|
||||
expect(getModelMaxTokens('deepseek/deepseek-chat')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek-chat'],
|
||||
);
|
||||
expect(getModelMaxTokens('openrouter/deepseek-reasoner')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek-reasoner'],
|
||||
);
|
||||
expect(getModelMaxTokens('openai/deepseek-v3')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['deepseek'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -728,30 +748,38 @@ describe('Meta Models Tests', () => {
|
|||
const { getModelMaxOutputTokens } = require('@librechat/api');
|
||||
|
||||
test('should return correct max output tokens for deepseek-chat', () => {
|
||||
expect(getModelMaxOutputTokens('deepseek-chat')).toBe(8000);
|
||||
expect(getModelMaxOutputTokens('deepseek-chat', EModelEndpoint.openAI)).toBe(8000);
|
||||
expect(getModelMaxOutputTokens('deepseek-chat', EModelEndpoint.custom)).toBe(8000);
|
||||
const expected = maxOutputTokensMap[EModelEndpoint.openAI]['deepseek-chat'];
|
||||
expect(getModelMaxOutputTokens('deepseek-chat')).toBe(expected);
|
||||
expect(getModelMaxOutputTokens('deepseek-chat', EModelEndpoint.openAI)).toBe(expected);
|
||||
expect(getModelMaxOutputTokens('deepseek-chat', EModelEndpoint.custom)).toBe(expected);
|
||||
});
|
||||
|
||||
test('should return correct max output tokens for deepseek-reasoner', () => {
|
||||
expect(getModelMaxOutputTokens('deepseek-reasoner')).toBe(64000);
|
||||
expect(getModelMaxOutputTokens('deepseek-reasoner', EModelEndpoint.openAI)).toBe(64000);
|
||||
expect(getModelMaxOutputTokens('deepseek-reasoner', EModelEndpoint.custom)).toBe(64000);
|
||||
const expected = maxOutputTokensMap[EModelEndpoint.openAI]['deepseek-reasoner'];
|
||||
expect(getModelMaxOutputTokens('deepseek-reasoner')).toBe(expected);
|
||||
expect(getModelMaxOutputTokens('deepseek-reasoner', EModelEndpoint.openAI)).toBe(expected);
|
||||
expect(getModelMaxOutputTokens('deepseek-reasoner', EModelEndpoint.custom)).toBe(expected);
|
||||
});
|
||||
|
||||
test('should return correct max output tokens for deepseek-r1', () => {
|
||||
expect(getModelMaxOutputTokens('deepseek-r1')).toBe(64000);
|
||||
expect(getModelMaxOutputTokens('deepseek-r1', EModelEndpoint.openAI)).toBe(64000);
|
||||
const expected = maxOutputTokensMap[EModelEndpoint.openAI]['deepseek-r1'];
|
||||
expect(getModelMaxOutputTokens('deepseek-r1')).toBe(expected);
|
||||
expect(getModelMaxOutputTokens('deepseek-r1', EModelEndpoint.openAI)).toBe(expected);
|
||||
});
|
||||
|
||||
test('should return correct max output tokens for deepseek base pattern', () => {
|
||||
expect(getModelMaxOutputTokens('deepseek')).toBe(8000);
|
||||
expect(getModelMaxOutputTokens('deepseek-v3')).toBe(8000);
|
||||
const expected = maxOutputTokensMap[EModelEndpoint.openAI]['deepseek'];
|
||||
expect(getModelMaxOutputTokens('deepseek')).toBe(expected);
|
||||
expect(getModelMaxOutputTokens('deepseek-v3')).toBe(expected);
|
||||
});
|
||||
|
||||
test('should handle DeepSeek models with provider prefixes for max output tokens', () => {
|
||||
expect(getModelMaxOutputTokens('deepseek/deepseek-chat')).toBe(8000);
|
||||
expect(getModelMaxOutputTokens('openrouter/deepseek-reasoner')).toBe(64000);
|
||||
expect(getModelMaxOutputTokens('deepseek/deepseek-chat')).toBe(
|
||||
maxOutputTokensMap[EModelEndpoint.openAI]['deepseek-chat'],
|
||||
);
|
||||
expect(getModelMaxOutputTokens('openrouter/deepseek-reasoner')).toBe(
|
||||
maxOutputTokensMap[EModelEndpoint.openAI]['deepseek-reasoner'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -796,68 +824,90 @@ describe('Meta Models Tests', () => {
|
|||
describe('Grok Model Tests - Tokens', () => {
|
||||
describe('getModelMaxTokens', () => {
|
||||
test('should return correct tokens for Grok vision models', () => {
|
||||
expect(getModelMaxTokens('grok-2-vision-1212')).toBe(32768);
|
||||
expect(getModelMaxTokens('grok-2-vision')).toBe(32768);
|
||||
expect(getModelMaxTokens('grok-2-vision-latest')).toBe(32768);
|
||||
const grok2VisionTokens = maxTokensMap[EModelEndpoint.openAI]['grok-2-vision'];
|
||||
expect(getModelMaxTokens('grok-2-vision-1212')).toBe(grok2VisionTokens);
|
||||
expect(getModelMaxTokens('grok-2-vision')).toBe(grok2VisionTokens);
|
||||
expect(getModelMaxTokens('grok-2-vision-latest')).toBe(grok2VisionTokens);
|
||||
});
|
||||
|
||||
test('should return correct tokens for Grok beta models', () => {
|
||||
expect(getModelMaxTokens('grok-vision-beta')).toBe(8192);
|
||||
expect(getModelMaxTokens('grok-beta')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-vision-beta')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-vision-beta'],
|
||||
);
|
||||
expect(getModelMaxTokens('grok-beta')).toBe(maxTokensMap[EModelEndpoint.openAI]['grok-beta']);
|
||||
});
|
||||
|
||||
test('should return correct tokens for Grok text models', () => {
|
||||
expect(getModelMaxTokens('grok-2-1212')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-2')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-2-latest')).toBe(131072);
|
||||
const grok2Tokens = maxTokensMap[EModelEndpoint.openAI]['grok-2'];
|
||||
expect(getModelMaxTokens('grok-2-1212')).toBe(grok2Tokens);
|
||||
expect(getModelMaxTokens('grok-2')).toBe(grok2Tokens);
|
||||
expect(getModelMaxTokens('grok-2-latest')).toBe(grok2Tokens);
|
||||
});
|
||||
|
||||
test('should return correct tokens for Grok 3 series models', () => {
|
||||
expect(getModelMaxTokens('grok-3')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-3-fast')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-3-mini')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-3-mini-fast')).toBe(131072);
|
||||
expect(getModelMaxTokens('grok-3')).toBe(maxTokensMap[EModelEndpoint.openAI]['grok-3']);
|
||||
expect(getModelMaxTokens('grok-3-fast')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-3-fast'],
|
||||
);
|
||||
expect(getModelMaxTokens('grok-3-mini')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-3-mini'],
|
||||
);
|
||||
expect(getModelMaxTokens('grok-3-mini-fast')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-3-mini-fast'],
|
||||
);
|
||||
});
|
||||
|
||||
test('should return correct tokens for Grok 4 model', () => {
|
||||
expect(getModelMaxTokens('grok-4-0709')).toBe(256000);
|
||||
expect(getModelMaxTokens('grok-4-0709')).toBe(maxTokensMap[EModelEndpoint.openAI]['grok-4']);
|
||||
});
|
||||
|
||||
test('should return correct tokens for Grok 4 Fast and Grok 4.1 Fast models', () => {
|
||||
expect(getModelMaxTokens('grok-4-fast')).toBe(2000000);
|
||||
expect(getModelMaxTokens('grok-4-1-fast-reasoning')).toBe(2000000);
|
||||
expect(getModelMaxTokens('grok-4-1-fast-non-reasoning')).toBe(2000000);
|
||||
const grok4FastTokens = maxTokensMap[EModelEndpoint.openAI]['grok-4-fast'];
|
||||
const grok41FastTokens = maxTokensMap[EModelEndpoint.openAI]['grok-4-1-fast'];
|
||||
expect(getModelMaxTokens('grok-4-fast')).toBe(grok4FastTokens);
|
||||
expect(getModelMaxTokens('grok-4-1-fast-reasoning')).toBe(grok41FastTokens);
|
||||
expect(getModelMaxTokens('grok-4-1-fast-non-reasoning')).toBe(grok41FastTokens);
|
||||
});
|
||||
|
||||
test('should return correct tokens for Grok Code Fast model', () => {
|
||||
expect(getModelMaxTokens('grok-code-fast-1')).toBe(256000);
|
||||
expect(getModelMaxTokens('grok-code-fast-1')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-code-fast'],
|
||||
);
|
||||
});
|
||||
|
||||
test('should handle partial matches for Grok models with prefixes', () => {
|
||||
// Vision models should match before general models
|
||||
expect(getModelMaxTokens('xai/grok-2-vision-1212')).toBe(32768);
|
||||
expect(getModelMaxTokens('xai/grok-2-vision')).toBe(32768);
|
||||
expect(getModelMaxTokens('xai/grok-2-vision-latest')).toBe(32768);
|
||||
// Beta models
|
||||
expect(getModelMaxTokens('xai/grok-vision-beta')).toBe(8192);
|
||||
expect(getModelMaxTokens('xai/grok-beta')).toBe(131072);
|
||||
// Text models
|
||||
expect(getModelMaxTokens('xai/grok-2-1212')).toBe(131072);
|
||||
expect(getModelMaxTokens('xai/grok-2')).toBe(131072);
|
||||
expect(getModelMaxTokens('xai/grok-2-latest')).toBe(131072);
|
||||
// Grok 3 models
|
||||
expect(getModelMaxTokens('xai/grok-3')).toBe(131072);
|
||||
expect(getModelMaxTokens('xai/grok-3-fast')).toBe(131072);
|
||||
expect(getModelMaxTokens('xai/grok-3-mini')).toBe(131072);
|
||||
expect(getModelMaxTokens('xai/grok-3-mini-fast')).toBe(131072);
|
||||
// Grok 4 model
|
||||
expect(getModelMaxTokens('xai/grok-4-0709')).toBe(256000);
|
||||
// Grok 4 Fast and 4.1 Fast models
|
||||
expect(getModelMaxTokens('xai/grok-4-fast')).toBe(2000000);
|
||||
expect(getModelMaxTokens('xai/grok-4-1-fast-reasoning')).toBe(2000000);
|
||||
expect(getModelMaxTokens('xai/grok-4-1-fast-non-reasoning')).toBe(2000000);
|
||||
// Grok Code Fast model
|
||||
expect(getModelMaxTokens('xai/grok-code-fast-1')).toBe(256000);
|
||||
const grok2VisionTokens = maxTokensMap[EModelEndpoint.openAI]['grok-2-vision'];
|
||||
const grokVisionBetaTokens = maxTokensMap[EModelEndpoint.openAI]['grok-vision-beta'];
|
||||
const grokBetaTokens = maxTokensMap[EModelEndpoint.openAI]['grok-beta'];
|
||||
const grok2Tokens = maxTokensMap[EModelEndpoint.openAI]['grok-2'];
|
||||
const grok3Tokens = maxTokensMap[EModelEndpoint.openAI]['grok-3'];
|
||||
const grok4Tokens = maxTokensMap[EModelEndpoint.openAI]['grok-4'];
|
||||
const grok4FastTokens = maxTokensMap[EModelEndpoint.openAI]['grok-4-fast'];
|
||||
const grok41FastTokens = maxTokensMap[EModelEndpoint.openAI]['grok-4-1-fast'];
|
||||
const grokCodeFastTokens = maxTokensMap[EModelEndpoint.openAI]['grok-code-fast'];
|
||||
expect(getModelMaxTokens('xai/grok-2-vision-1212')).toBe(grok2VisionTokens);
|
||||
expect(getModelMaxTokens('xai/grok-2-vision')).toBe(grok2VisionTokens);
|
||||
expect(getModelMaxTokens('xai/grok-2-vision-latest')).toBe(grok2VisionTokens);
|
||||
expect(getModelMaxTokens('xai/grok-vision-beta')).toBe(grokVisionBetaTokens);
|
||||
expect(getModelMaxTokens('xai/grok-beta')).toBe(grokBetaTokens);
|
||||
expect(getModelMaxTokens('xai/grok-2-1212')).toBe(grok2Tokens);
|
||||
expect(getModelMaxTokens('xai/grok-2')).toBe(grok2Tokens);
|
||||
expect(getModelMaxTokens('xai/grok-2-latest')).toBe(grok2Tokens);
|
||||
expect(getModelMaxTokens('xai/grok-3')).toBe(grok3Tokens);
|
||||
expect(getModelMaxTokens('xai/grok-3-fast')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-3-fast'],
|
||||
);
|
||||
expect(getModelMaxTokens('xai/grok-3-mini')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-3-mini'],
|
||||
);
|
||||
expect(getModelMaxTokens('xai/grok-3-mini-fast')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['grok-3-mini-fast'],
|
||||
);
|
||||
expect(getModelMaxTokens('xai/grok-4-0709')).toBe(grok4Tokens);
|
||||
expect(getModelMaxTokens('xai/grok-4-fast')).toBe(grok4FastTokens);
|
||||
expect(getModelMaxTokens('xai/grok-4-1-fast-reasoning')).toBe(grok41FastTokens);
|
||||
expect(getModelMaxTokens('xai/grok-4-1-fast-non-reasoning')).toBe(grok41FastTokens);
|
||||
expect(getModelMaxTokens('xai/grok-code-fast-1')).toBe(grokCodeFastTokens);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -1062,6 +1112,56 @@ describe('Claude Model Tests', () => {
|
|||
expect(matchModelName(model, EModelEndpoint.anthropic)).toBe(expectedModel);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return correct context length for Claude Opus 4.6 (1M)', () => {
|
||||
expect(getModelMaxTokens('claude-opus-4-6', EModelEndpoint.anthropic)).toBe(
|
||||
maxTokensMap[EModelEndpoint.anthropic]['claude-opus-4-6'],
|
||||
);
|
||||
expect(getModelMaxTokens('claude-opus-4-6')).toBe(
|
||||
maxTokensMap[EModelEndpoint.anthropic]['claude-opus-4-6'],
|
||||
);
|
||||
});
|
||||
|
||||
it('should return correct max output tokens for Claude Opus 4.6 (128K)', () => {
|
||||
const { getModelMaxOutputTokens } = require('@librechat/api');
|
||||
expect(getModelMaxOutputTokens('claude-opus-4-6', EModelEndpoint.anthropic)).toBe(
|
||||
maxOutputTokensMap[EModelEndpoint.anthropic]['claude-opus-4-6'],
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle Claude Opus 4.6 model name variations', () => {
|
||||
const modelVariations = [
|
||||
'claude-opus-4-6',
|
||||
'claude-opus-4-6-20250801',
|
||||
'claude-opus-4-6-latest',
|
||||
'anthropic/claude-opus-4-6',
|
||||
'claude-opus-4-6/anthropic',
|
||||
'claude-opus-4-6-preview',
|
||||
];
|
||||
|
||||
modelVariations.forEach((model) => {
|
||||
const modelKey = findMatchingPattern(model, maxTokensMap[EModelEndpoint.anthropic]);
|
||||
expect(modelKey).toBe('claude-opus-4-6');
|
||||
expect(getModelMaxTokens(model, EModelEndpoint.anthropic)).toBe(
|
||||
maxTokensMap[EModelEndpoint.anthropic]['claude-opus-4-6'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should match model names correctly for Claude Opus 4.6', () => {
|
||||
const modelVariations = [
|
||||
'claude-opus-4-6',
|
||||
'claude-opus-4-6-20250801',
|
||||
'claude-opus-4-6-latest',
|
||||
'anthropic/claude-opus-4-6',
|
||||
'claude-opus-4-6/anthropic',
|
||||
'claude-opus-4-6-preview',
|
||||
];
|
||||
|
||||
modelVariations.forEach((model) => {
|
||||
expect(matchModelName(model, EModelEndpoint.anthropic)).toBe('claude-opus-4-6');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Moonshot/Kimi Model Tests', () => {
|
||||
|
|
@ -1329,44 +1429,80 @@ describe('Qwen3 Model Tests', () => {
|
|||
describe('GLM Model Tests (Zhipu AI)', () => {
|
||||
describe('getModelMaxTokens', () => {
|
||||
test('should return correct tokens for GLM models', () => {
|
||||
expect(getModelMaxTokens('glm-4.6')).toBe(200000);
|
||||
expect(getModelMaxTokens('glm-4.5v')).toBe(66000);
|
||||
expect(getModelMaxTokens('glm-4.5-air')).toBe(131000);
|
||||
expect(getModelMaxTokens('glm-4.5')).toBe(131000);
|
||||
expect(getModelMaxTokens('glm-4-32b')).toBe(128000);
|
||||
expect(getModelMaxTokens('glm-4')).toBe(128000);
|
||||
expect(getModelMaxTokens('glm4')).toBe(128000);
|
||||
expect(getModelMaxTokens('glm-4.6')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4.6']);
|
||||
expect(getModelMaxTokens('glm-4.5v')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4.5v']);
|
||||
expect(getModelMaxTokens('glm-4.5-air')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5-air'],
|
||||
);
|
||||
expect(getModelMaxTokens('glm-4.5')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4.5']);
|
||||
expect(getModelMaxTokens('glm-4-32b')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4-32b']);
|
||||
expect(getModelMaxTokens('glm-4')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4']);
|
||||
expect(getModelMaxTokens('glm4')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm4']);
|
||||
});
|
||||
|
||||
test('should handle partial matches for GLM models with provider prefixes', () => {
|
||||
expect(getModelMaxTokens('z-ai/glm-4.6')).toBe(200000);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.5')).toBe(131000);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.5-air')).toBe(131000);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.5v')).toBe(66000);
|
||||
expect(getModelMaxTokens('z-ai/glm-4-32b')).toBe(128000);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.6')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.6'],
|
||||
);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.5')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5'],
|
||||
);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.5-air')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5-air'],
|
||||
);
|
||||
expect(getModelMaxTokens('z-ai/glm-4.5v')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5v'],
|
||||
);
|
||||
expect(getModelMaxTokens('z-ai/glm-4-32b')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4-32b'],
|
||||
);
|
||||
|
||||
expect(getModelMaxTokens('zai/glm-4.6')).toBe(200000);
|
||||
expect(getModelMaxTokens('zai/glm-4.5')).toBe(131000);
|
||||
expect(getModelMaxTokens('zai/glm-4.5-air')).toBe(131000);
|
||||
expect(getModelMaxTokens('zai/glm-4.5v')).toBe(66000);
|
||||
expect(getModelMaxTokens('zai/glm-4.6')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4.6']);
|
||||
expect(getModelMaxTokens('zai/glm-4.5')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4.5']);
|
||||
expect(getModelMaxTokens('zai/glm-4.5-air')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5-air'],
|
||||
);
|
||||
expect(getModelMaxTokens('zai/glm-4.5v')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5v'],
|
||||
);
|
||||
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.6')).toBe(200000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5')).toBe(131000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5-Air')).toBe(131000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5V')).toBe(66000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4-32B-0414')).toBe(128000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.6')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.6'],
|
||||
);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5'],
|
||||
);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5-Air')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5-air'],
|
||||
);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5V')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5v'],
|
||||
);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4-32B-0414')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4-32b'],
|
||||
);
|
||||
});
|
||||
|
||||
test('should handle GLM model variations with suffixes', () => {
|
||||
expect(getModelMaxTokens('glm-4.6-fp8')).toBe(200000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.6-FP8')).toBe(200000);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5-Air-FP8')).toBe(131000);
|
||||
expect(getModelMaxTokens('glm-4.6-fp8')).toBe(maxTokensMap[EModelEndpoint.openAI]['glm-4.6']);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.6-FP8')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.6'],
|
||||
);
|
||||
expect(getModelMaxTokens('zai-org/GLM-4.5-Air-FP8')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5-air'],
|
||||
);
|
||||
});
|
||||
|
||||
test('should prioritize more specific GLM patterns', () => {
|
||||
expect(getModelMaxTokens('glm-4.5-air-custom')).toBe(131000);
|
||||
expect(getModelMaxTokens('glm-4.5-custom')).toBe(131000);
|
||||
expect(getModelMaxTokens('glm-4.5v-custom')).toBe(66000);
|
||||
expect(getModelMaxTokens('glm-4.5-air-custom')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5-air'],
|
||||
);
|
||||
expect(getModelMaxTokens('glm-4.5-custom')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5'],
|
||||
);
|
||||
expect(getModelMaxTokens('glm-4.5v-custom')).toBe(
|
||||
maxTokensMap[EModelEndpoint.openAI]['glm-4.5v'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue