{"record":{"id":"376e8085730f7a31","repo":"mastra-ai/mastra","slug":"no-model-selected-use-models-to-select-a-model-f","errorCode":null,"errorMessage":"No model selected. Use /models to select a model first.","messagePattern":"No model selected\\. Use /models to select a model first\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/agents/model.ts","lineNumber":219,"sourceCode":" */\nexport function getDynamicModel(\n  { requestContext }: { requestContext: RequestContext },\n  settingsPath?: string,\n): ResolvedModel {\n  const agentControllerContext = requestContext.get('controller') as AgentControllerRequestContext<any> | undefined;\n\n  const modelId = agentControllerContext?.session?.modelId;\n  if (!modelId) {\n    // A missing controller context means the run was started without session\n    // request context at all (e.g. a signal delivered to an idle thread) —\n    // \"use /models\" would mislead there, the user's selection was never the\n    // problem.\n    if (!agentControllerContext) {\n      throw new Error(\n        'No model available: this run started without a controller session context, so no model selection could be resolved.',\n      );\n    }\n    throw new Error('No model selected. Use /models to select a model first.');\n  }\n\n  const thinkingLevel = resolveRequestThinkingLevel(agentControllerContext, settingsPath);\n\n  return resolveModel(modelId, { thinkingLevel, remapForCodexOAuth: true, requestContext });\n}\n\n/**\n * Goal judge model resolver for the agent's `goal.judge` config. Resolves the\n * configured goal judge model through mastracode's gateway so provider\n * credentials (stored in auth storage, not just env) are injected — a bare model\n * id handed to core's default model router would fail to find the API key.\n *\n * Returns `undefined` when no judge model is configured, which keeps the goal\n * step a complete no-op (the goal mechanism requires a judge to do anything).\n *\n * `settingsPath` must be the same source `createMastraCode()` reads from so the\n * judge model and the goal budget (`goalMaxTurns`) come from one config — with a","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/agents/model.ts#L201-L237","documentation":"getDynamicModel throws this when a controller session context exists but no modelId is set in it — i.e. the user has a session but never picked a model. The message directs them to the /models command.","triggerScenarios":"codeAgent → getDynamicModel sees a truthy agentControllerContext whose modelId is empty/undefined — an interactive session that has not made a model selection via /models.","commonSituations":"Fresh sessions where the user immediately invokes an action requiring the model, sessions where a previously selected model was cleared, or environments without a default model configured.","solutions":["Run /models in the session and select a model","Configure a default model for new sessions so modelId is pre-populated","Re-select the model if a config migration cleared it","Check why modelId was cleared if a selection was previously made (settings persistence issue)"],"exampleFix":"// before (in TUI)\n> run tests with the agent   // no model chosen yet\n// after\n> /models   → select anthropic/claude-sonnet-4\n> run tests with the agent","handlingStrategy":"validation","validationCode":"if (agentControllerContext && !agentControllerContext.modelId) {\n  throw new Error('select a model with /models before running');\n}","typeGuard":"const hasModelSelection = (ctx: AgentControllerContext | null): boolean =>\n  Boolean(ctx?.modelId);","tryCatchPattern":"try {\n  await runAgent(task);\n} catch (err) {\n  if ((err as Error).message.includes('No model selected')) {\n    console.error('Run /models to pick a model first');\n  } else throw err;\n}","preventionTips":["Configure a default model for new sessions","Check session state before actions that need the model","Surface model-selection UI early in onboarding"],"tags":["configuration","ux","model-selection"],"backgroundTag":"no-model-selected","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}