import { estimateTokens, estimateMessageTokens, needsCompaction, truncateToolResults, buildCompactPrompt} from "@open-gitagent/gitagent";// Estimate tokens in a stringconst tokens = estimateTokens("Hello world"); // ~3// Check if compaction needed (triggers at 75% of context window)// tokenEstimate is also returned and useful for loggingconst { needed, ratio, tokenEstimate } = needsCompaction(messages, 200000);if (needed) console.log(`Context at ${(ratio * 100).toFixed(0)}% — compaction needed`);// Truncate oversized tool results (keeps first + last half)const trimmed = truncateToolResults(messages, 10000);// Build a summarization prompt for the LLMconst prompt = buildCompactPrompt(messages);
Utility
Description
estimateTokens
Fast approximation: chars/4
needsCompaction
Triggers at 75% of model context window
truncateToolResults
Keeps first and last half of large results
buildCompactPrompt
Generates a prompt asking the LLM to summarize the conversation
Call needsCompaction before each turn in a long-running session and feed the output of buildCompactPrompt back into query() to keep token usage under control.