Summary
Before optimizing token usage or debugging unconscious processes, we need visibility into what's happening. Track every API call, every error, and every action so we can reconstruct what happened and find issues.
Requirements
Token tracking
- Log every Anthropic API call: model, input tokens, output tokens, cache hits, cost
- Track per-component: brain, worker, heartbeat, cron, (future) unconscious processes
- Daily/hourly summaries: total spend, breakdown by component
- Expose via tool so the agent can check its own costs
Error logging
- Structured error log with timestamps, component, error type, stack trace
- Tool call failures with the tool name, args, and error
- Hot-reload failures and rollbacks
- Worker task failures
Action audit trail
- Every tool call the brain makes (name, args, result summary, tokens)
- Every channel message (direction, channel, user, timestamp)
- Every file change detected by the kernel
- Every heartbeat/cron cycle and what it did
Access
- Log files in .state/logs/ (rotated daily)
- Tool for the agent to query its own logs (log_search, log_summary)
- Dashboard endpoint /api/logs for the UI (future)
Implementation
server/modules/logger.js — structured logger used by all components
server/modules/tools/logs.js — tools for the agent to query logs
- All existing components updated to log through the structured logger
Why first
Every unconscious process we build after this will automatically have observability. Build the foundation once, benefit forever.
Summary
Before optimizing token usage or debugging unconscious processes, we need visibility into what's happening. Track every API call, every error, and every action so we can reconstruct what happened and find issues.
Requirements
Token tracking
Error logging
Action audit trail
Access
Implementation
server/modules/logger.js— structured logger used by all componentsserver/modules/tools/logs.js— tools for the agent to query logsWhy first
Every unconscious process we build after this will automatically have observability. Build the foundation once, benefit forever.