🩹 fix: Resolve Codex Findings for Context Usage Tracking

This commit is contained in:
Danny Avila 2026-06-10 23:43:24 -04:00
parent 698649eda5
commit e5ab7cf1b1
6 changed files with 42 additions and 16 deletions

View file

@ -1,5 +1,5 @@
const { logger } = require('@librechat/data-schemas');
const { EModelEndpoint } = require('librechat-data-provider');
const { EModelEndpoint, normalizeEndpointName } = require('librechat-data-provider');
const { buildTokenConfigMap, getTokenConfigKey, tokenConfigCache } = require('@librechat/api');
const { getModelsConfig } = require('~/server/controllers/ModelController');
const { getValueKey, getMultiplier, getCacheMultiplier } = require('~/models');
@ -21,7 +21,8 @@ async function tokenConfigController(req, res) {
const customEndpoints = appConfig?.endpoints?.[EModelEndpoint.custom] ?? [];
const cache = tokenConfigCache();
for (const endpointConfig of customEndpoints) {
const name = endpointConfig?.name;
/** Models config and the token-config cache key by the normalized name */
const name = normalizeEndpointName(endpointConfig?.name);
if (!name) {
continue;
}

View file

@ -114,11 +114,20 @@ class ModelEndHandler {
this.collectedUsage.push(taggedUsage);
if (this.emitUsage) {
/** Normalize Anthropic/Bedrock-style top-level cache fields into details */
const cache_creation =
taggedUsage.input_token_details?.cache_creation ??
taggedUsage.cache_creation_input_tokens;
const cache_read =
taggedUsage.input_token_details?.cache_read ?? taggedUsage.cache_read_input_tokens;
await this.emitUsage({
input_tokens: taggedUsage.input_tokens,
output_tokens: taggedUsage.output_tokens,
total_tokens: taggedUsage.total_tokens,
input_token_details: taggedUsage.input_token_details,
input_token_details:
cache_creation != null || cache_read != null
? { cache_creation, cache_read }
: undefined,
model: taggedUsage.model,
provider: taggedUsage.provider,
usage_type: taggedUsage.usage_type,

View file

@ -1,11 +1,12 @@
const express = require('express');
const requireJwtAuth = require('~/server/middleware/requireJwtAuth');
const configMiddleware = require('~/server/middleware/config/app');
const endpointController = require('~/server/controllers/EndpointController');
const tokenConfigController = require('~/server/controllers/TokenConfigController');
const router = express.Router();
/** Auth required for role/tenant-scoped endpoint config resolution. */
router.get('/', requireJwtAuth, endpointController);
router.get('/token-config', requireJwtAuth, tokenConfigController);
router.get('/token-config', requireJwtAuth, configMiddleware, tokenConfigController);
module.exports = router;