Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

12 Pitfalls I Hit Auto-Logging Claude Code Subagents with a Stop Hook

Description and use

Automated logging of Claude Code subagents via a Stop hook enabled measurement of duration and errors, identified slow subagents, and cut weekly costs by 15–20 %.

Problem

Hard to identify which Claude Code subagents drive cost and performance without data, leading to inefficiencies and high expenses.

Outcome

Automated logging of subagent activity enabled data‑driven optimization, cutting weekly Claude Code spend by 15–20% and improving overall system performance.

Workflow

  • 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.

Evidence

Changing one habit based on that number dropped my weekly Claude Code spend by roughly 15–20%.

Source

A Stop hook is a shell script that runs every time a Claude Code session ends.

Source

  • Record date: 2026-09-15T21:15:52.783Z