fix(engine): align computeCacheHitRatio JSDoc with stored tokenUsage format

This commit is contained in:
Matthew Greenberg
2026-05-10 16:03:31 -04:00
parent 74e2b0b69b
commit 1880db08d4

View File

@@ -92,15 +92,13 @@ export async function accumulateSessionTokenUsage(
}
/**
* Compute the cache hit ratio: the fraction of effective input tokens served
* from cache. Returns a number in [0, 1]. Useful for measuring the
* effectiveness of prompt caching optimizations.
* Compute the cache hit ratio: `cachedTokens / (inputTokens + cachedTokens)`.
* Returns a number in [0, 1], or 0 when both arguments are 0.
*
* @param inputTokens - Non-cached input tokens (NOT including cache-write tokens;
* use only `tokens.input` here, not `tokens.input + tokens.cacheWrite`)
* @param cachedTokens - Tokens read from cache (`tokens.cacheRead`)
* @returns Cache hit ratio in [0, 1] matching Anthropic console's `cache_read / input` metric,
* or 0 if no tokens used
* Compatible with stored `task.tokenUsage` fields: pass `inputTokens` (which
* includes cache-write tokens per `accumulateSessionTokenUsage`) and
* `cachedTokens` (cache-read tokens). Note this differs slightly from the
* Anthropic console metric, which excludes cache-write from the denominator.
*/
export function computeCacheHitRatio(
inputTokens: number,