mastra-ai/mastra · error · Error

Agent with id ${agent.id} already exists

Error message

Agent with id ${agent.id} already exists

What it means

The in-memory agents domain enforces unique agent IDs: `create` throws if `this.db.agents` already contains an entry with the given `agent.id`. Agent creation is create-only; use `update` to modify existing agents.

Source

Thrown at packages/core/src/storage/domains/agents/inmemory.ts:49

  async dangerouslyClearAll(): Promise<void> {
    this.db.agents.clear();
    this.db.agentVersions.clear();
  }

  // ==========================================================================
  // Agent CRUD Methods
  // ==========================================================================

  async getById(id: string): Promise<StorageAgentType | null> {
    const agent = this.db.agents.get(id);
    return agent ? this.deepCopyAgent(agent) : null;
  }

  async create(input: { agent: StorageCreateAgentInput }): Promise<StorageAgentType> {
    const { agent } = input;

    if (this.db.agents.has(agent.id)) {
      throw new Error(`Agent with id ${agent.id} already exists`);
    }

    const now = new Date();
    // Default visibility to 'private' when an authorId is set; leave undefined for legacy unowned rows.
    const visibility = agent.visibility ?? (agent.authorId ? 'private' : undefined);
    const newAgent: StorageAgentType = {
      id: agent.id,
      status: 'draft',
      activeVersionId: undefined,
      authorId: agent.authorId,
      visibility,
      metadata: agent.metadata,
      favoriteCount: 0,
      createdAt: now,
      updatedAt: now,
    };

    this.db.agents.set(agent.id, newAgent);

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Check existence first (e.g. `getAgent(id)` or `db.agents.has(id)`) and skip or update instead of creating
  2. Generate unique IDs (crypto.randomUUID()) instead of hardcoded ones
  3. Catch this error and treat it as idempotent already-created during seeding

Example fix

// before
await agents.create({ agent: { id: 'agent-1', ... } });
// after
const existing = await agents.get({ agentId: 'agent-1' });
if (!existing) await agents.create({ agent: { id: 'agent-1', ... } });
Defensive patterns

Strategy: try-catch

Validate before calling

const existing = await agentsDomain.get({ agentId: agent.id });
if (existing) {
  // skip create or route to update instead
}

Type guard

null

Try / catch

try {
  await agentsDomain.create({ agent });
} catch (e) {
  if (e instanceof Error && /Agent with id .* already exists/.test(e.message)) {
    return agentsDomain.get({ agentId: agent.id }); // idempotent path
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `create` (directly or via `seedAgent`) with a `{ agent: { id } }` whose `id` already exists in the store — e.g. re-running a seed script or generating agents in a loop with a constant ID.

Common situations: Re-running seeding/bootstrapping scripts against the same in-memory instance; ID generation collisions (hardcoded or non-unique IDs); retrying a create after a timeout when the first attempt actually succeeded.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/25bbfaa98901891f. Report an issue: GitHub.