{"record":{"id":"15c8c225847756a6","repo":"plandex-ai/plandex","slug":"max-tokens-exceeded-before-adding-context","errorCode":null,"errorMessage":"Max tokens exceeded before adding context","messagePattern":"Max tokens exceeded before adding context","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/model/plan/tell_exec.go","lineNumber":247,"sourceCode":"\t\t\tcacheControl:        true,\n\t\t})\n\n\t\tif state.currentStage.PlanningPhase == shared.PlanningPhaseTasks {\n\t\t\tif req.AutoContext {\n\t\t\t\tmsg := types.ExtendedChatMessage{\n\t\t\t\t\tRole:    openai.ChatMessageRoleSystem,\n\t\t\t\t\tContent: []types.ExtendedChatMessagePart{},\n\t\t\t\t}\n\t\t\t\tfor _, part := range planStageSharedMsgs {\n\t\t\t\t\tmsg.Content = append(msg.Content, *part)\n\t\t\t\t}\n\t\t\t\tsharedMsgsTokens := model.GetMessagesTokenEstimate(msg)\n\n\t\t\t\ttokensRemaining := tentativeMaxTokens - (sharedMsgsTokens + tokensWithoutContext)\n\n\t\t\t\tif tokensRemaining < 0 {\n\t\t\t\t\tlog.Println(\"tokensRemaining is negative\")\n\t\t\t\t\tgo notify.NotifyErr(notify.SeverityError, fmt.Errorf(\"tokensRemaining is negative\"))\n\n\t\t\t\t\tactive.StreamDoneCh <- &shared.ApiError{\n\t\t\t\t\t\tType:   shared.ApiErrorTypeOther,\n\t\t\t\t\t\tStatus: http.StatusInternalServerError,\n\t\t\t\t\t\tMsg:    \"Max tokens exceeded before adding context\",\n\t\t\t\t\t}\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tplanningPhaseOnlyMsgs = state.formatModelContext(formatModelContextParams{\n\t\t\t\t\tincludeMaps:          false,\n\t\t\t\t\tsmartContextEnabled:  req.SmartContext,\n\t\t\t\t\tincludeApplyScript:   false, // already included in planStageSharedMsgs\n\t\t\t\t\tactiveOnly:           true,\n\t\t\t\t\tactivatePaths:        activatePaths,\n\t\t\t\t\tactivatePathsOrdered: activatePathsOrdered,\n\t\t\t\t\tmaxTokens:            int(float64(tokensRemaining) * 0.95), // leave a little extra room\n\t\t\t\t})","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/model/plan/tell_exec.go#L229-L265","documentation":"Before adding plan context to the prompt, execTellPlan estimates tokens: tokensRemaining = tentativeMaxTokens - (sharedMsgsTokens + tokensWithoutContext). If tokensRemaining is negative, the base messages already exceed the stage's max token budget before context is even added, and a 500 ApiError \"Max tokens exceeded before adding context\" is sent. This is a guard against sending an oversized prompt to the LLM.","triggerScenarios":"The sum of system/shared message tokens plus tokensWithoutContext exceeds tentativeMaxTokens for the current stage — a very large context map, huge custom system prompt/rules, or a model pack with a low GetCoderEffectiveMaxTokens / context max.","commonSituations":"Users loading many or very large files into plan context; custom model packs configured with an undersized maxTokens; switching to a smaller model via model pack change mid-plan; TellStage set to a stage whose model has a smaller budget than the accumulated conversation.","solutions":["Reduce plan context — remove unneeded files from the context map so token estimates fit the budget.","Increase the model pack's max tokens (GetModelPack / effective max tokens settings) or switch to a larger-context model.","Start a new plan/branch or clear the conversation if the accumulated shared messages are the bulk of the tokens.","Trim custom rules/system prompt files if they are inflating sharedMsgsTokens."],"exampleFix":"// before (model pack config)\n\"maxTokens\": 8192 // too small for loaded context\n// after\n\"maxTokens\": 131072 // sized to the model's real context window","handlingStrategy":"validation","validationCode":"// estimate before calling Tell; match the server formula\nestimated := model.GetMessagesTokenEstimate(msgs) + contextTokenEstimate\nif estimated >= modelPack.CoderEffectiveMaxTokens() {\n    return errors.New(\"context too large: trim plan context or raise maxTokens before telling\")\n}","typeGuard":null,"tryCatchPattern":"apiErr := <-active.StreamDoneCh\nif apiErr.Msg == \"Max tokens exceeded before adding context\" {\n    // reduce context and retry with a smaller prompt\n    plandexContextRemove(planId, \"large-file.txt\")\n    return retryTell(planId, branch)\n}","preventionTips":["Keep the plan context trimmed to files actually needed for the task.","Size model pack maxTokens to the model's real context window.","Watch token estimate warnings in the CLI before telling.","Avoid switching to smaller-context models on plans with heavy context."],"tags":["go","tokens","llm","context-limit"],"backgroundTag":"context-window-exceeded","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}