8 Forecasting as a language task
This chapter explores reframing time series forecasting as a language task so that large language models can be prompted to produce numeric predictions. Building on the PromptCast idea, the workflow turns past values into a natural language prompt, sends it to an LLM, and then post-processes the textual output to recover future values. The chapter reviews key prompting strategies—zero-shot, few-shot, and chain-of-thought—and shows how model choice shapes prompt design (for example, phrasing as questions for Flan-T5 versus explicit instructions for Llama). It also stresses practical safeguards such as scaling targets to avoid large numbers, constraining outputs to comma-separated numerals, and writing robust parsers to handle imperfect generations.
Using Flan-T5 locally, the chapter implements a forecasting function that scales inputs, applies few-shot prompting, and parses the response into numeric predictions before inverting the scale. In zero-shot settings the model often produced too few steps and repeated values, yielding flat forecasts; however, under rolling cross-validation its error landed in the same ballpark as one of the prior foundation models tested on the book’s retail dataset (albeit worse than finetuned, time-series–specific baselines). Incorporating an exogenous holiday flag through prompt conditioning notably degraded performance, suggesting sensitivity to covariates in this setup. For anomaly detection, a chain-of-thought style prompt and chunked processing failed to generalize: Flan-T5 tended to output a single “normal” label for entire chunks, leading to near-zero detection metrics.
Llama-3.2-3b-instruct was accessed via API with a system prompt to shape behavior and concise user prompts to request fixed-length numeric outputs. Despite simpler post-processing and stronger instruction following, its zero-shot forecasts were frequently unreasonable, and cross-validation results were clearly worse than Flan-T5 and prior specialized models in this scenario. For anomaly detection, the model over-flagged points, achieving high recall but very low precision and a poor F1 score. Overall, the chapter concludes that while LLMs can be coerced into forecasting and anomaly detection through careful prompt engineering and parsing, they are not purpose-built for time series, often underperform specialized methods, and require extra glue code. They are most useful when a natural language interface is essential and an LLM is already available; the next chapter explores making LLMs more fit-for-purpose for time series tasks.
General steps of forecasting with large language models. The values of the input sequence must be formatted as a prompt. This prompt is sent to the LLM which outputs a string. The string must then be processed to extract the future values.
Steps to forecast with a large language model like Flan-T5. First, we scale the data to avoid working with too large numbers. Then, we use few-shot prompting. The output is then processed to extract the numerical values. Finally, we inverse the scaling to bring the predictions back to the original scale.
Zero-shot predictions using Flan-T5. Here, most of the predictions are flat as the LLM seem to output the same values. Also, the model does not output all eight values, forcing us to repeat the last prediction to complete the sequence.
Predictions obtained from cross-validation with Flan-T5. The predictions seem to overlap with the actual values and some peaks and valleys were correctly predicted.
Cross-validation with exogenous features. Here, the quality of the predictions has clearly degraded compared to not using features.
Comparing the performance between using and not using features. Lower is better. Here, the model that uses features is significantly worse than the model not using features.
Zero-shot predictions from Llama-3.2. While we are forecasting for time steps with unknown values, the predictions for Store 1 seem unreasonable.
Predictions from cross-validation with Llama-3.2 Here, the predictions seem to be fairly off from the actual values.
Comparing the performance of Flan-T5 and Llama-3.2 from cross-validation. Here, Flan-T5 performs much better than Llama-3.2, as it achieves lower scores on both performance metrics. Lower is better.
Anomaly detection with Llama-3.2 Flagged anomalies are shown as diamonds. Here, we notice that the model labels way too many points as anomalies. Therefore, the recall will be high, but the precision will be very low, making the F1-score low as well.
F1-score of each foundation model tested so far for anomaly detection. Higher is better. Here, Moirai remains the top model for anomaly detection in this particular scenario, and Llama-3.2 achieves the worst performance.
Summary
- Large language models (LLMs) are foundation models in natural language processing that can handle different natural language tasks, like question answering, text classification and more.
- Since LLMs are trained for language tasks, we can frame the forecasting problem as a language task to use LLMs. This involves building a prompt and asking the model to predict the next set of values from an input sequence.
- To interact with LLMs, we use prompting and using few-shot prompts and chain-of-thought prompts usually give better results.
- We can work with LLMs both locally and via API calls.
- LLMs can perform forecasting tasks, but they were not built for this purpose, which may result in poor performances.
Time Series Forecasting Using Foundation Models ebook for free