siyuan-note/siyuan · error
agent context cannot be compacted enough: summary input exce
Error message
agent context cannot be compacted enough: summary input exceeds the model context
What it means
Before calling the model to produce a compaction summary, compactContext checks the summary request itself: if summaryInputBudget (contextInputBudget with summaryMaxTokens) is <= 0, or the estimated token count of compactionSummaryMessages(source) exceeds that budget, it returns errContextCannotBeCompacted with "summary input exceeds the model context". Even the summarization request would not fit into the model's context window.
Source
Thrown at kernel/agent/agent.go:858
nextCompaction, compactionStateErr = newRuntimeResponseCompaction(
sessionEntries, selectedEntryCount, responseOutput,
compactionOutputTokenCost(model, responseOutput, completionTokens))
} else if compaction != nil && len(compaction.ResponseOutput) > 0 {
return false, compactErr
} else {
logging.LogWarnf("responses compaction failed, fallback to summary: %s", compactErr)
}
}
if nextCompaction == nil {
source, sourceErr := buildCompactionSource(previousSummary, sourceMessages)
if sourceErr != nil {
return false, fmt.Errorf("%w: build summary source: %v", errContextCannotBeCompacted, sourceErr)
}
summaryRequestMessages := compactionSummaryMessages(source)
summaryInputBudget := contextInputBudget(contextLimit, summaryMaxTokens)
if summaryInputBudget <= 0 ||
estimateChatRequestTokens(model, summaryRequestMessages, nil) > summaryInputBudget {
return false, fmt.Errorf("%w: summary input exceeds the model context", errContextCannotBeCompacted)
}
summary, promptTokens, completionTokens, summaryErr := createProtocolCompactionSummary(
ctx, client, protocol, model, source, summaryMaxTokens, maxRetries, requestTimeout,
streamIdleTimeout, ch)
totalPrompt += promptTokens
totalCompletion += completionTokens
if summaryErr != nil {
return false, summaryErr
}
nextCompaction, compactionStateErr = newRuntimeProtocolSummaryCompaction(
sessionEntries, selectedEntryCount, summary, protocol)
}
if compactionStateErr != nil {
return false, fmt.Errorf(
"%w: build runtime state: %v", errContextCannotBeCompacted, compactionStateErr)
}
nextMessages := checkpointMessagesToOpenAIWithSummary(
selectedCheckpointMsgs, language, capabilities, nextCompaction)View on GitHub (pinned to 8641553a1f)
Solutions
- Use a model with a larger context window so the summary request fits
- Reduce summaryMaxTokens so more input budget remains for the summary request
- Compact earlier/more often (before history grows this large) so the source stays small
- Manually start a new session rather than forcing compaction of an oversized one
Example fix
// before summaryMaxTokens = 14000 // with contextLimit = 16384 the summary input budget <= 0 // after summaryMaxTokens = 2000 // leaves ample budget for the summary request input
Defensive patterns
Strategy: fallback
Validate before calling
summaryBudget := contextInputBudget(contextLimit, summaryMaxTokens)
if summaryBudget <= 0 {
return fmt.Errorf("summaryMaxTokens (%d) leaves no input budget under contextLimit (%d)",
summaryMaxTokens, contextLimit)
}
if estimateChatRequestTokens(model, compactionSummaryMessages(source), nil) > summaryBudget {
return fmt.Errorf("compaction source too large for the summary request; compact earlier")
} Try / catch
if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&
strings.Contains(err.Error(), "summary input exceeds the model context") {
// switch to a larger-context model or lower summaryMaxTokens, then retry
} Prevention
- Keep summaryMaxTokens well below the context limit so the summary request fits
- Compact frequently enough that the source stays far below the context window
- Use large-context models for sessions expected to accumulate long history
- Monitor estimated source tokens before forcing compaction
When it happens
Trigger: The compaction source (previous summary + selected messages) is so large that estimateChatRequestTokens > summaryInputBudget, or summaryMaxTokens consumes the whole context leaving a non-positive summary input budget.
Common situations: Small-context model used for a session with very large accumulated history; compactionSummaryMinTokens/summaryMaxTokens configured too high relative to the context limit; a previous summary that has grown as large as the raw history.
Understand the failure class
Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.
Related errors
- %w: no input budget remains
- agent context cannot be compacted enough: recent messages ex
- %w: model context length is unknown
- %w: the current turn is too large
- agent context cannot be compacted enough: compacted context
AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11).
Data as JSON: /api/errors/d19b30b31128dec4.
Report an issue: GitHub.