mastra-ai/mastra · error · Error
ModelByInputTokens threshold keys must be positive numbers.
Error message
ModelByInputTokens threshold keys must be positive numbers. Got: ${limitStr} What it means
normalizeThresholds converts the string keys of the upTo map into numeric token limits. Every key must parse to a finite number greater than zero; otherwise the processor would have unusable thresholds (NaN, 0, negative limits) and cannot order or compare them against input token counts.
Source
Thrown at packages/memory/src/processors/observational-memory/model-by-input-tokens.ts:38
'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'] }>;
constructor(config: ModelByInputTokensConfig) {
this.thresholds = normalizeThresholds(config);
}
resolve(inputTokens: number): AgentConfig['model'] {View on GitHub (pinned to 75dd419e61)
Solutions
- Change the offending key to a plain positive numeric string, e.g. { '128000': model }
- Remove unit suffixes and thousand separators from keys ('12k' -> '12000', '128,000' -> '128000')
- Log/inspect the upTo keys and convert them with Number() yourself to find which one is invalid
Example fix
// before
new ModelByInputTokens({ upTo: { '12k': 'openai/gpt-4o-mini', '128000': 'openai/gpt-4o' } });
// after
new ModelByInputTokens({ upTo: { '12000': 'openai/gpt-4o-mini', '128000': 'openai/gpt-4o' } }); Defensive patterns
Strategy: validation
Validate before calling
for (const key of Object.keys(upTo)) {
const n = Number(key);
if (!Number.isFinite(n) || n <= 0) throw new Error(`Invalid upTo key: "${key}"`);
} Type guard
function hasValidThresholdKeys(upTo: Record<string, unknown>): boolean {
return Object.keys(upTo).every(k => { const n = Number(k); return Number.isFinite(n) && n > 0; });
} Try / catch
try {
processor = new ModelByInputTokens({ upTo });
} catch (e) {
if (e instanceof Error && e.message.includes('positive numbers')) {
const fixed = Object.fromEntries(Object.entries(upTo).map(([k, v]) => [String(Math.max(1, Number(k) || 1)), v]));
processor = new ModelByInputTokens({ upTo: fixed });
} else throw e;
} Prevention
- Use plain numeric string keys without units or separators
- Coerce dynamic keys with Number() and validate before building the map
- Avoid template-literal keys built from possibly-undefined variables
When it happens
Trigger: `new ModelByInputTokens({ upTo: { '0': model } })`, `{ '-5000': model }`, `{ 'abc': model }`, or keys like `{ '': model }` / `{ '12e999': model }` that yield NaN or non-finite values when passed through Number().
Common situations: Typo in a threshold key (e.g. '1,28000' with a comma, or '12k' with a unit suffix); template-literal keys built from undefined variables producing 'undefined' or 'NaN'; locale-formatted numbers with dots/commas; copying config where a key was left as a placeholder.
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
- Extractor "${name}" must include instructions.
- ModelByInputTokens requires a valid model target for thresho
- Cookie password must be at least 32 characters. Set WORKOS_C
- Factory rule version is required.
- ${label} must be an object.
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/85ca2ae7c4951331.
Report an issue: GitHub.