Skip to content

feat(hook): trace Task subagents into the same Langfuse session#16

Open
LeonardoDaviti wants to merge 1 commit into
langfuse:mainfrom
LeonardoDaviti:feat/trace-subagents
Open

feat(hook): trace Task subagents into the same Langfuse session#16
LeonardoDaviti wants to merge 1 commit into
langfuse:mainfrom
LeonardoDaviti:feat/trace-subagents

Conversation

@LeonardoDaviti

Copy link
Copy Markdown

Subagents spawned via the Task tool run in isolated sub-sessions with their own transcripts under /subagents/agent-*.jsonl. The hook only parsed the parent transcript, so all subagent model calls, tool use and cost were dropped — only the orchestrator model appeared and session cost was undercounted.

Sweep the subagents/ directory on Stop/SessionEnd and feed each subagent transcript through the existing incremental pipeline (read_new_jsonl -> build_turns -> emit_turn), emitting it as a separate trace in the same session, linked to the spawning Task tool_use via parent_tool_use_id metadata and subagent / agent:<type> / parent_tool:<id> tags. Idempotent via per-file offset state; recurses for nested subagents (spawnDepth > 1).

emit_turn gains optional trace_name/root_name/extra_tags/extra_metadata so subagent traces reuse the same emission logic. main() no longer early-returns when the parent transcript has no new rows, so subagents are still swept.

Subagents spawned via the Task tool run in isolated sub-sessions with their
own transcripts under <session>/subagents/agent-*.jsonl. The hook only parsed
the parent transcript, so all subagent model calls, tool use and cost were
dropped — only the orchestrator model appeared and session cost was
undercounted.

Sweep the subagents/ directory on Stop/SessionEnd and feed each subagent
transcript through the existing incremental pipeline (read_new_jsonl ->
build_turns -> emit_turn), emitting it as a separate trace in the same session,
linked to the spawning Task tool_use via `parent_tool_use_id` metadata and
`subagent` / `agent:<type>` / `parent_tool:<id>` tags. Idempotent via per-file
offset state; recurses for nested subagents (spawnDepth > 1).

emit_turn gains optional trace_name/root_name/extra_tags/extra_metadata so
subagent traces reuse the same emission logic. main() no longer early-returns
when the parent transcript has no new rows, so subagents are still swept.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants