Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( //go:embed prompts/compaction-system.txt SystemPrompt string //go:embed prompts/compaction-user.txt UserPrompt string )
Functions ¶
func EstimateMessageTokens ¶
EstimateMessageTokens returns a rough token-count estimate for a single chat message based on its text length. This is intentionally conservative (overestimates) so that proactive compaction fires before we hit the limit.
The estimate accounts for message content, multi-content text parts, reasoning content, tool call arguments, and a small per-message overhead for role/metadata tokens.
func FirstIndexInBudget ¶ added in v1.53.0
FirstIndexInBudget returns the smallest index N such that messages[N:] fits within contextLimit, snapping to a user/assistant turn boundary. Used to truncate the conversation handed to the summarization model so the request itself doesn't blow the context window.
When the entire slice fits within contextLimit, the function returns the index of the earliest user/assistant message in the suffix — older tool-only messages (which can't legally start a conversation) are dropped. In the unusual case of a tool-only conversation with no user/asst turns, it returns len(messages); callers should treat that as "nothing to send" and skip the truncation.
func ShouldCompact ¶
ShouldCompact reports whether a session's context usage has crossed the compaction threshold. It returns true when the total token count (input + output + addedTokens) exceeds [contextThreshold] (90%) of contextLimit.
func SplitIndexForKeep ¶ added in v1.53.0
SplitIndexForKeep walks messages from the end and returns the earliest index whose suffix fits in maxTokens, snapping to user/assistant boundaries. All messages from the returned index onward are intended to be preserved verbatim across a compaction; messages before it are the candidates to summarize. Returns len(messages) when everything fits in the keep budget — i.e. compact everything.
The boundary snap matters for providers (notably Anthropic) that reject conversations starting on a tool-result message: by stopping the kept window on a user/assistant turn, we guarantee the kept suffix begins on a clean conversational turn.
Types ¶
This section is empty.