Record Class CacheUsage

java.lang.Object
java.lang.Record
com.tnsai.llm.cache.CacheUsage

public record CacheUsage(int cacheReadInputTokens, int cacheCreationInputTokens) extends Record
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
    Modifier and Type
    Field
    Description
    static final CacheUsage
    Empty cache usage (no caching).
  • Constructor Summary

    Constructors
    Constructor
    Description
    CacheUsage(int cacheReadInputTokens, int cacheCreationInputTokens)
    Creates an instance of a CacheUsage record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the value of the cacheCreationInputTokens record component.
    int
    Returns the value of the cacheReadInputTokens record component.
    double
    calculateSavingsRatio(int totalInputTokens)
    Calculates savings ratio compared to non-cached request.
    final boolean
    Indicates whether some other object is "equal to" this one.
    boolean
    Checks if any cache was created.
    boolean
    Checks if any cache was used.
    final int
    Returns a hash code value for this object.
    static CacheUsage
    of(int readTokens, int creationTokens)
    Creates cache usage from read and creation token counts.
    Returns a string representation of this record class.
    int
    Gets total cached tokens (read + creation).

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY

      public static final CacheUsage EMPTY
      Empty cache usage (no caching).
  • Constructor Details

    • CacheUsage

      public CacheUsage(int cacheReadInputTokens, int cacheCreationInputTokens)
      Creates an instance of a CacheUsage record class.
      Parameters:
      cacheReadInputTokens - the value for the cacheReadInputTokens record component
      cacheCreationInputTokens - the value for the cacheCreationInputTokens record component
  • Method Details

    • of

      public static CacheUsage of(int readTokens, int creationTokens)
      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

      public String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • cacheReadInputTokens

      public int cacheReadInputTokens()
      Returns the value of the cacheReadInputTokens record component.
      Returns:
      the value of the cacheReadInputTokens record component
    • cacheCreationInputTokens

      public int cacheCreationInputTokens()
      Returns the value of the cacheCreationInputTokens record component.
      Returns:
      the value of the cacheCreationInputTokens record component