plandex-ai/plandex · error

Token limit exceeded before adding conversation

Error message

Token limit exceeded before adding conversation

What it means

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.

Source

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

	// print out breakdown of token usage
	log.Printf("Latest summary tokens: %d\n", state.latestSummaryTokens)
	log.Printf("Total tokens before convo: %d\n", state.tokensBeforeConvo)

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

	if state.tokensBeforeConvo > effectiveMaxTokens {
		// token limit already exceeded before adding conversation
		err := fmt.Errorf("token limit exceeded before adding conversation")
		log.Printf("Error: %v\n", err)
		go notify.NotifyErr(notify.SeverityError, fmt.Errorf("token limit exceeded before adding conversation"))

		active.StreamDoneCh <- &shared.ApiError{
			Type:   shared.ApiErrorTypeOther,
			Status: http.StatusInternalServerError,
			Msg:    "Token limit exceeded before adding conversation",
		}
		return
	}

	if !state.addConversationMessages() {
		return
	}

	// add the prompt message to the end of the messages slice
	if promptMessage != nil {
		state.messages = append(state.messages, *promptMessage)

View on GitHub (pinned to e2d772072e)

Solutions

  1. Remove files from the plan context or split work into a new plan to shrink tokensBeforeConvo.
  2. Raise effectiveMaxTokens in the model pack or select a model with a larger context window.
  3. Clear/reset the plan conversation (convo tokens are excluded here, so focus on context + system prompt size).
  4. Trim custom rules and system prompt content if they dominate the pre-conversation token count.

Example fix

// before
plandex context rm huge-dataset.json
// after (keep only what the task needs)
plandex context rm src/generated/*.min.js # reduce context until it fits the model budget
Defensive patterns

Strategy: validation

Validate before calling

// pre-flight token check mirroring the server: system+context must fit effectiveMaxTokens
if plan.TokensBeforeConvoEstimate() > modelPack.CoderEffectiveMaxTokens() {
    return errors.New("pre-conversation tokens exceed budget: trim context or use a larger-context model")
}

Try / catch

apiErr := <-active.StreamDoneCh
if apiErr.Msg == "Token limit exceeded before adding conversation" {
    // shrink context first, then retry
    plandexContextRemove(planId, "large-file.txt")
    return retryTell(planId, branch)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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