Package com.tnsai.llm.cache
Record Class CacheUsage
java.lang.Object
java.lang.Record
com.tnsai.llm.cache.CacheUsage
Cache usage statistics from an LLM response.
CacheUsage contains metrics about prompt cache usage, including tokens read from cache and tokens written to cache.
Token Types
- Cache Read Tokens: Tokens served from cache (90% cheaper)
- Cache Creation Tokens: Tokens written to cache (25% more expensive)
Cost Calculation
// For Claude Sonnet ($3/1M input) Cache read cost = cache_read_tokens * $0.30 / 1M (90% savings) Cache write cost = cache_creation_tokens * $3.75 / 1M (25% premium) Regular cost = input_tokens * $3.00 / 1M
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCacheUsage(int cacheReadInputTokens, int cacheCreationInputTokens) Creates an instance of aCacheUsagerecord class. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the value of thecacheCreationInputTokensrecord component.intReturns the value of thecacheReadInputTokensrecord component.doublecalculateSavingsRatio(int totalInputTokens) Calculates savings ratio compared to non-cached request.final booleanIndicates whether some other object is "equal to" this one.booleanChecks if any cache was created.booleanChecks if any cache was used.final inthashCode()Returns a hash code value for this object.static CacheUsageof(int readTokens, int creationTokens) Creates cache usage from read and creation token counts.toString()Returns a string representation of this record class.intGets total cached tokens (read + creation).
-
Field Details
-
EMPTY
Empty cache usage (no caching).
-
-
Constructor Details
-
CacheUsage
public CacheUsage(int cacheReadInputTokens, int cacheCreationInputTokens) Creates an instance of aCacheUsagerecord class.- Parameters:
cacheReadInputTokens- the value for thecacheReadInputTokensrecord componentcacheCreationInputTokens- the value for thecacheCreationInputTokensrecord component
-
-
Method Details
-
of
Creates cache usage from read and creation token counts. -
hasCacheHit
public boolean hasCacheHit()Checks if any cache was used. -
hasCacheCreation
public boolean hasCacheCreation()Checks if any cache was created. -
totalCachedTokens
public int totalCachedTokens()Gets total cached tokens (read + creation). -
calculateSavingsRatio
public double calculateSavingsRatio(int totalInputTokens) Calculates savings ratio compared to non-cached request.Assumes:
- Cache reads are 90% cheaper (0.1x cost)
- Cache creations are 25% more expensive (1.25x cost)
- Parameters:
totalInputTokens- Total input tokens in the request- Returns:
- Savings ratio (0.0 = no savings, 1.0 = 100% savings)
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='. -
cacheReadInputTokens
public int cacheReadInputTokens()Returns the value of thecacheReadInputTokensrecord component.- Returns:
- the value of the
cacheReadInputTokensrecord component
-
cacheCreationInputTokens
public int cacheCreationInputTokens()Returns the value of thecacheCreationInputTokensrecord component.- Returns:
- the value of the
cacheCreationInputTokensrecord component
-