plandex-ai/plandex · error
too many contexts: %d
Error message
too many contexts: %d
What it means
LoadContexts enforces the server-side cap shared.MaxContextCount on how many contexts a plan branch may hold. It sums existing contexts plus the incoming request batch and errors if the total would exceed the limit. The client normally enforces this first, so hitting it server-side means the client-side guard was bypassed or raced with concurrent loads.
Source
Thrown at app/server/db/context_helpers_load.go:105
planConfig, err := GetPlanConfig(planId)
if err != nil {
return nil, nil, fmt.Errorf("error getting plan config: %v", err)
}
plannerMaxTokens := settings.GetPlannerEffectiveMaxTokens()
contextLoaderMaxTokens := settings.GetArchitectEffectiveMaxTokens()
mapContextsByFilePath := make(map[string]Context)
existingContexts, err := GetPlanContexts(orgId, planId, false, false)
if err != nil {
return nil, nil, fmt.Errorf("error getting existing contexts: %v", err)
}
// check overall context limits - these should be getting enforced by the client, so just error out if exceeded
numExistingContexts := len(existingContexts)
if numExistingContexts+len(*req) > shared.MaxContextCount {
return nil, nil, fmt.Errorf("too many contexts: %d", numExistingContexts+len(*req))
}
var totalContextSize int64
for _, context := range existingContexts {
totalContextSize += context.BodySize
}
for _, context := range *req {
size := int64(len(context.Body))
totalContextSize += size
if size > shared.MaxContextBodySize {
return nil, nil, fmt.Errorf("context body is too large: %d", size)
}
}
if totalContextSize > shared.MaxTotalContextSize {
return nil, nil, fmt.Errorf("total context size is too large: %d", totalContextSize)
}
View on GitHub (pinned to e2d772072e)
Solutions
- Remove unneeded contexts from the plan branch (plandex rm) before adding more.
- Split the load into smaller batches and clear contexts between batches.
- Start a new branch or plan for the additional files.
- If calling the API directly, check the current context count against MaxContextCount before sending.
Example fix
// before: one giant load
req := make([]shared.LoadContextParams, 300) // exceeds MaxContextCount with existing
res, err := LoadContexts(ctx, params)
// after: batch and trim
const batchSize = shared.MaxContextCount - len(existingContexts)
for i := 0; i < len(allParams); i += batchSize {
batch := allParams[i : i+batchSize]
if _, err := LoadContexts(ctx, LoadContextsParams{Req: &batch, ...}); err != nil {
return err
}
} Defensive patterns
Strategy: validation
Validate before calling
existing, _ := GetPlanContexts(orgId, planId, false, false)
if len(existing)+len(reqParams) > shared.MaxContextCount {
return fmt.Errorf("would exceed MaxContextCount (%d > %d)", len(existing)+len(reqParams), shared.MaxContextCount)
} Try / catch
resp, err := client.LoadContexts(req)
if err != nil {
if strings.Contains(err.Error(), "too many contexts") {
// trim contexts and retry with a smaller batch
return retryWithFewerContexts()
}
return err
} Prevention
- Check existing context count before bulk loads
- Split large file sets into batches under MaxContextCount
- Remove stale contexts (plandex rm) before adding new ones
- Enforce the limit client-side in any direct API integrations
When it happens
Trigger: A LoadContexts request where len(existingContexts) + len(req) > shared.MaxContextCount — e.g. loading many files at once onto a branch that already holds close to the maximum number of contexts.
Common situations: Bulk-loading a large directory of files; scripts/integrations calling the API directly without the CLI's limit checks; concurrent sessions adding contexts to the same plan branch.
Related errors
- context body is too large: %d
- error getting org session: %v
- invalid context index: %s
- no context found with name: %s
- invalid value for --debug: %v
AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05).
Data as JSON: /api/errors/aa4fbca65c591fea.
Report an issue: GitHub.