{"record":{"id":"da8bea9c5486976f","repo":"mastra-ai/mastra","slug":"memorythread-methodname-requires-an-agentid","errorCode":null,"errorMessage":"MemoryThread.${methodName}() requires an agentId. Pass it via getMemoryThread({ threadId, agentId }) or as a parameter to ${methodName}().","messagePattern":"MemoryThread\\.(.+?)\\(\\) requires an agentId\\. Pass it via getMemoryThread\\((.+?)\\) or as a parameter to (.+?)\\(\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"client-sdks/client-js/src/resources/memory-thread.ts","lineNumber":49,"sourceCode":"    super(options);\n  }\n\n  /**\n   * Builds the query string for agentId (if provided)\n   */\n  private getAgentIdQueryParam(prefix: '?' | '&' = '?', overrideAgentId?: string): string {\n    const agentId = overrideAgentId ?? this.agentId;\n    return agentId ? `${prefix}agentId=${agentId}` : '';\n  }\n\n  /**\n   * Resolves the agentId to use for a write request. Prefers the per-call value, falls back\n   * to the constructor value, and throws if neither is set.\n   */\n  private requireAgentId(perCallAgentId: string | undefined, methodName: string): string {\n    const agentId = perCallAgentId ?? this.agentId;\n    if (!agentId) {\n      throw new Error(\n        `MemoryThread.${methodName}() requires an agentId. ` +\n          `Pass it via getMemoryThread({ threadId, agentId }) or as a parameter to ${methodName}().`,\n      );\n    }\n    return agentId;\n  }\n\n  /**\n   * Retrieves the memory thread details\n   * @param requestContext - Optional request context to pass as query parameter\n   * @returns Promise containing thread details including title and metadata\n   */\n  get(requestContext?: RequestContext | Record<string, any>): Promise<StorageThreadType> {\n    const agentIdParam = this.getAgentIdQueryParam('?');\n    const contextParam = requestContextQueryString(requestContext, agentIdParam ? '&' : '?');\n    return this.request(`/memory/threads/${this.threadId}${agentIdParam}${contextParam}`);\n  }\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/client-sdks/client-js/src/resources/memory-thread.ts#L31-L67","documentation":"Thrown by MemoryThread.requireAgentId when neither the per-call agentId nor the constructor agentId is set, but a write operation needs one to resolve the request path. MemoryThread operations are agent-scoped, so the library refuses to guess which agent owns the thread.","triggerScenarios":"Calling getMemoryThread({ threadId }) without agentId, then invoking any mutating MemoryThread method (e.g. updateTitle, addMessages) without passing agentId as a parameter (client-sdks/client-js/src/resources/memory-thread.ts:49).","commonSituations":"Refactored code that previously fetched threads agent-less; shared helper functions that construct MemoryThread without the agent context; migrating after the API started requiring agentId per thread call.","solutions":["Pass agentId when fetching the thread: getMemoryThread({ threadId, agentId: 'myAgent' }).","Or pass agentId as a parameter to the individual MemoryThread method call.","Construct the MemoryThread via an Agent instance so the agentId is wired automatically."],"exampleFix":"// before\nconst thread = await memory.getMemoryThread({ threadId: 't1' });\nawait thread.updateTitle('New title');\n\n// after\nconst thread = await memory.getMemoryThread({ threadId: 't1', agentId: 'myAgent' });\nawait thread.updateTitle('New title');","handlingStrategy":"validation","validationCode":"function assertAgentId(agentId: string | undefined, op: string): asserts agentId {\n  if (!agentId) throw new Error(`${op} requires an agentId`);\n}\n// call before any MemoryThread write\nassertAgentId(threadAgentId, 'updateTitle');","typeGuard":"function hasAgentId(t: { agentId?: string }): t is { agentId: string } {\n  return typeof t.agentId === 'string' && t.agentId.length > 0;\n}","tryCatchPattern":"try {\n  await thread.updateTitle('New title');\n} catch (e) {\n  if (/requires an agentId/.test(e.message)) {\n    await memory.getMemoryThread({ threadId, agentId: 'myAgent' }).then(t => t.updateTitle('New title'));\n  } else throw e;\n}","preventionTips":["Always pass { threadId, agentId } to getMemoryThread","Type agentId as required (string, not string | undefined) in app-level helpers","Prefer constructing MemoryThread through an Agent instance","Centralize thread fetching in one helper that enforces agentId"],"tags":["api-misuse","typescript","client-js"],"backgroundTag":"missing-required-parameter","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}