{"record":{"id":"d19b30b31128dec4","repo":"siyuan-note/siyuan","slug":"agent-context-cannot-be-compacted-enough-summary","errorCode":null,"errorMessage":"agent context cannot be compacted enough: summary input exceeds the model context","messagePattern":"agent context cannot be compacted enough: summary input exceeds the model context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":858,"sourceCode":"\t\t\t\t\tnextCompaction, compactionStateErr = newRuntimeResponseCompaction(\n\t\t\t\t\t\tsessionEntries, selectedEntryCount, responseOutput,\n\t\t\t\t\t\tcompactionOutputTokenCost(model, responseOutput, completionTokens))\n\t\t\t\t} else if compaction != nil && len(compaction.ResponseOutput) > 0 {\n\t\t\t\t\treturn false, compactErr\n\t\t\t\t} else {\n\t\t\t\t\tlogging.LogWarnf(\"responses compaction failed, fallback to summary: %s\", compactErr)\n\t\t\t\t}\n\t\t\t}\n\t\t\tif nextCompaction == nil {\n\t\t\t\tsource, sourceErr := buildCompactionSource(previousSummary, sourceMessages)\n\t\t\t\tif sourceErr != nil {\n\t\t\t\t\treturn false, fmt.Errorf(\"%w: build summary source: %v\", errContextCannotBeCompacted, sourceErr)\n\t\t\t\t}\n\t\t\t\tsummaryRequestMessages := compactionSummaryMessages(source)\n\t\t\t\tsummaryInputBudget := contextInputBudget(contextLimit, summaryMaxTokens)\n\t\t\t\tif summaryInputBudget <= 0 ||\n\t\t\t\t\testimateChatRequestTokens(model, summaryRequestMessages, nil) > summaryInputBudget {\n\t\t\t\t\treturn false, fmt.Errorf(\"%w: summary input exceeds the model context\", errContextCannotBeCompacted)\n\t\t\t\t}\n\t\t\t\tsummary, promptTokens, completionTokens, summaryErr := createProtocolCompactionSummary(\n\t\t\t\t\tctx, client, protocol, model, source, summaryMaxTokens, maxRetries, requestTimeout,\n\t\t\t\t\tstreamIdleTimeout, ch)\n\t\t\t\ttotalPrompt += promptTokens\n\t\t\t\ttotalCompletion += completionTokens\n\t\t\t\tif summaryErr != nil {\n\t\t\t\t\treturn false, summaryErr\n\t\t\t\t}\n\t\t\t\tnextCompaction, compactionStateErr = newRuntimeProtocolSummaryCompaction(\n\t\t\t\t\tsessionEntries, selectedEntryCount, summary, protocol)\n\t\t\t}\n\t\t\tif compactionStateErr != nil {\n\t\t\t\treturn false, fmt.Errorf(\n\t\t\t\t\t\"%w: build runtime state: %v\", errContextCannotBeCompacted, compactionStateErr)\n\t\t\t}\n\t\t\tnextMessages := checkpointMessagesToOpenAIWithSummary(\n\t\t\t\tselectedCheckpointMsgs, language, capabilities, nextCompaction)","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/agent.go#L840-L876","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nsummaryMaxTokens = 14000 // with contextLimit = 16384 the summary input budget <= 0\n// after\nsummaryMaxTokens = 2000 // leaves ample budget for the summary request input","handlingStrategy":"fallback","validationCode":"summaryBudget := contextInputBudget(contextLimit, summaryMaxTokens)\nif summaryBudget <= 0 {\n    return fmt.Errorf(\"summaryMaxTokens (%d) leaves no input budget under contextLimit (%d)\",\n        summaryMaxTokens, contextLimit)\n}\nif estimateChatRequestTokens(model, compactionSummaryMessages(source), nil) > summaryBudget {\n    return fmt.Errorf(\"compaction source too large for the summary request; compact earlier\")\n}","typeGuard":null,"tryCatchPattern":"if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&\n    strings.Contains(err.Error(), \"summary input exceeds the model context\") {\n    // switch to a larger-context model or lower summaryMaxTokens, then retry\n}","preventionTips":["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"],"tags":["agent","compaction","context-window","token-budget"],"backgroundTag":"payload-too-large","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}