{"record":{"id":"e74a426b99fd77cb","repo":"mastra-ai/mastra","slug":"stored-response-body-previous-response-id-belon","errorCode":null,"errorMessage":"Stored response ${body.previous_response_id} belongs to agent ${owningResponseTurnRecord.metadata.agentId}, not ${body.agent_id}","messagePattern":"Stored response (.+?) belongs to agent (.+?), not (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"packages/server/src/server/handlers/responses.ts","lineNumber":662,"sourceCode":"      resolvedAgent = await resolveResponseAgent({ mastra, agentId: body.agent_id });\n      previousResponseTurnRecord = await findResponseTurnRecord({\n        agent: resolvedAgent,\n        responseId: body.previous_response_id,\n        requestContext,\n      });\n\n      if (!previousResponseTurnRecord) {\n        const owningResponseTurnRecord = await findResponseTurnRecordAcrossAgents({\n          mastra,\n          responseId: body.previous_response_id,\n          requestContext,\n        });\n\n        if (owningResponseTurnRecord) {\n          if (owningResponseTurnRecord.metadata.agentId === body.agent_id) {\n            previousResponseTurnRecord = owningResponseTurnRecord;\n          } else {\n            throw new HTTPException(400, {\n              message: `Stored response ${body.previous_response_id} belongs to agent ${owningResponseTurnRecord.metadata.agentId}, not ${body.agent_id}`,\n            });\n          }\n        }\n\n        if (!previousResponseTurnRecord) {\n          throw new HTTPException(404, { message: `Stored response ${body.previous_response_id} was not found` });\n        }\n      }\n    } else {\n      if (!mastra) {\n        throw new HTTPException(500, { message: 'Mastra instance is required for agent-backed responses' });\n      }\n\n      previousResponseTurnRecord = await findResponseTurnRecordAcrossAgents({\n        mastra,\n        responseId: body.previous_response_id,\n        requestContext,","sourceCodeStart":644,"sourceCodeEnd":680,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/responses.ts#L644-L680","documentation":"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.","triggerScenarios":"POST /api/responses where body.agent_id = 'agent-B' but body.previous_response_id references a response stored by 'agent-A'.","commonSituations":"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.","solutions":["Send previous_response_id only with the same agent_id that produced it","Start a fresh conversation when switching agents (omit previous_response_id)","Track (agentId, previousResponseId) pairs together in client state","If handoff between agents is intended, pass conversation content as input rather than referencing the other agent's stored response"],"exampleFix":"// before\nclient.responses.create({ agent_id: 'agentB', previous_response_id: 'resp_from_agentA', input: 'continue' });\n// after\nclient.responses.create({ agent_id: 'agentA', previous_response_id: 'resp_from_agentA', input: 'continue' });","handlingStrategy":"validation","validationCode":"if (previousResponseId && currentAgentId !== ownerAgentIdOf(previousResponseId)) throw new Error('previous_response_id belongs to a different agent');","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Keep {agentId, previousResponseId} pairs atomic in client state","Reset conversation chaining when the user switches agents","Type client state so agent switches clear previous_response_id"],"tags":["server","validation","http-400","ownership"],"backgroundTag":"resource-ownership-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}