CodeAD: Synthesize Code of Rules for Log-based Anomaly Detection with LLMs
Preprint
Abstract
Log-based anomaly detection (LogAD) is critical for maintaining the reliability and availability of large-scale online service systems. While machine learning, deep learning, and large language models (LLMs)-based methods have advanced the LogAD, they often suffer from limited interpretability, high inference costs, and extensive preprocessing requirements, limiting their practicality for real-time, high-volume log analysis. In contrast, rule-based systems offer efficiency and transparency, but require significant manual effort and are difficult to scale across diverse and evolving environments. In this paper, We present CodeAD, a novel framework that automatically synthesizes lightweight Python rule functions for LogAD using LLMs. CodeAD introduces a hierarchical clustering and anchor-grounded sampling strategy to construct representative contrastive log windows, enabling LLMs to discern discriminative anomaly patterns. To ensure robustness and generalizability, CodeAD employs an agentic workflow that iteratively generates, tests, repairs, and refines the rules until it meets correctness and abstraction requirements. The synthesized rules are interpretable, lightweight, and directly executable on raw logs, supporting efficient and transparent online anomaly detection. Our comprehensive experiments on three public datasets (BGL, Hadoop, Thunderbird) demonstrate that CodeAD achieves an average absolute improvement of 3.6% F1 score over the state-of-the-art baselines, while processing large datasets up to 4x faster and at a fraction of the cost (total LLM invocation cost under 4 USD per dataset). These results highlight CodeAD as a practical and scalable solution for online monitoring systems, enabling interpretable, efficient, and automated LogAD in real-world environment.Keywords
All Related Versions
This publication has 59 references indexed in Scilit:
- A survey on large language models for software engineeringScience China Information Sciences, 2026
- A comprehensive study of machine learning techniques for log-based anomaly detectionEmpirical Software Engineering, 2025
- LogLM: From Task-Based to Instruction-Based Automated Log Analysis2025
- GELog: a GPT-Enhanced Log Representation Method for Anomaly Detection2025
- EagerLog: Active Learning Enhanced Retrieval Augmented Generation for Log-based Anomaly Detection2025
- Impact of log parsing on deep learning-based anomaly detectionEmpirical Software Engineering, 2024
- Log Anomaly to Resolution: AI Based Proactive Incident Remediation2021
- Log-based Anomaly Detection Without Log Parsing2021
- Tools and Benchmarks for Automated Log Parsing2019
- What Supercomputers Say: A Study of Five System Logs2007