What You’ll Learn
- Creating a sandbox with
AuditConfig combined with PII redaction and injection defense
- What the audit log captures (requests, PII counts, block counts, transformations, timestamps)
- The
AuditEntry model and all its fields
- Shorthand boolean syntax:
audit=True for default settings
Prerequisites
- Declaw instance running and
DECLAW_API_KEY / DECLAW_DOMAIN set
pip install declaw python-dotenv
Code Walkthrough
This example is available in Python. TypeScript support coming soon.
1. Create a sandbox with audit logging
Audit logging works best alongside other security features so events are correlated. Here we combine it with PII redaction and injection defense:
from declaw import (
Sandbox,
SecurityPolicy,
PIIConfig,
AuditConfig,
AuditEntry,
)
sbx = Sandbox.create(
template="base",
timeout=300,
security=SecurityPolicy(
pii=PIIConfig(enabled=True, types=["email", "ssn"], action="redact"),
injection_defense=True,
audit=AuditConfig(
enabled=True,
log_request_body=True,
log_response_body=False,
retention_hours=48,
),
),
)
AuditConfig field | Default | Description |
|---|
enabled | False | Toggle audit logging on or off. |
log_request_body | True | Include HTTP request bodies in audit entries. |
log_response_body | False | Include HTTP response bodies (can be large; off by default). |
retention_hours | 24 | How long audit entries are retained before being purged. |
2. What the audit log captures
Every HTTP request and response passing through the Declaw proxy produces an AuditEntry:
- Every HTTP request/response passing through the Declaw proxy
- PII redaction counts — how many PII instances were redacted per request
- Injection block counts — how many injection attempts were blocked
- Transformation application counts — how many rules fired
- Timestamps for each event
- Traffic direction (
inbound vs outbound)
- HTTP method, URL, and status code
- Request body (when
log_request_body=True)
- Response body (when
log_response_body=True)
Audit logs are retained for the configured retention period and can be queried for compliance reporting and incident investigation.
3. The AuditEntry model
import datetime
from declaw import AuditEntry
sample_entry = AuditEntry(
timestamp=datetime.datetime.now(),
method="POST",
url="https://api.openai.com/v1/chat/completions",
status_code=200,
pii_redactions=3,
injection_blocks=0,
transformations_applied=1,
direction="outbound",
)
print(sample_entry.to_dict())
Sample JSON output:
{
"timestamp": "2026-04-02T12:00:00.000000",
"method": "POST",
"url": "https://api.openai.com/v1/chat/completions",
"status_code": 200,
"pii_redactions": 3,
"injection_blocks": 0,
"transformations_applied": 1,
"direction": "outbound"
}
| Field | Description |
|---|
timestamp | ISO-8601 timestamp of when the request was processed |
method | HTTP method (GET, POST, PUT, DELETE, etc.) |
url | The target URL of the request |
status_code | HTTP status code of the response (0 if not yet received) |
pii_redactions | Number of PII instances redacted in this request/response |
injection_blocks | Number of injection attempts blocked (0 or 1 typically) |
transformations_applied | Number of transformation rules that fired |
direction | Traffic direction: outbound (to external) or inbound (from external) |
4. Shorthand boolean syntax
For default settings — log_request_body=True, log_response_body=False, retention_hours=24 — pass True directly:
sbx = Sandbox.create(
template="base",
security=SecurityPolicy(audit=True),
)
5. Cleanup
Expected Output
============================================================
Declaw Audit Logging Example
============================================================
--- Creating Sandbox with Audit Logging ---
Sandbox created: sbx_abc123
Security policy applied:
pii.enabled: True
pii.types: ['email', 'ssn']
pii.action: redact
injection_defense.enabled: True
audit.enabled: True
audit.log_request_body: True
audit.log_response_body: False
audit.retention_hours: 48
------------------------------------------------------------
What Audit Logging Captures
------------------------------------------------------------
- Every HTTP request/response passing through the Declaw proxy
- PII redaction counts
- Injection block counts
- Transformation application counts
- Timestamps for each event
- Traffic direction (inbound vs outbound)
...
------------------------------------------------------------
AuditEntry Structure
------------------------------------------------------------
Sample audit entry (as JSON):
{
"timestamp": "2026-04-02T12:00:00.000000",
"method": "POST",
"url": "https://api.openai.com/v1/chat/completions",
"status_code": 200,
"pii_redactions": 3,
"injection_blocks": 0,
"transformations_applied": 1,
"direction": "outbound"
}
--- Cleaning Up ---
Sandbox killed.
============================================================
Done!
============================================================