Online anomaly detection helps you catch issues in real time—like spotting traffic spikes on a website. Using detect_anomalies_online, you can monitor the latest data with rolling forecasts. This guide shows you how to get started and how it differs from historical detection.
Overview
Online (real-time) anomaly detection helps you identify unusual behavior in time series data as soon as it appears. With TimeGPT, you can:Flexible Control
More flexibility and control over the anomaly detection process.
Local & Global
Perform local and global anomaly detection.
Stream Processing
Detect anomalies on stream data as it arrives.
Quick Start
1
1. Set up your environment
Initialize your Python environment by importing the required libraries:
Environment Setup
2
2. Configure your NixtlaClient
Provide your API key (and optionally a custom base URL).
Nixtla Client Configuration
Using an Azure AI endpoint
To use an Azure AI endpoint, set the
To use an Azure AI endpoint, set the
base_url
argument:Azure AI Endpoint Setup
3
3. Load your dataset
We use a minute-level time series dataset that monitors server usage. This dataset is ideal for showcasing streaming data scenarios, where the task is to detect server failures or downtime in real time.
Load Server Dataset
We observe that the time series remains stable during the initial period; however, a spike occurs in the last 20 steps, indicating anomalous behavior. Our goal is to capture this abnormal jump as soon as it appears.

Server Data with Spike Anomaly
4
4. Detect anomalies in real time
Use the

detect_anomalies_online
method to identify anomalies by leveraging TimeGPT’s forecasting capabilities.Key Parameters
• df: A DataFrame containing your time series.
• time_col: Datestamp column.
• target_col: Variable to forecast.
• h: Forecast horizon (steps ahead).
• freq: Frequency (e.g., ‘min’).
• level: Confidence level (default 99%).
• detection_size: Number of recent steps to analyze for anomalies.
• df: A DataFrame containing your time series.
• time_col: Datestamp column.
• target_col: Variable to forecast.
• h: Forecast horizon (steps ahead).
• freq: Frequency (e.g., ‘min’).
• level: Confidence level (default 99%).
• detection_size: Number of recent steps to analyze for anomalies.
Run Online Anomaly Detection
Here we use a detection size of 100 to illustrate the anomaly detection process. In production, running detections more frequently with smaller detection sizes can help identify anomalies as soon as they occur.
Anomaly Detection Log Output
Anomaly Detection Log Output
Log Output
View last 5 anomaly detections
View last 5 anomaly detections
unique_id | ts | y | TimeGPT | anomaly | anomaly_score | TimeGPT-hi-99 | TimeGPT-lo-99 |
---|---|---|---|---|---|---|---|
machine-1-1_y_29 | 2020-02-01 22:11:00 | 0.606017 | 0.544625 | True | 18.463266 | 0.553161 | 0.536090 |
machine-1-1_y_29 | 2020-02-01 22:12:00 | 0.044413 | 0.570869 | True | -158.933850 | 0.579404 | 0.562333 |
machine-1-1_y_29 | 2020-02-01 22:13:00 | 0.038682 | 0.560303 | True | -157.474880 | 0.568839 | 0.551767 |
machine-1-1_y_29 | 2020-02-01 22:14:00 | 0.024355 | 0.521797 | True | -150.178240 | 0.530333 | 0.513261 |
machine-1-1_y_29 | 2020-02-01 22:15:00 | 0.044413 | 0.467860 | True | -127.848560 | 0.476396 | 0.459325 |

Identified Anomalies
5
5. Next steps
From the plot, we observe that the anomalous period is promptly detected. For a deeper dive into
detect_anomalies_online
—including parameter tuning and strategies for fine-tuning anomaly detection—stay tuned for our upcoming tutorial.