{"record":{"id":"ccb29445cf0011a5","repo":"google-research/timesfm","slug":"context-horizon-must-be-less-than-the-context-li","errorCode":null,"errorMessage":"Context + horizon must be less than the context limit. {fc.max_context} + {fc.max_horizon} > {self.model.config.context_limit}.","messagePattern":"Context \\+ horizon must be less than the context limit\\. (.+?) \\+ (.+?) > (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/timesfm/timesfm_2p5/timesfm_2p5_flax.py","lineNumber":533,"sourceCode":"    fc = forecast_config\n    if fc.max_context % self.model.p != 0:\n      logging.info(\n        \"When compiling, max context needs to be multiple of the patch size\"\n        \" %d. Using max context = %d instead.\",\n        self.model.p,\n        new_context := math.ceil(fc.max_context / self.model.p) * self.model.p,\n      )\n      fc = dataclasses.replace(fc, max_context=new_context)\n    if fc.max_horizon % self.model.o != 0:\n      logging.info(\n        \"When compiling, max horizon needs to be multiple of the output patch\"\n        \" size %d. Using max horizon = %d instead.\",\n        self.model.o,\n        new_horizon := math.ceil(fc.max_horizon / self.model.o) * self.model.o,\n      )\n      fc = dataclasses.replace(fc, max_horizon=new_horizon)\n    if fc.max_context + fc.max_horizon > self.model.config.context_limit:\n      raise ValueError(\n        \"Context + horizon must be less than the context limit.\"\n        f\" {fc.max_context} + {fc.max_horizon} >\"\n        f\" {self.model.config.context_limit}.\"\n      )\n    if fc.use_continuous_quantile_head and (fc.max_horizon > self.model.os):\n      raise ValueError(\n        f\"Continuous quantile head is not supported for horizons > {self.model.os}.\"\n      )\n\n    self.forecast_config = fc\n    self.model.compile(\n      context=self.forecast_config.max_context,\n      horizon=self.forecast_config.max_horizon,\n      per_core_batch_size=fc.per_core_batch_size,\n    )\n    self.per_core_batch_size = self.forecast_config.per_core_batch_size\n    self.num_devices = self.model.num_devices\n    self.global_batch_size = (","sourceCodeStart":515,"sourceCodeEnd":551,"githubUrl":"https://github.com/google-research/timesfm/blob/331c6d33cb1ac2611de3056d0ac7164aab6301eb/src/timesfm/timesfm_2p5/timesfm_2p5_flax.py#L515-L551","documentation":"The TimesFM model has a fixed `context_limit` (max tokens/points the transformer can attend to). At compile time, `max_context + max_horizon` from the ForecastConfig must not exceed it, because compiled decoding processes context and future patches together. Exceeding it would require out-of-bounds positions, so compile() raises ValueError immediately with the offending numbers.","triggerScenarios":"Calling `model.compile(ForecastConfig(max_context=X, max_horizon=Y))` where X + Y > model.config.context_limit; raising max_horizon for covariates while keeping a large max_context; compiling for a smaller-capacity model variant with the same config used for a larger one.","commonSituations":"Copying a config from a blog/notebook for a different model size; increasing max_horizon to satisfy the covariate horizon check (error 16) without lowering max_context; choosing an overly large max_context 'to be safe'.","solutions":["Lower `max_context` (extra context beyond the model's patch capacity is trimmed anyway)","Lower `max_horizon` to the maximum you actually need","Check `model.config.context_limit` and pick max_context + max_horizon <= context_limit before compiling"],"exampleFix":"// before\nmodel.compile(forecast_config=ForecastConfig(max_context=1024, max_horizon=512))\n// after\nmodel.compile(forecast_config=ForecastConfig(max_context=768, max_horizon=256))  # within context_limit","handlingStrategy":"validation","validationCode":"if fc.max_context + fc.max_horizon > model.config.context_limit:\n    fc = dataclasses.replace(fc, max_context=model.config.context_limit - fc.max_horizon)\nmodel.compile(forecast_config=fc)","typeGuard":null,"tryCatchPattern":"try:\n    model.compile(forecast_config=fc)\nexcept ValueError as e:\n    if 'context limit' in str(e):\n        fc = dataclasses.replace(fc, max_context=model.config.context_limit - fc.max_horizon)\n        model.compile(forecast_config=fc)\n    else:\n        raise","preventionTips":["Read model.config.context_limit before building any ForecastConfig","Never copy max_context/max_horizon pairs from configs made for other model variants","Budget context vs horizon explicitly (context_limit - max_horizon = usable context)"],"tags":["python","validation","context-limit","config"],"backgroundTag":"context-length-exceeded","analyzedSha":"331c6d33cb1ac2611de3056d0ac7164aab6301eb","analyzedAt":"2026-08-29T01:04:23.138Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}