@juspay/neurolink
Version:
Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio
86 lines (85 loc) • 3.52 kB
TypeScript
/**
* Centralized token usage extraction utilities
* Handles multiple provider formats and optional fields
*
* Consolidates token extraction logic from:
* - GenerationHandler.ts
* - analytics.ts
* - streamAnalytics.ts
*/
import type { TokenUsage, RawUsageObject, TokenExtractionOptions } from "../types/index.js";
/**
* Extract input token count from various provider formats
* Priority: input > inputTokens > promptTokens
*/
export declare function extractInputTokens(usage: RawUsageObject): number;
/**
* Extract output token count from various provider formats
* Priority: output > outputTokens > completionTokens
*/
export declare function extractOutputTokens(usage: RawUsageObject): number;
/**
* Extract total token count from various provider formats
* Falls back to input + output if total is not provided
*/
export declare function extractTotalTokens(usage: RawUsageObject, input: number, output: number): number;
/**
* Extract reasoning/thinking token count from various provider formats
* Supports: reasoningTokens, thinkingTokens, reasoning_tokens, reasoning
*/
export declare function extractReasoningTokens(usage: RawUsageObject): number | undefined;
/**
* Extract cache creation token count from various provider formats
* Supports: cacheCreationInputTokens, cacheCreationTokens
*/
export declare function extractCacheCreationTokens(usage: RawUsageObject): number | undefined;
/**
* Extract cache read token count from various provider formats
* Supports: cacheReadInputTokens, cacheReadTokens
*/
export declare function extractCacheReadTokens(usage: RawUsageObject): number | undefined;
/**
* Calculate cache savings percentage
*
* This represents the percentage of input tokens served from cache.
* For Anthropic, cache read tokens cost 0.1x, so actual cost savings = cacheSavingsPercent * 0.9
* For other providers, cost savings may vary based on their cache pricing.
*
* @param cacheReadTokens Number of tokens read from cache
* @param inputTokens Number of non-cached input tokens
* @returns Percentage of tokens served from cache (0-100), or undefined if no cache usage
*/
export declare function calculateCacheSavingsPercent(cacheReadTokens: number | undefined, inputTokens: number): number | undefined;
/**
* Extract token usage from various provider response formats
*
* Handles multiple input formats:
* - BaseProvider normalized format (input/output/total)
* - AI SDK format (inputTokens/outputTokens/totalTokens)
* - OpenAI/Mistral format (promptTokens/completionTokens)
* - Nested usage objects
*
* Also extracts optional fields:
* - Cache creation and read tokens (Anthropic-style)
* - Reasoning/thinking tokens (OpenAI o1, Anthropic, Google)
* - Cache savings percentage
*
* @param result Raw usage object from provider response
* @param options Extraction options
* @returns Normalized TokenUsage object
*/
export declare function extractTokenUsage(result: RawUsageObject | undefined | null, options?: TokenExtractionOptions): TokenUsage;
/**
* Create a default/empty TokenUsage object
* Useful for error handling and fallback scenarios
*/
export declare function createEmptyTokenUsage(): TokenUsage;
/**
* Check if a TokenUsage object has any non-zero values
*/
export declare function hasTokenUsage(usage: TokenUsage): boolean;
/**
* Merge two TokenUsage objects by summing their values
* Useful for aggregating usage across multiple calls
*/
export declare function mergeTokenUsage(a: TokenUsage, b: TokenUsage): TokenUsage;