feat(handlers): add agent usage metrics and sub-agent tracking#17
feat(handlers): add agent usage metrics and sub-agent tracking#17digitalfiz merged 1 commit intomainfrom
Conversation
- Tag token.usage, cost.usage, cache.count, message.count and model.usage counters with an 'agent' attribute sourced from session totals (populated by the chat.message hook); defaults to 'unknown' when absent - Add 'agent' to api_request and api_error log record attributes - Handle SubtaskPart in handleMessagePartUpdated: increment new opencode.subtask.count counter (attrs: agent, session.id, project.id) and emit subtask_invoked log event with agent, description, prompt_length - Tag opencode.session.count and session.created log records with is_subagent boolean derived from Session.parentID presence - Store agent in SessionTotals; seed to 'unknown' on session.created, updated by chat.message hook on each user prompt - Add subtaskCounter to Instruments type and register instrument in otel.ts - 151 tests passing, typecheck clean
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughChanges add subtask invocation tracking via a new metric counter and propagate an Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can approve the review once all CodeRabbit's comments are resolved.Enable the |
Summary
Agent attribution on existing metrics —
opencode.token.usage,opencode.cost.usage,opencode.cache.count,opencode.message.count, andopencode.model.usagenow carry anagentattribute, enabling per-agent cost/token breakdowns in dashboards. The agent name is sourced fromSessionTotals(populated by thechat.messagehook on each user prompt) and defaults to"unknown"when absent. Theapi_requestandapi_errorlog records are also tagged.Sub-agent invocation counter — new
opencode.subtask.countcounter fires on everySubtaskPartevent (when opencode spawns a sub-agent), withagent,session.id, andproject.idattributes. Asubtask_invokedlog record is always emitted alongside it (respectsOPENCODE_DISABLE_METRICS=subtask.count).Session-level sub-agent tagging —
opencode.session.countand thesession.createdlog record now includeis_subagent(boolean), derived from the presence ofSession.parentID, making it straightforward to distinguish root sessions from sub-agent child sessions.Changes
src/types.tssubtaskCountertoInstruments; addedagentfield toSessionTotalssrc/otel.tsopencode.subtask.countcounter instrumentsrc/util.tsagentfield when accumulating session totalssrc/handlers/message.tsagentattr to all 5 counters and both log records; addedSubtaskParthandlingsrc/handlers/session.tsis_subagentattr to session counter and log record; seededagent: "unknown"inSessionTotalssrc/index.tschat.messagehook now writes agent intosessionTotalsfor the sessiontests/Testing
Summary by CodeRabbit
Release Notes
New Features
Tests