LLMContextUsage
classin packageklyn.llm
public class LLMContextUsage
LLMContextUsage

Immutable accounting snapshot, safe to publish from a session's owner thread. Reported usage describes the last provider request; an estimate describes retained messages plus tools. Neither promises exact future tokenization.

Example

print(session.contextUsage.tokens)

Fields
Modifier and Type Member Description
public readonly estimated
estimated as Boolean
True when any component is estimated.
public readonly estimatedToolTokens
estimatedToolTokens as Int
Estimated tool-schema budget, for restoring a gauge without rebuilding tools.
public readonly tokens
tokens as Int
Occupied tokens, bounded to Int.MAX_VALUE.
Constructors
Modifier and Type Member Description
public LLMContextUsage
LLMContextUsage(tokens as Int, estimated as Boolean, estimatedToolTokens as Int = 0)
Captures accounting without retaining mutable messages.
Methods
Modifier and Type Member Description
public static bound
bound(tokens as Long) as Int
Bounds an accumulated estimate without integer overflow.
public static estimateContext
estimateContext(messages as ArrayList<LLMThreadMessage>, toolTokens as Int = 0) as Int
Estimates a restored context once, using its last known tool budget.
public static estimateMessage
estimateMessage(message as LLMThreadMessage, imageTokens as Int = 1024, includeAttachments as Boolean = true) as Int
Estimates text at four characters per token, with message framing.
public static messageCharacters
messageCharacters(message as LLMThreadMessage) as Long
Estimates message text and structured calls, including null content.