{"record":{"id":"b7d68b1be1368649","repo":"danny-avila/LibreChat","slug":"subagent-graph-exceeds-the-maximum-depth-of-max","errorCode":null,"errorMessage":"Subagent graph exceeds the maximum depth of ${MAX_SUBAGENT_DEPTH} at agent ${agent.id}.","messagePattern":"Subagent graph exceeds the maximum depth of (.+?) at agent (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"api/server/services/Endpoints/agents/initialize.js","lineNumber":928,"sourceCode":"    return config;\n  };\n\n  const buildLazySubagentDescriptors = async (agent, depth = 0, ancestors = new Set()) => {\n    if (!subagentsCapabilityEnabled || !agent.subagents?.enabled) {\n      return [];\n    }\n    if (agent.subagents.allowSelf !== false) {\n      countExpandedSubagentDescriptor(agent.id);\n    }\n    const subagentIds = getExplicitSubagentIds(agent);\n    if (subagentIds.length > 0 && depth >= MAX_SUBAGENT_DEPTH) {\n      logger.warn('[initializeClient] Subagent graph depth limit exceeded', {\n        agentId: agent.id,\n        primaryAgentId: primaryConfig.id,\n        depth,\n        maxSubagentDepth: MAX_SUBAGENT_DEPTH,\n      });\n      throw new Error(\n        `Subagent graph exceeds the maximum depth of ${MAX_SUBAGENT_DEPTH} at agent ${agent.id}.`,\n      );\n    }\n    const nextAncestors = new Set(ancestors);\n    nextAncestors.add(agent.id);\n    const descriptors = [];\n    for (const subagentId of subagentIds) {\n      if (skippedAgentIds.has(subagentId) || nextAncestors.has(subagentId)) continue;\n      if (subagentId !== primaryConfig.id) {\n        assertSubagentGraphRoom(subagentId);\n      }\n      const existing =\n        subagentId === primaryConfig.id ? primaryConfig : agentConfigs.get(subagentId);\n      if (existing) {\n        countExpandedSubagentDescriptor(subagentId);\n        if (subagentId !== primaryConfig.id) {\n          subagentGraphIds.add(subagentId);\n        }","sourceCodeStart":910,"sourceCodeEnd":946,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/server/services/Endpoints/agents/initialize.js#L910-L946","documentation":"Thrown when expanding a subagent graph node that itself declares subagents at a depth already equal to MAX_SUBAGENT_DEPTH. This prevents unboundedly deep delegation chains that could recurse or stack deeply.","triggerScenarios":"A chain of agents A->B->C->... where each delegates to the next, and the deepest agent still declares further subagents at the configured depth ceiling.","commonSituations":"Agents referencing each other transitively; a template agent that always lists itself or a long chain as a subagent; deep organizational delegation modeled as nested agents.","solutions":["Flatten the delegation chain so no path exceeds the maximum depth.","Remove deep or self-referential subagent edges (set allowSelf:false where appropriate).","Raise MAX_SUBAGENT_DEPTH only if a deeper chain is intentional and safe.","Audit agent definitions for unintended transitive references."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (subagentIds.length > 0 && depth >= MAX_SUBAGENT_DEPTH) {\n  throw new Error(`Depth cap (${MAX_SUBAGENT_DEPTH}) reached at ${agent.id}`);\n}","typeGuard":null,"tryCatchPattern":"try { await initializeClient(req, res, endpointOption); }\ncatch (e) { if (/maximum depth/.test(e.message)) return res.status(400).json({ error: 'Subagent nesting too deep' }); throw e; }","preventionTips":["Avoid self-referential or cyclic subagent edges.","Flatten delegation chains in agent design.","Warn authors in the UI when a graph approaches the depth cap."],"tags":["agents","subagents","limits","recursion","depth"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}