{"record":{"id":"0fcf981dce44b260","repo":"siyuan-note/siyuan","slug":"w-model-context-length-is-unknown","errorCode":null,"errorMessage":"%w: model context length is unknown","messagePattern":"%w: model context length is unknown","errorType":"error_code","errorClass":"errContextCannotBeCompacted","httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":764,"sourceCode":"\t\t\tif imageInputDisabled {\n\t\t\t\treturn downgradeImageInput(source)\n\t\t\t}\n\t\t\treturn source, false\n\t\t}\n\t\tcompactionErrorMessage := func(err error) string {\n\t\t\tif errors.Is(err, errContextCannotBeCompacted) || isContextOverflow(err) {\n\t\t\t\treturn kernelModel.Conf.Language(352)\n\t\t\t}\n\t\t\trequestMessages, _ := projectImageMessages(messages)\n\t\t\treturn getAgentRequestErrorMessage(err, requestMessages)\n\t\t}\n\n\t\tcompactContext := func(requestTools []openai.Tool, force bool) (bool, error) {\n\t\t\tif contextLimit <= 0 {\n\t\t\t\tif !force {\n\t\t\t\t\treturn false, nil\n\t\t\t\t}\n\t\t\t\treturn false, fmt.Errorf(\"%w: model context length is unknown\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\tinputBudget := contextInputBudget(contextLimit, maxCompletionTokens)\n\t\t\tif inputBudget <= 0 {\n\t\t\t\treturn false, fmt.Errorf(\"%w: no input budget remains\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\testimatedMessages, _ := projectImageMessages(messages)\n\t\t\tif !force && estimateProtocolRequestTokens(model, protocol, estimatedMessages, checkpointMsgs,\n\t\t\t\tcompaction, requestTools) <= inputBudget {\n\t\t\t\treturn false, nil\n\t\t\t}\n\n\t\t\tcoveredEntryCount := 0\n\t\t\tpreviousSummary := \"\"\n\t\t\tif compaction != nil {\n\t\t\t\tcoveredEntryCount = compaction.CoveredEntryCount\n\t\t\t\tpreviousSummary = compaction.Summary\n\t\t\t}\n\t\t\tcandidates := compactionCandidateEntryCounts(sessionEntries, coveredEntryCount, userEntryID)","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/agent.go#L746-L782","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Configure the model's context length in the AI provider/model settings so contextLimit > 0","Switch to a model whose context length is known/supported","If compaction was not required, note the non-forced path returns (false, nil) and the session continues without compaction"],"exampleFix":"// before\nmodel := openai.NewConfig(baseURL, apiKey, \"my-model\") // context length unknown\n// after\ncfg := openai.NewConfig(baseURL, apiKey, \"my-model\")\ncfg.ContextLength = 128000 // declare the model's context window","handlingStrategy":"validation","validationCode":"if contextLimit <= 0 {\n    return fmt.Errorf(\"cannot run agent: model %q has no context length configured\", model)\n}","typeGuard":null,"tryCatchPattern":"result, err := agent.Run(ctx, input)\nif errors.Is(err, errContextCannotBeCompacted) && strings.Contains(err.Error(), \"model context length is unknown\") {\n    // configure the model's context length or switch models, then retry once\n}","preventionTips":["Always declare the context length for custom/local OpenAI-compatible models","Validate AI provider config at startup so contextLimit > 0 before sessions start","Prefer models whose metadata the kernel already knows","After switching models, re-check context-length configuration"],"tags":["agent","compaction","context-window","configuration"],"backgroundTag":"missing-required-config-field","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}