Fix logging for parallel tool calls in Langfuse hook#6
Open
junwai7159 wants to merge 1 commit into
Open
Conversation
Ensure parallel tool calls are logged correctly to Langfuse by fixing conditional handling and adding clearer context to log entries in hooks/langfuse_hook.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a turn fires several tools in parallel (e.g. 3
WebFetchcalls at once or multiple subagents launched together viaAgent), only the last tool span shows up in Langfuse. The rest silently vanish.Root cause
Claude Code writes one logical assistant message (single
message.id) as several transcript rows: the thinking/text rows first, then one row per paralleltool_useblock, and these rows can be interleaved withtool_resultrows.build_turns()de-dupes assistant rows bymessage.idwith a "latest row wins" assignment:So every parallel tool call but the last gets overwritten before emit.
Raw transcript evidence
A real turn that fired 4 parallel
Readcalls. These are 4 separate JSONL rows (distinctuuid+timestamp), but all share onemessage.idand each carries a singletool_useblock (fields trimmed to the relevant ones):All four rows hash to the same key in
assistant_latest, so the dict assignment keeps only row 4 (subagent_four.md) — rows 1–3 are silently overwritten and never emitted to Langfuse.Note the rows aren't contiguous in the file either:
tool_resultrows for the earlier calls interleave between them, so a positional fix wouldn't be safe — merging bymessage.idis.Fix
Merge the content blocks across rows that share a
message.idinstead of overwriting, so alltool_useblocks survive.Metadata (
model,usage,timestamp) is identical across the split rows, so the first row's values are kept.Notes
~/.claude/state/langfuse_state.jsonare not rewritten.