TokenUsage
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.
Properties
the part of inputTokens the provider reused from an earlier request.
the part of inputTokens the provider kept for later requests.
all the tokens the model read, reused or not.
the tokens the model wrote.
Functions
Returns these tokens and those of other added up. A call whose provider reports no usage has null, which adds nothing.