UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

75 lines (74 loc) • 3.61 kB
/** * Centralized token usage extraction utilities * Handles multiple provider formats and optional fields * * Which field holds which count, in priority order, is data: * `usageFields` and `usageRebase` in src/lib/providers/rules/usage.json. */ import type { TokenUsage, RawUsageObject, TokenExtractionOptions } from "../types/index.js"; /** Input token count (usage.json `usageFields.input`), or 0. */ export declare function extractInputTokens(usage: RawUsageObject): number; /** Output token count (usage.json `usageFields.output`), or 0. */ export declare function extractOutputTokens(usage: RawUsageObject): number; /** * Total token count. A literal 0 next to non-zero components means the * provider omitted the figure, so only a positive total wins over the sum. */ export declare function extractTotalTokens(usage: RawUsageObject, input: number, output: number): number; /** Reasoning/thinking token count, or undefined when absent or zero. */ export declare function extractReasoningTokens(usage: RawUsageObject): number | undefined; /** Cache-write token count (non-overlapping convention), or undefined. */ export declare function extractCacheCreationTokens(usage: RawUsageObject): number | undefined; /** Cache-read token count (non-overlapping convention), or undefined. */ export declare function extractCacheReadTokens(usage: RawUsageObject): number | undefined; /** * Cache-read count in the OVERLAPPING convention (OpenAI / DeepSeek / NIM / * OpenAI-compatible): a SUBSET already included in the prompt count, so a * caller MUST subtract it from `input` to avoid double-counting. */ export declare function extractCachedInputTokensOverlapping(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;