siyuan-note/siyuan · error · errContextCannotBeCompacted

%w: model context length is unknown

Error message

%w: model context length is unknown

What it means

Inside the agent's compactContext closure, if contextLimit <= 0 (the model's context window length is unknown) and compaction is forced, the function returns fmt.Errorf wrapping errContextCannotBeCompacted with "model context length is unknown". The kernel cannot compute an input budget without a known context limit, so it refuses to compact rather than guessing.

Source

Thrown at kernel/agent/agent.go:764

			if imageInputDisabled {
				return downgradeImageInput(source)
			}
			return source, false
		}
		compactionErrorMessage := func(err error) string {
			if errors.Is(err, errContextCannotBeCompacted) || isContextOverflow(err) {
				return kernelModel.Conf.Language(352)
			}
			requestMessages, _ := projectImageMessages(messages)
			return getAgentRequestErrorMessage(err, requestMessages)
		}

		compactContext := func(requestTools []openai.Tool, force bool) (bool, error) {
			if contextLimit <= 0 {
				if !force {
					return false, nil
				}
				return false, fmt.Errorf("%w: model context length is unknown", errContextCannotBeCompacted)
			}
			inputBudget := contextInputBudget(contextLimit, maxCompletionTokens)
			if inputBudget <= 0 {
				return false, fmt.Errorf("%w: no input budget remains", errContextCannotBeCompacted)
			}
			estimatedMessages, _ := projectImageMessages(messages)
			if !force && estimateProtocolRequestTokens(model, protocol, estimatedMessages, checkpointMsgs,
				compaction, requestTools) <= inputBudget {
				return false, nil
			}

			coveredEntryCount := 0
			previousSummary := ""
			if compaction != nil {
				coveredEntryCount = compaction.CoveredEntryCount
				previousSummary = compaction.Summary
			}
			candidates := compactionCandidateEntryCounts(sessionEntries, coveredEntryCount, userEntryID)

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Configure the model's context length in the AI provider/model settings so contextLimit > 0
  2. Switch to a model whose context length is known/supported
  3. If compaction was not required, note the non-forced path returns (false, nil) and the session continues without compaction

Example fix

// before
model := openai.NewConfig(baseURL, apiKey, "my-model") // context length unknown
// after
cfg := openai.NewConfig(baseURL, apiKey, "my-model")
cfg.ContextLength = 128000 // declare the model's context window
Defensive patterns

Strategy: validation

Validate before calling

if contextLimit <= 0 {
    return fmt.Errorf("cannot run agent: model %q has no context length configured", model)
}

Try / catch

result, err := agent.Run(ctx, input)
if errors.Is(err, errContextCannotBeCompacted) && strings.Contains(err.Error(), "model context length is unknown") {
    // configure the model's context length or switch models, then retry once
}

Prevention

When it happens

Trigger: A forced compaction request (force=true) while the configured model does not report a context length (contextLimit <= 0), e.g. a custom/local model registered without its context-window metadata.

Common situations: Using a custom OpenAI-compatible endpoint or locally served model where the context length is not configured; after a model switch the new model lacks context metadata; long sessions hit forced compaction and reveal the missing metadata.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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