Dev.to Security 🔐 Cybersecurity 👁 0 📖 10 min read

Your Self-Hosted Agent Runs. Now Put the Gate Outside the Model

Most self-hosting guides end at docker run. The container starts, the agent answers in the chat pane, and the guide declares victory. The part that decides whether the agent is safe to leave alone with your inbox starts

Your Self-Hosted Agent Runs. Now Put the Gate Outside the Model

Most self-hosting guides end at docker run. The container starts, the agent answers in the chat pane, and the guide declares victory. The part that decides whether the agent is safe to leave alone with your inbox starts right after that command, and it has very little to do with the model.

A self-hosted agent that can send email and move money needs three things the model never touches: somewhere to keep the keys that the agent process can use but not read in bulk, a policy file the agent can't edit, and a log the agent can't rewrite. All three fit in an afternoon. What follows is the version we'd hand to a teammate, with the code small enough to read in one sitting.

Here's why the third point matters more than it looks. n8n's Nod, released November 10, puts a separate confirmation step in front of sending, paying, posting and deleting, and n8n says plainly in the same announcement that the step is model-driven and can make mistakes. That candor is useful. It tells you where the remaining risk sits: anywhere a model is asked to approve its own actions, the approval is a probability.

Keys: where the agent's secrets live

The first decision after the container is up is how the agent reaches a model. For a stack you assemble yourself, the simplest answer is your own provider account: change the endpoint and the key, keep everything else. Whose key it is matters less than one property: swapping the model must not drag the credentials, memory, policy and log along with it.

Where the key lives matters as much as whose it is. The pattern we use:

  • Model keys and service credentials go in the OS keychain or a secrets file readable only by the agent's service user (chmod 600, owned by that user).

  • The agent reads a key at the moment it calls a service. It never gets a "list all secrets" capability.

  • Nothing secret goes into the agent's memory store or its prompt context. If the model can see a key, a prompt injection can ask the model to print it.

That last rule is the one people break first, usually by pasting a token into a system prompt "just for testing." Keep a grep in your pre-commit hook for anything that looks like sk- or Bearer in prompt templates; it costs nothing and catches the habit early.

Two operational facts come with renting a model, whoever's key you use. Whatever the agent sends to the model vendor falls under that vendor's data policy, so a task whose content must not leave the building belongs on a local model. And rate limits become your problem in a busy week, which is a good reason to keep a second provider's key configured even while you mostly use one.

The gate: a policy file the model never sees as editable

The core of the setup is a function that sits between "the model wants to do X" and "X happens." The model proposes an action with a type and a target. The gate looks the action type up in a policy file the owner wrote, and returns one of three answers: allow, ask a human, or deny. The model's own opinion about whether the action is fine is not an input.

The policy file first. TOML, because humans edit it:

Owner-edited. The agent process can read this file but not write it.

default = "ask"

[actions]
"fs.read"       = "allow"
"draft.create"  = "allow"
"email.send"    = "ask"
"payment.send"  = "ask"
"file.delete"   = "ask"
"policy.write"  = "deny"

[allowlists]
"email.send" = ["[email protected]", "[email protected]"]

Three details carry most of the weight. default = "ask" means an action type nobody anticipated goes to a human instead of slipping through. policy.write = "deny" means the agent can't loosen its own rules, even if a prompt asks it to. And the allowlist lets replies to known addresses go out on their own while anything to a new address still waits for you.

Make the "can read but not write" part real at the filesystem level: the policy file belongs to your user, the agent runs as a different service user with read permission only. A rule the agent could edit is a suggestion.

Now the gate (Python 3.11+, standard library only):


"""Minimal action gate: the model proposes, this file decides."""
import hashlib, json, sys, time, tomllib
from pathlib import Path

POLICY = Path("policy.toml")
LOG = Path("actions.log.jsonl")

def load_policy():
    with POLICY.open("rb") as f:
        return tomllib.load(f)

def decide(policy, action, target):
    rule = policy["actions"].get(action, policy.get("default", "ask"))
    allowed = policy.get("allowlists", {}).get(action)
    if rule == "ask" and allowed and target in allowed:
        rule = "allow"   # known recipients skip the human, by owner rule
    return rule

def append_log(entry):
    prev = "0" * 64
    if LOG.exists():
        lines = LOG.read_text().splitlines()
        if lines:
            prev = json.loads(lines[-1])["hash"]
    entry["prev"] = prev
    body = json.dumps(entry, sort_keys=True)
    entry["hash"] = hashlib.sha256((prev + body).encode()).hexdigest()
    with LOG.open("a") as f:
        f.write(json.dumps(entry, sort_keys=True) + "\n")

def gate(action, target, ask_human=input):
    policy = load_policy()
    rule = decide(policy, action, target)
    outcome = rule
    if rule == "ask":
        reply = ask_human(f"Agent wants {action} -> {target}. Approve? [y/N] ")
        outcome = "approved" if reply.strip().lower() == "y" else "refused"
    append_log({"ts": time.time(), "action": action,
                "target": target, "rule": rule, "outcome": outcome})
    return outcome in ("allow", "approved")

def verify_log():
    prev = "0" * 64
    for n, line in enumerate(LOG.read_text().splitlines(), 1):
        e = json.loads(line)
        h = e.pop("hash")
        if e["prev"] != prev:
            return f"broken chain at line {n}"
        if hashlib.sha256((prev + json.dumps(e, sort_keys=True)).encode()).hexdigest() != h:
            return f"edited entry at line {n}"
        prev = h
    return "log intact"

if __name__ == "__main__":
    if sys.argv[1:] == ["verify"]:
        print(verify_log())

