{"record":{"id":"502793fbc4bdae29","repo":"mastra-ai/mastra","slug":"modelbyinputtokens-input-token-count-inputtoke","errorCode":null,"errorMessage":"ModelByInputTokens: input token count (${inputTokens}) exceeds the largest configured threshold (${maxLimit}). Please configure a higher threshold or use a larger model.","messagePattern":"ModelByInputTokens: input token count \\((.+?)\\) exceeds the largest configured threshold \\((.+?)\\)\\. Please configure a higher threshold or use a larger model\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/memory/src/processors/observational-memory/model-by-input-tokens.ts","lineNumber":64,"sourceCode":"  return entries.map(([limitStr, model]) => ({ limit: Number(limitStr), model })).sort((a, b) => a.limit - b.limit);\n}\n\nexport class ModelByInputTokens {\n  private readonly thresholds: Array<{ limit: number; model: AgentConfig['model'] }>;\n\n  constructor(config: ModelByInputTokensConfig) {\n    this.thresholds = normalizeThresholds(config);\n  }\n\n  resolve(inputTokens: number): AgentConfig['model'] {\n    for (const { limit, model } of this.thresholds) {\n      if (inputTokens <= limit) {\n        return model;\n      }\n    }\n\n    const maxLimit = this.thresholds[this.thresholds.length - 1]!.limit;\n    throw new Error(\n      `ModelByInputTokens: input token count (${inputTokens}) exceeds the largest configured threshold (${maxLimit}). ` +\n        `Please configure a higher threshold or use a larger model.`,\n    );\n  }\n\n  getThresholds(): number[] {\n    return this.thresholds.map(t => t.limit);\n  }\n}\n","sourceCodeStart":46,"sourceCodeEnd":74,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/processors/observational-memory/model-by-input-tokens.ts#L46-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a higher threshold entry covering the expected maximum, e.g. { upTo: { ..., 200000: 'larger-model' } }","Raise the largest threshold's value and pair it with a larger-context model","Pre-check token counts and truncate/summarize input before calling resolve"],"exampleFix":"// before\nnew ModelByInputTokens({ upTo: { '8000': smallModel, '128000': largeModel } }); // resolve(150000) throws\n// after\nnew ModelByInputTokens({ upTo: { '8000': smallModel, '128000': largeModel, '1000000': xlModel } });","handlingStrategy":"try-catch","validationCode":"const maxLimit = Math.max(...Object.keys(upTo).map(Number));\nif (estimateInputTokens(messages) > maxLimit) {\n  // truncate/summarize input or add a higher threshold first\n}","typeGuard":"function fitsConfiguredMax(inputTokens: number, upTo: Record<string, unknown>): boolean {\n  return inputTokens <= Math.max(...Object.keys(upTo).map(Number));\n}","tryCatchPattern":"let model: string;\ntry {\n  model = processor.resolve(inputTokens);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('exceeds the largest configured threshold')) {\n    model = largestAvailableModel;\n  } else throw e;\n}","preventionTips":["Configure a top threshold at or above the model's full context window","Estimate token counts from conversation history before resolve","Truncate or summarize oversized inputs before selection"],"tags":["observational-memory","token-limits","runtime"],"backgroundTag":"token-limit-exceeded","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}