TokenUsage

@Serializable
data class TokenUsage(val inputTokens: Int = 0, val outputTokens: 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)
}

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)

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val total: Int

The tokens read and written together.

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.