siyuan-note/siyuan · error

agent context cannot be compacted enough: compacted context

Error message

agent context cannot be compacted enough: compacted context still exceeds the input budget

What it means

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.

Source

Thrown at kernel/agent/agent.go:880

					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)
			estimatedNextMessages, _ := projectImageMessages(nextMessages)
			if estimateProtocolRequestTokens(model, protocol, estimatedNextMessages, selectedCheckpointMsgs,
				nextCompaction, requestTools) > inputBudget {
				return false, fmt.Errorf("%w: compacted context still exceeds the input budget", errContextCannotBeCompacted)
			}
			if err := saveRuntimeCompaction(sessionID, nextCompaction); err != nil {
				return false, fmt.Errorf("%w: persist compaction: %v", errContextCannotBeCompacted, err)
			}
			compaction = nextCompaction
			checkpointMsgs = selectedCheckpointMsgs
			messages = nextMessages
			return true, nil
		}

		for {
			select {
			case <-ctx.Done():
				return
			default:
			}

			roundID := fmt.Sprintf("%s_%d", turn.TurnID, modelRound)

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Start a new agent session instead of compacting the current one
  2. Reduce the size of checkpoint messages (drop or truncate large tool outputs) before compaction
  3. Switch to a model with a larger input context window
  4. Lower image attachment counts/sizes so projected image tokens fit the budget
  5. Raise the compaction aggressiveness so more history is summarized away

Example fix

// before: retrying the same compaction and hitting the error
next := compact(messages)
run(next) // agent context cannot be compacted enough
// after: start a fresh session when compaction cannot fit
if err := run(messages); errors.Is(err, errContextCannotBeCompacted) {
    session = newSession(summaryOf(messages))
}
Defensive patterns

Strategy: validation

Validate before calling

if estimateProtocolRequestTokens(model, protocol, msgs, checkpoints, compaction, tools) >= inputBudget {
    // start a new session or trim history before invoking the agent
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/80d0f5470db0c6bb. Report an issue: GitHub.