plandex-ai/plandex · error

Max tokens exceeded before adding conversation

Error message

Max tokens exceeded before adding conversation

What it means

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.

Source

Thrown at app/server/model/plan/tell_exec.go:667

		model.GetMessagesTokenEstimate(clone.messages...) +
			model.GetMessagesTokenEstimate(*promptMessage) +
			clone.latestSummaryTokens +
			model.TokensPerRequest

	var effectiveMaxTokens int
	if clone.currentStage.TellStage == shared.TellStagePlanning {
		if clone.currentStage.PlanningPhase == shared.PlanningPhaseContext {
			effectiveMaxTokens = clone.settings.GetArchitectEffectiveMaxTokens()
		} else {
			effectiveMaxTokens = clone.settings.GetPlannerEffectiveMaxTokens()
		}
	} else if clone.currentStage.TellStage == shared.TellStageImplementation {
		effectiveMaxTokens = clone.settings.GetCoderEffectiveMaxTokens()
	}

	if clone.tokensBeforeConvo > effectiveMaxTokens {
		log.Println("tokensBeforeConvo exceeds max tokens during dry run")
		go notify.NotifyErr(notify.SeverityError, fmt.Errorf("tokensBeforeConvo exceeds max tokens during dry run"))

		state.activePlan.StreamDoneCh <- &shared.ApiError{
			Type:   shared.ApiErrorTypeOther,
			Status: http.StatusInternalServerError,
			Msg:    "Max tokens exceeded before adding conversation",
		}
		return false, 0
	}

	if !clone.addConversationMessages() {
		return false, 0
	}

	clone.messages = append(clone.messages, *promptMessage)

	return true, model.GetMessagesTokenEstimate(clone.messages...) + model.TokensPerRequest
}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Compact/summarize plan context to reduce tokensBeforeConvo
  2. Remove some attached context files
  3. Switch to a model pack with a larger context window
  4. Raise the stage max-tokens setting

Example fix

// before
effectiveMaxTokens := settings.GetCoderEffectiveMaxTokens() // 8000
// after: choose a bigger-context model pack
settings.SetModelPack("claude-3-7-sonnet") // larger effective max tokens
Defensive patterns

Strategy: validation

Validate before calling

est := estimateTokens(sysPrompt) + estimateTokens(prompt) + latestSummaryTokens + tokensPerRequest
if est > settings.GetPlannerEffectiveMaxTokens() {
    return fmt.Errorf("dry run: ~%d tokens exceeds %d budget; compact context first", est, settings.GetPlannerEffectiveMaxTokens())
}

Try / catch

if apiErr := <-state.activePlan.StreamDoneCh; apiErr != nil && strings.Contains(apiErr.Msg, "Max tokens exceeded before adding conversation") {
    // trigger context compaction or switch to a larger-context model pack
}

Prevention

When it happens

Trigger: Dry-run token estimation on a clone state where tokensBeforeConvo (sys prompt + prompt + summary + overhead) exceeds GetPlannerEffectiveMaxTokens / GetCoderEffectiveMaxTokens depending on the current TellStage.

Common situations: Pre-flight check before a large prompt: huge attached context, oversized plan summary, or a model pack with a small context window.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/0ac712ddccf00aa9. Report an issue: GitHub.