TokenUsage

@Serializable
data class TokenUsage(val inputTokens: Int = 0, val outputTokens: Int = 0, val cacheReadTokens: Int = 0, val cacheWriteTokens: Int = 0)

The tokens a call to a ChatModel read (inputTokens) and wrote (outputTokens), or those of several calls added up. TokenUsage() is no tokens, the start of a count:

data class Order(val messages: List<ChatMessage>, val tokens: TokenUsage = TokenUsage())

node("summarize", work = { order -> model.chat(ChatRequest(order.messages)) }) { order, response ->
order.copy(messages = order.messages + response.message, tokens = order.tokens + response.usage)
}

A provider that reuses what it read before charges less for those tokens, and charges more for the ones it keeps for later. They are part of inputTokens, so what was read at the full price is inputTokens - cacheReadTokens - cacheWriteTokens.

It is @Serializable, so a state that holds one can be saved by a checkpointer that uses kotlinx.serialization.

Constructors

Link copied to clipboard
constructor(inputTokens: Int = 0, outputTokens: Int = 0, cacheReadTokens: Int = 0, cacheWriteTokens: Int = 0)

Properties

Link copied to clipboard

the part of inputTokens the provider reused from an earlier request.

Link copied to clipboard

the part of inputTokens the provider kept for later requests.

Link copied to clipboard

all the tokens the model read, reused or not.

Link copied to clipboard

the tokens the model wrote.

Link copied to clipboard
val total: Int

The tokens read and written together, reused tokens included.

Functions

Link copied to clipboard
operator fun plus(other: TokenUsage?): TokenUsage

Returns these tokens and those of other added up. A call whose provider reports no usage has null, which adds nothing.