{"record":{"id":"f6b6d48090858a7b","repo":"discordjs/discord.js","slug":"not-enough-sessions-remaining-to-spawn-shardids","errorCode":null,"errorMessage":"Not enough sessions remaining to spawn ${shardIds.length} shards; only ${data.session_start_limit.remaining} remaining; resets at ${new Date(Date.now() + data.session_start_limit.reset_after).toISOString()}","messagePattern":"Not enough sessions remaining to spawn (.+?) shards; only (.+?) remaining; resets at (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ws/src/ws/WebSocketManager.ts","lineNumber":364,"sourceCode":"\t\t} else {\n\t\t\tconst data = await this.fetchGatewayInformation();\n\t\t\tshardIds = [...range(this.options.shardCount ?? data.shards)];\n\t\t}\n\n\t\tthis.shardIds = shardIds;\n\t\treturn shardIds;\n\t}\n\n\tpublic async connect() {\n\t\tconst shardCount = await this.getShardCount();\n\t\t// Spawn shards and adjust internal state\n\t\tawait this.updateShardCount(shardCount);\n\n\t\tconst shardIds = await this.getShardIds();\n\t\tconst data = await this.fetchGatewayInformation();\n\n\t\tif (data.session_start_limit.remaining < shardIds.length) {\n\t\t\tthrow new Error(\n\t\t\t\t`Not enough sessions remaining to spawn ${shardIds.length} shards; only ${\n\t\t\t\t\tdata.session_start_limit.remaining\n\t\t\t\t} remaining; resets at ${new Date(Date.now() + data.session_start_limit.reset_after).toISOString()}`,\n\t\t\t);\n\t\t}\n\n\t\tawait this.strategy.connect();\n\t}\n\n\tpublic setToken(token: string): void {\n\t\tif (this.#token) {\n\t\t\tthrow new Error('Token has already been set');\n\t\t}\n\n\t\tthis.#token = token;\n\t}\n\n\tpublic destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>) {","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/ws/src/ws/WebSocketManager.ts#L346-L382","documentation":"Before spawning shards, the manager checks Discord's session_start_limit. If the remaining identify sessions are fewer than the number of shards to spawn, it throws, because connecting would exceed Discord's rate limit on new sessions. The message includes the reset time.","triggerScenarios":"Calling manager.connect() (or strategy.connect() via updateShardCount path) when data.session_start_limit.remaining < shardIds.length — typically after repeated bot restarts within the reset window.","commonSituations":"Frequent dev-mode restarts of a large bot burning identify sessions; scaling up shard count beyond remaining sessions; multiple processes sharing one token each identifying many shards.","solutions":["Wait until the session limit resets (time given in the error message) before reconnecting","Reduce the number of shards spawned per attempt; connect them in batches as the limit replenishes","Avoid unnecessary full restarts — use resume/reconnect logic instead of fresh identifies","Check session_start_limit via fetchGatewayInformation before planning a shard scale-up"],"exampleFix":"// before\nawait manager.connect();\n// after\nconst info = await rest.get(Routes.gatewayBot());\nif (info.session_start_limit.remaining < manager.getShardCount()) {\n  const waitMs = info.session_start_limit.reset_after;\n  console.log(`Waiting ${waitMs}ms for session limit reset`);\n  await new Promise((r) => setTimeout(r, waitMs));\n}\nawait manager.connect();","handlingStrategy":"retry","validationCode":"const info = await rest.get(Routes.gatewayBot());\nif (info.session_start_limit.remaining < shardCount) {\n  console.warn(`Only ${info.session_start_limit.remaining} sessions left; resets at ${new Date(Date.now() + info.session_start_limit.reset_after)}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await manager.connect();\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Not enough sessions remaining')) {\n    await new Promise((r) => setTimeout(r, resetAfterMs));\n    await manager.connect();\n  } else throw e;\n}","preventionTips":["Check session_start_limit before every full restart or shard scale-up","Avoid rapid restart loops in development on large sharded bots","Batch shard connections to stay within the identify budget"],"tags":["rate-limit","discord-api","session-limit"],"backgroundTag":"session-start-limit-exceeded","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}