compaction

package
v1.53.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//go:embed prompts/compaction-system.txt
	SystemPrompt string

	//go:embed prompts/compaction-user.txt
	UserPrompt string
)

Functions

func EstimateMessageTokens

func EstimateMessageTokens(msg *chat.Message) int64

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

func FirstIndexInBudget(messages []chat.Message, contextLimit int64) int

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

func ShouldCompact(inputTokens, outputTokens, addedTokens, contextLimit int64) bool

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

func SplitIndexForKeep(messages []chat.Message, maxTokens int64) int

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL