Tool-Call Safety Patterns for LLM Agents in Production

Allowing an agent to call tools directly is powerful and risky. Safety comes from permission layers, command constraints, and auditability.

Step 1: Introduce allowlist-based tool policy

{
  "allowed": ["git status", "npm test", "wp post list"],
  "blocked": ["rm -rf", "git reset --hard"]
}

Step 2: Add argument-level validators

def validate(cmd: str) -> bool:
    if "--force" in cmd and "wp" in cmd:
        return False
    return True

Step 3: Log every tool call with before/after state

audit.append({
  "cmd": cmd,
  "cwd": cwd,
  "started_at": ts,
  "exit_code": rc,
})

Pitfall

Relying on prompt instructions alone for safety. Policy must be enforced outside model output.

Preview: first 50% is visible. Unlock to read the full article.
To view this content, you must be a member of CodeWithWilliamJiamin's Patreon at $1 or more
Already a qualifying Patreon member? Refresh to access this content.