GLMPilot / packages /server /src /utils /token-counter.ts
E5K7's picture
Initial commit: Rebranded to GLMPilot and migrated to GLM-5 API
c2c8c8d
import { estimateTokens } from '@glmpilot/shared';
import { logger } from './logger.js';
/**
* Estimate token count and warn if approaching limits.
*/
export function countTokens(text: string, context?: string): number {
const count = estimateTokens(text);
if (count > 25000) {
logger.warn(`High token count: ~${count} tokens${context ? ` for ${context}` : ''}`);
}
return count;
}
/**
* Check if text would exceed a token limit.
*/
export function wouldExceedTokenLimit(text: string, limit: number): boolean {
return estimateTokens(text) > limit;
}