mastra-ai/mastra · error · Error

ModelByInputTokens requires a valid model target for thresho

Error message

ModelByInputTokens requires a valid model target for threshold ${limitStr}

What it means

Each upTo value must be a valid tiered model target (a resolvable model string or model object recognized by isTieredModelTarget). If a value does not match the expected shape, the processor cannot resolve that threshold to an actual model and refuses to construct.

Source

Thrown at packages/memory/src/processors/observational-memory/model-by-input-tokens.ts:42

    ('doGenerate' in model && 'doStream' in model)
  );
}

function normalizeThresholds(config: ModelByInputTokensConfig) {
  const entries = Object.entries(config.upTo);

  if (entries.length === 0) {
    throw new Error('ModelByInputTokens requires at least one threshold in "upTo"');
  }

  for (const [limitStr, model] of entries) {
    const limit = Number(limitStr);
    if (!Number.isFinite(limit) || limit <= 0) {
      throw new Error(`ModelByInputTokens threshold keys must be positive numbers. Got: ${limitStr}`);
    }

    if (!isTieredModelTarget(model)) {
      throw new Error(`ModelByInputTokens requires a valid model target for threshold ${limitStr}`);
    }
  }

  return entries.map(([limitStr, model]) => ({ limit: Number(limitStr), model })).sort((a, b) => a.limit - b.limit);
}

export class ModelByInputTokens {
  private readonly thresholds: Array<{ limit: number; model: AgentConfig['model'] }>;

  constructor(config: ModelByInputTokensConfig) {
    this.thresholds = normalizeThresholds(config);
  }

  resolve(inputTokens: number): AgentConfig['model'] {
    for (const { limit, model } of this.thresholds) {
      if (inputTokens <= limit) {
        return model;
      }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Set the threshold value to a valid model target, e.g. a model id string like 'openai/gpt-4o'
  2. Log the value before constructing to confirm it is not undefined/null; fix the lookup or env var that produces it
  3. Check the isTieredModelTarget expectations (model id string or model object) after any library version upgrade

Example fix

// before
new ModelByInputTokens({ upTo: { '128000': process.env.LARGE_MODEL } }); // undefined
// after
new ModelByInputTokens({ upTo: { '128000': process.env.LARGE_MODEL ?? 'openai/gpt-4o' } });
Defensive patterns

Strategy: validation

Validate before calling

for (const [key, model] of Object.entries(upTo)) {
  if (model == null || (typeof model !== 'string' && typeof model !== 'object')) {
    throw new Error(`Invalid model target for threshold ${key}`);
  }
}

Type guard

function isValidModelTarget(m: unknown): boolean {
  return typeof m === 'string' ? m.length > 0 : (typeof m === 'object' && m !== null);
}

Try / catch

try {
  processor = new ModelByInputTokens({ upTo });
} catch (e) {
  if (e instanceof Error && e.message.includes('valid model target')) {
    processor = new ModelByInputTokens({ upTo: Object.fromEntries(Object.entries(upTo).map(([k]) => [k, fallbackModel])) });
  } else throw e;
}

Prevention

When it happens

Trigger: `new ModelByInputTokens({ upTo: { '128000': null } })`, passing an empty object, a wrong-typed value (number, boolean), or a malformed model id/shape that fails the isTieredModelTarget check.

Common situations: Environment variable or lookup that resolves to undefined/null before being placed in the config; renamed model factory function returning a different shape after an upgrade; JSON config where the model value was mistyped.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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