Skip to content
GitHubDiscord

AI Governance (lexigram-ai-governance)

AI governance for the Lexigram Framework — policy enforcement, audit trails, budget tracking


AI usage governance for the Lexigram Framework. Enforces budget caps, rate limits, and model access policies on LLM requests — with a full audit trail, soft-limit callbacks, TPM/cost sliding windows, and hot-reloadable configuration. Zero-config usage starts with sensible defaults.

Terminal window
uv add lexigram-ai-governance
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.ai.governance import GovernanceModule
from lexigram.ai.governance.config import GovernanceConfig
@module(imports=[
GovernanceModule.configure(
GovernanceConfig(
monthly_budget=50.0,
enforce_budget=True,
soft_limit_pct=0.8,
rpm_limit=60,
restricted_models=["gpt-4o"],
)
)
])
class AppModule(Module):
pass
app = Application(modules=[AppModule])
if __name__ == "__main__":
app.run()

Zero-config usage: Call GovernanceModule.configure() with no arguments to use defaults.

application.yaml
ai_governance:
enabled: true
monthly_budget: 100.0
enforce_budget: true
soft_limit_pct: 0.8
rpm_limit: 60
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_AI_GOVERNANCE__MONTHLY_BUDGET=100.0
# Environment variables for each field
from lexigram.ai.governance.config import GovernanceConfig
from lexigram.ai.governance import GovernanceModule
config = GovernanceConfig(
monthly_budget=100.0,
enforce_budget=True,
soft_limit_pct=0.8,
rpm_limit=60,
)
GovernanceModule.configure(config)
FieldDefaultEnv varDescription
enabledTrueLEX_AI_GOVERNANCE__ENABLEDMaster on/off switch for governance enforcement
monthly_budgetNoneLEX_AI_GOVERNANCE__MONTHLY_BUDGETMonthly budget cap in dollars
enforce_budgetTrueLEX_AI_GOVERNANCE__ENFORCE_BUDGETHard-block requests when budget is reached
soft_limit_pctNoneLEX_AI_GOVERNANCE__SOFT_LIMIT_PCTWarn at this fraction of budget
max_request_costNoneLEX_AI_GOVERNANCE__MAX_REQUEST_COSTPer-request cost cap in dollars
rpm_limitNoneLEX_AI_GOVERNANCE__RPM_LIMITRequests per minute cap
tpm_limitNoneLEX_AI_GOVERNANCE__TPM_LIMITTokens per minute cap
max_tokens_per_requestNoneLEX_AI_GOVERNANCE__MAX_TOKENS_PER_REQUESTHard token ceiling per request
restricted_models[]LEX_AI_GOVERNANCE__RESTRICTED_MODELSModels blocked for all users
model_allowlist{}LEX_AI_GOVERNANCE__MODEL_ALLOWLISTPer-user/role allowlist with glob patterns
model_denylist{}LEX_AI_GOVERNANCE__MODEL_DENYLISTPer-user/role denylist
MethodDescription
GovernanceModule.configure(config)Configure with explicit config
GovernanceModule.stub(config)Minimal config for testing
  • Budget enforcement: Monthly budget caps with soft-limit callbacks
  • Rate limiting: RPM and TPM sliding windows via BudgetTracker
  • Model access control: Per-user and per-role allowlist/denylist with glob patterns
  • Audit trail: Full governance decision recording via AIAuditStore
  • Hot reload: Update limits at runtime without restarting
  • Persistence backends: In-memory, Redis, and database backends
async with Application.boot(modules=[GovernanceModule.stub(
GovernanceConfig(restricted_models=["gpt-4o"])
)]) as app:
# your test code
...
FileWhat it contains
src/lexigram/ai/governance/module.pyGovernanceModule.configure(), .stub()
src/lexigram/ai/governance/config.pyGovernanceConfig
src/lexigram/ai/governance/services/manager.pyAIGovernanceManager core logic
src/lexigram/ai/governance/budget/tracker.pyBudgetTracker TPM / cost enforcement
src/lexigram/ai/governance/audit/AIAuditStore, AIAuditEvent, query models
src/lexigram/ai/governance/persistence/persistence.pyPersistence backends
src/lexigram/ai/governance/di/provider.pyGovernanceProvider boot and registration