Peregrine falcon gliding across a pale sky, with the article title about generating Falco rules with an LLM
Cover based on a photo by Alan Schmierer, via Wikimedia Commons, CC0 1.0

You can generate Falco rules with an LLM in a few seconds, and the YAML will usually look plausible. Then Falco refuses to load it. Models borrow field names from other tools (process.name instead of proc.name), invent priorities (high), and lean on macros that only exist in the default ruleset. The ones that do load often fire on cron and package installs. Using an LLM for Falco rule writing works when the model drafts and Falco decides: falco -V for the rule itself, then replaying captured system-call traces to check detections and false positives before anything reaches a cluster.

This guide walks through that loop on one small example, a rule for reads of /etc/shadow, and then applies it to a Kubernetes service account token rule. Every Falco output quoted below comes from running Falco 0.45.2 on the rules shown. The rule drafts were written for this guide to show mistakes models commonly make; they are not a captured model transcript. Model output is never the evidence. Falco's output is.

It's the runtime-detection sibling of our guides to Sigma and YARA rules with an LLM and Semgrep rules with an LLM. Same idea, different validator.

Defensive and authorized work only. Write and test rules on systems you own or run for a client who hired you. Don't paste production hostnames, secrets or customer data into a prompt you aren't cleared to share; see what to send the model below.

What a Falco rule looks like

Falco is an Apache-2.0 project that describes itself as "Cloud Native Runtime Security." It watches system calls (and plugin event sources) and fires when an event matches a rule's condition. The latest release at the time of this update is 0.45.2, published October 29, 2026.

Per Falco's basic elements reference, a rule needs rule, desc, condition, output and priority. Priority must be one of EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFORMATIONAL or DEBUG, and the same page suggests WARNING for unauthorized reads of sensitive state. Rules can also use list and macro objects, and conditions use Falco's own condition syntax and supported fields, not Sigma modifiers or Semgrep patterns.

That's a good fit for a model: short YAML, a known schema and a clear behavior to describe. What it can't know is your environment, such as which daemons legitimately read credentials, or which macros exist in the ruleset you actually load. That's why the trace replay matters more than the prompt.

The loop: the model drafts, Falco decides

  1. Give the model evidence. Describe the behavior, paste the field names it may use (from falco --list), and include one existing rule from your ruleset as a style example. Ask for YAML only.
  2. Validate with your real config. Run falco -c falco.yaml -V falco_rules.yaml -V your_rule.yaml. Load the same plugins and base rules you run in production, or validation tells you nothing useful.
  3. Replay positive traces. Run the rule against .scap captures that contain the behavior. It should fire.
  4. Replay negative traces. Run it against captures of normal activity: package installs, cron, deploys. It should stay quiet.
  5. Feed back exact errors and noisy events. Paste Falco's error or the false-positive output lines back to the model and ask for a corrected rule, not an explanation.
  6. Review, then deploy. A human reads the final condition before it ships.
Diagram of the loop to generate Falco rules with an LLM: the model drafts a rule, falco -V validates fields, macros and priority, replaying positive and negative .scap traces checks detections and false positives, exact errors go back to the model, and only passing rules reach human review and deployment
Diagram of the loop to generate Falco rules with an LLM: the model drafts a rule, falco -V validates fields, macros and priority, replaying positive and negative .scap traces checks detections and false positives, exact errors go back to the model, and only passing rules reach human review and deployment Image: RedRouter, Original

Step 1: Validate the first draft with falco -V

Here's a typical first draft for "alert when something other than an auth tool reads /etc/shadow":

- rule: Shadow File Read by Non-Auth Process
  desc: Detects a process outside the expected authentication tools opening /etc/shadow for reading
  condition: >
    open_read and fd.name = /etc/shadow
    and not process.name in (sshd, sudo, su, login, passwd, unix_chkpwd)
  output: Shadow file read (user=%user.name proc=%proc.name cmdline=%proc.cmdline container=%container.name)
  priority: high

It reads well. Falco stopped on four problems, one at a time.

Invalid priority. high isn't a Falco priority:

$ falco -c falco.yaml -V rule.yaml
rule.yaml: Invalid
...
  priority: high
            ^