Your agent's tool layer calls gate(action, target) before every side effect and only proceeds on True. The policy is reloaded on each call, so an edit you make takes effect on the next action without a restart. ask_human defaults to a terminal prompt; in practice you'd swap it for a push notification or a chat message with approve and refuse buttons, and the rest of the file doesn't change.

Two extensions are worth adding once the basics hold. The first is a rate cap per action class: even an allowed class like replies to known contacts should stop at a number per hour that you'd consider normal, because an agent stuck in a loop can do a lot of allowed damage fast. It's a counter keyed on action type, read from the same policy file. The second is a dry-run mode, where the gate logs what it would have decided and always returns False. Run a new tool in dry-run for a few days and you get a list of every action it would take before it takes any of them.

A quick run to see it behave:

from gate import gate, verify_log
print(gate("fs.read", "inbox/"))                                  # True  (allow)
print(gate("email.send", "[email protected]"))               # True  (allowlisted)
print(gate("email.send", "[email protected]",
           ask_human=lambda q: "n"))                              # False (human said no)
print(gate("policy.write", "policy.toml"))                        # False (deny)
print(verify_log())             
                              # log intact




The log: append-only, chained, on your disk

Every call to the gate writes one JSON line, whether the answer was yes or no. Each line carries the SHA-256 of the line before it, so the file forms a chain. Edit any past line, even to flip a "refused" into "approved," and python gate.py verify names the line that no longer matches.

A hash chain on a file you control has one honest limit: someone with write access to the whole file can rewrite every line and recompute the chain. Two cheap habits close most of that gap. Copy the latest hash somewhere the agent can't reach once a day (a note on your phone works). And include the log directory in your nightly backup, so an old copy exists to compare against. For a single-owner agent that combination is usually enough; teams with compliance needs should ship the log to write-once storage instead.

What earns the log its keep is the weekly read. Filter for rule == "ask" and look at what the agent tried to do that needed you. That short list tells you which classes are safe to move to allow (four quiet weeks is our usual bar) and which ones keep surprising you.

What the gate changes, in one picture

Who said yes? 40 injected instructions, two gate designs
The chart is a scenario, built to show the shape of the difference. It does not report a benchmark. What makes the shape reliable is mechanical: under the platform gate, the send and pay columns can only be non-zero if the policy file says allow for those classes or a human typed y. Nothing the model reads can change either of those.

The weekly read of the log is also where the scenario's numbers would come from in your own setup. Count the held sends and payments, and count how many of them you refused. If you refuse almost none for a month, the class may be ready for an allowlist; if you refuse a steady few, the gate is doing the job you installed it for.

Notice also what the gate leaves alone. Reads and drafts go through in both designs, because the owner allowed them. A prompt injection that makes the agent read a file it was already allowed to read, or write a silly draft, still happens. The gate narrows the damage to what your policy permits; how strict to make that policy is your judgment.

How KarmaBox does the same job without the glue code

Our own product, KarmaBox, is a platform and network for running your own AI avatars, and each piece of the sixty lines above has a built-in counterpart there.

The policy file becomes per-avatar permission settings. Six action classes (sending externally, publishing publicly, deleting, spending money or trading, writing to the ledger, executing commands on a machine) are each set to do it directly, ask first, or never. The defaults follow the same logic as default = "ask": ledger writes, which only the owner sees and can undo, run directly; sending and publishing ask first; spending defaults to never. Since October 23, 2026, payment always needs the owner's own approval, with no switch to automate it. In policy.toml you could write payment.send = "allow". In KarmaBox you can't.

The gate runs outside the model, so it survives a model swap. When someone other than the owner is directing an avatar, it checks with the owner first, which catches a stranger's steering email.

The log becomes a receipt per task: what was asked, what the avatar did, what it caught, and what it handed back to a human. Work that matters can go on to an acceptance avatar, which checks the result line by line and never edits or sends anything itself.

In an internal beta run on November 15, 2026 (test account), we asked a cross-border team's ads avatar to lower two Amazon keyword bids before the ads account was connected. It couldn't read the real keyword IDs, so it changed nothing and returned a pending plan instead of guessing an ID.

The KarmaBox ads avatar replying that the Amazon ads account is not connected
Key custody works differently. In the script the agent borrows your keys. A KarmaBox avatar has its own name, outbound address, inbox and phone line, so client replies and verification codes go through the avatar's address rather than your account. Cloud models come built in, so there is no model key to store, and a task that would spend money on one waits for your approval. The data-policy point above still applies.

The model can stay in the building too. Avatars run on a KarmaBox in your office, where chat, writing, retrieval, image understanding, transcription and video models run on the device. Since October 22, 2026, a local-loop enforcement gate makes local work fail rather than fall back to the cloud without telling you. In an unplugged run on October 28 to 30, it completed 197 local inferences in a day with no business data leaving.

If you're building your own, the sixty lines above get you most of the way. If you'd rather not maintain them, the left column is a checklist for any platform and the right column is KarmaBox's answer. Either way the target is criterion five of a sovereign AI avatar: it can act, and its actions are judged outside the model.

Where this leaves the model

None of this makes the model less useful. It still plans the work, writes the drafts and picks the next step. It just stops being the last line of defense, which it was never good at being. When a newer model arrives you point the endpoint at it and the gate, the keys and the log stay exactly where they were.

A gate, scripted or built in, addresses one risk: an agent acting beyond what its owner allowed. It doesn't write a good policy for you, since a class set to "allow" is enforced as faithfully as one set to "never." It doesn't protect a machine that nobody patches or backs up, and it doesn't make a cloud model's data policy go away for the tasks you send there. Those stay with the owner, whichever way you build it.

The longer version, covering which layers must stay on your machine, what you can keep renting, and what self-hosting costs in hours and dollars, is our guide to the self-hosted AI agent.

Which action class would you leave at "ask" the longest?

Sources

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.