| title | description | tags | ||||
|---|---|---|---|---|---|---|
Analytics Agent with Built-in Evaluation |
Production-ready analytics agent with automated metrics collection and safety validation |
|
Template: Production-ready analytics agent with automated metrics collection
Use Case: SQL query generation with quality tracking, performance monitoring, and safety validation
Related Guide: Agent Evaluation
| File | Purpose |
|---|---|
analytics-agent.md |
Agent definition with evaluation criteria |
hooks/post-response-metrics.sh |
Automated metrics logging after each response |
eval/metrics.sh |
Analysis script for aggregating collected metrics |
eval/report-template.md |
Template for monthly evaluation reports |
# Copy agent definition
cp analytics-agent.md ~/.claude/agents/
# Or for project-specific:
cp analytics-agent.md /path/to/project/.claude/agents/# Copy hook to project
cp hooks/post-response-metrics.sh /path/to/project/.claude/hooks/
# Make executable
chmod +x /path/to/project/.claude/hooks/post-response-metrics.shAdd to .claude/settings.json:
{
"hooks": {
"postToolUse": [
{
"command": ".claude/hooks/post-response-metrics.sh",
"enabled": true,
"description": "Log analytics agent metrics"
}
]
}
}mkdir -p /path/to/project/.claude/logs# In Claude Code session
"Use analytics-agent to generate a SQL query for [task description]"# View raw metrics log
cat .claude/logs/analytics-metrics.jsonl
# Run analysis
./examples/agents/analytics-with-eval/eval/metrics.sh# Copy template
cp eval/report-template.md reports/analytics-2026-02.md
# Fill in metrics from metrics.sh outputThe hook automatically logs:
| Metric | Description | Source |
|---|---|---|
timestamp |
ISO 8601 timestamp | System |
query |
Generated SQL query | Agent response |
exec_time |
Query execution time | Database |
safety |
PASS/FAIL for destructive ops | Query analysis |
row_count |
Rows returned | Database |
error |
Error message if query failed | Database |
Log format: JSONL (JSON Lines) in .claude/logs/analytics-metrics.jsonl
$ ./eval/metrics.sh
=== Analytics Agent Metrics Report ===
Period: 2026-02-01 to 2026-02-10
Total queries: 45
Safety checks:
- PASS: 42 (93%)
- FAIL: 3 (7%)
Execution time:
- Mean: 2.3s
- Median: 1.8s
- P95: 5.2s
- P99: 8.1s
Common failures:
1. DELETE without WHERE clause (2 occurrences)
2. DROP TABLE in query (1 occurrence)
Recommendations:
- Review agent instructions to emphasize WHERE clause requirement
- Add explicit prohibition on DROP operationsEdit hooks/post-response-metrics.sh line 12-16:
# Add more patterns
if echo "$QUERY" | grep -iE 'DELETE|DROP|TRUNCATE|ALTER'; then
SAFETY="FAIL"
fiExtend the JSON log structure:
echo "{
\"timestamp\":\"$(date -Iseconds)\",
\"query\":\"$QUERY\",
\"your_metric\":\"$VALUE\"
}" >> .claude/logs/analytics-metrics.jsonlUpdate POST_RESPONSE_LOG variable in hook script.
Check:
- Hook is executable:
ls -l .claude/hooks/*.sh - Hook is enabled in settings.json
- Agent name matches:
analytics-agent(hyphenated)
Debug:
# Test hook manually
export CLAUDE_RESPONSE='{"content":"SELECT * FROM users;"}'
./.claude/hooks/post-response-metrics.shCheck:
- Log directory exists:
mkdir -p .claude/logs - Write permissions:
touch .claude/logs/test.log - Query extraction pattern matches your SQL format
Check:
jqis installed:which jq- Log file is valid JSONL:
jq . .claude/logs/analytics-metrics.jsonl
- Hook adds ~50ms overhead per response
- Log file grows ~200 bytes per query
- Rotate logs monthly to prevent bloat
- Logs contain actual SQL queries (may include sensitive data)
- Add to
.gitignore:.claude/logs/ - Consider redacting sensitive values in hook script
- Hook requires database access for
exec_timemeasurement - Configure connection in hook script or use environment variables
- Ensure read-only credentials for safety
- Agent Evaluation Guide: Complete evaluation methodology
- Hooks Documentation: Hook system reference
- nao Framework: Production analytics agent framework (inspiration)
Same as parent repository (MIT)
Questions? Open an issue or discussion in the main repository.