We are going to build a lightweight forecasting agent that feeds historical time series data to an LLM and returns structured predictions with trend commentary. This is useful for ops teams and developers who need quick baseline forecasts without spinning up a separate ML pipeline. Because Oxlo.ai charges a flat rate per request, you can pass long context windows of historical data without worrying about ballooning token costs.
What you'll need
Before starting, make sure you have the following:
- Python 3.10 or newer
- An Oxlo.ai API key from https://portal.oxlo.ai
- The OpenAI SDK and Pandas installed:
pip install openai pandas
Step 1: Set up the Oxlo.ai client
First, import the required packages and initialize the OpenAI-compatible client pointing to Oxlo.ai. I keep my API key in an environment variable so it does not end up in source control.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ.get("OXLO_API_KEY")
)
Step 2: Prepare sample time series data
I will generate ninety days of synthetic daily CPU load for a server. The series has a slight upward trend and random noise so the model has something to extrapolate.
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
def generate_cpu_data(days=90):
rng = np.random.default_rng(42)
base = datetime.now() - timedelta(days=days)
dates = [base + timedelta(days=i) for i in range(days)]
trend = np.linspace(45, 65, days)
noise = rng.normal(0, 5, days)
values = np.clip(trend + noise, 0, 100)
return pd.DataFrame({
"date": [d.strftime("%Y-%m-%d") for d in dates],
"cpu_percent": values.round(2)
})
df = generate_cpu_data()
print(df.tail())
Step 3: Write the forecaster system prompt
The prompt tells the model to act as a forecasting analyst and to return only valid JSON. I ask for a seven day forecast, a short trend summary, and a confidence level.
SYSTEM_PROMPT = """You are a time series forecasting assistant.
The user will provide a CSV string containing historical daily observations with columns 'date' and 'cpu_percent'.
Your job is to predict the next 7 days.
Respond with a single JSON object containing exactly these keys:
- forecast: an array of 7 objects, each with 'date' (YYYY-MM-DD) and 'predicted_cpu_percent' (number)
- trend_summary: a one sentence description of the observed trend
- confidence: one of low, medium, or high
Do not wrap the JSON in markdown code fences."""
Step 4: Build the forecasting function
This helper formats the recent history as a CSV string, sends it to Oxlo.ai, and parses the JSON response. I send the last sixty days to give the model enough context while staying well within context limits.
import json
def forecast_cpu(df, context_days=60):
context = df.tail(context_days).to_csv(index=False)
user_message = f"Historical data:\n{context}\n\nPredict the next 7 days."
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
return json.loads(raw)
Run it
Call the function on our synthetic dataset and print the result.
if __name__ == "__main__":
df = generate_cpu_data()
result = forecast_cpu(df)
print(json.dumps(result, indent=2))
Example output:
{
"forecast": [
{"date": "2024-07-15", "predicted_cpu_percent": 66.4},
{"date": "2024-07-16", "predicted_cpu_percent": 67.1},
{"date": "2024-07-17", "predicted_cpu_percent": 65.8},
{"date": "2024-07-18", "predicted_cpu_percent": 68.2},
{"date": "2024-07-19", "predicted_cpu_percent": 67.5},
{"date": "2024-07-20", "predicted_cpu_percent": 69.0},
{"date": "2024-07-21", "predicted_cpu_percent": 68.4}
],
"trend_summary": "The series shows a gradual upward trend from the mid 40s to the high 60s with moderate daily volatility.",
"confidence": "medium"
}
Wrap-up
This agent gives you a working baseline forecast in under fifty lines of code. Two concrete next steps: backtest the forecaster against a holdout set to measure directional accuracy, or wrap the forecast_cpu function in a FastAPI endpoint so your monitoring stack can request predictions on demand. If you need deeper reasoning for complex seasonality, swap llama-3.3-70b for deepseek-r1-671b or kimi-k2.6 on Oxlo.ai without changing any client code.
Top comments (0)