I Built an Open-Source Governance Layer for the Claude API — Here's Why and How
说明与用途
为 Claude API 构建的开源治理层,实现了 PII 检测、关键字屏蔽、提示长度限制、令牌预算、警报和防篡改审计日志,且延迟极低。
问题
Enterprise teams need to enforce PII protection, audit logging, cost control, and access governance when using the Claude API, but the SDK provides no built‑in compliance features.
结果
The kit adds a single‑line install, zero‑infra, and <10 ms overhead while providing PII detection, keyword blocking, prompt length enforcement, cost budgeting, alerts, and tamper‑evident audit logs.
工作流
- Install the package via pip
- Instantiate GovernanceLayer with PII filter, blocked keywords, and prompt length limits
- Instantiate TokenMonitor with daily budget and alert threshold
- Create an AgentOrchestrator passing governance and monitor
- Create an agent with system prompt and persona
- Run the agent with a user ID
- Handle GovernanceViolation exceptions
- Inspect result content, cost, and governance status
证据
Every call goes through five stages: GovernanceLayer (pre‑flight) – PII scan, blocked keywords, length guard → Claude API call → GovernanceLayer (post‑response) – PII in output, GxP citation check → TokenMonitor – cost calculation, budget enforcement, alerts → AuditLogger – append‑only SHA‑256 tamper‑evident record → RunResult
I made a deliberate choice: governance decisions use compiled regex patterns, not another model call.
- 记录日期: 2026-09-18T19:30:29.434Z