Token risk analysis and audit logging for Claude Code via native hooks
Project description
Prompt Guardian
Token risk analysis and audit logging for Claude Code via native hooks.
Guardian monitors every prompt and tool call in your Claude Code sessions, classifies risk using heuristic patterns and the Claude API, warns you before dangerous actions, and logs everything to a local SQLite database.
Install
pip install prompt-guardian
guardian install # register hooks globally (~/.claude/settings.json)
guardian install --project # or project-level (.claude/settings.json)
After installing, restart Claude Code. Guardian runs automatically on every session.
How it works
Guardian hooks into six Claude Code events:
- UserPromptSubmit — classifies your prompt and injects a warning into Claude's context if risk is medium or above. Claude surfaces this warning at the start of its response.
- PreToolUse — classifies tool inputs (Bash, Write, Edit) before execution
- PostToolUse / PostToolUseFailure — logs success, duration, and failure reasons
- Stop — finalizes the turn with actual token counts
Guardian never blocks your workflow. It warns and logs — that's it.
Risk levels
| Level | Examples |
|---|---|
| critical | rm -rf, drop table, mkfs, `curl |
| high | credential exposure, git push --force, npm publish |
| medium | sudo, git push, docker run, kubectl, terraform apply |
| low | everything else |
By default, guardian uses heuristic regex patterns for speed. Enable API classification (Claude Haiku) for more accurate results on medium+ prompts:
guardian config set-key # stores your Anthropic API key in ~/.guardian/config.json
Then set api_analysis.enabled: true in ~/.guardian/config.json.
CLI commands
# Installation
guardian install [--project] # register hooks
guardian uninstall [--project] # remove hooks
guardian status # check registration + DB stats
# Reporting
guardian report [--limit N] [--risk LEVEL] [--since DURATION] [--session] [--current-session]
guardian top [--limit N] # top N prompts by token usage
guardian stats # aggregate counts by risk, action, tool, project
guardian patterns # which heuristic patterns have fired most
guardian false-positives # where heuristic over-classified vs API
# Management
guardian clear # wipe all history (with confirmation)
guardian config set-key # set Anthropic API key interactively
Examples
guardian report --risk medium --since 7d
guardian report --current-session
guardian top --limit 5
guardian patterns
Configuration
~/.guardian/config.json:
{
"api_key": "sk-ant-...",
"api_analysis": {
"enabled": true,
"always_api": false,
"timeout_seconds": 5,
"model": "claude-haiku-4-5-20251001"
},
"auto_approve_tools": ["npm test", "npm run lint", "git status"],
"log_prompt_previews": true
}
Data
All data is stored locally at ~/.guardian/history.sqlite. Nothing is sent anywhere (except prompts to the Anthropic API for classification, if enabled).
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prompt_guardian-0.1.1.tar.gz.
File metadata
- Download URL: prompt_guardian-0.1.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6add0206e40361ef7fb9a44593873e2acdce96c0e74ab5bf329682c7026e81e6
|
|
| MD5 |
aa22bac2a4224f15c4318c49a70a7b12
|
|
| BLAKE2b-256 |
76a5d507849fc32ad423f0c8b264b2b35605e1e452ffad2e217b28fee0b5a009
|
File details
Details for the file prompt_guardian-0.1.1-py3-none-any.whl.
File metadata
- Download URL: prompt_guardian-0.1.1-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66a34b094e741f527f90645ccec75ca0c3d05422c2370667624d5bfaac98c67d
|
|
| MD5 |
1a6232fbbe0e462f6b062efce3fa0372
|
|
| BLAKE2b-256 |
3a5d3bf55a42a556e37186d57f800a0bca7c5a4a021df620b6290aa0f74e07e4
|