siyuan-note/siyuan · error

agent context cannot be compacted enough

Error message

agent context cannot be compacted enough

What it means

errContextCannotBeCompacted signals that the agent's conversation context could not be shrunk below the model's input limit via compaction. It is returned by createProtocolCompactionSummary in two cases: the protocol cannot produce a summary at all, or (at agent.go:764) the model's context length is unknown so no compaction budget can be computed. The UI maps it (and context-overflow errors) to a localized message (Language 352).

Source

Thrown at kernel/agent/compaction.go:41

	"encoding/json"
	"errors"
	"fmt"
	"io"
	"strings"
	"time"

	"github.com/sashabaranov/go-openai"
	"github.com/siyuan-note/siyuan/kernel/util"
)

const (
	compactionVersion          = 1
	compactionSummaryMinTokens = 256
	compactionSummaryMaxTokens = 2048
	compactionSummaryOverhead  = 128
)

var errContextCannotBeCompacted = errors.New("agent context cannot be compacted enough")
var errCompactionSummaryEmpty = errors.New("agent compaction summary is empty")

func isContextOverflow(err error) bool {
	msg := err.Error()
	return strings.Contains(msg, "context_length_exceeded") ||
		strings.Contains(msg, "maximum context length") ||
		strings.Contains(msg, "reduce the length") ||
		strings.Contains(msg, "too many tokens") ||
		strings.Contains(msg, "input is too long") ||
		strings.Contains(msg, "exceeds the context window") ||
		strings.Contains(msg, "超出上下文") ||
		strings.Contains(msg, "上下文长度")
}

func cloneRuntimeCompaction(compaction *runtimeCompaction) *runtimeCompaction {
	if compaction == nil {
		return nil
	}

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Switch to or select a model whose context length is known (present in the context catalog)
  2. Refresh the model context catalog (see refreshModelsDevContextCatalog) so contextLimit can be resolved
  3. Manually trim the conversation: delete old messages/attachments or start a new session
  4. Lower maxCompletionTokens so the input budget is larger if the limit is marginally exceeded

Example fix

// before: model without known context limit
conf.Model = "my-custom-model" // contextWindow unknown
// after
conf.Model = "gpt-4o" // contextWindow known, budget computable
Defensive patterns

Strategy: fallback

Validate before calling

limit, known := modelContextLimit(conf.Model)
if !known { return errors.New("model context length unknown; choose a cataloged model before long sessions") }

Try / catch

if errors.Is(err, errContextCannotBeCompacted) || isContextOverflow(err) { showUserLocalizedMessage(kernelModel.Conf.Language(352)); return }

Prevention

When it happens

Trigger: Running agent compaction when the model reports/implies an unknown context limit (no contextWindow metadata), or when the compaction protocol cannot reduce the context far enough given compactionSummaryMinTokens/MaxTokens budgets.

Common situations: Using a custom or newly released model whose context length is not in the models.dev catalog; extremely long conversations that cannot fit even after summarization; offline usage where the context catalog was never refreshed.

Related errors


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