{"record":{"id":"e946422240f6e7e0","repo":"mastra-ai/mastra","slug":"modelbyinputtokens-requires-a-valid-model-target-f","errorCode":null,"errorMessage":"ModelByInputTokens requires a valid model target for threshold ${limitStr}","messagePattern":"ModelByInputTokens requires a valid model target for threshold (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/memory/src/processors/observational-memory/model-by-input-tokens.ts","lineNumber":42,"sourceCode":"    ('doGenerate' in model && 'doStream' in model)\n  );\n}\n\nfunction normalizeThresholds(config: ModelByInputTokensConfig) {\n  const entries = Object.entries(config.upTo);\n\n  if (entries.length === 0) {\n    throw new Error('ModelByInputTokens requires at least one threshold in \"upTo\"');\n  }\n\n  for (const [limitStr, model] of entries) {\n    const limit = Number(limitStr);\n    if (!Number.isFinite(limit) || limit <= 0) {\n      throw new Error(`ModelByInputTokens threshold keys must be positive numbers. Got: ${limitStr}`);\n    }\n\n    if (!isTieredModelTarget(model)) {\n      throw new Error(`ModelByInputTokens requires a valid model target for threshold ${limitStr}`);\n    }\n  }\n\n  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      }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/processors/observational-memory/model-by-input-tokens.ts#L24-L60","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Set the threshold value to a valid model target, e.g. a model id string like 'openai/gpt-4o'","Log the value before constructing to confirm it is not undefined/null; fix the lookup or env var that produces it","Check the isTieredModelTarget expectations (model id string or model object) after any library version upgrade"],"exampleFix":"// before\nnew ModelByInputTokens({ upTo: { '128000': process.env.LARGE_MODEL } }); // undefined\n// after\nnew ModelByInputTokens({ upTo: { '128000': process.env.LARGE_MODEL ?? 'openai/gpt-4o' } });","handlingStrategy":"validation","validationCode":"for (const [key, model] of Object.entries(upTo)) {\n  if (model == null || (typeof model !== 'string' && typeof model !== 'object')) {\n    throw new Error(`Invalid model target for threshold ${key}`);\n  }\n}","typeGuard":"function isValidModelTarget(m: unknown): boolean {\n  return typeof m === 'string' ? m.length > 0 : (typeof m === 'object' && m !== null);\n}","tryCatchPattern":"try {\n  processor = new ModelByInputTokens({ upTo });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('valid model target')) {\n    processor = new ModelByInputTokens({ upTo: Object.fromEntries(Object.entries(upTo).map(([k]) => [k, fallbackModel])) });\n  } else throw e;\n}","preventionTips":["Provide ?? fallbackModel defaults for env-derived model values","Verify model factory return shapes after library upgrades","Validate every upTo value is non-null before construction"],"tags":["config","validation","observational-memory"],"backgroundTag":"invalid-config-value","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}