mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-09-01 19:41:32 +00:00
📊 feat: Real-Time Context Window & Token Usage Tracking
This commit is contained in:
parent
197a1dc4e2
commit
2c7f5c38af
44 changed files with 2271 additions and 50 deletions
50
api/server/controllers/TokenConfigController.js
Normal file
50
api/server/controllers/TokenConfigController.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
const { logger } = require('@librechat/data-schemas');
|
||||
const { EModelEndpoint } = require('librechat-data-provider');
|
||||
const { buildTokenConfigMap, getTokenConfigKey, tokenConfigCache } = require('@librechat/api');
|
||||
const { getModelsConfig } = require('~/server/controllers/ModelController');
|
||||
const { getValueKey, getMultiplier, getCacheMultiplier } = require('~/models');
|
||||
|
||||
/**
|
||||
* Returns server-resolved context windows (and pricing when
|
||||
* `interface.contextCost` is enabled) for every configured model.
|
||||
* @param {ServerRequest} req
|
||||
* @param {ServerResponse} res
|
||||
*/
|
||||
async function tokenConfigController(req, res) {
|
||||
try {
|
||||
const appConfig = req.config;
|
||||
const includePricing = appConfig?.interfaceConfig?.contextCost === true;
|
||||
const modelsConfig = await getModelsConfig(req);
|
||||
|
||||
/** @type {Record<string, import('@librechat/api').EndpointTokenConfig | undefined>} */
|
||||
const endpointTokenConfigs = {};
|
||||
const customEndpoints = appConfig?.endpoints?.[EModelEndpoint.custom] ?? [];
|
||||
const cache = tokenConfigCache();
|
||||
for (const endpointConfig of customEndpoints) {
|
||||
const name = endpointConfig?.name;
|
||||
if (!name) {
|
||||
continue;
|
||||
}
|
||||
if (endpointConfig.tokenConfig != null) {
|
||||
endpointTokenConfigs[name] = endpointConfig.tokenConfig;
|
||||
continue;
|
||||
}
|
||||
const tokenKey = getTokenConfigKey(endpointConfig, name, req.user.id);
|
||||
const cached = await cache.get(tokenKey);
|
||||
if (cached) {
|
||||
endpointTokenConfigs[name] = cached;
|
||||
}
|
||||
}
|
||||
|
||||
const tokenConfigMap = buildTokenConfigMap(
|
||||
{ modelsConfig, endpointTokenConfigs, includePricing },
|
||||
{ getValueKey, getMultiplier, getCacheMultiplier },
|
||||
);
|
||||
res.json(tokenConfigMap);
|
||||
} catch (error) {
|
||||
logger.error('[tokenConfigController]', error);
|
||||
res.status(500).json({ error: 'Failed to resolve token config' });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = tokenConfigController;
|
||||
Loading…
Add table
Add a link
Reference in a new issue