12 Pitfalls I Hit Auto-Logging Claude Code Subagents with a Stop Hook
Описание и применение
Автоматизированный логинг подагентов Claude Code с помощью Stop hook позволил измерять время и ошибки, выявить медленные подагенты и сократить еженедельные расходы на 15–20 %.
Задача
Hard to identify which Claude Code subagents drive cost and performance without data, leading to inefficiencies and high expenses.
Результат
Automated logging of subagent activity enabled data‑driven optimization, cutting weekly Claude Code spend by 15–20% and improving overall system performance.
Рабочий процесс
- 1. Add a line to ~/.claude/settings.json to enable the Stop hook.
- 2. Create stop_agent_tracker.sh that sets environment variables and calls a Python script.
- 3. Python script reads transcript_path from stdin, parses transcript.jsonl.
- 4. Deduplicate entries by session_id and tool_use_id.
- 5. Pass 1: build dictionaries of tool_use (Agent) and tool_result entries.
- 6. Pass 2: match tool_use_id to tool_result, compute duration and error flag.
- 7. Append structured records to ~/.claude/logs/agent-invocations.jsonl.
- 8. Weekly, aggregate logs with jq to compute average duration per subagent and error rates.
- 9. Identify slow or error‑prone subagents (e.g., code-reviewer at 37s).
- 10. Refactor or replace those subagents to reduce cost and latency.
Доказательства
Changing one habit based on that number dropped my weekly Claude Code spend by roughly 15–20%.
A Stop hook is a shell script that runs every time a Claude Code session ends.
- Дата записи: 2026-09-15T21:15:52.783Z