{"record":{"id":"281aec872ef334a9","repo":"plandex-ai/plandex","slug":"token-limit-exceeded-before-adding-conversation","errorCode":null,"errorMessage":"Token limit exceeded before adding conversation","messagePattern":"Token limit exceeded before adding conversation","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/model/plan/tell_exec.go","lineNumber":341,"sourceCode":"\n\t// print out breakdown of token usage\n\tlog.Printf(\"Latest summary tokens: %d\\n\", state.latestSummaryTokens)\n\tlog.Printf(\"Total tokens before convo: %d\\n\", state.tokensBeforeConvo)\n\n\tvar effectiveMaxTokens int\n\tif state.currentStage.TellStage == shared.TellStagePlanning {\n\t\tif state.currentStage.PlanningPhase == shared.PlanningPhaseContext {\n\t\t\teffectiveMaxTokens = state.settings.GetArchitectEffectiveMaxTokens()\n\t\t} else {\n\t\t\teffectiveMaxTokens = state.settings.GetPlannerEffectiveMaxTokens()\n\t\t}\n\t} else if state.currentStage.TellStage == shared.TellStageImplementation {\n\t\teffectiveMaxTokens = state.settings.GetCoderEffectiveMaxTokens()\n\t}\n\n\tif state.tokensBeforeConvo > effectiveMaxTokens {\n\t\t// token limit already exceeded before adding conversation\n\t\terr := fmt.Errorf(\"token limit exceeded before adding conversation\")\n\t\tlog.Printf(\"Error: %v\\n\", err)\n\t\tgo notify.NotifyErr(notify.SeverityError, fmt.Errorf(\"token limit exceeded before adding conversation\"))\n\n\t\tactive.StreamDoneCh <- &shared.ApiError{\n\t\t\tType:   shared.ApiErrorTypeOther,\n\t\t\tStatus: http.StatusInternalServerError,\n\t\t\tMsg:    \"Token limit exceeded before adding conversation\",\n\t\t}\n\t\treturn\n\t}\n\n\tif !state.addConversationMessages() {\n\t\treturn\n\t}\n\n\t// add the prompt message to the end of the messages slice\n\tif promptMessage != nil {\n\t\tstate.messages = append(state.messages, *promptMessage)","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/model/plan/tell_exec.go#L323-L359","documentation":"After computing the conversation tokens, execTellPlan derives effectiveMaxTokens for the current stage (implementation stages use GetCoderEffectiveMaxTokens) and checks state.tokensBeforeConvo against it. If tokensBeforeConvo already exceeds the budget before the conversation is added, a 500 ApiError \"Token limit exceeded before adding conversation\" is sent. Like error 717, this prevents sending an over-budget prompt to the model, but it fires at the pre-conversation checkpoint.","triggerScenarios":"state.tokensBeforeConvo > effectiveMaxTokens at the pre-convo check in execTellPlan — i.e. system prompt + context tokens alone exceed the stage's effective max token budget, before any chat history is appended.","commonSituations":"Very large context map loaded into the plan; model switched to one with a smaller context window (small effective max tokens) mid-plan; oversized system prompt from custom rules; long-running plans whose accumulated prompt parts outgrew the configured budget.","solutions":["Remove files from the plan context or split work into a new plan to shrink tokensBeforeConvo.","Raise effectiveMaxTokens in the model pack or select a model with a larger context window.","Clear/reset the plan conversation (convo tokens are excluded here, so focus on context + system prompt size).","Trim custom rules and system prompt content if they dominate the pre-conversation token count."],"exampleFix":"// before\nplandex context rm huge-dataset.json\n// after (keep only what the task needs)\nplandex context rm src/generated/*.min.js # reduce context until it fits the model budget","handlingStrategy":"validation","validationCode":"// pre-flight token check mirroring the server: system+context must fit effectiveMaxTokens\nif plan.TokensBeforeConvoEstimate() > modelPack.CoderEffectiveMaxTokens() {\n    return errors.New(\"pre-conversation tokens exceed budget: trim context or use a larger-context model\")\n}","typeGuard":null,"tryCatchPattern":"apiErr := <-active.StreamDoneCh\nif apiErr.Msg == \"Token limit exceeded before adding conversation\" {\n    // shrink context first, then retry\n    plandexContextRemove(planId, \"large-file.txt\")\n    return retryTell(planId, branch)\n}","preventionTips":["Cap the plan context size well below the model's context window.","Select a model pack whose effective max tokens comfortably exceeds system prompt + context.","Split oversized plans into smaller, focused plans or branches.","Periodically clean accumulated context in long-lived plans."],"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"}