Evaluating LLMs for Secure Code Auditing: Metrics, Test Sets, and Run Protocols
For secure code auditing, strong results on general-purpose benchmarks are not a reliable proxy for finding real defects with an acceptable false-positive rate. This article presents a task-specific evaluation approach for large language models in secure code auditing. It covers how to define recall, precision, F1, turn consumption, and resource consumption; how to build a reliable case set with coverage, annotation, contamination control, and difficulty stratification; how to design repeatable run protocols with repetition, round limits, network isolation, and consistent environments; and how to score and summarize results with a minimal Python framework. It also discusses common interpretation pitfalls and practical recommendations for adopting the evaluation process in security workflows.
1. Why Security Auditing Needs Its Own Evaluation Method
General capability leaderboards measure aggregate performance on question answering, reasoning, or code generation, which does not fully match the requirements of security auditing tasks. Security auditing requires a model to find real defects in a large codebase while minimizing false positives. These two capabilities carry different weights. A model that performs well on general leaderboards may not achieve the same results in vulnerability discovery.
Therefore, evaluation should be designed around the security task itself. The object of evaluation should be “the ability to find real vulnerabilities,” not “the ability to write plausible-looking analysis reports.” Only when evaluation conclusions are based on this premise can they guide practical tool selection and workflow design.
2. Define Metrics Before Running the Data
Before evaluation, define the metrics clearly. Common metrics include:
- Recall: the proportion of real vulnerabilities found by the model under test out of all real vulnerabilities.
- Precision: the proportion of vulnerabilities reported by the model under test that are actually real.
- F1 score: the harmonic mean of recall and precision, used to measure both at the same time.
- Turn consumption: the number of exploration rounds used to complete a case, reflecting exploration efficiency.
- Resource consumption: the number of tokens or calls used to complete a case, reflecting runtime cost.
Metrics involve trade-offs. Pursuing recall alone produces many false positives, forcing security teams to verify each one and increasing manual cost. Looking only at precision may miss high-risk issues. Before selection, determine which end the business values more, then choose the primary metric and treat the others as constraints.
3. Building the Case Set
The quality of the case set determines the credibility of evaluation conclusions. Consider the following when building it:
- Coverage: cases should cover common vulnerability classes, such as injection, access control, memory safety, deserialization, and sensitive information disclosure. Avoid testing only one class of problem.
- Annotation standard: each case needs an explicit list of real vulnerabilities. Annotation should be completed independently by at least two experienced auditors and then aligned. Disagreements should be recorded and adjudicated by a third person.
- Deduplication and contamination control: cases should not appear in the public training materials of the models under test; otherwise results will skew high. If this cannot be confirmed, state the limitation in the report.
- Difficulty stratification: simple cases and complex cases should be reported separately so that averages do not hide weaknesses on complex problems.
With 32 cases, if the number of real vulnerabilities varies widely across cases, averaging per case and averaging across the total number of vulnerabilities may lead to different conclusions. Reports should present both and state which one is used as the primary conclusion.
4. Run Protocol: Repetition, Limits, and Isolation
A single run is heavily affected by randomness, so conclusions need repeated runs to stabilize. The protocol should specify the following:
- Repetition count: run each model on each case multiple times. A common practice is 3 runs, reporting both mean and standard deviation.
- Exploration round limit: set a uniform maximum number of rounds per run, for example 30. The limit must be identical for all subjects; otherwise comparison is meaningless.
- Network isolation: during evaluation, the model under test should not access the internet, to avoid retrieving answers externally and affecting fairness.
- Consistent environment: tool permissions, visible file scope, and timeout settings should be identical and recorded in the evaluation report.
When analyzing statistics, note the small sample size. Three repetitions plus 32 cases gives 96 runs per model. Metrics for a single model may still fluctuate significantly. When the gap between two models is small, conclusions should be drawn cautiously. If necessary, increase repetitions or perform significance testing.
5. A Minimal Evaluation Framework Example
The following code demonstrates how to convert single-run results into metrics and aggregate them by model. It uses only general data structures and statistical logic and can be replaced with the output format of any auditing system.
from dataclasses import dataclassfrom statistics import mean, stdev@dataclassclass RunResult:model_name: strcase_id: strrun_index: intreported: set[str] # Vulnerability IDs reported by the model under testground_truth: set[str] # Ground-truth vulnerability IDs from manual annotationrounds_used: inttokens_used: intdef score_run(r: RunResult) -> dict:tp = len(r.reported & r.ground_truth)fp = len(r.reported - r.ground_truth)fn = len(r.ground_truth - r.reported)recall = tp / (tp + fn) if (tp + fn) else 0.0precision = tp / (tp + fp) if (tp + fp) else 0.0f1 = (2 * precision * recall / (precision + recall)if (precision + recall) else 0.0)return {"recall": recall,"precision": precision,"f1": f1,"rounds": r.rounds_used,"tokens": r.tokens_used,}def summarize(results: list[RunResult]) -> dict:"""Input: all run results for the same model. Score each run first, then compute mean and standard deviation across runs."""scores = [score_run(r) for r in results]summary = {}for key in ("recall", "precision", "f1", "rounds", "tokens"):values = [s[key] for s in scores]summary[key] = {"mean": mean(values),"std": stdev(values) if len(values) > 1 else 0.0,}return summary
The example separates scoring from aggregation. Scoring depends only on the result of a single run, and aggregation depends only on scoring results. The two parts can be tested and reused independently.
6. Common Pitfalls in Interpreting Results
- Treating a single highest score as model capability. Look at both the mean and the variation range.
- Ignoring cost. When recall is similar, a lower-resource option is better suited to large-scale scanning.
- Ignoring the downstream cost of false positives. Low precision shifts workload to manual review.
- Generalizing conclusions to untested codebases. Programming language, framework, and vulnerability distribution all affect results, so the scope of conclusions should be stated clearly.
- Overlooking bias in the evaluation itself, such as annotator judgment tendencies and differences in prompt design.
7. Implementation Recommendations
- Start with historical vulnerabilities already confirmed internally to build a small case set and establish a reproducible evaluation process.
- Rank metrics by business priority and define the primary metric and constraints.
- Save complete run logs for each evaluation so that causes of false positives and misses can be reviewed.
- Make evaluation periodic. Rerun it when model versions under test are updated or the codebase structure changes significantly.
- Use evaluation conclusions to support decisions. Final confirmation and remediation of vulnerabilities still require security engineers.
