Using a local LLM to redact text before it goes to a cloud model

I’d like to find more uses for local models, but the open-weight models that come closest to even a small hosted model like Claude Haiku need well over 100 GB of memory. Redacting text before it goes to a cloud LLM doesn’t need a model that smart, so it’s what I use a local model for most. The one I use, Qwen3.8 27B, runs on my 16 GB GPU with llama.cpp, and it caught 38 of 40 fake secrets I planted in my logs.

Where it’s practical, I’d rather not give an LLM personal details the task doesn’t need, which is the least-privilege rule that OWASP’s Top 10 for LLM applications applies to sensitive data. Plenty of people connect Claude to their whole inbox, and that’s a reasonable trade for the convenience, but it’s not one I make. The local model reads the text first and marks what to redact, and only the redacted copy goes to the cloud. Some things will slip through, so this is for text that wouldn’t be a big deal to leak. Anything really sensitive never goes to a cloud model at all, and the local model does the task itself.

Patterns first, then the model

Regexes catch fixed formats like email addresses, phone numbers, account numbers and API keys, and they’re cheap enough to run over everything. Rather than writing my own, I’d start from the pattern recognizers in Microsoft’s Presidio, an open-source detector for personal data, which check card numbers against their checksum and parse phone numbers with Google’s libphonenumber, and gitleaks’ rules for API keys and tokens. They can’t catch values with no fixed shape, like a person’s name, a street address or a password typed into a sentence.

The local model gets the text after the regex pass, with line numbers, and returns what else to redact. This is a cut-down version of my script, for any OpenAI-compatible server such as llama.cpp’s llama-server:

import json, os, urllib.request

PROMPT = """Flag anything in this text that would be harmful if it leaked:
passwords, API keys and tokens, Wi-Fi keys, people's names, street addresses,
public IP addresses, account numbers, government IDs, medical details.
Each line starts with its number and a tab.
For each item give the line number and the exact text copied from that line.

{text}"""

SCHEMA = {"type": "object", "required": ["items"], "properties": {"items": {"type": "array", "items": {
    "type": "object", "required": ["line", "text"],
    "properties": {"line": {"type": "integer"}, "text": {"type": "string"}}}}}}


def flag(lines):
    numbered = "\n".join(f"{i}\t{line}" for i, line in enumerate(lines, 1))
    body = {
        "messages": [{"role": "user", "content": PROMPT.format(text=numbered)}],
        "temperature": 0,
        "max_tokens": 4096,  # a model can loop on a chunk until the context fills
        "response_format": {"type": "json_schema", "json_schema": {"name": "redactions", "schema": SCHEMA}},
    }
    req = urllib.request.Request(f"{os.environ['LLAMA_URL']}/v1/chat/completions",  # e.g. http://localhost:8080
                                 data=json.dumps(body).encode(), headers={"Content-Type": "application/json"})
    choice = json.load(urllib.request.urlopen(req, timeout=900))["choices"][0]
    if choice["finish_reason"] == "length":
        raise ValueError("cut off, so this chunk wasn't reviewed")
    items = json.loads(choice["message"]["content"])["items"]
    # keep a span only if it is really on the line the model named
    return [it for it in items if 1 <= it["line"] <= len(lines) and it["text"] and it["text"] in lines[it["line"] - 1]]


def redact(lines):
    for it in flag(lines):
        lines[it["line"] - 1] = lines[it["line"] - 1].replace(it["text"], "[redacted]")
    return lines

I send 60 to 150 lines per request. The model can hallucinate a match, and without the check in the last line of flag() a hallucinated match would count as a catch even though nothing got redacted.

Measuring what it misses

A model that flags nothing looks fast and clean, so I measure how much it catches with planted values. A script appends fake values (API tokens, passwords, Wi-Fi keys, names and emails, public IPs) to every 15th line of a copy of real log lines. I borrowed the idea from the fake-data generator in Microsoft’s presidio-research, which fills sentence templates with fake personal details. A planted value counts as caught when a flagged span on its line contains it or is part of it. I also compare what each model flagged on the real lines against Qwen3.8 27B, since the planted values are only a few shapes of secret.

These come from several runs on my homelab’s logs, each with 20 or 40 planted values and each timed against Qwen3.8 27B in the same sitting. Gemma 4 26B-A4B and Qwen3.6 35B-A3B are mixture-of-experts models, which use only 4B and 3B of their parameters per token.

ModelPlanted values caughtRun time vs Qwen3.8 27B (lower is faster)
Qwen3.8 27B, quantized to 4-bit20 of 20 in one run, then 36 of 40 and 38 of 40 with two versions of the model file (compared below)1.00x
IBM’s Granite 4.2 8B15 of 201.82x
Google’s Gemma 4 26B-A4B18 of 200.34x, with one chunk timed out
Qwen3.6 35B-A3B36 of 400.64x

Each score is a single run, so a gap of two values could be noise. Qwen3.6 35B-A3B scored about the same as Qwen3.8 on the planted values and ran faster, but it was faster because it flagged less. On the real log lines it flagged a bit over half as many items, and what it skipped included public IPs and DNS names. So before I believe a speedup on this job, I rerun the planted-value test.

Same model, new quantization

The 4-bit file I run comes from Unsloth, which publishes quantized versions of open models. It re-cut Qwen3.8 27B with version 3.1 of its quantization recipe (“Dynamic 3.1”), replacing the 3.0 file:

 Dynamic 3.0Dynamic 3.1
Planted values caught36 of 4038 of 40
Public IPs on the planted copy it missed and the other caughtabout 20about 20
Values on the real log lines it missed and the other caught115
Tokens writtenbaseline11% fewer, so faster

None of the 15 values 3.1 missed was private (mostly the contact URLs web crawlers put in their User-Agent), so I switched to 3.1. A new file under the same model name moved what gets redacted in both directions, so I rerun the planted-value test whenever the model file changes.

Misses add up in long documents

Catching 95% of sensitive values isn’t enough for a document full of them. If a document holds 40, the chance that none slip through is 0.95^40, about 13%. So I don’t send a redacted copy of a document full of personal details at all. I work out what the cloud model needs locally, like a few totals, and send only that. Logs are a better fit, since only a few values per chunk are sensitive and the rest are mostly hostnames.

Dedicated PII models

I haven’t run a dedicated model for personally identifiable information (PII) against my planted values. PrisMark, a small single-author benchmark on synthetic English text, ranked a local LLM first:

ToolKindPrivate values fully maskedPII-like text wrongly flagged (order IDs, public figures)Time per example
Gemma 4 12Blocal LLM93.6%6%1.25 s
NVIDIA’s GLiNER PII modelsmall named-entity recognition model87.9%51%30 ms
Qwen3.5 9Blocal LLM87.3%16%1.25 s
Microsoft’s Presidiorules plus named-entity recognition56.5%56%14 ms

A partly masked name doesn’t count as masked there. My planted-value test counts a partly flagged value as caught, so its scores can’t be compared with this column. The speed gap matters less to me with the GPU otherwise idle. My test doesn’t check for wrongly flagged text, so I don’t know how often my setup over-redacts. Presidio itself can call an LLM, including a local one, as one of its recognizers (docs).