Auditing and logging with HooksConfig
Build a structured JSON audit trail per run
Compliance and debugging teams need structured records of every tool an agent touched: agent name, tool name, arguments, and timestamp. Attach a lightweight JSON logger via HooksConfig.
Example
import json
from praisonai import Agent, HooksConfig
def query_customer(customer_id: str) -> str:
"""Return customer plan tier."""
return f'Customer {customer_id}: Enterprise'
audit_records = []
def audit_tool(event):
audit_records.append({'tool': event.tool_name, 'args': event.arguments})
agent = Agent(
name='CRMHelper',
instructions='Always call query_customer and reply in one sentence.',
tools=[query_customer],
hooks=HooksConfig(on_tool_call=audit_tool),
output='silent',
)
print('Answer :', agent.start('Check tier for C-900'))
print('Audit JSON:', json.dumps(audit_records))Example explained
- event.context.agent_id — Tags every audit record with the exact agent that triggered the tool call.
- json.dumps(audit_records) — Emits machine-readable logs ready for Datadog, BigQuery, or SIEM ingestion.
Keep hook callbacks fast and non-blocking
Hooks execute synchronously in the agent loop. Push logs to an in-memory buffer or async queue rather than making slow blocking HTTP calls inside on_tool_call.
Common pitfall: raising uncaught exceptions inside an observer hook
Wrap logging/serialization inside try/except in your audit hook so a telemetry hiccup never crashes a user-facing agent reply.
Reference: documentation