plandex-ai/plandex · error

Error updating plan tokens:

Error message

Error updating plan tokens: 

What it means

DeleteContextHandler returns this 500 when db.AddPlanContextTokens fails while subtracting the tokens of just-deleted contexts from the plan branch's token count. The contexts themselves were already removed and committed inside ExecRepoOperation, so the failure is in the follow-up token accounting write. The underlying database error is appended to the message and logged.

Source

Thrown at app/server/handlers/plans_context.go:426

		err = repo.GitAddAndCommit(branchName, commitMsg)

		if err != nil {
			return fmt.Errorf("error committing changes: %v", err)
		}

		return nil
	})

	if err != nil {
		log.Printf("Error deleting contexts: %v\n", err)
		http.Error(w, "Error deleting contexts: "+err.Error(), http.StatusInternalServerError)
		return
	}

	err = db.AddPlanContextTokens(planId, branchName, -removeTokens)
	if err != nil {
		log.Printf("Error updating plan tokens: %v\n", err)
		http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)
		return
	}

	res := shared.DeleteContextResponse{
		TokensRemoved: removeTokens,
		TotalTokens:   branch.ContextTokens - removeTokens,
		Msg:           commitMsg,
	}

	bytes, err := json.Marshal(res)

	if err != nil {
		log.Printf("Error marshalling response: %v\n", err)
		http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)
		return
	}

	log.Println("Successfully deleted contexts")

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check the server log for the underlying AddPlanContextTokens error appended to this message
  2. Verify the plan and branch still exist in the database (planId, branchName from the request)
  3. Confirm the Postgres connection configured for the server is healthy and not exhausted
  4. Retry the delete-context request; if it recurs, inspect plans/plan tokens table state for stale rows
Defensive patterns

Strategy: retry

Validate before calling

const res = await fetch(`${base}/plans/${planId}/contexts`, {method: 'DELETE'});
if (res.status === 404) throw new Error('Plan or branch no longer exists — refresh before deleting contexts');

Try / catch

for (let i = 0; i < 3; i++) {
  try {
    const res = await deletePlanContexts(planId, ids);
    return res;
  } catch (e) {
    if (i === 2 || !/Error updating plan tokens/.test(e.message)) throw e;
    await new Promise(r => setTimeout(r, 500 * 2 ** i));
  }
}

Prevention

When it happens

Trigger: A DELETE-context request removed contexts successfully, but the subsequent AddPlanContextTokens(planId, branchName, -removeTokens) call fails — typically because the plan/branch row was concurrently deleted or renamed, the database is down/unreachable, or the plans table write hits a lock/serialization conflict.

Common situations: Another client deleted the plan or branch between the context removal and the token update; Postgres connection pool exhaustion or outage; the branch name no longer matches a stored branch row; transient DB constraint or lock-timeout errors.

Related errors


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