LLMChatSession
classin packageklyn.llm
public class LLMChatSession extends Object
└ LLMChatSession

High-level chat session with history, tool-calling and optional compaction. Mutable state belongs to one owner thread. Other threads consume published LLMContextUsage snapshots rather than reading this session concurrently.

Properties
Modifier and Type Member Description
public readonly property contextLengthTokens
contextLengthTokens as Int
Configured context length in tokens.
public readonly property contextTokens
contextTokens as Int
Current cached context occupancy, read in O(1) on the session owner thread.
public readonly property contextUsage
contextUsage as LLMContextUsage
Captures accounting in O(1), without reading mutable message history.
public readonly property contextUsagePercent
contextUsagePercent as Int
Last known context usage percentage, capped to 100.
public readonly property lastCompactionAfterUsage
lastCompactionAfterUsage as LLMContextUsage
Estimated replacement accounting before subsequent provider calls, or null.
public readonly property lastCompactionBeforeUsage
lastCompactionBeforeUsage as LLMContextUsage
Accounting immediately before the last compaction attempt, or null.
public readonly property lastCompletionTokens
lastCompletionTokens as Int
Last completion token count reported by the provider.
public readonly property lastPromptTokens
lastPromptTokens as Int
Last prompt token count reported by the provider.
public readonly property lastTotalTokens
lastTotalTokens as Int
Last total token count reported by the provider.
Inherited Properties
propertyInherited Properties from Object: type
Constructors
Modifier and Type Member Description
public LLMChatSession
LLMChatSession(provider as Object)
Creates a session bound to one provider.
Methods
Modifier and Type Member Description
public appendMessage
appendMessage(message as LLMThreadMessage) as Void
Appends and checkpoints an externally supplied message on the owner thread.
public ask
ask(prompt as String) as String throws Exception
Sends one user prompt and returns the final assistant answer.
public askMessage
askMessage(userMessage as LLMThreadMessage) as String throws Exception
Sends a user message with optional image and attachment references.
public compact
compact() as Boolean
Compacts the active context on demand using the configured compactor.
public consumeCompactionFlag
consumeCompactionFlag() as Boolean
Returns and clears the compaction flag.
public messages Returns a typed snapshot of the active context, not the uncompacted archive.
public thread
thread() as ArrayList
Returns the legacy mutable history on the session owner thread.
public withCompactionObserver
withCompactionObserver(observer as LLMCompactionObserver) as LLMChatSession
Attaches an observer notified before the context compactor starts.
public withCompactionProvider
withCompactionProvider(provider as LLMProvider) as LLMChatSession
Selects a separate transport for context summaries, keeping ordinary requests on the session provider.
public withCompactor
withCompactor(strategy as String, threshold as Int) as LLMChatSession
Configures context compaction.
public withContextWindow
withContextWindow(contextLengthTokens as Int, compactAtPercent as Int = 90) as LLMChatSession
Configures token-based context accounting.
public withHistoryObserver
withHistoryObserver(observer as LLMHistoryObserver) as LLMChatSession
Installs a synchronous history journal; null disables checkpoint callbacks.
public withMaxToolCalls
withMaxToolCalls(limit as Int) as LLMChatSession
Configures the maximum number of tool calls.
public withMessages
withMessages(messages as ArrayList<LLMThreadMessage>) as LLMChatSession
Restores an existing provider history without sharing its list container.
public withSystem
withSystem(systemPrompt as String) as LLMChatSession
Sets or replaces the system prompt.
public withToolCallObserver
withToolCallObserver(observer as LLMToolCallObserver) as LLMChatSession
Attaches an observer notified around every requested tool call.
public withTools
withTools(tools as ToolRegistry) as LLMChatSession
Attaches a tool registry.
Inherited Methods
methodInherited Methods from Object: deepCopy, fromJson, hash, toDict, toJson, toMap, toString, toXml