mastra-ai/mastra · error · Error

ModelByInputTokens: input token count (${inputTokens}) excee

Error message

ModelByInputTokens: input token count (${inputTokens}) exceeds the largest configured threshold (${maxLimit}). Please configure a higher threshold or use a larger model.

What it means

resolve() picks the model whose upTo threshold is the first one at or above the current input token count. If the input token count exceeds the largest configured threshold, there is no configured model big enough, so it throws instead of silently picking the largest model.

Source

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

  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;
      }
    }

    const maxLimit = this.thresholds[this.thresholds.length - 1]!.limit;
    throw new Error(
      `ModelByInputTokens: input token count (${inputTokens}) exceeds the largest configured threshold (${maxLimit}). ` +
        `Please configure a higher threshold or use a larger model.`,
    );
  }

  getThresholds(): number[] {
    return this.thresholds.map(t => t.limit);
  }
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Add a higher threshold entry covering the expected maximum, e.g. { upTo: { ..., 200000: 'larger-model' } }
  2. Raise the largest threshold's value and pair it with a larger-context model
  3. Pre-check token counts and truncate/summarize input before calling resolve

Example fix

// before
new ModelByInputTokens({ upTo: { '8000': smallModel, '128000': largeModel } }); // resolve(150000) throws
// after
new ModelByInputTokens({ upTo: { '8000': smallModel, '128000': largeModel, '1000000': xlModel } });
Defensive patterns

Strategy: try-catch

Validate before calling

const maxLimit = Math.max(...Object.keys(upTo).map(Number));
if (estimateInputTokens(messages) > maxLimit) {
  // truncate/summarize input or add a higher threshold first
}

Type guard

function fitsConfiguredMax(inputTokens: number, upTo: Record<string, unknown>): boolean {
  return inputTokens <= Math.max(...Object.keys(upTo).map(Number));
}

Try / catch

let model: string;
try {
  model = processor.resolve(inputTokens);
} catch (e) {
  if (e instanceof Error && e.message.includes('exceeds the largest configured threshold')) {
    model = largestAvailableModel;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `processor.resolve(inputTokens)` (directly or via observational memory) with a token count greater than the highest key in upTo, e.g. resolve(200000) when the largest threshold is 128000.

Common situations: Long conversations or huge pasted documents pushing input tokens past the largest configured tier; underestimating token counts when configuring thresholds; switching to a model family with larger context without updating the thresholds.

Related errors


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