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

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

说明与用途

通过 Stop hook 自动记录 Claude Code 子代理的调用,测量耗时和错误,识别慢子代理,降低每周成本 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