LOAD_ERR_YAML_VALIDATE (Error validating internal structure of YAML file): Invalid priority

Missing plugin. After changing it to WARNING and validating alongside the default rules, the stock config shipped in the 0.45.2 tarball fails before it even reaches the new rule. That config has load_plugins: [], and the default ruleset needs the container plugin:

$ falco -c falco.yaml -V falco_rules.yaml -V rule.yaml
Error: Plugin requirement not satisfied, must load one of: container (>= 0.4.0)

Container fields such as %container.name and container.id come from the container plugin. Validate with load_plugins: [container], as you'd run it in production.

Undefined macro. Validating the rule file on its own, with the plugin loaded:

LOAD_ERR_VALIDATE (Error validating rule/macro/list/exception objects): Undefined macro 'open_read' used in filter.

open_read is real, but it's defined in falco_rules.yaml, not in Falco itself. In 0.45.2 it expands to evt.type in (open,openat,openat2) and evt.is_open_read=true and fd.typechar='f' and fd.num>=0. Always validate your rule together with the base rules it depends on.

Nonexistent field. With both files loaded, the last error appears:

LOAD_ERR_COMPILE_CONDITION (Error compiling condition): filter_check called with nonexistent field process.name

Falco's field is proc.name. After that fix:

falco_rules.yaml: Ok
rule.yaml: Ok

Every one of those errors is cheap to fix and easy to paste back to a model. Not one of them would have been caught by reading the YAML.

Step 2: Replay traces to test detections

A rule that loads isn't a rule that works. Falco can replay a capture file instead of watching the live kernel; the falco.yaml for 0.45.2 documents a replay engine that takes engine.replay.capture_file. The Falco project publishes positive and negative trace sets that its testing repo uses; we downloaded traces-positive.zip and traces-negative.zip from download.falco.org.

falco -c falco.yaml \
  -r falco_rules.yaml -r rule.yaml \
  -o engine.kind=replay \
  -o engine.replay.capture_file=traces-positive/read-sensitive-file-untrusted.scap

The first replay surprised us. The capture contains cat /etc/shadow, and only the stock rule fired:

Events detected: 1
Triggered rules by rule name:
   Read sensitive file untrusted: 1

Our rule matched too, but Falco never told us. Falco's default rule_matching: first stops at the first matching rule, and the stock rule loads first. The falco.yaml comments warn that with first, "only the first defined rule will trigger, possibly shadowing other rules." When testing a new rule next to the default ruleset, add -o rule_matching=all:

Events detected: 2
Triggered rules by rule name:
   Read sensitive file untrusted: 1
   Shadow File Read by Non-Auth Process: 1

Keep this in mind for production as well. If your rule duplicates a stock rule, it may never alert where you think it will.

Step 3: Replay negative traces to find false positives

Next, the same rule against all 11 negative traces (normal activity such as curl-install, kernel-upgrade, git-push and three "staging" hosts). It fired 13 times across 6 of them:

TraceAlertsProcess that read /etc/shadow
curl-install3dpkg-preconfigu, perl, frontend
curl-uninstall3frontend, perl
kernel-upgrade3dpkg-preconfigu, frontend, perl
staging-collector1crond
staging-db2cron
staging-worker1crond

That's the classic LLM mistake. The model listed the auth tools it knew about and missed cron and Debian package tooling, which read /etc/shadow all the time. A second draft excluded cron_binaries and package_mgmt_binaries (both lists exist in the default rules), plus the debconf frontend and dpkg-preconfigu processes. That cut the noise to 4 alerts in 3 traces, all one process:

