{"record":{"id":"cc02bd136bb87a93","repo":"mastra-ai/mastra","slug":"agent-with-id-input-agent-id-already-exists","errorCode":null,"errorMessage":"Agent with id ${input.agent.id} already exists","messagePattern":"Agent with id (.+?) already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/agents/source.ts","lineNumber":214,"sourceCode":"    for (const [versionId, version] of this.providerVersions.entries()) {\n      if (version.agentId === agentId) {\n        this.providerVersions.delete(versionId);\n      }\n    }\n    await this.memory.delete(agentId);\n    await this.hydrateAgent(agentId);\n  }\n\n  async getById(id: string): Promise<StorageAgentType | null> {\n    await this.hydrateAgent(id);\n    return this.memory.getById(id);\n  }\n\n  async create(input: { agent: StorageCreateAgentInput }): Promise<StorageAgentType> {\n    await this.hydrateAgent(input.agent.id);\n    const existing = await this.memory.getById(input.agent.id);\n    if (existing) {\n      throw new Error(`Agent with id ${input.agent.id} already exists`);\n    }\n\n    await this.persistSnapshot(input.agent.id, { ...input.agent }, 'Initial version');\n    const created = await this.memory.create(input);\n    this.knownAgentIds.add(input.agent.id);\n    return created;\n  }\n\n  async update(input: StorageUpdateAgentInput): Promise<StorageAgentType> {\n    await this.hydrateAgent(input.id);\n    return this.memory.update(input);\n  }\n\n  async delete(id: string): Promise<void> {\n    this.knownAgentIds.delete(id);\n    this.hydratedAgents.delete(id);\n    this.loadedHistory.delete(id);\n    for (const versionId of this.providerVersions.keys()) {","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/agents/source.ts#L196-L232","documentation":"create() first hydrates the agent from the source provider, then checks the memory store for an existing agent with the same id. If one is already present, it throws this error rather than silently overwriting. Agent IDs are unique keys in the storage layer.","triggerScenarios":"Calling storage.agents.create({ agent }) (directly or via the `created` hook path) with an agent.id that already exists in memory storage after hydration.","commonSituations":"Re-running a seed/bootstrap script without idempotency, re-importing agents from a source that already contains the id, retrying a failed create that partially succeeded, or a race between two writers creating the same id.","solutions":["Use a different unique agent id","Check existence first with getById and update instead of create","Delete the existing agent if the re-create is intentional","Make bootstrap scripts idempotent (upsert semantics or skip-if-exists)"],"exampleFix":"// before\nawait storage.agents.create({ agent: { id: 'my-agent', ... } })\n// after\nconst existing = await storage.agents.getById({ id: 'my-agent' });\nif (!existing) await storage.agents.create({ agent: { id: 'my-agent', ... } });","handlingStrategy":"try-catch","validationCode":"const existing = await memory.getById(agent.id);\nif (existing) return existing; // or update instead of create","typeGuard":"null","tryCatchPattern":"try {\n  await storage.agents.create({ agent });\n} catch (e) {\n  if (e.message === `Agent with id ${agent.id} already exists`) {\n    await storage.agents.update?.({ agent }); // upsert fallback\n    return;\n  }\n  throw e;\n}","preventionTips":["Use UUIDs for agent ids","Make seed scripts idempotent (check-then-create)","Prefer upsert-style flows over blind create","Don't reuse ids across imports/retries"],"tags":["storage","agents","duplicate-key","conflict"],"backgroundTag":"duplicate-resource-id","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}