{"record":{"id":"80d0f5470db0c6bb","repo":"siyuan-note/siyuan","slug":"agent-context-cannot-be-compacted-enough-compacte","errorCode":null,"errorMessage":"agent context cannot be compacted enough: compacted context still exceeds the input budget","messagePattern":"agent context cannot be compacted enough: compacted context still exceeds the input budget","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":880,"sourceCode":"\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)\n\t\t\testimatedNextMessages, _ := projectImageMessages(nextMessages)\n\t\t\tif estimateProtocolRequestTokens(model, protocol, estimatedNextMessages, selectedCheckpointMsgs,\n\t\t\t\tnextCompaction, requestTools) > inputBudget {\n\t\t\t\treturn false, fmt.Errorf(\"%w: compacted context still exceeds the input budget\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\tif err := saveRuntimeCompaction(sessionID, nextCompaction); err != nil {\n\t\t\t\treturn false, fmt.Errorf(\"%w: persist compaction: %v\", errContextCannotBeCompacted, err)\n\t\t\t}\n\t\t\tcompaction = nextCompaction\n\t\t\tcheckpointMsgs = selectedCheckpointMsgs\n\t\t\tmessages = nextMessages\n\t\t\treturn true, nil\n\t\t}\n\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn\n\t\t\tdefault:\n\t\t\t}\n\n\t\t\troundID := fmt.Sprintf(\"%s_%d\", turn.TurnID, modelRound)","sourceCodeStart":862,"sourceCodeEnd":898,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/agent.go#L862-L898","documentation":"This error is returned by the agent's iterative context-compaction loop when, even after applying a newly computed compaction, the estimated token count of the resulting OpenAI message list still exceeds the model's input budget. It wraps errContextCannotBeCompacted and signals that no further automatic shrinking can make the conversation fit.","triggerScenarios":"Occurs inside the compaction attempt path: after checkpointMessagesToOpenAIWithSummary builds the next message list and projectImageMessages estimates images, estimateProtocolRequestTokens(... ) > inputBudget still holds, so the loop aborts instead of persisting the compaction.","commonSituations":"Very long agent sessions with many large tool results; models with small context windows; checkpoints whose messages are individually huge so summarization cannot cut enough; image-heavy conversations where projected image tokens dominate.","solutions":["Start a new agent session instead of compacting the current one","Reduce the size of checkpoint messages (drop or truncate large tool outputs) before compaction","Switch to a model with a larger input context window","Lower image attachment counts/sizes so projected image tokens fit the budget","Raise the compaction aggressiveness so more history is summarized away"],"exampleFix":"// before: retrying the same compaction and hitting the error\nnext := compact(messages)\nrun(next) // agent context cannot be compacted enough\n// after: start a fresh session when compaction cannot fit\nif err := run(messages); errors.Is(err, errContextCannotBeCompacted) {\n    session = newSession(summaryOf(messages))\n}","handlingStrategy":"validation","validationCode":"if estimateProtocolRequestTokens(model, protocol, msgs, checkpoints, compaction, tools) >= inputBudget {\n    // start a new session or trim history before invoking the agent\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep individual tool outputs small; truncate before storing checkpoints","Prefer models with larger context windows for long agent sessions","Limit image attachments, which heavily inflate projected tokens","Start fresh sessions periodically instead of letting one session grow indefinitely"],"tags":["agent","context-window","compaction","token-limit"],"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"}