mastra-ai/mastra · critical · Error

Agent model not available

Error message

Agent model not available

What it means

During durable execution preparation (prepareForDurableExecution), step 8 resolves the agent's runtime model via agent.getModel({ requestContext }). If it resolves to null/undefined there is no model to persist into the run, so preparation fails with a plain Error('Agent model not available').

Source

Thrown at packages/core/src/agent/durable/preparation.ts:516

      clientTools: execOptions?.clientTools,
      threadId,
      resourceId,
      runId,
      requestContext,
      memoryConfig: execOptions?.memory?.options,
      autoResumeSuspendedTools: execOptions?.autoResumeSuspendedTools,
      hooks: execOptions?.hooks,
      delegation: execOptions?.delegation,
      methodType,
    });
  } catch (error) {
    logger?.warn?.(`[DurableAgent] Error converting tools: ${error}`);
  }

  // 8. Get model (and model list if configured)
  const model = await typedAgent.getModel({ requestContext });
  if (!model) {
    throw new Error('Agent model not available');
  }

  // Client-executed results fire only after processors accept the request and
  // the required runtime model has resolved.
  if (!tripwireData) {
    await fireClientToolOutputHooks({
      messages,
      tools,
      abortSignal: execOptions?.abortSignal,
      logger,
    });
  }

  const modelList = await typedAgent.getModelList(requestContext);

  // 8b. Get scorers configuration
  const overrideScorers = (execOptions as any)?.scorers;
  let scorers: Record<string, { scorer: any; sampling?: any }> | undefined;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Set a static `model` on the Agent constructor options.
  2. If using dynamic model resolution, ensure the requestContext contains the expected key and the resolver returns a valid model.
  3. Log/inspect agent.getModel({ requestContext }) result at startup to catch null resolution early.

Example fix

// before
const agent = new Agent({ name: 'helper', instructions: '...' });
// after
const agent = new Agent({ name: 'helper', instructions: '...', model: openai('gpt-4o') });
Defensive patterns

Strategy: validation

Validate before calling

const model = await agent.getModel({ requestContext });
if (!model) {
  throw new Error(`Agent ${agent.name} has no resolvable model; check config and requestContext`);
}
// proceed with durable run

Type guard

function hasModel(m: unknown): m is MastraLanguageModel {
  return !!m && typeof m === 'object' && 'doStream' in m;
}

Try / catch

try {
  await durableAgent.generate({ messages });
} catch (e) {
  if (e instanceof Error && e.message === 'Agent model not available') {
    logger.error('Model missing: check agent config or requestContext model key');
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling generate/stream on a DurableAgent when getModel({ requestContext }) returns undefined — e.g. no model configured on the agent and no model resolvable from the request context, or a dynamic model resolver returning null at runtime.

Common situations: Agents declared without a `model` (relying on a default that isn't set), requestContext-based model routing where the context key is missing, or model getters that return null on missing API keys/configuration.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/1ea8a80d3a754bc4. Report an issue: GitHub.