A regex in front of the LLM: how a hand-calibrated filter cut operating cost about 90 times

LLM
agents
evaluation
local-models
optimization
Before a language model reads a document, a regex can decide whether it should. This post follows one such filter from an F1 of 0.05 to 0.86 on held-out data, shows how a heuristic reference inflated the score to 0.94, why a model proposing edits was not enough on its own, and what the filter is worth in API cost.
Author

Pedro Carvalho Brom

Published

June 3, 2026

Code repositorypcbrom/autoresearch

Picture the pipeline. Hundreds of thousands of documents arrive, a small minority of them matter, there are no labels, and the model that will read them is billed per token. Every document of noise that reaches the model is paid for. The cheapest token is the one that is never sent, so the real design question is the filter that decides what gets sent, and how you know it works when nobody has labeled anything.

This post follows one such filter, a regex built to separate a minority class from noise before an LLM touches the text. The project’s domain and its specific terms stay undisclosed here, as they were in the original post, and every number below is one that was published there.

This post expands a LinkedIn post of 3 June 2026: Aqui eu conto como reduzir o custo de operação em até 90 vezes, in Portuguese.

The regex is the financial shield

In a pipeline where a regex feeds an LLM, the regex is the part that protects the budget. It cuts the noise before the model reads anything, and the model only works on what survives. That puts two quantities in tension. Whatever the filter drops never reaches the model, so a filter that is too strict loses the documents the pipeline exists to find. A filter that is too loose sends noise, and noise is cost. In the production setting of this project, the final filter lets 97.5% of the target information through, and 81% of the noise never reaches the LLM.

Getting there took a trail of F1 scores, and the most instructive point on that trail is a number that went down.

Without labels, the reference is part of the test

F1 is the harmonic mean of precision, the share of what the filter passes that is really target, and recall, the share of the target that the filter passes. An F1 score needs a reference, and with no labels the reference has to come from somewhere. The first good F1 came from a heuristic pseudo-labeler, and it read 0.94. That reference labeled ambiguous documents as noise, and the score was inflated. When the heuristic was replaced by a neural gold standard, labels produced by Gemma 26B, the F1 dropped to 0.83. The second number was the real one. The apparent progress had come from the evaluation method, not from the regex.

This is the trap worth carrying into your own work. If the score improved at the same time as the way of scoring changed, you have learned nothing yet about the filter. A reference that agrees too easily with the thing it grades is not measuring it, and the same failure shows up in another form when a large model labels an extraction benchmark and hides the very failure the benchmark should catch.

Letting a model edit the regex

To calibrate the regex against that neural gold standard, I adapted the propose, measure and decide pattern of the karpathy/autoresearch repository into my own version of autoresearch, which is open source. The loop worked like this. Gemma 26B proposed edits to the regex as JSON, the script ran, the F1 was captured, and the edit was either kept or reverted. Before each edit, a Claude subagent judged whether the Gemma proposal was a hallucination.

That judge had work to do. After three iterations, Gemma suggested a term in Spanish that occurred once in 90 days of history and sat entirely outside the semantic scope of the class. It was a cross-lingual hallucination, a proposal that looks like a pattern and is not one. A loop that only checks the metric treats such a term like any other candidate, and the judge sits before each edit precisely so that a proposal like this is caught before it changes the regex.

What delivered was reading the false positives

The honest summary of the original post is that an AI evolving the regex by itself did not deliver. What delivered was human calibration by sampling, which means looking at the false positives one by one. In the 40 false positives left at the end of the loop, five recurring categories of noise stood out, and each one became a deny pattern, an explicit exclusion added to the filter.

The full trail of the F1 score reads as follows.

  • 0.05 for the initial regex.
  • 0.94 against the heuristic pseudo-labeler, inflated because that reference labeled ambiguous documents as noise.
  • 0.83 against the neural gold standard from Gemma 26B, the real number.
  • 0.87 after the five categorical cuts, on the calibration set of 296 samples.
  • 0.86 on the test set of 182 samples.

The number to quote is the last one, on samples that the cuts were not built on, and it sits one point below calibration. Read the trail again and the division of labor is plain. The loop measured, kept and reverted, and kept a record of every attempt. The categories that moved the score came from a person reading the errors and naming what they had in common. The original post credits both halves, the human calibration for the cuts and the loop for evolving the regex until little noise was left for the LLM.

What the filter is worth

To put a price on the filter, I ran a Monte Carlo simulation of the cost of sending documents to Gemini 2.5 Flash, the priority LLM of the project, with and without the filter. It ran 10,000 iterations, counted tokens with tiktoken, an OpenAI tokenizer, so the counts for Gemini are an approximation, and used the real API prices at the time of the post, June 2026. The published results are in Brazilian reais.

  • One source over one year costs BRL 8 with the filter against BRL 684 without it.
  • Multiple sources over one year cost BRL 190 with the filter against BRL 17,124 without it.

The ratio reaches about 90 times. At that scale the preprocessing filter stops being an optimization and becomes a precondition, because it decides whether the project is viable at all, and it pays for itself quickly. Notice what the simulation is really measuring. The per-call price of the model is the same in both columns. What changes is how many of the calls are noise.

What to take from it

Three habits come out of this case. Treat the reference as part of what you are testing, and expect the score to move when the reference changes. Put a judge between a model’s proposal and the code it edits, because a fluent proposal can still come from outside the problem. And read the false positives yourself, since the categories that matter are rarely the ones a metric can name.

So, the pipeline you run today. How much of what it sends to the model is noise, and has anyone looked at that noise closely enough to name it?


References.

BROM, P. C. autoresearch. GitHub repository, 2026. Available at: https://github.com/pcbrom/autoresearch.

KARPATHY, A. autoresearch. GitHub repository. Available at: https://github.com/karpathy/autoresearch.

The loop is open source on GitHub under the MIT license: github.com/pcbrom/autoresearch. The version that closes the loop with a coding agent is described in autoresearch v1.1. I write about this kind of work, scientific method, statistics, and AI applied with rigor, on LinkedIn: linkedin.com/in/pcbrom.