mastra-ai/mastra · error · HTTPException

Stored response ${body.previous_response_id} belongs to agen

Error message

Stored response ${body.previous_response_id} belongs to agent ${owningResponseTurnRecord.metadata.agentId}, not ${body.agent_id}

What it means

Stored responses are owned by the agent that produced them. When previous_response_id resolves to a turn record whose metadata.agentId differs from body.agent_id, prepareCreateResponseRequest throws this 400 to prevent one agent from continuing another agent's conversation state.

Source

Thrown at packages/server/src/server/handlers/responses.ts:662

      resolvedAgent = await resolveResponseAgent({ mastra, agentId: body.agent_id });
      previousResponseTurnRecord = await findResponseTurnRecord({
        agent: resolvedAgent,
        responseId: body.previous_response_id,
        requestContext,
      });

      if (!previousResponseTurnRecord) {
        const owningResponseTurnRecord = await findResponseTurnRecordAcrossAgents({
          mastra,
          responseId: body.previous_response_id,
          requestContext,
        });

        if (owningResponseTurnRecord) {
          if (owningResponseTurnRecord.metadata.agentId === body.agent_id) {
            previousResponseTurnRecord = owningResponseTurnRecord;
          } else {
            throw new HTTPException(400, {
              message: `Stored response ${body.previous_response_id} belongs to agent ${owningResponseTurnRecord.metadata.agentId}, not ${body.agent_id}`,
            });
          }
        }

        if (!previousResponseTurnRecord) {
          throw new HTTPException(404, { message: `Stored response ${body.previous_response_id} was not found` });
        }
      }
    } else {
      if (!mastra) {
        throw new HTTPException(500, { message: 'Mastra instance is required for agent-backed responses' });
      }

      previousResponseTurnRecord = await findResponseTurnRecordAcrossAgents({
        mastra,
        responseId: body.previous_response_id,
        requestContext,

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Send previous_response_id only with the same agent_id that produced it
  2. Start a fresh conversation when switching agents (omit previous_response_id)
  3. Track (agentId, previousResponseId) pairs together in client state
  4. If handoff between agents is intended, pass conversation content as input rather than referencing the other agent's stored response

Example fix

// before
client.responses.create({ agent_id: 'agentB', previous_response_id: 'resp_from_agentA', input: 'continue' });
// after
client.responses.create({ agent_id: 'agentA', previous_response_id: 'resp_from_agentA', input: 'continue' });
Defensive patterns

Strategy: validation

Validate before calling

if (previousResponseId && currentAgentId !== ownerAgentIdOf(previousResponseId)) throw new Error('previous_response_id belongs to a different agent');

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: POST /api/responses where body.agent_id = 'agent-B' but body.previous_response_id references a response stored by 'agent-A'.

Common situations: Switching agents mid-conversation while reusing the previous response id; a shared UI client that keeps one previous_response_id across agent switches; load-balanced environments where agent ids differ per deployment.

Related errors


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