Deterministic guardrails for AI coding agents: blocks unsafe edits before they land
pip install loki-guardrails==0.1.6 Copy PIP instructions
Loki checks every edit your agent makes with real analyzers and precise rules. It blocks the dangerous ones before the file changes, tells the agent exactly what to fix, and does all of this without calling a model.
Real hook output from docs/demo/demo.sh. Only the typing is simulated.
Head to head with Interlinked CLI (82307ea), both installed as Claude Code hooks. Each ran in a fresh sandboxed repository per case and repetition. A defect counts only if the right diagnostic appears on every repetition.
Median hook latency is on par (402 ms vs 389 ms) with a much shorter tail (p95 1.6 s vs 6.4 s). Without project-local JS tools, Loki takes 334 ms against Interlinked's 10.8 s. Every holdout defect Interlinked catches, Loki catches too.
These are synthetic cases, not production sessions. The blind holdout was written by an agent that could not see either product. Read the full results and the benchmark protocol.
Loki needs Python 3.11 or newer and has no runtime dependencies.
uv tool install loki-guardrails # or: pipx install loki-guardrails 2. Set up a repository:
cd your-repo loki init --dir . This adds a pinned copy of the engine and your policy in .loki/, hooks for every supported agent (.claude/, .codex/, .factory/, .pi/, .omp/), starter Ruff, Oxlint and golangci-lint configuration, and a .github/workflows/loki.yml CI check. Existing configuration files are left alone.
git add .loki .claude .codex .factory .pi .omp .ruff.toml .oxlintrc.json \ .golangci.yml .github/workflows/loki.yml git commit -m "Add Loki guardrails" 4. Use your agent as usual. On every edit:
The first edit starts a background daemon that keeps analyzers warm.
5. Check the whole repository whenever you like:
loki scan # everything changed since the last commit loki scan --base origin/main # everything on your branch loki slop # sloppiness score and complexity hotspots loki slop --base main # how this branch changed the score The installed CI workflow runs scan --strict on every push and pull request. Strict mode fails when an analyzer is missing instead of skipping it.
uv tool upgrade loki-guardrails loki init --dir . --force # replaces the engine in .loki/; review and commit Hooks always run the engine in .loki/loki.py, not the global loki, so each repository keeps the version its policy was reviewed against until you commit an upgrade. You can try Loki without installing it via uvx --from loki-guardrails loki init --dir ., or run python3 loki.py init from a clone of this repository. The install guide covers each host in detail.
flowchart LR A[Agent proposes a write] --> B{Pre-write} B -->|path, policy, secrets,
TypeScript types| C[Deny with the exact finding] B -->|clean| D[File is written] D --> E{Post-write} E -->|mypy, golangci-lint, Clippy,
Credo, Sobelow, Oxlint| F[Report net-new findings] E --> G[Advisory: new hotspots,
clones, import cycles] H[(Warm daemon)] -. keeps analyzers warm .-> B H -.-> E Before the write, Loki reconstructs the file the agent is about to produce. It checks the path against protected policy, runs the tool-free content rules, and, where supported, runs Ruff and a TypeScript type check on the proposed content. A denial carries the precise diagnostic. After the write, the language's analyzers run on what actually landed. New findings go back to the agent, while existing debt stays quiet. In CI, scan repeats the checks against a base revision, including test integrity, dependency policy and lockfile consistency. Loki blocks its own policy files, hook registration and CI workflow from agent edits, so an agent can't switch it off.
Missing analyzers are reported as NOT CHECKED, never as clean, and LOKI_STRICT=1 turns them into failures. The reference documents every rule, threshold and limitation.
loki slop puts a single number on how hard code is to change. It combines the erosion metric from Measuring code sloppiness with trellis's scoring:
$ loki slop Sloppiness index: 26/100 (lower is better; trellis 0.2.0-provisional weights) complexity-erosion 26.2 eroded share 0.94, 1 of 4 functions with CC > 10 duplication 0.0 density 0.00, 0 clone groups import-cycle 0.0 density 0.00, 0 cycles hotspot src/discounts.ts:1 discount CC 17, 15 SLOC, mass 65.8 Complexity is exact for Python, TypeScript/JavaScript, Go and Elixir using each language's own parser. It is approximated structurally for Rust, Java, Kotlin, C#, C/C++, Swift, Scala, PHP and Ruby. Clones are detected on normalized tokens, so renamed copies count. --base compares against a revision, and max_index or max_index_increase can gate CI.
Policy lives in .loki/loki.json and is always read from the committed base, so an agent can't loosen it mid-session:
{ "rule_packs": ["core", "python", "typescript", "phoenix", "shell"], "shell_commands": [ {"command": "npm test", "cwd": ".", "inputs": ["package.json", "package-lock.json"]} ], "slop": {"max_index": 40, "max_index_increase": 2, "block": false}, "elixir_security": {"sobelow": {"block": "high", "warn": "medium"}} } Setting Default Effect typescript_check on when tsconfig.json exists false disables it; true makes setup problems block slop.block false Turns new hotspots, clones and cycles from advice into failures shell_commands none Exact, reviewed shell commands the shell guard admits (enable the guard with loki init --shell-guard) LOKI_STRICT=1 off Missing analyzers fail instead of reporting NOT CHECKED LOKI_PYTHON= active venv or conda env, then .venv The project's interpreter, asked whether a new import exists (--python on hook, protect and scan) LOKI_DAEMON=0 on Runs every hook in-process Supported agents Agent Integration Claude Code .claude/settings.json hooks Codex .codex/hooks.json hooks Factory Droid .factory/hooks.json hooks Pi .pi/extensions/loki.ts OMP .omp/extensions/loki.ts Ultron built in (loki init --minimal installs .loki/ only) Documentation Reference: every check, rule, threshold and limitation Benchmark protocol and the latest results Blind holdout cases and the runner Development uv run --frozen pytest -q bun test ./tests/loki-shim.test.ts ./tests/loki-omp.test.ts ruff check --config templates/.ruff.toml loki.py tests benchmarks Regenerate the demo with asciinema rec -c "bash docs/demo/demo.sh" docs/demo/demo.cast followed by agg docs/demo/demo.cast docs/demo/demo.gif.
ai-agents claude-code codex guardrails hooks linting static-analysis
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.




