Skip to main content

Example: Database Agent

An AI agent that queries a REST API backed by a database — with policies that prevent writes and require reasoning for any sensitive query.

Policy

version: "1"
rules:
# Block all write operations to the database API
- tool: database
action: POST
decision: deny
message: "Direct database writes are not permitted via agent"

- tool: database
action: PUT
decision: deny

- tool: database
action: DELETE
decision: deny
message: "Deletes are prohibited"

# Allow reads with optional reasoning
- tool: database
action: GET
decision: allow

Agent code

import httpx
from agentic_guard import AgenticGuard, PolicyViolationError

gate = AgenticGuard(
api_key="ag_xxxxxxxxxxxx",
base_url="https://api.agentic-guard.com",
mode="enforce",
)

PROXY = "https://api.agentic-guard.com/proxy/ag_xxx/"

def query_users(filters: dict) -> list:
resp = httpx.get(
f"{PROXY}api.myapp.internal/users",
params=filters,
headers={"X-Agent-Reasoning": "Fetching users for weekly report"},
)
return resp.json()

def query_orders(user_id: str) -> list:
resp = httpx.get(
f"{PROXY}api.myapp.internal/orders",
params={"user_id": user_id},
)
return resp.json()

Adding the database credential

Store the API key in the vault:

  • Hostname: api.myapp.internal
  • Type: header
  • Header name: X-API-Key
  • Value: sk_live_xxxxxxxx

Every request to api.myapp.internal will have X-API-Key: sk_live_xxxxxxxx injected automatically.

Read-only enforcement

With the policy above, any attempt to POST, PUT, or DELETE returns a 403 immediately — the request is never forwarded to the database API. The audit log records the denial with the reason.