@tehreet/conduit
Version:
LLM API gateway with intelligent routing, robust process management, and health monitoring
131 lines • 3.27 kB
TypeScript
export interface TokenCountResult {
count: number;
method: 'tiktoken' | 'estimate' | 'cached';
}
export interface BatchTokenCountRequest {
id: string;
text: string;
model?: string;
}
export interface BatchTokenCountResult {
id: string;
count: number;
method: 'tiktoken' | 'estimate' | 'cached';
}
export interface TokenCountCache {
text: string;
model: string;
result: TokenCountResult;
timestamp: number;
}
export interface TokenCountOptions {
useCache?: boolean;
cacheMaxAge?: number;
enableBatching?: boolean;
batchSize?: number;
batchDelay?: number;
}
export declare class TokenCounter {
private encoders;
private cache;
private batchQueue;
private batchTimer;
private batchResolvers;
private options;
/**
* Count tokens for a given text and model
*/
countTokens(text: string, model?: string, options?: Partial<TokenCountOptions>): Promise<TokenCountResult>;
/**
* Count tokens for a message array
*/
countMessagesTokens(messages: Array<{
role: string;
content: string;
}>, model?: string): Promise<TokenCountResult>;
/**
* Simple token estimation (roughly 4 chars per token)
*/
private estimateTokens;
/**
* Map Claude model names to tiktoken models for approximation
*/
private mapToTiktokenModel;
/**
* Count tokens from streaming content
*/
createStreamCounter(model?: string): {
addChunk(chunk: string): void;
getTotal(): number;
};
/**
* Batch count tokens for multiple texts
*/
batchCountTokens(requests: BatchTokenCountRequest[], options?: Partial<TokenCountOptions>): Promise<BatchTokenCountResult[]>;
/**
* Count tokens with batching support (queued)
*/
countTokensBatched(text: string, model?: string, options?: Partial<TokenCountOptions>): Promise<TokenCountResult>;
/**
* Get model-specific token counting configuration
*/
getModelConfig(model: string): {
multiplier: number;
overhead: number;
};
/**
* Update token counter options
*/
updateOptions(options: Partial<TokenCountOptions>): void;
/**
* Get current options
*/
getOptions(): TokenCountOptions;
/**
* Clear token count cache
*/
clearCache(): void;
/**
* Get cache statistics
*/
getCacheStats(): {
size: number;
hitRate: number;
oldestEntry: Date | null;
newestEntry: Date | null;
};
/**
* Schedule batch processing
*/
private scheduleBatchProcessing;
/**
* Process the current batch queue
*/
private processBatch;
/**
* Get cached result
*/
private getCachedResult;
/**
* Set cached result
*/
private setCachedResult;
/**
* Generate cache key
*/
private getCacheKey;
/**
* Simple hash function for cache keys
*/
private simpleHash;
/**
* Clean up old cache entries
*/
private cleanupCache;
/**
* Clean up encoders to free memory
*/
cleanup(): void;
}
export declare const tokenCounter: TokenCounter;
//# sourceMappingURL=token-counter.d.ts.map