{"record":{"id":"0ac712ddccf00aa9","repo":"plandex-ai/plandex","slug":"max-tokens-exceeded-before-adding-conversation","errorCode":null,"errorMessage":"Max tokens exceeded before adding conversation","messagePattern":"Max tokens exceeded before adding conversation","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/model/plan/tell_exec.go","lineNumber":667,"sourceCode":"\t\tmodel.GetMessagesTokenEstimate(clone.messages...) +\n\t\t\tmodel.GetMessagesTokenEstimate(*promptMessage) +\n\t\t\tclone.latestSummaryTokens +\n\t\t\tmodel.TokensPerRequest\n\n\tvar effectiveMaxTokens int\n\tif clone.currentStage.TellStage == shared.TellStagePlanning {\n\t\tif clone.currentStage.PlanningPhase == shared.PlanningPhaseContext {\n\t\t\teffectiveMaxTokens = clone.settings.GetArchitectEffectiveMaxTokens()\n\t\t} else {\n\t\t\teffectiveMaxTokens = clone.settings.GetPlannerEffectiveMaxTokens()\n\t\t}\n\t} else if clone.currentStage.TellStage == shared.TellStageImplementation {\n\t\teffectiveMaxTokens = clone.settings.GetCoderEffectiveMaxTokens()\n\t}\n\n\tif clone.tokensBeforeConvo > effectiveMaxTokens {\n\t\tlog.Println(\"tokensBeforeConvo exceeds max tokens during dry run\")\n\t\tgo notify.NotifyErr(notify.SeverityError, fmt.Errorf(\"tokensBeforeConvo exceeds max tokens during dry run\"))\n\n\t\tstate.activePlan.StreamDoneCh <- &shared.ApiError{\n\t\t\tType:   shared.ApiErrorTypeOther,\n\t\t\tStatus: http.StatusInternalServerError,\n\t\t\tMsg:    \"Max tokens exceeded before adding conversation\",\n\t\t}\n\t\treturn false, 0\n\t}\n\n\tif !clone.addConversationMessages() {\n\t\treturn false, 0\n\t}\n\n\tclone.messages = append(clone.messages, *promptMessage)\n\n\treturn true, model.GetMessagesTokenEstimate(clone.messages...) + model.TokensPerRequest\n}\n","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/model/plan/tell_exec.go#L649-L685","documentation":"Dry-run counterpart of error 720: in dryRunCalculateTokensWithoutContext the cloned state's tokensBeforeConvo exceeds the stage's effective max tokens, so the dry run aborts before building the real request. It reports a 500 ApiError 'Max tokens exceeded before adding conversation' and notifies.","triggerScenarios":"Dry-run token estimation on a clone state where tokensBeforeConvo (sys prompt + prompt + summary + overhead) exceeds GetPlannerEffectiveMaxTokens / GetCoderEffectiveMaxTokens depending on the current TellStage.","commonSituations":"Pre-flight check before a large prompt: huge attached context, oversized plan summary, or a model pack with a small context window.","solutions":["Compact/summarize plan context to reduce tokensBeforeConvo","Remove some attached context files","Switch to a model pack with a larger context window","Raise the stage max-tokens setting"],"exampleFix":"// before\neffectiveMaxTokens := settings.GetCoderEffectiveMaxTokens() // 8000\n// after: choose a bigger-context model pack\nsettings.SetModelPack(\"claude-3-7-sonnet\") // larger effective max tokens\n","handlingStrategy":"validation","validationCode":"est := estimateTokens(sysPrompt) + estimateTokens(prompt) + latestSummaryTokens + tokensPerRequest\nif est > settings.GetPlannerEffectiveMaxTokens() {\n    return fmt.Errorf(\"dry run: ~%d tokens exceeds %d budget; compact context first\", est, settings.GetPlannerEffectiveMaxTokens())\n}","typeGuard":null,"tryCatchPattern":"if apiErr := <-state.activePlan.StreamDoneCh; apiErr != nil && strings.Contains(apiErr.Msg, \"Max tokens exceeded before adding conversation\") {\n    // trigger context compaction or switch to a larger-context model pack\n}","preventionTips":["Run context compaction before very large prompts","Trim attached files to what's needed for the current task","Track latestSummaryTokens growth on long-running plans","Use model packs with headroom above your max prompt size"],"tags":["tokens","dry-run","context-limit","plan"],"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-12T22:17:10.623Z"}