Shadow file read (user=root proc=perl parent=man-db.postinst cmdline=perl -e @pwd = getpwnam(\"man\") ...

The default ruleset already has a macro for exactly that case, mandb_postinst. Adding and not mandb_postinst gave the third draft:

- rule: Shadow File Read by Non-Auth Process
  desc: Detects a process outside the expected authentication, cron and package tools opening /etc/shadow for reading
  condition: >
    open_read and fd.name = /etc/shadow
    and not proc.name in (sshd, sudo, su, login, passwd, unix_chkpwd, cron_binaries, package_mgmt_binaries)
    and not proc.pname in (package_mgmt_binaries, frontend)
    and not proc.name in (dpkg-preconfigu, frontend)
    and not mandb_postinst
  output: Shadow file read (user=%user.name proc=%proc.name parent=%proc.pname cmdline=%proc.cmdline file=%fd.name)
  priority: WARNING

Result: 0 alerts across all 11 negative traces, and it still fired on the three positive traces that read /etc/shadow (read-sensitive-file-untrusted, read-sensitive-file-after-startup and falco-event-generator).

There's a lesson beyond the rule. Falco's stock "Read sensitive file untrusted" rule already covers this behavior, with a much longer exception list built from years of real noise. If a model's rule overlaps a default rule, you're usually better off tuning the stock rule with exceptions or overrides than shipping a weaker copy. Ask the model to check the default ruleset first.

A Kubernetes example: service account token reads

Custom rules earn their keep on behavior specific to your environment. In Kubernetes, pods get a service account token mounted at /var/run/secrets/kubernetes.io/serviceaccount/token by default (Kubernetes docs), and stealing it maps to MITRE ATT&CK T1552.007, Container API. The stock ruleset we checked has no rule for reads of that path, and only you know which workloads should read it.

- list: sa_token_readers
  items: [my-operator, vault-agent]

- macro: sa_token_path
  condition: >
    (fd.name startswith /var/run/secrets/kubernetes.io/serviceaccount/
     or fd.name startswith /run/secrets/kubernetes.io/serviceaccount/)

- rule: Service Account Token Read by Unexpected Process
  desc: >
    A process inside a container opened the mounted Kubernetes service account
    token, and it is not on the list of workloads expected to call the API server.
  condition: >
    open_read and container.id != host
    and sa_token_path
    and not proc.name in (sa_token_readers)
  output: >
    Service account token read (proc=%proc.name parent=%proc.pname cmdline=%proc.cmdline
    file=%fd.name container=%container.name image=%container.image.repository
    pod=%k8s.pod.name ns=%k8s.ns.name)
  priority: WARNING
  tags: [container, k8s, credential_access, mitre_credential_access]

Replace the list items with your real readers. Falco 0.45.2 validated it (Ok) with the container plugin loaded, and it raised nothing on the public kube-demo and docker-compose negative traces. Neither public trace contains a read of the token, so they can't serve as a true positive. Test that in a lab cluster you control: deploy Falco (for example with the Falco Helm charts), start a throwaway pod, run cat on the token path with kubectl exec, and confirm one alert. Then let it run alongside your normal workloads for a while and add every legitimate reader to the list before you raise the priority or page anyone.

Automating the loop with an OpenAI-compatible API

The loop scripts easily. This sketch asks a model for a rule, runs falco -V, replays one positive and one negative trace, and sends any error or false-positive lines back. It uses the OpenAI Python SDK with RedRouter's https://redrouter.org/v1 base URL, as covered in our OpenAI-compatible API guide and the RedRouter quickstart.

import pathlib, subprocess
from openai import OpenAI

client = OpenAI(base_url="https://redrouter.org/v1")  # reads OPENAI_API_KEY
BASE = ["falco", "-c", "falco.yaml", "-r", "falco_rules.yaml"]
rule = pathlib.Path("rules/shadow-read.yaml")
task = "Alert when a non-auth process opens /etc/shadow. Use only fields from fields.txt."
fields = pathlib.Path("fields.txt").read_text()  # falco -c falco.yaml --list -N

def replay(trace):
    out = subprocess.run(BASE + ["-r", str(rule), "-o", "rule_matching=all",
        "-o", "engine.kind=replay", "-o", f"engine.replay.capture_file={trace}"],
        capture_output=True, text=True).stdout
    return [l for l in out.splitlines() if "Shadow file read" in l]

feedback = "No previous attempt."
for attempt in range(4):
    resp = client.chat.completions.create(
        model="glm-5.3-flash-red",
        messages=[
            {"role": "system", "content": "Return exactly one Falco rule as YAML. No prose, no code fences."},
            {"role": "user", "content": f"{task}\nFields:\n{fields[:20000]}\nFeedback:\n{feedback}"},
        ],
    )
    rule.write_text(resp.choices[0].message.content)
    check = subprocess.run(["falco", "-c", "falco.yaml", "-V", "falco_rules.yaml",
                            "-V", str(rule)], capture_output=True, text=True)
    if check.returncode != 0:
        feedback = (check.stdout + check.stderr)[-4000:]
        continue
    hits = replay("traces-positive/read-sensitive-file-untrusted.scap")
    noise = replay("traces-negative/staging-db.scap")
    if hits and not noise:
        print(f"passed on attempt {attempt + 1}")
        break
    feedback = f"Positive hits: {len(hits)} (want >0). False positives:\n" + "\n".join(noise)

Two rules for the script. The model never chooses the traces, and a pass still goes to a human, because two traces are a smoke test, not coverage. Run the full negative set before you trust it.

Which model to use

Rule drafting is short structured output, so a fast model with a large context window is usually enough. GLM 5.3 Flash Red (glm-5.3-flash-red) is $0.25 in / $0.10 cached / $1.00 out per 1M tokens, 1M context, which leaves room for the full field list and several example rules. red-cyber-1 (Beta) is $0.28 in / $0.10 cached / $1.10 out per 1M tokens, 1M context and is the model RedRouter's docs feature for authorized security work. See pricing and the model catalog for current rates and limits.

Detection engineering means describing attacker behavior in precise terms: credential theft, container escapes, reverse shells. Some general-purpose assistants hesitate at exactly that wording, even for defensive rules. RedRouter is built for authorized security work, and if you'd rather keep your current agent and swap only the model behind it, our agent broker post covers that pattern.

What to send the model

Send the behavior, the field list and a sanitized example rule. Falco output lines from real hosts can contain usernames, command lines, image names and pod names, so strip or replace those before pasting false positives back. On RedRouter, account Zero Data Retention is an opt-in setting and is off by default; the data handling docs explain it alongside per-key content storage, and what RedRouter keeps covers the rest.

Common mistakes when an LLM writes Falco rules

  • Wrong field names. process.name, file.path, container.image. Give the model the output of falco --list -N and let falco -V catch the rest.
  • Made-up priorities. high, medium, critical in lowercase. Only the eight documented values load.
  • Undeclared macros. open_read, spawned_process and container live in the default ruleset. Validate with it loaded.
  • Missing plugins. Container and Kubernetes fields need the container plugin in load_plugins.
  • Shadowed rules. With rule_matching: first, a rule that overlaps a stock rule may never fire. Test with all.
  • No negative testing. Cron, package managers and config management read sensitive files constantly. Replay normal activity before you deploy.

FAQ

Can an LLM write Falco rules that work?

Yes, if every draft is treated as untested. In this guide the first draft failed falco -V on its priority, an undefined macro and a wrong field name, and the stock config needed the container plugin loaded. Once it loaded, it raised 13 false positives on Falco's negative traces. The third draft raised none and still caught the positive traces.

How do I validate a Falco rule?

Run falco -c falco.yaml -V falco_rules.yaml -V my_rule.yaml with the same plugins and base rules you use in production. -V (--validate) loads and checks the rules, then exits without monitoring anything.

How do I test a Falco rule without a live attack?

Replay a capture file: -o engine.kind=replay -o engine.replay.capture_file=trace.scap. Use captures that contain the behavior for true positives and captures of normal activity for false positives. Add -o rule_matching=all if your rule overlaps a default rule.

Why didn't my new Falco rule fire when the event happened?

Check rule_matching. With the default first, Falco stops at the first rule that matches an event, so an earlier stock rule can hide yours. Also confirm the rule loaded at all and that its priority is at or above the configured minimum priority.

Should I write a new rule or tune a default Falco rule?

If a default rule already covers the behavior, tune it with exceptions or overrides. Write a new rule for behavior specific to your environment, such as which workloads may read a service account token.

Which model should I use to generate Falco rules?

Any model that follows a "YAML only" instruction and can take the field list in its context. GLM 5.3 Flash Red is a low-cost default on RedRouter, and red-cyber-1 (Beta) is the featured model for authorized security work. Judge either by whether its rules pass validation and replay.

Sources