mastra-ai/mastra · error · Error

ModelByInputTokens requires at least one threshold in "upTo"

Error message

ModelByInputTokens requires at least one threshold in "upTo"

What it means

ModelByInputTokens is a processor that selects a model based on input token count using a map of thresholds ("upTo") keyed by token limits. The constructor normalizes this config, and throws immediately if the upTo map has no entries at all, because there would be no model to resolve to for any input.

Source

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

  if (!model || typeof model !== 'object') {
    return false;
  }

  return (
    'modelId' in model ||
    'id' in model ||
    'providerId' in model ||
    'provider' in model ||
    ('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'] }>;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Add at least one threshold entry to upTo, e.g. { upTo: { 128000: 'openai/gpt-4o' } }
  2. If building config dynamically, add a fallback/default threshold entry before constructing
  3. Guard construction: only instantiate ModelByInputTokens when Object.keys(config.upTo).length > 0

Example fix

// before
new ModelByInputTokens({ upTo: {} });
// after
new ModelByInputTokens({ upTo: { 128000: 'openai/gpt-4o' } });
Defensive patterns

Strategy: validation

Validate before calling

if (!config.upTo || Object.keys(config.upTo).length === 0) {
  throw new Error('upTo must contain at least one threshold before constructing ModelByInputTokens');
}
const processor = new ModelByInputTokens(config);

Type guard

function hasThresholds(c: { upTo: Record<string, unknown> }): c is { upTo: Record<string, unknown> & { [k: string]: unknown } } {
  return typeof c.upTo === 'object' && c.upTo !== null && Object.keys(c.upTo).length > 0;
}

Try / catch

let processor;
try {
  processor = new ModelByInputTokens(config);
} catch (e) {
  if (e instanceof Error && e.message.includes('at least one threshold')) {
    processor = new ModelByInputTokens({ upTo: { [defaultLimit]: defaultModel } });
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `new ModelByInputTokens({ upTo: {} })` or `new ModelByInputTokens({ upTo: {} as any })` — an empty upTo object — during construction.

Common situations: Building the config programmatically from environment variables or feature flags where all threshold branches are disabled or the env vars are unset, yielding an empty object; spreading an empty partial config; a refactor that removed the last threshold entry.

Related errors


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