All posts

Five guardrails to run before AI-written code lands

Nov 13, 2026 2 min read Oleg Savelev
security guardrails teams

Every engineering team has a rules file telling the agent "never hardcode secrets." It works until turn forty, when the agent gets impatient with an unconfigured environment variable and quietly writes os.environ.get("API_KEY", "example_secret_fallback"). Passive instructions degrade mid-session; deterministic local checks do not.

These are the five failure modes we see land in real commits, and the exact shape each guard takes to protect your repository.

1. Hardcoded secret fallbacks

The pattern: a default credential tucked into an environment lookup so the code works in local development without setup.

# Blocked before it reaches git:
API_KEY = os.environ.get("STRIPE_KEY", "example_secret_fallback")

The guard raises an error instead of falling back: if the environment variable is absent, the code fails loudly rather than shipping a key into git history. One org-wide rotation incident costs far more than running this check on every task.

2. TLS verification bypass

verify=False in Python requests, or rejectUnauthorized: false in Node. The agent hits a self-signed corporate endpoint and "fixes" the SSL error by turning verification off. The guard blocks the bypass and enforces the correct fix: mount your root certificate into the local trust store.

3. SQL built by string interpolation

# Blocked:
cursor.execute(f"SELECT * FROM users WHERE id = {user_id}")

F-strings and string concatenation inside query text are deterministic red flags, regardless of where user_id originated. The guard requires parameterized queries, enforcing the secure pattern the agent skipped.

4. CORS wildcard with credentials

Combining Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true turns any webpage into an authenticated client. It passes every functional test and fails every security review. The guard blocks it at commit time before it reaches code review.

5. Destructive shell and blanket staging

Two dangerous patterns: rm -rf commands pointed at project paths, and git add . executed while .env or private keys sit in the working tree. Blanket staging is the primary way everyday commits leak production credentials. The guard inspects the staging index and stops the commit when sensitive files are present.

Why pre-commit checks beat prompt rules

All five checks are cheap, local, and deterministic: they run on the developer's machine in milliseconds with zero model calls. That is the architectural difference: prompt rules are probabilistic, but guards are deterministic. Your agent can draft a bad pattern, but it cannot commit or stage it.

TokenSafe ships all five guardrails enabled by default. They integrate into Claude Code, Cursor, Codex, Windsurf, and Antigravity lifecycle hooks in one command:

npx tokensafe@latest init

Explore the full security guard documentation or see how measuring your bills pairs with local safety. Teams scaling AI seats can start a 7-day team trial to enforce shared guard policies across the monorepo.

The right place to catch an AI mistake is the staging area, while it is still one keystroke away from entering git history.