{"record":{"id":"1ebb629455f847f3","repo":"mastra-ai/mastra","slug":"no-model-with-a-configured-api-key-found-please-s","errorCode":null,"errorMessage":"No model with a configured API key found. Please set the required environment variable for your model provider.","messagePattern":"No model with a configured API key found\\. Please set the required environment variable for your model provider\\.","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"packages/server/src/server/handlers/agents.ts","lineNumber":3511,"sourceCode":"export const ENHANCE_INSTRUCTIONS_ROUTE = createRoute({\n  method: 'POST',\n  path: '/agents/:agentId/instructions/enhance',\n  responseType: 'json',\n  pathParamSchema: agentIdPathParams,\n  bodySchema: enhanceInstructionsBodySchema,\n  responseSchema: enhanceInstructionsResponseSchema,\n  summary: 'Enhance agent instructions',\n  description: 'Uses AI to enhance or modify agent instructions based on user feedback',\n  tags: ['Agents'],\n  requiresAuth: true,\n  handler: async ({ mastra, agentId, instructions, comment }) => {\n    try {\n      const agent = await getAgentFromSystem({ mastra, agentId });\n\n      // Find the first model with a connected provider (similar to how chat works)\n      const model = await findConnectedModel(agent);\n      if (!model) {\n        throw new HTTPException(400, {\n          message:\n            'No model with a configured API key found. Please set the required environment variable for your model provider.',\n        });\n      }\n\n      const systemPromptAgent = new Agent({\n        id: 'system-prompt-enhancer',\n        name: 'system-prompt-enhancer',\n        instructions: ENHANCE_SYSTEM_PROMPT_INSTRUCTIONS,\n        model,\n      });\n\n      const result = await systemPromptAgent.generate(\n        `We need to improve the system prompt.\nCurrent: ${instructions}\n${comment ? `User feedback: ${comment}` : ''}`,\n        {\n          structuredOutput: {","sourceCodeStart":3493,"sourceCodeEnd":3529,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/agents.ts#L3493-L3529","documentation":"This HTTP 400 error is thrown by the agents handler when `findConnectedModel(agent)` returns no model whose provider has a configured API key. The server needs at least one model with credentials present in the environment (or model config) to build a system-prompt agent. It mirrors how chat resolves models: only providers with a usable key are considered connected.","triggerScenarios":"Calling the agent system-prompt endpoint (GET on the route at packages/server/src/server/handlers/agents.ts:3511) when `getAgentFromSystem` succeeds but no model attached to the agent has a provider API key set in the environment.","commonSituations":"Running the Mastra server without provider env vars (e.g. OPENAI_API_KEY), .env not loaded by the server process, using a provider that requires custom env configuration, or an agent whose model config points at a provider with no credentials.","solutions":["Set the required environment variable for your model provider (e.g. OPENAI_API_KEY) in the shell or .env file the Mastra server loads, then restart the server.","Verify the agent's model configuration points at a provider you have credentials for.","If using a custom/nonstandard provider, ensure its key is registered in the way Mastra's findConnectedModel expects (model-level config or standard env var)."],"exampleFix":"// before\n// server started with: pnpm mastra dev  (no OPENAI_API_KEY set)\n// after\n// .env\nOPENAI_API_KEY=sk-...\n// then: pnpm mastra dev","handlingStrategy":"validation","validationCode":"// before calling the endpoint, check the provider key is set\nif (!process.env.OPENAI_API_KEY) {\n  throw new Error('Set OPENAI_API_KEY (or your provider key) before fetching the system prompt');\n}","typeGuard":null,"tryCatchPattern":"try {\n  const res = await fetch(`/api/agents/${id}/system-prompt`);\n  if (res.status === 400) throw new Error('No model with configured API key; check provider env vars');\n  const data = await res.json();\n} catch (e) { console.error(e.message); }","preventionTips":["Keep provider API keys in a .env loaded by the Mastra server process and validate them at startup.","Add a boot-time check that fails fast when required provider env vars are missing.","Pin agents to providers you have credentials for."],"tags":["http-400","api-key","environment","model-provider"],"backgroundTag":"missing-env-var","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}