A package for detecting anomalies in time series data using LLMs
Project description
Anomaly Agent
๐ค A powerful Python library for detecting anomalies in time series data using Large Language Models (LLMs). Built with modern LangGraph architecture for robust, scalable anomaly detection across multiple variables and domains.
โจ Key Features
- ๐ง LLM-Powered Detection: Leverages advanced language models for intelligent anomaly identification
- ๐ Two-Stage Pipeline: Detection and optional verification phases to reduce false positives
- ๐ Multi-Variable Support: Analyze multiple time series variables simultaneously
- ๐ผ๏ธ Multimodal Analysis: Optional visual plots sent alongside data for enhanced pattern recognition
- ๐ฏ Domain Awareness: Contextual understanding of different data types and domains
- โก Modern Architecture: Built on LangGraph with Pydantic validation and robust error handling
- ๐ ๏ธ Customizable: Custom prompts, configurable verification, and flexible model selection
- ๐ Rich Output: Structured anomaly descriptions with timestamps and confidence indicators
- ๐ PostHog Integration: Built-in LLM analytics tracking when PostHog is configured
๐ Installation
pip install anomaly-agent
๐๏ธ How It Works
The Anomaly Agent uses a sophisticated two-stage pipeline powered by LangGraph state machines:
graph TD
A[๐ Input Time Series Data] --> B[๐ Detection Stage]
B --> C{๐ Verification Enabled?}
C -->|Yes| D[โ
Verification Stage]
C -->|No| E[๐ค Output Anomalies]
D --> F{๐ฏ Anomalies Confirmed?}
F -->|Yes| E
F -->|No| G[โ Filtered Out]
G --> E
style A fill:#e1f5fe
style B fill:#f3e5f5
style D fill:#fff3e0
style E fill:#e8f5e8
๐ง Architecture Components
- ๐ Detection Node: Uses LLM to identify potential anomalies with statistical and contextual analysis
- โ Verification Node (Optional): Secondary LLM review to reduce false positives with stricter criteria
- ๐ฏ State Management: Pydantic-based validation and error handling throughout the pipeline
- ๐ Multi-Variable Processing: Parallel analysis of multiple time series columns
โก Quick Start
Basic Usage
import pandas as pd
from anomaly_agent import AnomalyAgent
# Your time series data
df = pd.DataFrame({
'timestamp': pd.date_range('2024-01-01', periods=100, freq='D'),
'temperature': [20 + i*0.1 + (10 if i==50 else 0) for i in range(100)],
'pressure': [1013 + i*0.2 + (50 if i==75 else 0) for i in range(100)]
})
# Create agent and detect anomalies
agent = AnomalyAgent()
anomalies = agent.detect_anomalies(df)
# Convert to DataFrame for analysis
df_anomalies = agent.get_anomalies_df(anomalies)
print(df_anomalies)
Advanced Configuration
from anomaly_agent import AnomalyAgent
# Customize model and verification behavior
agent = AnomalyAgent(
model_name="gpt-4o-mini", # Choose your preferred model
verify_anomalies=True, # Enable verification stage
timestamp_col="date", # Custom timestamp column name
include_plot=True # Include visual plot for multimodal analysis
)
# Custom prompts for domain-specific detection
financial_detection_prompt = """
You are a financial analyst detecting market anomalies.
Focus on: unusual price movements, volume spikes, trend reversals.
Consider market hours and economic events in your analysis.
"""
agent = AnomalyAgent(detection_prompt=financial_detection_prompt)
anomalies = agent.detect_anomalies(financial_data)
Multimodal Analysis with Visual Plots
Enable include_plot=True to send a time series visualization alongside the numeric data. This leverages multimodal LLM capabilities (e.g., gpt-4o-mini) for enhanced pattern recognition:
from anomaly_agent import AnomalyAgent
# Enable visual analysis - LLM sees both the plot and numeric data
agent = AnomalyAgent(include_plot=True)
anomalies = agent.detect_anomalies(df)
When enabled:
- A matplotlib plot is generated for each time series variable
- The plot is encoded as base64 PNG and sent to the LLM
- The LLM analyzes both visual patterns and numeric values
- Works with any multimodal-capable model (gpt-4o-mini, gpt-4o, etc.)
This is particularly useful for:
- Detecting visual patterns that may not be obvious in raw numbers
- Identifying trend changes, seasonality, and outliers
- Getting more context-aware anomaly descriptions
๐ Examples and Notebooks
๐ Examples Directory
Explore comprehensive examples in the examples/ folder:
examples.py: Complete CLI examples with different scenarioscustom_prompts_example.py: Domain-specific prompt customizationexamples.ipynb: Interactive Jupyter notebook with visualizations
๐ฎ Interactive Examples
# Run basic example
python examples/examples.py --example basic --plot
# Try real-world sensor data scenario
python examples/examples.py --example real-world --plot
# Custom model and plotting
python examples/examples.py --model gpt-4o-mini --example multiple --plot
๐ Jupyter Notebooks
Launch the interactive notebook:
๐ Output Formats
Long Format (Default)
df_anomalies = agent.get_anomalies_df(anomalies)
| timestamp | variable_name | value | anomaly_description |
|---|---|---|---|
| 2024-01-15 | temperature | 35.2 | Significant temperature spike... |
| 2024-01-20 | pressure | 1089.3 | Unusual pressure reading... |
Wide Format
df_anomalies = agent.get_anomalies_df(anomalies, format="wide")
| timestamp | temperature | temperature_description | pressure | pressure_description |
|---|---|---|---|---|
| 2024-01-15 | 35.2 | Significant spike... | NaN | NaN |
| 2024-01-20 | NaN | NaN | 1089.3 | Unusual reading... |
๐๏ธ Model Configuration
Choose the right model for your needs and budget:
| Model | Cost (Input/Output per 1M tokens) | Best For | Performance |
|---|---|---|---|
gpt-5-nano |
$0.05 / $0.40 | Cost-effective anomaly detection | โญโญโญ |
gpt-5-mini |
$0.25 / $2.00 | Enhanced reasoning for complex patterns | โญโญโญโญ |
gpt-5 |
$1.25 / $10.00 | Sophisticated domain-specific analysis | โญโญโญโญโญ |
gpt-4o-mini |
$0.60 / $2.40 | Legacy support with good performance | โญโญโญโญ |
# Cost-optimized (default)
agent = AnomalyAgent(model_name="gpt-5-nano")
# Enhanced reasoning
agent = AnomalyAgent(model_name="gpt-5-mini")
# Premium analysis
agent = AnomalyAgent(model_name="gpt-5")
๐ฏ Use Cases
๐ข Business & Operations
- ๐ Sales Analytics: Detect unusual sales patterns, seasonal anomalies
- ๐ญ Manufacturing: Monitor equipment performance, quality metrics
- ๐ฐ Financial Services: Fraud detection, market anomaly identification
- ๐ Web Analytics: Traffic spikes, user behavior anomalies
๐ฌ Science & Engineering
- ๐ก๏ธ IoT Sensors: Temperature, humidity, pressure monitoring
- โก Energy Systems: Power consumption, grid stability analysis
- ๐ฉบ Healthcare: Patient monitoring, medical device readings
- ๐ Environmental: Weather patterns, pollution levels
๐ Data Quality
- ๐ Data Validation: Identify measurement errors, sensor failures
- ๐ ETL Monitoring: Pipeline anomalies, data drift detection
- ๐ฏ Quality Assurance: Automated anomaly flagging in data workflows
๐ ๏ธ Development
This project uses uv for fast, reliable dependency management. All commands automatically handle virtual environment management.
๐๏ธ Setup
# Clone the repository
git clone https://github.com/andrewm4894/anomaly-agent.git
cd anomaly-agent
# Install dependencies (creates .venv automatically)
make sync-dev
๐งช Testing
# Run all tests with coverage
make test
# Run specific test categories
uv run pytest tests/test_agent.py -v # Core functionality
uv run pytest tests/test_prompts.py -v # Prompt system
uv run pytest tests/test_graph_architecture.py -v # Advanced architecture
# Integration tests (requires OPENAI_API_KEY in .env)
uv run pytest tests/ -m integration -v
๐ Code Quality
# Install pre-commit hooks
make pre-commit-install
# Run all quality checks
make pre-commit
# Individual tools
uv run black anomaly_agent/ # Formatting
uv run isort anomaly_agent/ # Import sorting
uv run flake8 anomaly_agent/ # Linting
uv run mypy anomaly_agent/ # Type checking
๐ฆ Dependencies
# Add new dependencies
make add PACKAGE=pandas # Runtime dependency
make add-dev PACKAGE=pytest # Development dependency
# Update all dependencies
make update
# Remove dependencies
make remove PACKAGE=old-package
โ๏ธ Environment Setup
Create a .env file in your project root:
# Required for anomaly detection
OPENAI_API_KEY=your-openai-api-key-here
# Optional: Custom model defaults
DEFAULT_MODEL_NAME=gpt-5-nano
The agent automatically loads environment variables via python-dotenv.
๐๏ธ Architecture Deep Dive
For detailed technical information about the internal architecture, see ARCHITECTURE.md.
Key architectural features:
- ๐ง LangGraph State Machines: Robust workflow management with proper error handling
- โ Pydantic Validation: Type-safe data models throughout the pipeline
- ๐ฏ GraphManager Caching: Optimized performance with reusable compiled graphs
- ๐ Class-based Nodes: Modular, maintainable node architecture
- ๐ Async Support: Streaming and parallel processing capabilities
๐ค Contributing
We welcome contributions! Please see our contributing guidelines for details.
- ๐ด Fork the repository
- ๐ฟ Create a feature branch (
git checkout -b feature/amazing-feature) - โ
Test your changes (
make test) - ๐ Commit your changes (
git commit -m 'Add amazing feature') - ๐ Push to the branch (
git push origin feature/amazing-feature) - ๐ฏ Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anomaly_agent-0.13.2.tar.gz.
File metadata
- Download URL: anomaly_agent-0.13.2.tar.gz
- Upload date:
- Size: 447.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da71469699c4b5a880e15508080818ce8e534613c8d3d09f5d09ac22ec575614
|
|
| MD5 |
972515700140f321f739693d2ba8bbb0
|
|
| BLAKE2b-256 |
e7037edad08a095ed95e39d8b405f207b2e629d22913c336806aa6d3be21d281
|
Provenance
The following attestation bundles were made for anomaly_agent-0.13.2.tar.gz:
Publisher:
release.yml on andrewm4894/anomaly-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
anomaly_agent-0.13.2.tar.gz -
Subject digest:
da71469699c4b5a880e15508080818ce8e534613c8d3d09f5d09ac22ec575614 - Sigstore transparency entry: 1339600518
- Sigstore integration time:
-
Permalink:
andrewm4894/anomaly-agent@b4a027d12538b206c0c11c215e10d93f5ef691e3 -
Branch / Tag:
refs/tags/v0.13.2 - Owner: https://github.com/andrewm4894
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b4a027d12538b206c0c11c215e10d93f5ef691e3 -
Trigger Event:
release
-
Statement type:
File details
Details for the file anomaly_agent-0.13.2-py3-none-any.whl.
File metadata
- Download URL: anomaly_agent-0.13.2-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cda16f86efbc22a469575680edfe61256a2f6f417dfff41d6541487401484529
|
|
| MD5 |
19d5e38cf89191145b8acb367bae66d0
|
|
| BLAKE2b-256 |
dbcf38f6813e12c1a73845ffeaedd1d67bdc9dc47f688060d1ae642d049da290
|
Provenance
The following attestation bundles were made for anomaly_agent-0.13.2-py3-none-any.whl:
Publisher:
release.yml on andrewm4894/anomaly-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
anomaly_agent-0.13.2-py3-none-any.whl -
Subject digest:
cda16f86efbc22a469575680edfe61256a2f6f417dfff41d6541487401484529 - Sigstore transparency entry: 1339600536
- Sigstore integration time:
-
Permalink:
andrewm4894/anomaly-agent@b4a027d12538b206c0c11c215e10d93f5ef691e3 -
Branch / Tag:
refs/tags/v0.13.2 - Owner: https://github.com/andrewm4894
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b4a027d12538b206c0c11c215e10d93f5ef691e3 -
Trigger Event:
release
-
Statement type: