{"record":{"id":"b505e552a4f87867","repo":"mastra-ai/mastra","slug":"model-config-with-id-modelconfigid-not-found","errorCode":null,"errorMessage":"Model config with id ${modelConfigId} not found","messagePattern":"Model config with id (.+?) not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"packages/server/src/server/handlers/agents.ts","lineNumber":3395,"sourceCode":"  pathParamSchema: modelConfigIdPathParams,\n  bodySchema: updateAgentModelInModelListBodySchema,\n  responseSchema: modelManagementResponseSchema,\n  summary: 'Update model in model list',\n  description: 'Updates a specific model configuration in the agent model list',\n  tags: ['Agents', 'Models'],\n  requiresAuth: true,\n  handler: async ({ mastra, agentId, modelConfigId, model: bodyModel, maxRetries, enabled }) => {\n    try {\n      const agent = await getAgentFromSystem({ mastra, agentId });\n\n      const modelList = await agent.getModelList();\n      if (!modelList || modelList.length === 0) {\n        throw new HTTPException(400, { message: 'Agent model list is not found or empty' });\n      }\n\n      const modelConfig = modelList.find(config => config.id === modelConfigId);\n      if (!modelConfig) {\n        throw new HTTPException(404, { message: `Model config with id ${modelConfigId} not found` });\n      }\n\n      const newModel =\n        bodyModel?.modelId && bodyModel?.provider ? `${bodyModel.provider}/${bodyModel.modelId}` : modelConfig.model;\n\n      const updated = {\n        ...modelConfig,\n        model: newModel,\n        ...(maxRetries !== undefined ? { maxRetries } : {}),\n        ...(enabled !== undefined ? { enabled } : {}),\n      };\n\n      agent.updateModelInModelList(updated);\n\n      return { message: 'Model updated in model list' };\n    } catch (error) {\n      return handleError(error, 'error updating model in model list');\n    }","sourceCodeStart":3377,"sourceCodeEnd":3413,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/agents.ts#L3377-L3413","documentation":"Thrown (HTTP 404) when `modelConfigId` in the request does not match any id in the agent's non-empty model list — `modelList.find(config => config.id === modelConfigId)` returns undefined.","triggerScenarios":"Updating a model config using an id that does not exist: stale id from a deleted config, typo in the id, or ids regenerated after an agent re-save.","commonSituations":"Frontend holding a cached model list after another client deleted/recreated configs; switching agents but reusing a config id from a different agent; ids changed after server/storage migration.","solutions":["Re-fetch the agent's model list and use a current `modelConfigId` from it.","Verify you are targeting the correct agentId — config ids are per-agent.","If the config was deleted, recreate it before updating.","Clear cached model lists in your UI after mutations so ids stay fresh."],"exampleFix":"// before\nawait client.getAgent('agent-x').updateModel({ modelConfigId: 'stale-id', maxRetries: 3 });\n// after\nconst list = await client.getAgent('agent-x').getModels();\nawait client.getAgent('agent-x').updateModel({ modelConfigId: list[0].id, maxRetries: 3 });","handlingStrategy":"validation","validationCode":"const models = await client.getAgent(agentId).getModels();\nif (!models?.some(m => m.id === modelConfigId)) throw new Error(`modelConfigId ${modelConfigId} not found on agent ${agentId}; refresh the model list`);","typeGuard":"function hasModelConfig(models: Array<{ id: string }> | null, id: string): models is Array<{ id: string }> & { find(c: { id: string }): { id: string } } { return !!models && models.some(c => c.id === id); }","tryCatchPattern":"try { await client.getAgent(agentId).updateModel({ modelConfigId, ...patch }); } catch (e) { if (is404(e) && /Model config with id/.test(e.message)) { const fresh = await client.getAgent(agentId).getModels(); /* retry with fresh id */ } else throw e; }","preventionTips":["Always source modelConfigId from a fresh GET of the model list, never from stale caches.","Invalidate model-list caches after any create/delete mutation.","Scope config ids per agent; never reuse ids across agents.","Detect 404s on model configs and auto-refresh the list before surfacing errors to users."],"tags":["http-404","agents","model-list","not-found","stale-identifier"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}