File size: 1,497 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { Content } from '@google/genai';
import type { ContextEnvironment } from '../pipeline/environment.js';
import type { ConcreteNode } from '../graph/types.js';
import { debugLogger } from '../../utils/debugLogger.js';
export function performCalibration(
env: ContextEnvironment,
finalNodes: readonly ConcreteNode[],
finalContents: Content[],
) {
if (!env.renderOptions?.calibrateTokenCalculation) {
return;
}
void (async () => {
try {
const exactResp = await env.llmClient.countTokens({
contents: finalContents,
});
const exactTokens =
typeof exactResp.totalTokens === 'number' ? exactResp.totalTokens : 0;
const estimatedTokens =
env.tokenCalculator.calculateConcreteListTokens(finalNodes);
const delta = Math.abs(exactTokens - estimatedTokens);
const tolerance = Math.max(exactTokens, estimatedTokens) * 0.2; // 20% tolerance
env.tracer.logEvent('Render', 'Token Calibration Measurement', {
exactTokens,
estimatedTokens,
delta,
isWithinTolerance: delta <= tolerance,
});
if (delta > tolerance) {
debugLogger.error(
`[Token Calibration] Large deviation detected: exact ${exactTokens} vs estimated ${estimatedTokens} (delta: ${delta})`,
);
}
} catch {
// Ignore API failures during background calibration
}
})();
}
|