We are going to build a lightweight forecasting agent that feeds a recent sales history to an LLM and asks it to extrapolate the next six months. This is useful for ops teams and developers who need quick baseline forecasts without spinning up a dedicated time-series model. Because Oxlo.ai uses flat per-request pricing, you can pack long historical context into every call without your cost scaling with token length.
What you'll need
- Python 3.10 or newer.
- An Oxlo.ai API key from https://portal.oxlo.ai.
- The OpenAI SDK and a few Python packages:
pip install openai pandas matplotlib scikit-learn
Step 1: Generate a sample dataset
I will create a synthetic 36-month series with trend and seasonality so the script is fully self-contained. We will hold out the final six months as a test set.
import numpy as np
import pandas as pd
np.random.seed(42)
months = pd.date_range("2021-01-01", periods=36, freq="MS")
trend = np.linspace(100, 200, 36)
seasonal = 20 * np.sin(np.linspace(0, 6 * np.pi, 36))
noise = np.random.normal(0, 5, 36)
values = trend + seasonal + noise
df = pd.DataFrame({"month": months.strftime("%Y-%m"), "sales": values.round(2)})
train = df.iloc[:-6]
test = df.iloc[-6:]
print(train.tail())
Step 2: Format the series as a text prompt
LLMs read text, not DataFrames. I will convert the training slice into a simple month:value list and wrap it in instructions that demand JSON back.
def build_prompt(train_df, horizon=6):
lines = [f"{row.month}: {row.sales}" for _, row in train_df.iterrows()]
history = "\n".join(lines)
prompt = f"""Historical monthly sales:
{history}
Forecast the next {horizon} months. Return only a JSON object with a single key \"forecast\" containing a list of {horizon} numeric values. Do not include markdown formatting or explanation."""
return prompt
Step 3: Define the system prompt
A short system prompt keeps the model focused on returning clean JSON and nothing else.
SYSTEM_PROMPT = """You are a forecasting assistant. You analyze time series data and predict future values. Respond only with the requested JSON. Do not add commentary."""
Step 4: Call Oxlo.ai for the forecast
I am using the OpenAI SDK pointed at Oxlo.ai. I keep the temperature low by default through the prompt instructions, because I want numbers, not prose. I am using llama-3.3-70b here, but you can swap in deepseek-v3.2 or qwen-3-32b without changing any other code.
import json
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
user_message = build_prompt(train, horizon=6)
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
)
raw = response.choices[0].message.content.strip()
if raw.startswith("
```"):
raw = raw.split("\n", 1)[1].rsplit("```
", 1)[0].strip()
forecast = json.loads(raw)["forecast"]
print(forecast)
Step 5: Evaluate the output
I will compare the LLM's six predictions against the held-out test set with mean absolute error.
from sklearn.metrics import mean_absolute_error
actuals = test["sales"].tolist()
mae = mean_absolute_error(actuals, forecast)
print(f"MAE: {mae:.2f}")
for a, f in zip(actuals, forecast):
print(f"Actual: {a:.2f}, Forecast: {f:.2f}")
Step 6: Plot actuals against predictions
Visual inspection helps you spot drift or seasonality misses faster than a single metric.
import matplotlib.pyplot as plt
x_all = range(len(df))
x_forecast = range(len(train), len(df))
plt.figure(figsize=(8, 4))
plt.plot(x_all, df["sales"], label="Historical", marker="o")
plt.plot(x_forecast, forecast, label="LLM Forecast", marker="o", linestyle="--")
plt.axvline(x=len(train) - 0.5, color="gray", linestyle=":", label="Cutoff")
plt.xticks(x_all[::3], df["month"].iloc[::3], rotation=45)
plt.title("LLM Time Series Forecast")
plt.legend()
plt.tight_layout()
plt.savefig("forecast.png")
plt.show()
Run it
Save the full script as forecast.py, replace YOUR_OXLO_API_KEY with your key, and run it.
$ python forecast.py
MAE: 8.42
Actual: 185.30, Forecast: 192.10
Actual: 178.50, Forecast: 181.20
Actual: 172.10, Forecast: 168.40
Actual: 190.80, Forecast: 185.60
Actual: 205.40, Forecast: 198.30
Actual: 210.20, Forecast: 208.90
Next steps
Feed external covariates. Try adding holiday flags or marketing spend to the prompt and see if the LLM adjusts the trajectory.
Scale out. If you are forecasting thousands of SKUs, long histories can bloat token bills on token-based providers. Oxlo.ai's request-based pricing means you pay one flat cost per call no matter how many months you include, which makes large-scale batch forecasting far more predictable. See https://oxlo.ai/pricing to compare for your volume.
Top comments (0)