Abstract
Log-based Anomaly Detection (LogAD) is critical for maintaining large-scale cloud systems, yet existing methods often force a trade-off between automation and interpretability. This paper introduces CodeAD, a framework that leverages Large Language Models (LLMs) to automatically synthesize Python-based detection rules. By using LLMs as offline logic compilers rather than online detectors, CodeAD achieves the automation of deep learning with the execution speed and transparency of traditional rule-based systems. Evaluated on major datasets, CodeAD outperforms state-of-the-art models in F1 score while remaining significantly more cost-effective and efficient.
Introduction
System logs are the primary telemetry for diagnosing failures in complex distributed systems. However, monitoring these logs at scale presents a dilemma. Traditional manual rule-based systems are fast and interpretable but require immense human effort to maintain. Conversely, Deep Learning (DL) and direct LLM-based methods offer high automation but function as "black boxes" that are computationally expensive and slow for real-time, high-volume log streams.
CodeAD addresses this by synthesizing lightweight Python functions that encapsulate detection logic. This approach provides the "best of both worlds": the reasoning capabilities of AI and the sub-millisecond inference speed of native code.
Methodology
CodeAD operates through a bifurcated architecture consisting of an offline synthesis phase and an online monitoring phase.
1. Offline Synthesis
To handle millions of logs without overwhelming the LLM, CodeAD employs Hierarchical Agglomerative Clustering (HAC) to group similar log windows. From these clusters, the system performs Anchor-Grounded Sampling, selecting representative "anchor" windows and their most similar normal/abnormal counterparts. This contrastive set allows the LLM to pinpoint discriminative features, such as specific error keywords or frequency spikes.
2. Agentic Workflow
The core of the synthesis is a multi-agent loop designed to ensure code quality and generalizability:
- Generation Agent: Produces the initial Python detection logic.
- Local Testing & Repair: The code is executed against sampled windows; if it fails or contains syntax errors, a Repair Agent fixes the logic.
- Refine Agent: Optimizes the code to ensure it identifies patterns rather than overfitting to specific transient data like timestamps or IP addresses.
3. Online Monitoring
The resulting library of Python functions is deployed for real-time execution. Because these are simple scripts, they run on standard CPUs without requiring GPUs or expensive API calls, making them ideal for high-throughput streaming environments.
Experimental Evaluation
The framework was evaluated on three standard datasets: BGL (supercomputer logs), Hadoop (distributed file system), and Thunderbird (large-scale system logs with over 200 million messages).
Key Results
- Accuracy: CodeAD achieved an average 3.6% improvement in F1 score over the state-of-the-art AdaptiveLog baseline.
- Efficiency: On the Thunderbird dataset, CodeAD was 4x faster than other automated methods during the monitoring phase.
- Cost: Synthesizing rules for an entire dataset cost less than $4 USD, demonstrating high industrial viability.
- Interpretability: The output is human-readable Python code with comments, allowing Site Reliability Engineers (SREs) to audit or manually adjust rules as needed.
Conclusion and Future Work
CodeAD demonstrates that LLMs are most effective in system monitoring when used as generators of executable logic. This paradigm shift ensures that anomaly detection remains explainable and efficient at scale. Future research will focus on unsupervised synthesis to remove the dependency on labeled data and extending the framework to detect complex anomalies that span multiple services or long time horizons.
Summary updated