From 2466ca6b764e2d574ecfa36582f21cb2071411b8 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 15 Jun 2026 16:26:13 +0200 Subject: [PATCH 1/4] fix(claude-agent-sdk): Fix subagents span nesting and input --- ...agent-sdk-v0.2.76-auto-hook.span-tree.json | 73 +------ ...-agent-sdk-v0.2.76-auto-hook.span-tree.txt | 73 +------ ...e-agent-sdk-v0.2.76-wrapped.span-tree.json | 73 +------ ...de-agent-sdk-v0.2.76-wrapped.span-tree.txt | 73 +------ ...agent-sdk-v0.2.79-auto-hook.span-tree.json | 73 +------ ...-agent-sdk-v0.2.79-auto-hook.span-tree.txt | 73 +------ ...e-agent-sdk-v0.2.79-wrapped.span-tree.json | 73 +------ ...de-agent-sdk-v0.2.79-wrapped.span-tree.txt | 73 +------ ...agent-sdk-v0.2.81-auto-hook.span-tree.json | 73 +------ ...-agent-sdk-v0.2.81-auto-hook.span-tree.txt | 73 +------ ...e-agent-sdk-v0.2.81-wrapped.span-tree.json | 73 +------ ...de-agent-sdk-v0.2.81-wrapped.span-tree.txt | 73 +------ .../assertions.ts | 27 +++ .../plugins/claude-agent-sdk-plugin.ts | 186 +++++++++++++++--- js/tests/provider-wrappers.test.ts | 129 ++++++++++++ 15 files changed, 390 insertions(+), 828 deletions(-) diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json index b2f458745..334a48254 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json @@ -422,27 +422,14 @@ } ], "input": [ - { - "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "Add 15 and 27 using calculator", - "prompt": "Please add 15 and 27 together using the calculator tool. Report the result to me.", - "subagent_type": "math-expert" - }, - "name": "Agent", - "type": "tool_use" + "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -569,24 +556,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "name": "mcp__calculator__calculator", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ @@ -718,27 +687,14 @@ "type": "llm", "children": [], "input": [ - { - "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "echo greeting", - "prompt": "Please echo a greeting message", - "subagent_type": "echo" - }, - "name": "Agent", - "type": "tool_use" + "text": "Please echo a greeting message", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -886,23 +842,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "command": "echo hello", - "description": "Echo hello message" - }, - "name": "Bash", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt index f498893a2..2f145e5d8 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt @@ -418,26 +418,13 @@ span_tree: │ │ └── anthropic.messages.create [llm] │ │ input: [ │ │ { - │ │ "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - │ │ "role": "user" - │ │ }, - │ │ { │ │ "content": [ │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "description": "Add 15 and 27 using calculator", - │ │ "prompt": "Please add 15 and 27 together using the calculator tool. Report the result to me.", - │ │ "subagent_type": "math-expert" - │ │ }, - │ │ "name": "Agent", - │ │ "type": "tool_use" + │ │ "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", + │ │ "type": "text" │ │ } │ │ ], - │ │ "role": "assistant" + │ │ "role": "user" │ │ } │ │ ] │ │ output: [ @@ -514,24 +501,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "a": 15, - │ "b": 27, - │ "operation": "add" - │ }, - │ "name": "mcp__calculator__calculator", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ @@ -705,26 +674,13 @@ span_tree: │ │ ├── anthropic.messages.create [llm] │ │ │ input: [ │ │ │ { - │ │ │ "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - │ │ │ "role": "user" - │ │ │ }, - │ │ │ { │ │ │ "content": [ │ │ │ { - │ │ │ "caller": { - │ │ │ "type": "direct" - │ │ │ }, - │ │ │ "id": "", - │ │ │ "input": { - │ │ │ "description": "echo greeting", - │ │ │ "prompt": "Please echo a greeting message", - │ │ │ "subagent_type": "echo" - │ │ │ }, - │ │ │ "name": "Agent", - │ │ │ "type": "tool_use" + │ │ │ "text": "Please echo a greeting message", + │ │ │ "type": "text" │ │ │ } │ │ │ ], - │ │ │ "role": "assistant" + │ │ │ "role": "user" │ │ │ } │ │ │ ] │ │ │ output: [ @@ -797,23 +753,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "command": "echo hello", - │ "description": "Echo hello message" - │ }, - │ "name": "Bash", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json index 5d9fff7af..4f8a58dc2 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json @@ -422,27 +422,14 @@ } ], "input": [ - { - "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "Add 15 and 27 using calculator", - "prompt": "Please add 15 and 27 together using the calculator tool. Report the result to me.", - "subagent_type": "math-expert" - }, - "name": "Agent", - "type": "tool_use" + "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -569,24 +556,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "name": "mcp__calculator__calculator", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ @@ -718,27 +687,14 @@ "type": "llm", "children": [], "input": [ - { - "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "echo greeting", - "prompt": "Please echo a greeting message", - "subagent_type": "echo" - }, - "name": "Agent", - "type": "tool_use" + "text": "Please echo a greeting message", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -886,23 +842,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "command": "echo hello", - "description": "Echo hello message" - }, - "name": "Bash", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt index 84fc3e813..c63802891 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt @@ -418,26 +418,13 @@ span_tree: │ │ └── anthropic.messages.create [llm] │ │ input: [ │ │ { - │ │ "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - │ │ "role": "user" - │ │ }, - │ │ { │ │ "content": [ │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "description": "Add 15 and 27 using calculator", - │ │ "prompt": "Please add 15 and 27 together using the calculator tool. Report the result to me.", - │ │ "subagent_type": "math-expert" - │ │ }, - │ │ "name": "Agent", - │ │ "type": "tool_use" + │ │ "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", + │ │ "type": "text" │ │ } │ │ ], - │ │ "role": "assistant" + │ │ "role": "user" │ │ } │ │ ] │ │ output: [ @@ -514,24 +501,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "a": 15, - │ "b": 27, - │ "operation": "add" - │ }, - │ "name": "mcp__calculator__calculator", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ @@ -705,26 +674,13 @@ span_tree: │ │ ├── anthropic.messages.create [llm] │ │ │ input: [ │ │ │ { - │ │ │ "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - │ │ │ "role": "user" - │ │ │ }, - │ │ │ { │ │ │ "content": [ │ │ │ { - │ │ │ "caller": { - │ │ │ "type": "direct" - │ │ │ }, - │ │ │ "id": "", - │ │ │ "input": { - │ │ │ "description": "echo greeting", - │ │ │ "prompt": "Please echo a greeting message", - │ │ │ "subagent_type": "echo" - │ │ │ }, - │ │ │ "name": "Agent", - │ │ │ "type": "tool_use" + │ │ │ "text": "Please echo a greeting message", + │ │ │ "type": "text" │ │ │ } │ │ │ ], - │ │ │ "role": "assistant" + │ │ │ "role": "user" │ │ │ } │ │ │ ] │ │ │ output: [ @@ -797,23 +753,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "command": "echo hello", - │ "description": "Echo hello message" - │ }, - │ "name": "Bash", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json index 2d6ea7b2e..c6ec2ef48 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json @@ -422,27 +422,14 @@ } ], "input": [ - { - "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "Add 15 and 27 using calculator", - "prompt": "Use the calculator tool to add 15 and 27 together. Report the sum.", - "subagent_type": "math-expert" - }, - "name": "Agent", - "type": "tool_use" + "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -569,24 +556,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "name": "mcp__calculator__calculator", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ @@ -718,27 +687,14 @@ "type": "llm", "children": [], "input": [ - { - "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "echo greeting", - "prompt": "Run an echo command to display a greeting message.", - "subagent_type": "echo" - }, - "name": "Agent", - "type": "tool_use" + "text": "Run an echo command to display a greeting message.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -886,23 +842,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "command": "echo hello", - "description": "Display a greeting message" - }, - "name": "Bash", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt index 566fa1060..2c972b611 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt @@ -418,26 +418,13 @@ span_tree: │ │ └── anthropic.messages.create [llm] │ │ input: [ │ │ { - │ │ "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - │ │ "role": "user" - │ │ }, - │ │ { │ │ "content": [ │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "description": "Add 15 and 27 using calculator", - │ │ "prompt": "Use the calculator tool to add 15 and 27 together. Report the sum.", - │ │ "subagent_type": "math-expert" - │ │ }, - │ │ "name": "Agent", - │ │ "type": "tool_use" + │ │ "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", + │ │ "type": "text" │ │ } │ │ ], - │ │ "role": "assistant" + │ │ "role": "user" │ │ } │ │ ] │ │ output: [ @@ -514,24 +501,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "a": 15, - │ "b": 27, - │ "operation": "add" - │ }, - │ "name": "mcp__calculator__calculator", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ @@ -705,26 +674,13 @@ span_tree: │ │ ├── anthropic.messages.create [llm] │ │ │ input: [ │ │ │ { - │ │ │ "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - │ │ │ "role": "user" - │ │ │ }, - │ │ │ { │ │ │ "content": [ │ │ │ { - │ │ │ "caller": { - │ │ │ "type": "direct" - │ │ │ }, - │ │ │ "id": "", - │ │ │ "input": { - │ │ │ "description": "echo greeting", - │ │ │ "prompt": "Run an echo command to display a greeting message.", - │ │ │ "subagent_type": "echo" - │ │ │ }, - │ │ │ "name": "Agent", - │ │ │ "type": "tool_use" + │ │ │ "text": "Run an echo command to display a greeting message.", + │ │ │ "type": "text" │ │ │ } │ │ │ ], - │ │ │ "role": "assistant" + │ │ │ "role": "user" │ │ │ } │ │ │ ] │ │ │ output: [ @@ -797,23 +753,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "command": "echo hello", - │ "description": "Display a greeting message" - │ }, - │ "name": "Bash", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json index bb1b94822..2e27a7e9c 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json @@ -422,27 +422,14 @@ } ], "input": [ - { - "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "Add 15 and 27 using calculator", - "prompt": "Use the calculator tool to add 15 and 27 together. Report the sum.", - "subagent_type": "math-expert" - }, - "name": "Agent", - "type": "tool_use" + "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -569,24 +556,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "name": "mcp__calculator__calculator", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ @@ -718,27 +687,14 @@ "type": "llm", "children": [], "input": [ - { - "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "echo greeting", - "prompt": "Run an echo command to display a greeting message.", - "subagent_type": "echo" - }, - "name": "Agent", - "type": "tool_use" + "text": "Run an echo command to display a greeting message.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -886,23 +842,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "command": "echo hello", - "description": "Display a greeting message" - }, - "name": "Bash", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt index 9dc498ca9..d56e953c5 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt @@ -418,26 +418,13 @@ span_tree: │ │ └── anthropic.messages.create [llm] │ │ input: [ │ │ { - │ │ "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - │ │ "role": "user" - │ │ }, - │ │ { │ │ "content": [ │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "description": "Add 15 and 27 using calculator", - │ │ "prompt": "Use the calculator tool to add 15 and 27 together. Report the sum.", - │ │ "subagent_type": "math-expert" - │ │ }, - │ │ "name": "Agent", - │ │ "type": "tool_use" + │ │ "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", + │ │ "type": "text" │ │ } │ │ ], - │ │ "role": "assistant" + │ │ "role": "user" │ │ } │ │ ] │ │ output: [ @@ -514,24 +501,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "a": 15, - │ "b": 27, - │ "operation": "add" - │ }, - │ "name": "mcp__calculator__calculator", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ @@ -705,26 +674,13 @@ span_tree: │ │ ├── anthropic.messages.create [llm] │ │ │ input: [ │ │ │ { - │ │ │ "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - │ │ │ "role": "user" - │ │ │ }, - │ │ │ { │ │ │ "content": [ │ │ │ { - │ │ │ "caller": { - │ │ │ "type": "direct" - │ │ │ }, - │ │ │ "id": "", - │ │ │ "input": { - │ │ │ "description": "echo greeting", - │ │ │ "prompt": "Run an echo command to display a greeting message.", - │ │ │ "subagent_type": "echo" - │ │ │ }, - │ │ │ "name": "Agent", - │ │ │ "type": "tool_use" + │ │ │ "text": "Run an echo command to display a greeting message.", + │ │ │ "type": "text" │ │ │ } │ │ │ ], - │ │ │ "role": "assistant" + │ │ │ "role": "user" │ │ │ } │ │ │ ] │ │ │ output: [ @@ -797,23 +753,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "command": "echo hello", - │ "description": "Display a greeting message" - │ }, - │ "name": "Bash", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json index 5731f1d7f..41e893b3d 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json @@ -422,27 +422,14 @@ } ], "input": [ - { - "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "Add 15 and 27 together", - "prompt": "Please use the calculator tool to add 15 and 27 together. Report the result.", - "subagent_type": "math-expert" - }, - "name": "Agent", - "type": "tool_use" + "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -569,24 +556,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "name": "mcp__calculator__calculator", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ @@ -718,27 +687,14 @@ "type": "llm", "children": [], "input": [ - { - "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "echo greeting", - "prompt": "Run a bash echo command to output a greeting message.", - "subagent_type": "echo" - }, - "name": "Agent", - "type": "tool_use" + "text": "Run a bash echo command to output a greeting message.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -886,23 +842,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "command": "echo \"hello\"", - "description": "Output a greeting message" - }, - "name": "Bash", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt index b59ad2bed..66ceb109d 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt @@ -418,26 +418,13 @@ span_tree: │ │ └── anthropic.messages.create [llm] │ │ input: [ │ │ { - │ │ "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - │ │ "role": "user" - │ │ }, - │ │ { │ │ "content": [ │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "description": "Add 15 and 27 together", - │ │ "prompt": "Please use the calculator tool to add 15 and 27 together. Report the result.", - │ │ "subagent_type": "math-expert" - │ │ }, - │ │ "name": "Agent", - │ │ "type": "tool_use" + │ │ "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", + │ │ "type": "text" │ │ } │ │ ], - │ │ "role": "assistant" + │ │ "role": "user" │ │ } │ │ ] │ │ output: [ @@ -514,24 +501,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "a": 15, - │ "b": 27, - │ "operation": "add" - │ }, - │ "name": "mcp__calculator__calculator", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ @@ -705,26 +674,13 @@ span_tree: │ │ ├── anthropic.messages.create [llm] │ │ │ input: [ │ │ │ { - │ │ │ "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - │ │ │ "role": "user" - │ │ │ }, - │ │ │ { │ │ │ "content": [ │ │ │ { - │ │ │ "caller": { - │ │ │ "type": "direct" - │ │ │ }, - │ │ │ "id": "", - │ │ │ "input": { - │ │ │ "description": "echo greeting", - │ │ │ "prompt": "Run a bash echo command to output a greeting message.", - │ │ │ "subagent_type": "echo" - │ │ │ }, - │ │ │ "name": "Agent", - │ │ │ "type": "tool_use" + │ │ │ "text": "Run a bash echo command to output a greeting message.", + │ │ │ "type": "text" │ │ │ } │ │ │ ], - │ │ │ "role": "assistant" + │ │ │ "role": "user" │ │ │ } │ │ │ ] │ │ │ output: [ @@ -797,23 +753,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "command": "echo \"hello\"", - │ "description": "Output a greeting message" - │ }, - │ "name": "Bash", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json index ecb1df2ec..efbd4ff55 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json @@ -422,27 +422,14 @@ } ], "input": [ - { - "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "Add 15 and 27 together", - "prompt": "Please use the calculator tool to add 15 and 27 together. Report the result.", - "subagent_type": "math-expert" - }, - "name": "Agent", - "type": "tool_use" + "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -569,24 +556,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "name": "mcp__calculator__calculator", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ @@ -718,27 +687,14 @@ "type": "llm", "children": [], "input": [ - { - "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - "role": "user" - }, { "content": [ { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "description": "echo greeting", - "prompt": "Run a bash echo command to output a greeting message.", - "subagent_type": "echo" - }, - "name": "Agent", - "type": "tool_use" + "text": "Run a bash echo command to output a greeting message.", + "type": "text" } ], - "role": "assistant" + "role": "user" } ], "output": [ @@ -886,23 +842,6 @@ } ], "role": "assistant" - }, - { - "content": [ - { - "caller": { - "type": "direct" - }, - "id": "", - "input": { - "command": "echo \"hello\"", - "description": "Output a greeting message" - }, - "name": "Bash", - "type": "tool_use" - } - ], - "role": "assistant" } ], "output": [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt index c795904c0..aa20ba3e9 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt @@ -418,26 +418,13 @@ span_tree: │ │ └── anthropic.messages.create [llm] │ │ input: [ │ │ { - │ │ "content": "Spawn a math-expert subagent to add 15 and 27 using the calculator tool. Report the result. Do not solve it yourself.", - │ │ "role": "user" - │ │ }, - │ │ { │ │ "content": [ │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "description": "Add 15 and 27 together", - │ │ "prompt": "Please use the calculator tool to add 15 and 27 together. Report the result.", - │ │ "subagent_type": "math-expert" - │ │ }, - │ │ "name": "Agent", - │ │ "type": "tool_use" + │ │ "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", + │ │ "type": "text" │ │ } │ │ ], - │ │ "role": "assistant" + │ │ "role": "user" │ │ } │ │ ] │ │ output: [ @@ -514,24 +501,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "a": 15, - │ "b": 27, - │ "operation": "add" - │ }, - │ "name": "mcp__calculator__calculator", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ @@ -705,26 +674,13 @@ span_tree: │ │ ├── anthropic.messages.create [llm] │ │ │ input: [ │ │ │ { - │ │ │ "content": "You MUST call the Agent tool now with subagent_type=\"echo\" and description \"echo greeting\". Do not call Bash yourself. Do not answer with text yourself; delegate to the echo sub-agent.", - │ │ │ "role": "user" - │ │ │ }, - │ │ │ { │ │ │ "content": [ │ │ │ { - │ │ │ "caller": { - │ │ │ "type": "direct" - │ │ │ }, - │ │ │ "id": "", - │ │ │ "input": { - │ │ │ "description": "echo greeting", - │ │ │ "prompt": "Run a bash echo command to output a greeting message.", - │ │ │ "subagent_type": "echo" - │ │ │ }, - │ │ │ "name": "Agent", - │ │ │ "type": "tool_use" + │ │ │ "text": "Run a bash echo command to output a greeting message.", + │ │ │ "type": "text" │ │ │ } │ │ │ ], - │ │ │ "role": "assistant" + │ │ │ "role": "user" │ │ │ } │ │ │ ] │ │ │ output: [ @@ -797,23 +753,6 @@ span_tree: │ } │ ], │ "role": "assistant" - │ }, - │ { - │ "content": [ - │ { - │ "caller": { - │ "type": "direct" - │ }, - │ "id": "", - │ "input": { - │ "command": "echo \"hello\"", - │ "description": "Output a greeting message" - │ }, - │ "name": "Bash", - │ "type": "tool_use" - │ } - │ ], - │ "role": "assistant" │ } │ ] │ output: [ diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts b/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts index b5f07ac18..2c0eafe75 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts @@ -582,6 +582,12 @@ export function defineClaudeAgentSDKInstrumentationAssertions(options: { "anthropic.messages.create", nestedTask?.span.id, ).at(-1); + const nestedTaskLlmInput = nestedTaskLlm?.input as + | Array<{ content?: unknown; role?: string }> + | undefined; + const handoffToolInput = handoffTool?.input as + | { prompt?: unknown } + | undefined; const tool = findToolSpanByLocalHandler(events, "calculator-local-handler-add") ?? findToolSpanByOperation(events, "add"); @@ -616,6 +622,27 @@ export function defineClaudeAgentSDKInstrumentationAssertions(options: { expect(nestedTaskLlm?.span.parentIds).not.toContain( taskRoot?.span.id ?? "", ); + const nestedTaskLlmInputContent = nestedTaskLlmInput?.[0]?.content; + const nestedTaskLlmInputText = + typeof nestedTaskLlmInputContent === "string" + ? nestedTaskLlmInputContent + : Array.isArray(nestedTaskLlmInputContent) + ? nestedTaskLlmInputContent + .map((block) => + typeof block === "object" && + block !== null && + "text" in block && + typeof block.text === "string" + ? block.text + : "", + ) + .join("") + : undefined; + expect(nestedTaskLlmInput?.[0]).toMatchObject({ + role: "user", + }); + expect(nestedTaskLlmInputText).toBe(handoffToolInput?.prompt); + expect(nestedTaskLlmInputText).not.toBe(taskRoot?.input); if (tool) { expect(tool.span.parentIds).not.toContain(taskRoot?.span.id ?? ""); if (toolParent?.span.type === "llm") { diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts b/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts index bf8bcaa9c..e123c9693 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts @@ -56,6 +56,12 @@ type SubAgentDetails = { workflowName?: string; }; const ROOT_LLM_PARENT_KEY = "__root__"; +const SUB_AGENT_PROMPT_SOURCE_PRIORITY = { + delegation: 0, + lifecycle: 1, + sidechain: 2, +} as const; +type SubAgentPromptSource = keyof typeof SUB_AGENT_PROMPT_SOURCE_PRIORITY; function llmParentKey(parentToolUseId: string | null): string { return parentToolUseId ?? ROOT_LLM_PARENT_KEY; @@ -253,26 +259,59 @@ function extractUsageFromMessage( } function buildLLMInput( - prompt: string | AsyncIterable | undefined, + promptMessages: ClaudeConversationMessage[], conversationHistory: ClaudeConversationMessage[], - capturedPromptMessages?: ClaudeAgentSDKMessage[], ): ClaudeConversationMessage[] | undefined { - const promptMessages: ClaudeConversationMessage[] = []; + const inputParts = [...promptMessages, ...conversationHistory]; + return inputParts.length > 0 ? inputParts : undefined; +} + +function conversationMessageFromSDKMessage( + message: ClaudeAgentSDKMessage, +): ClaudeConversationMessage | undefined { + const role = message.message?.role; + const content = message.message?.content; + if (role && content !== undefined) { + return { content, role }; + } + + return undefined; +} + +function messageContentHasBlockType( + message: ClaudeAgentSDKMessage, + blockType: string, +): boolean { + const content = message.message?.content; + return ( + Array.isArray(content) && + content.some( + (block) => + typeof block === "object" && + block !== null && + "type" in block && + block.type === blockType, + ) + ); +} +function buildRootPromptMessages( + prompt: string | AsyncIterable | undefined, + capturedPromptMessages?: ClaudeAgentSDKMessage[], +): ClaudeConversationMessage[] { if (typeof prompt === "string") { - promptMessages.push({ content: prompt, role: "user" }); - } else if (capturedPromptMessages && capturedPromptMessages.length > 0) { - for (const msg of capturedPromptMessages) { - const role = msg.message?.role; - const content = msg.message?.content; - if (role && content !== undefined) { - promptMessages.push({ content, role }); - } - } + return [{ content: prompt, role: "user" }]; } - const inputParts = [...promptMessages, ...conversationHistory]; - return inputParts.length > 0 ? inputParts : undefined; + if (!capturedPromptMessages || capturedPromptMessages.length === 0) { + return []; + } + + return capturedPromptMessages + .map(conversationMessageFromSDKMessage) + .filter( + (message): message is ClaudeConversationMessage => message !== undefined, + ); } function formatCapturedMessages( @@ -283,11 +322,10 @@ function formatCapturedMessages( async function createLLMSpanForMessages( messages: ClaudeAgentSDKMessage[], - prompt: string | AsyncIterable | undefined, + promptMessages: ClaudeConversationMessage[], conversationHistory: ClaudeConversationMessage[], options: ClaudeAgentSDKQueryOptions, startTime: number, - capturedPromptMessages: ClaudeAgentSDKMessage[] | undefined, parentSpan: string, existingSpan?: Span, ): Promise { @@ -302,11 +340,7 @@ async function createLLMSpanForMessages( const model = lastMessage.message.model || options.model; const usage = extractUsageFromMessage(lastMessage); - const input = buildLLMInput( - prompt, - conversationHistory, - capturedPromptMessages, - ); + const input = buildLLMInput(promptMessages, conversationHistory); const outputs = messages .map((m) => m.message?.content && m.message?.role @@ -810,6 +844,7 @@ type QueryState = { accumulatedOutputTokens: number; activeLlmSpansByParentToolUse: Map; activeToolSpans: Map; + conversationHistoryByParentKey: Map; capturedPromptMessages: ClaudeAgentSDKMessage[] | undefined; currentMessageId: string | undefined; currentMessageStartTime: number; @@ -820,7 +855,9 @@ type QueryState = { originalPrompt: string | AsyncIterable | undefined; processing: Promise; promptDone: Promise; + promptMessagesByParentKey: Map; promptStarted: () => boolean; + promptSourcePriorityByParentKey: Map; span: Span; subAgentDetailsByToolUseId: Map; subAgentSpans: Map; @@ -831,6 +868,40 @@ type QueryState = { localToolContext: ClaudeAgentSDKLocalToolContext; }; +function setSubAgentPromptMessages( + state: QueryState, + parentToolUseId: string, + promptMessages: ClaudeConversationMessage[], + source: SubAgentPromptSource, +): void { + if (promptMessages.length === 0) { + return; + } + + const parentKey = llmParentKey(parentToolUseId); + const sourcePriority = SUB_AGENT_PROMPT_SOURCE_PRIORITY[source]; + const currentPriority = + state.promptSourcePriorityByParentKey.get(parentKey) ?? -1; + + if (sourcePriority > currentPriority) { + state.promptMessagesByParentKey.set(parentKey, promptMessages); + state.promptSourcePriorityByParentKey.set(parentKey, sourcePriority); + } +} + +function getConversationHistory( + state: QueryState, + parentKey: string, +): ClaudeConversationMessage[] { + let conversationHistory = state.conversationHistoryByParentKey.get(parentKey); + if (!conversationHistory) { + conversationHistory = []; + state.conversationHistoryByParentKey.set(parentKey, conversationHistory); + } + + return conversationHistory; +} + async function finalizeCurrentMessageGroup(state: QueryState): Promise { if (state.currentMessages.length === 0) { return; @@ -838,6 +909,13 @@ async function finalizeCurrentMessageGroup(state: QueryState): Promise { const parentToolUseId = state.currentMessages[0]?.parent_tool_use_id ?? null; const parentKey = llmParentKey(parentToolUseId); + const conversationHistory = getConversationHistory(state, parentKey); + const promptMessages = parentToolUseId + ? (state.promptMessagesByParentKey.get(parentKey) ?? []) + : buildRootPromptMessages( + state.originalPrompt, + state.capturedPromptMessages, + ); let parentSpan = await state.span.export(); if (parentToolUseId) { const subAgentSpan = state.subAgentSpans.get(parentToolUseId); @@ -849,11 +927,10 @@ async function finalizeCurrentMessageGroup(state: QueryState): Promise { const llmSpanResult = await createLLMSpanForMessages( state.currentMessages, - state.originalPrompt, - state.finalResults, + promptMessages, + conversationHistory, state.options, state.currentMessageStartTime, - state.capturedPromptMessages, parentSpan, existingLlmSpan, ); @@ -869,6 +946,7 @@ async function finalizeCurrentMessageGroup(state: QueryState): Promise { } if (llmSpanResult.finalMessage) { + conversationHistory.push(llmSpanResult.finalMessage); state.finalResults.push(llmSpanResult.finalMessage); } } @@ -927,6 +1005,16 @@ function maybeTrackToolUseContext( description: getStringProperty(block.input, "description"), toolUseId: block.id, }); + + const prompt = getStringProperty(block.input, "prompt"); + if (prompt) { + setSubAgentPromptMessages( + state, + block.id, + [{ content: prompt, role: "user" }], + "delegation", + ); + } } } } @@ -1087,6 +1175,14 @@ async function maybeHandleTaskLifecycleMessage( if (message.subtype === "task_started") { const prompt = getStringProperty(message, "prompt"); + if (prompt) { + setSubAgentPromptMessages( + state, + toolUseId, + [{ content: prompt, role: "user" }], + "lifecycle", + ); + } subAgentSpan.log({ input: prompt, metadata, @@ -1152,6 +1248,36 @@ async function handleStreamMessage( } await maybeStartSubAgentSpan(state, message); + const messageParentToolUseId = message.parent_tool_use_id; + if (messageParentToolUseId && message.type === "user") { + const conversationMessage = conversationMessageFromSDKMessage(message); + if (conversationMessage?.role === "user") { + await finalizeCurrentMessageGroup(state); + const parentKey = llmParentKey(messageParentToolUseId); + const conversationHistory = getConversationHistory(state, parentKey); + const currentPromptSourcePriority = + state.promptSourcePriorityByParentKey.get(parentKey) ?? -1; + const canUseAsInitialSidechainPrompt = + conversationHistory.length === 0 && + currentPromptSourcePriority < + SUB_AGENT_PROMPT_SOURCE_PRIORITY.sidechain && + !messageContentHasBlockType(message, "tool_result"); + + if (!canUseAsInitialSidechainPrompt) { + conversationHistory.push(conversationMessage); + return; + } + + setSubAgentPromptMessages( + state, + messageParentToolUseId, + [conversationMessage], + "sidechain", + ); + return; + } + } + const messageId = message.message?.id; if (messageId && messageId !== state.currentMessageId) { await finalizeCurrentMessageGroup(state); @@ -1342,6 +1468,10 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { const activeToolSpans = new Map(); const activeLlmSpansByParentToolUse = new Map(); + const conversationHistoryByParentKey = new Map< + string, + ClaudeConversationMessage[] + >(); const subAgentSpans = new Map(); const endedSubAgentSpans = new Set(); const toolUseToParent = new Map(); @@ -1351,6 +1481,11 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { }; const subAgentDetailsByToolUseId = new Map(); const taskIdToToolUseId = new Map(); + const promptMessagesByParentKey = new Map< + string, + ClaudeConversationMessage[] + >(); + const promptSourcePriorityByParentKey = new Map(); const localToolContext = createClaudeLocalToolContext(); const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers); @@ -1448,6 +1583,7 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { accumulatedOutputTokens: 0, activeLlmSpansByParentToolUse, activeToolSpans, + conversationHistoryByParentKey, capturedPromptMessages, currentMessageId: undefined, currentMessageStartTime: startTime, @@ -1458,7 +1594,9 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { originalPrompt, processing: Promise.resolve(), promptDone, + promptMessagesByParentKey, promptStarted: () => promptStarted, + promptSourcePriorityByParentKey, span, subAgentDetailsByToolUseId, subAgentSpans, diff --git a/js/tests/provider-wrappers.test.ts b/js/tests/provider-wrappers.test.ts index 800704b0a..4683d4229 100644 --- a/js/tests/provider-wrappers.test.ts +++ b/js/tests/provider-wrappers.test.ts @@ -511,6 +511,135 @@ describe("provider wrapper", () => { }, ); + test("Claude Agent SDK keeps parented user tool results in sub-agent conversation history", async () => { + const rootPrompt = "Delegate the calculation."; + const subAgentPrompt = "Use the calculator to add 15 and 27."; + const wrappedSDK = wrapClaudeAgentSDK({ + query: () => + (async function* () { + yield { + type: "assistant", + message: { + id: "root-assistant", + role: "assistant", + content: [ + { + id: "agent-tool-use", + input: { + description: "math specialist", + prompt: subAgentPrompt, + subagent_type: "math-expert", + }, + name: "Agent", + type: "tool_use", + }, + ], + usage: { input_tokens: 1, output_tokens: 1 }, + }, + }; + yield { + type: "user", + parent_tool_use_id: "agent-tool-use", + message: { + role: "user", + content: [{ text: subAgentPrompt, type: "text" }], + }, + }; + yield { + type: "assistant", + parent_tool_use_id: "agent-tool-use", + message: { + id: "sub-agent-tool-call", + role: "assistant", + content: [ + { + id: "calculator-tool-use", + input: { a: 15, b: 27, operation: "add" }, + name: "mcp__calculator__calculator", + type: "tool_use", + }, + ], + usage: { input_tokens: 1, output_tokens: 1 }, + }, + }; + yield { + type: "user", + parent_tool_use_id: "agent-tool-use", + message: { + role: "user", + content: [ + { + content: "42", + tool_use_id: "calculator-tool-use", + type: "tool_result", + }, + ], + }, + }; + yield { + type: "assistant", + parent_tool_use_id: "agent-tool-use", + message: { + id: "sub-agent-final", + role: "assistant", + content: [{ text: "The answer is 42.", type: "text" }], + usage: { input_tokens: 1, output_tokens: 1 }, + }, + }; + yield { + type: "result", + usage: { input_tokens: 1, output_tokens: 3 }, + }; + })(), + }); + + for await (const _message of wrappedSDK.query({ + prompt: rootPrompt, + options: { model: "test-model" }, + } as any)) { + } + + const spans = await backgroundLogger.drain(); + const finalSubAgentLlm = spans.find( + (span: any) => + span.span_attributes.name === "anthropic.messages.create" && + Array.isArray(span.output) && + span.output.some((message: any) => + message.content?.some?.( + (block: any) => block.text === "The answer is 42.", + ), + ), + ); + + expect(finalSubAgentLlm?.input).toMatchObject([ + { + content: [{ text: subAgentPrompt, type: "text" }], + role: "user", + }, + { + content: [ + { + id: "calculator-tool-use", + name: "mcp__calculator__calculator", + type: "tool_use", + }, + ], + role: "assistant", + }, + { + content: [ + { + content: "42", + tool_use_id: "calculator-tool-use", + type: "tool_result", + }, + ], + role: "user", + }, + ]); + expect(JSON.stringify(finalSubAgentLlm?.input)).not.toContain(rootPrompt); + }); + test("Claude Agent SDK injects tracing hooks while preserving user hooks", async () => { let capturedOptions: any; const userPreHook = vi.fn().mockResolvedValue({}); From 6b83a39f0d8a555b53e02b1b831eae138bdb8690 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 15 Jun 2026 16:29:19 +0200 Subject: [PATCH 2/4] cs --- .changeset/funky-pigs-clap.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/funky-pigs-clap.md diff --git a/.changeset/funky-pigs-clap.md b/.changeset/funky-pigs-clap.md new file mode 100644 index 000000000..d42470984 --- /dev/null +++ b/.changeset/funky-pigs-clap.md @@ -0,0 +1,5 @@ +--- +"braintrust": patch +--- + +fix(claude-agent-sdk): Fix subagents span nesting and input From 199482383422c8c5e9da116d933c42e669e6658e Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 15 Jun 2026 17:15:41 +0200 Subject: [PATCH 3/4] fixes --- ...agent-sdk-v0.2.76-auto-hook.span-tree.json | 171 ++++++++-------- ...-agent-sdk-v0.2.76-auto-hook.span-tree.txt | 192 +++++++++--------- ...e-agent-sdk-v0.2.76-wrapped.span-tree.json | 171 ++++++++-------- ...de-agent-sdk-v0.2.76-wrapped.span-tree.txt | 192 +++++++++--------- ...agent-sdk-v0.2.79-auto-hook.span-tree.json | 171 ++++++++-------- ...-agent-sdk-v0.2.79-auto-hook.span-tree.txt | 192 +++++++++--------- ...e-agent-sdk-v0.2.79-wrapped.span-tree.json | 171 ++++++++-------- ...de-agent-sdk-v0.2.79-wrapped.span-tree.txt | 192 +++++++++--------- ...agent-sdk-v0.2.81-auto-hook.span-tree.json | 171 ++++++++-------- ...-agent-sdk-v0.2.81-auto-hook.span-tree.txt | 192 +++++++++--------- ...e-agent-sdk-v0.2.81-wrapped.span-tree.json | 171 ++++++++-------- ...de-agent-sdk-v0.2.81-wrapped.span-tree.txt | 192 +++++++++--------- .../assertions.ts | 17 +- .../claude-agent-sdk-local-tool-context.ts | 5 +- .../claude-agent-sdk-local-tool-spans.ts | 4 +- .../plugins/claude-agent-sdk-plugin.ts | 32 ++- js/tests/provider-wrappers.test.ts | 151 +++++++++++++- 17 files changed, 1258 insertions(+), 1129 deletions(-) diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json index 334a48254..ca15791c6 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.json @@ -14,37 +14,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-multiply", - "children": [] - } - ], - "input": { - "a": 15, - "b": 7, - "operation": "multiply" - }, - "output": { - "content": [ - { - "text": "multiply(15, 7) = 105", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to multiply 15 by 7. Do not answer from memory.", @@ -91,6 +61,35 @@ "tokens": 18794 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-multiply", + "children": [] + } + ], + "input": { + "a": 15, + "b": 7, + "operation": "multiply" + }, + "output": { + "content": [ + { + "text": "multiply(15, 7) = 105", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } + }, { "name": "anthropic.messages.create", "type": "llm", @@ -390,37 +389,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-add", - "children": [] - } - ], - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "output": { - "content": [ - { - "text": "add(15, 27) = 42", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": [ @@ -462,6 +431,35 @@ "prompt_tokens": 22031, "tokens": 22034 } + }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-add", + "children": [] + } + ], + "input": { + "a": 15, + "b": 27, + "operation": "add" + }, + "output": { + "content": [ + { + "text": "add(15, 27) = 42", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } } ], "input": "Please add 15 and 27 together using the calculator tool. Report the result to me.", @@ -914,31 +912,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-divide", - "children": [], - "error": "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" - } - ], - "input": { - "a": 2, - "b": 0, - "operation": "divide" - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - }, - "error": "division by zero" - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to divide 2 by 0. Do not recover from the error.", @@ -985,6 +959,29 @@ "tokens": 18802 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-divide", + "children": [], + "error": "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" + } + ], + "input": { + "a": 2, + "b": 0, + "operation": "divide" + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + }, + "error": "division by zero" + }, { "name": "anthropic.messages.create", "type": "llm", diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt index 2f145e5d8..95eeb871e 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-auto-hook.span-tree.txt @@ -72,27 +72,27 @@ span_tree: │ │ "prompt_tokens": 18793, │ │ "tokens": 18794 │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 7, - │ │ "operation": "multiply" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "multiply(15, 7) = 105", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ ├── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 7, + │ │ "operation": "multiply" + │ │ } + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "multiply(15, 7) = 105", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-multiply + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-multiply │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -415,69 +415,69 @@ span_tree: │ │ "claude_agent_sdk.tool_use_id": "toolu_01F7fpreMTSnWawFUTXi7RgG", │ │ "claude_agent_sdk.total_tokens": 22037 │ │ } - │ │ └── anthropic.messages.create [llm] - │ │ input: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ }, - │ │ "name": "mcp__calculator__calculator", - │ │ "type": "tool_use" - │ │ } - │ │ ], - │ │ "role": "assistant" - │ │ } - │ │ ] - │ │ metadata: { - │ │ "model": "claude-haiku-4-5-20251001" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 3, - │ │ "prompt_cache_creation_tokens": 9148, - │ │ "prompt_cached_tokens": 12880, - │ │ "prompt_tokens": 22031, - │ │ "tokens": 22034 + │ │ ├── anthropic.messages.create [llm] + │ │ │ input: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", + │ │ │ "type": "text" + │ │ │ } + │ │ │ ], + │ │ │ "role": "user" + │ │ │ } + │ │ │ ] + │ │ │ output: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "caller": { + │ │ │ "type": "direct" + │ │ │ }, + │ │ │ "id": "", + │ │ │ "input": { + │ │ │ "a": 15, + │ │ │ "b": 27, + │ │ │ "operation": "add" + │ │ │ }, + │ │ │ "name": "mcp__calculator__calculator", + │ │ │ "type": "tool_use" + │ │ │ } + │ │ │ ], + │ │ │ "role": "assistant" + │ │ │ } + │ │ │ ] + │ │ │ metadata: { + │ │ │ "model": "claude-haiku-4-5-20251001" + │ │ │ } + │ │ │ metrics: { + │ │ │ "completion_tokens": 3, + │ │ │ "prompt_cache_creation_tokens": 9148, + │ │ │ "prompt_cached_tokens": 12880, + │ │ │ "prompt_tokens": 22031, + │ │ │ "tokens": 22034 + │ │ │ } + │ │ └── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 27, + │ │ "operation": "add" │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "add(15, 27) = 42", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "add(15, 27) = 42", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-add + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-add │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -854,21 +854,21 @@ span_tree: │ "prompt_tokens": 18794, │ "tokens": 18802 │ } - │ └── tool: calculator/calculator [tool] - │ input: { - │ "a": 2, - │ "b": 0, - │ "operation": "divide" - │ } - │ metadata: { - │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", - │ "gen_ai.tool.name": "calculator", - │ "mcp.server": "calculator" - │ } - │ error: "division by zero" - │ └── calculator-local-handler-divide - │ error: "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" + ├── tool: calculator/calculator [tool] + │ input: { + │ "a": 2, + │ "b": 0, + │ "operation": "divide" + │ } + │ metadata: { + │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", + │ "gen_ai.tool.name": "calculator", + │ "mcp.server": "calculator" + │ } + │ error: "division by zero" + │ └── calculator-local-handler-divide + │ error: "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" └── anthropic.messages.create [llm] input: [ { diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json index 4f8a58dc2..e3e5fd407 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.json @@ -14,37 +14,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-multiply", - "children": [] - } - ], - "input": { - "a": 15, - "b": 7, - "operation": "multiply" - }, - "output": { - "content": [ - { - "text": "multiply(15, 7) = 105", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to multiply 15 by 7. Do not answer from memory.", @@ -91,6 +61,35 @@ "tokens": 18794 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-multiply", + "children": [] + } + ], + "input": { + "a": 15, + "b": 7, + "operation": "multiply" + }, + "output": { + "content": [ + { + "text": "multiply(15, 7) = 105", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } + }, { "name": "anthropic.messages.create", "type": "llm", @@ -390,37 +389,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-add", - "children": [] - } - ], - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "output": { - "content": [ - { - "text": "add(15, 27) = 42", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": [ @@ -462,6 +431,35 @@ "prompt_tokens": 22031, "tokens": 22034 } + }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-add", + "children": [] + } + ], + "input": { + "a": 15, + "b": 27, + "operation": "add" + }, + "output": { + "content": [ + { + "text": "add(15, 27) = 42", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } } ], "input": "Please add 15 and 27 together using the calculator tool. Report the result to me.", @@ -914,31 +912,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-divide", - "children": [], - "error": "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" - } - ], - "input": { - "a": 2, - "b": 0, - "operation": "divide" - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - }, - "error": "division by zero" - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to divide 2 by 0. Do not recover from the error.", @@ -985,6 +959,29 @@ "tokens": 18802 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-divide", + "children": [], + "error": "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" + } + ], + "input": { + "a": 2, + "b": 0, + "operation": "divide" + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + }, + "error": "division by zero" + }, { "name": "anthropic.messages.create", "type": "llm", diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt index c63802891..10744d4a6 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.76-wrapped.span-tree.txt @@ -72,27 +72,27 @@ span_tree: │ │ "prompt_tokens": 18793, │ │ "tokens": 18794 │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 7, - │ │ "operation": "multiply" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "multiply(15, 7) = 105", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ ├── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 7, + │ │ "operation": "multiply" + │ │ } + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "multiply(15, 7) = 105", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-multiply + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_01VxxkUqbM8wfd3hj1SmgRLd", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-multiply │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -415,69 +415,69 @@ span_tree: │ │ "claude_agent_sdk.tool_use_id": "toolu_01F7fpreMTSnWawFUTXi7RgG", │ │ "claude_agent_sdk.total_tokens": 22037 │ │ } - │ │ └── anthropic.messages.create [llm] - │ │ input: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ }, - │ │ "name": "mcp__calculator__calculator", - │ │ "type": "tool_use" - │ │ } - │ │ ], - │ │ "role": "assistant" - │ │ } - │ │ ] - │ │ metadata: { - │ │ "model": "claude-haiku-4-5-20251001" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 3, - │ │ "prompt_cache_creation_tokens": 9148, - │ │ "prompt_cached_tokens": 12880, - │ │ "prompt_tokens": 22031, - │ │ "tokens": 22034 + │ │ ├── anthropic.messages.create [llm] + │ │ │ input: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "text": "Please add 15 and 27 together using the calculator tool. Report the result to me.", + │ │ │ "type": "text" + │ │ │ } + │ │ │ ], + │ │ │ "role": "user" + │ │ │ } + │ │ │ ] + │ │ │ output: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "caller": { + │ │ │ "type": "direct" + │ │ │ }, + │ │ │ "id": "", + │ │ │ "input": { + │ │ │ "a": 15, + │ │ │ "b": 27, + │ │ │ "operation": "add" + │ │ │ }, + │ │ │ "name": "mcp__calculator__calculator", + │ │ │ "type": "tool_use" + │ │ │ } + │ │ │ ], + │ │ │ "role": "assistant" + │ │ │ } + │ │ │ ] + │ │ │ metadata: { + │ │ │ "model": "claude-haiku-4-5-20251001" + │ │ │ } + │ │ │ metrics: { + │ │ │ "completion_tokens": 3, + │ │ │ "prompt_cache_creation_tokens": 9148, + │ │ │ "prompt_cached_tokens": 12880, + │ │ │ "prompt_tokens": 22031, + │ │ │ "tokens": 22034 + │ │ │ } + │ │ └── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 27, + │ │ "operation": "add" │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "add(15, 27) = 42", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "add(15, 27) = 42", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-add + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_01BUZHbp64XJFUZRU2vuUrJ9", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-add │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -854,21 +854,21 @@ span_tree: │ "prompt_tokens": 18794, │ "tokens": 18802 │ } - │ └── tool: calculator/calculator [tool] - │ input: { - │ "a": 2, - │ "b": 0, - │ "operation": "divide" - │ } - │ metadata: { - │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", - │ "gen_ai.tool.name": "calculator", - │ "mcp.server": "calculator" - │ } - │ error: "division by zero" - │ └── calculator-local-handler-divide - │ error: "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" + ├── tool: calculator/calculator [tool] + │ input: { + │ "a": 2, + │ "b": 0, + │ "operation": "divide" + │ } + │ metadata: { + │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ "gen_ai.tool.call.id": "toolu_01CMsRz4pP2m3xfVrPNDKi3r", + │ "gen_ai.tool.name": "calculator", + │ "mcp.server": "calculator" + │ } + │ error: "division by zero" + │ └── calculator-local-handler-divide + │ error: "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" └── anthropic.messages.create [llm] input: [ { diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json index c6ec2ef48..cf9ed4e16 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.json @@ -14,37 +14,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-multiply", - "children": [] - } - ], - "input": { - "a": 15, - "b": 7, - "operation": "multiply" - }, - "output": { - "content": [ - { - "text": "multiply(15, 7) = 105", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to multiply 15 by 7. Do not answer from memory.", @@ -91,6 +61,35 @@ "tokens": 18884 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-multiply", + "children": [] + } + ], + "input": { + "a": 15, + "b": 7, + "operation": "multiply" + }, + "output": { + "content": [ + { + "text": "multiply(15, 7) = 105", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } + }, { "name": "anthropic.messages.create", "type": "llm", @@ -390,37 +389,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-add", - "children": [] - } - ], - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "output": { - "content": [ - { - "text": "add(15, 27) = 42", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": [ @@ -462,6 +431,35 @@ "prompt_tokens": 22218, "tokens": 22226 } + }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-add", + "children": [] + } + ], + "input": { + "a": 15, + "b": 27, + "operation": "add" + }, + "output": { + "content": [ + { + "text": "add(15, 27) = 42", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } } ], "input": "Use the calculator tool to add 15 and 27 together. Report the sum.", @@ -914,31 +912,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-divide", - "children": [], - "error": "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" - } - ], - "input": { - "a": 2, - "b": 0, - "operation": "divide" - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - }, - "error": "division by zero" - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to divide 2 by 0. Do not recover from the error.", @@ -985,6 +959,29 @@ "tokens": 18890 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-divide", + "children": [], + "error": "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" + } + ], + "input": { + "a": 2, + "b": 0, + "operation": "divide" + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + }, + "error": "division by zero" + }, { "name": "anthropic.messages.create", "type": "llm", diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt index 2c972b611..594bda6c8 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-auto-hook.span-tree.txt @@ -72,27 +72,27 @@ span_tree: │ │ "prompt_tokens": 18881, │ │ "tokens": 18884 │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 7, - │ │ "operation": "multiply" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "multiply(15, 7) = 105", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ ├── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 7, + │ │ "operation": "multiply" + │ │ } + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "multiply(15, 7) = 105", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-multiply + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-multiply │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -415,69 +415,69 @@ span_tree: │ │ "claude_agent_sdk.tool_use_id": "toolu_01WQPJ1E4tonQtywdoKhy5Bh", │ │ "claude_agent_sdk.total_tokens": 22234 │ │ } - │ │ └── anthropic.messages.create [llm] - │ │ input: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ }, - │ │ "name": "mcp__calculator__calculator", - │ │ "type": "tool_use" - │ │ } - │ │ ], - │ │ "role": "assistant" - │ │ } - │ │ ] - │ │ metadata: { - │ │ "model": "claude-haiku-4-5-20251001" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 8, - │ │ "prompt_cache_creation_tokens": 9335, - │ │ "prompt_cached_tokens": 12880, - │ │ "prompt_tokens": 22218, - │ │ "tokens": 22226 + │ │ ├── anthropic.messages.create [llm] + │ │ │ input: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", + │ │ │ "type": "text" + │ │ │ } + │ │ │ ], + │ │ │ "role": "user" + │ │ │ } + │ │ │ ] + │ │ │ output: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "caller": { + │ │ │ "type": "direct" + │ │ │ }, + │ │ │ "id": "", + │ │ │ "input": { + │ │ │ "a": 15, + │ │ │ "b": 27, + │ │ │ "operation": "add" + │ │ │ }, + │ │ │ "name": "mcp__calculator__calculator", + │ │ │ "type": "tool_use" + │ │ │ } + │ │ │ ], + │ │ │ "role": "assistant" + │ │ │ } + │ │ │ ] + │ │ │ metadata: { + │ │ │ "model": "claude-haiku-4-5-20251001" + │ │ │ } + │ │ │ metrics: { + │ │ │ "completion_tokens": 8, + │ │ │ "prompt_cache_creation_tokens": 9335, + │ │ │ "prompt_cached_tokens": 12880, + │ │ │ "prompt_tokens": 22218, + │ │ │ "tokens": 22226 + │ │ │ } + │ │ └── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 27, + │ │ "operation": "add" │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "add(15, 27) = 42", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "add(15, 27) = 42", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-add + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-add │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -854,21 +854,21 @@ span_tree: │ "prompt_tokens": 18882, │ "tokens": 18890 │ } - │ └── tool: calculator/calculator [tool] - │ input: { - │ "a": 2, - │ "b": 0, - │ "operation": "divide" - │ } - │ metadata: { - │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", - │ "gen_ai.tool.name": "calculator", - │ "mcp.server": "calculator" - │ } - │ error: "division by zero" - │ └── calculator-local-handler-divide - │ error: "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" + ├── tool: calculator/calculator [tool] + │ input: { + │ "a": 2, + │ "b": 0, + │ "operation": "divide" + │ } + │ metadata: { + │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", + │ "gen_ai.tool.name": "calculator", + │ "mcp.server": "calculator" + │ } + │ error: "division by zero" + │ └── calculator-local-handler-divide + │ error: "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" └── anthropic.messages.create [llm] input: [ { diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json index 2e27a7e9c..bc39edcf0 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.json @@ -14,37 +14,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-multiply", - "children": [] - } - ], - "input": { - "a": 15, - "b": 7, - "operation": "multiply" - }, - "output": { - "content": [ - { - "text": "multiply(15, 7) = 105", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to multiply 15 by 7. Do not answer from memory.", @@ -91,6 +61,35 @@ "tokens": 18884 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-multiply", + "children": [] + } + ], + "input": { + "a": 15, + "b": 7, + "operation": "multiply" + }, + "output": { + "content": [ + { + "text": "multiply(15, 7) = 105", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } + }, { "name": "anthropic.messages.create", "type": "llm", @@ -390,37 +389,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-add", - "children": [] - } - ], - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "output": { - "content": [ - { - "text": "add(15, 27) = 42", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": [ @@ -462,6 +431,35 @@ "prompt_tokens": 22218, "tokens": 22226 } + }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-add", + "children": [] + } + ], + "input": { + "a": 15, + "b": 27, + "operation": "add" + }, + "output": { + "content": [ + { + "text": "add(15, 27) = 42", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } } ], "input": "Use the calculator tool to add 15 and 27 together. Report the sum.", @@ -914,31 +912,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-divide", - "children": [], - "error": "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" - } - ], - "input": { - "a": 2, - "b": 0, - "operation": "divide" - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - }, - "error": "division by zero" - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to divide 2 by 0. Do not recover from the error.", @@ -985,6 +959,29 @@ "tokens": 18890 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-divide", + "children": [], + "error": "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" + } + ], + "input": { + "a": 2, + "b": 0, + "operation": "divide" + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + }, + "error": "division by zero" + }, { "name": "anthropic.messages.create", "type": "llm", diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt index d56e953c5..4f18f59dc 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.79-wrapped.span-tree.txt @@ -72,27 +72,27 @@ span_tree: │ │ "prompt_tokens": 18881, │ │ "tokens": 18884 │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 7, - │ │ "operation": "multiply" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "multiply(15, 7) = 105", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ ├── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 7, + │ │ "operation": "multiply" + │ │ } + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "multiply(15, 7) = 105", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-multiply + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_014C2HCQDmrmSYtkyWwretfS", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-multiply │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -415,69 +415,69 @@ span_tree: │ │ "claude_agent_sdk.tool_use_id": "toolu_01WQPJ1E4tonQtywdoKhy5Bh", │ │ "claude_agent_sdk.total_tokens": 22234 │ │ } - │ │ └── anthropic.messages.create [llm] - │ │ input: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ }, - │ │ "name": "mcp__calculator__calculator", - │ │ "type": "tool_use" - │ │ } - │ │ ], - │ │ "role": "assistant" - │ │ } - │ │ ] - │ │ metadata: { - │ │ "model": "claude-haiku-4-5-20251001" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 8, - │ │ "prompt_cache_creation_tokens": 9335, - │ │ "prompt_cached_tokens": 12880, - │ │ "prompt_tokens": 22218, - │ │ "tokens": 22226 + │ │ ├── anthropic.messages.create [llm] + │ │ │ input: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "text": "Use the calculator tool to add 15 and 27 together. Report the sum.", + │ │ │ "type": "text" + │ │ │ } + │ │ │ ], + │ │ │ "role": "user" + │ │ │ } + │ │ │ ] + │ │ │ output: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "caller": { + │ │ │ "type": "direct" + │ │ │ }, + │ │ │ "id": "", + │ │ │ "input": { + │ │ │ "a": 15, + │ │ │ "b": 27, + │ │ │ "operation": "add" + │ │ │ }, + │ │ │ "name": "mcp__calculator__calculator", + │ │ │ "type": "tool_use" + │ │ │ } + │ │ │ ], + │ │ │ "role": "assistant" + │ │ │ } + │ │ │ ] + │ │ │ metadata: { + │ │ │ "model": "claude-haiku-4-5-20251001" + │ │ │ } + │ │ │ metrics: { + │ │ │ "completion_tokens": 8, + │ │ │ "prompt_cache_creation_tokens": 9335, + │ │ │ "prompt_cached_tokens": 12880, + │ │ │ "prompt_tokens": 22218, + │ │ │ "tokens": 22226 + │ │ │ } + │ │ └── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 27, + │ │ "operation": "add" │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "add(15, 27) = 42", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "add(15, 27) = 42", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-add + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_0118HmrDYqo7W6ZHNA7mgGQf", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-add │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -854,21 +854,21 @@ span_tree: │ "prompt_tokens": 18882, │ "tokens": 18890 │ } - │ └── tool: calculator/calculator [tool] - │ input: { - │ "a": 2, - │ "b": 0, - │ "operation": "divide" - │ } - │ metadata: { - │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", - │ "gen_ai.tool.name": "calculator", - │ "mcp.server": "calculator" - │ } - │ error: "division by zero" - │ └── calculator-local-handler-divide - │ error: "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" + ├── tool: calculator/calculator [tool] + │ input: { + │ "a": 2, + │ "b": 0, + │ "operation": "divide" + │ } + │ metadata: { + │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ "gen_ai.tool.call.id": "toolu_01Y5oGGkuKRD8jBgnySf8vwX", + │ "gen_ai.tool.name": "calculator", + │ "mcp.server": "calculator" + │ } + │ error: "division by zero" + │ └── calculator-local-handler-divide + │ error: "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" └── anthropic.messages.create [llm] input: [ { diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json index 41e893b3d..37e977c0c 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.json @@ -14,37 +14,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-multiply", - "children": [] - } - ], - "input": { - "a": 15, - "b": 7, - "operation": "multiply" - }, - "output": { - "content": [ - { - "text": "multiply(15, 7) = 105", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to multiply 15 by 7. Do not answer from memory.", @@ -91,6 +61,35 @@ "tokens": 18934 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-multiply", + "children": [] + } + ], + "input": { + "a": 15, + "b": 7, + "operation": "multiply" + }, + "output": { + "content": [ + { + "text": "multiply(15, 7) = 105", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } + }, { "name": "anthropic.messages.create", "type": "llm", @@ -390,37 +389,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-add", - "children": [] - } - ], - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "output": { - "content": [ - { - "text": "add(15, 27) = 42", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": [ @@ -462,6 +431,35 @@ "prompt_tokens": 22756, "tokens": 22764 } + }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-add", + "children": [] + } + ], + "input": { + "a": 15, + "b": 27, + "operation": "add" + }, + "output": { + "content": [ + { + "text": "add(15, 27) = 42", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } } ], "input": "Please use the calculator tool to add 15 and 27 together. Report the result.", @@ -914,31 +912,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-divide", - "children": [], - "error": "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" - } - ], - "input": { - "a": 2, - "b": 0, - "operation": "divide" - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - }, - "error": "division by zero" - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to divide 2 by 0. Do not recover from the error.", @@ -985,6 +959,29 @@ "tokens": 19309 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-divide", + "children": [], + "error": "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" + } + ], + "input": { + "a": 2, + "b": 0, + "operation": "divide" + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + }, + "error": "division by zero" + }, { "name": "anthropic.messages.create", "type": "llm", diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt index 66ceb109d..b6860680f 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-auto-hook.span-tree.txt @@ -72,27 +72,27 @@ span_tree: │ │ "prompt_tokens": 18927, │ │ "tokens": 18934 │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 7, - │ │ "operation": "multiply" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "multiply(15, 7) = 105", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ ├── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 7, + │ │ "operation": "multiply" + │ │ } + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "multiply(15, 7) = 105", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-multiply + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-multiply │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -415,69 +415,69 @@ span_tree: │ │ "claude_agent_sdk.tool_use_id": "toolu_01JKWJX27jjQTVZ6zGBZETBo", │ │ "claude_agent_sdk.total_tokens": 22772 │ │ } - │ │ └── anthropic.messages.create [llm] - │ │ input: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ }, - │ │ "name": "mcp__calculator__calculator", - │ │ "type": "tool_use" - │ │ } - │ │ ], - │ │ "role": "assistant" - │ │ } - │ │ ] - │ │ metadata: { - │ │ "model": "claude-haiku-4-5-20251001" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 8, - │ │ "prompt_cache_creation_tokens": 9514, - │ │ "prompt_cached_tokens": 13239, - │ │ "prompt_tokens": 22756, - │ │ "tokens": 22764 + │ │ ├── anthropic.messages.create [llm] + │ │ │ input: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", + │ │ │ "type": "text" + │ │ │ } + │ │ │ ], + │ │ │ "role": "user" + │ │ │ } + │ │ │ ] + │ │ │ output: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "caller": { + │ │ │ "type": "direct" + │ │ │ }, + │ │ │ "id": "", + │ │ │ "input": { + │ │ │ "a": 15, + │ │ │ "b": 27, + │ │ │ "operation": "add" + │ │ │ }, + │ │ │ "name": "mcp__calculator__calculator", + │ │ │ "type": "tool_use" + │ │ │ } + │ │ │ ], + │ │ │ "role": "assistant" + │ │ │ } + │ │ │ ] + │ │ │ metadata: { + │ │ │ "model": "claude-haiku-4-5-20251001" + │ │ │ } + │ │ │ metrics: { + │ │ │ "completion_tokens": 8, + │ │ │ "prompt_cache_creation_tokens": 9514, + │ │ │ "prompt_cached_tokens": 13239, + │ │ │ "prompt_tokens": 22756, + │ │ │ "tokens": 22764 + │ │ │ } + │ │ └── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 27, + │ │ "operation": "add" │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "add(15, 27) = 42", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "add(15, 27) = 42", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-add + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-add │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -854,21 +854,21 @@ span_tree: │ "prompt_tokens": 19301, │ "tokens": 19309 │ } - │ └── tool: calculator/calculator [tool] - │ input: { - │ "a": 2, - │ "b": 0, - │ "operation": "divide" - │ } - │ metadata: { - │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", - │ "gen_ai.tool.name": "calculator", - │ "mcp.server": "calculator" - │ } - │ error: "division by zero" - │ └── calculator-local-handler-divide - │ error: "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" + ├── tool: calculator/calculator [tool] + │ input: { + │ "a": 2, + │ "b": 0, + │ "operation": "divide" + │ } + │ metadata: { + │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", + │ "gen_ai.tool.name": "calculator", + │ "mcp.server": "calculator" + │ } + │ error: "division by zero" + │ └── calculator-local-handler-divide + │ error: "division by zero\n\nError: division by zero\n at traced.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.mjs:0:0)\n at withCurrent (/js/dist/index.mjs:0:0)\n at /js/dist/index.mjs:0:0\n at runCatchFinally (/js/dist/index.mjs:0:0)\n at traced (/js/dist/index.mjs:0:0)\n at /e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0\n at runHandler (/js/dist/index.mjs:0:0)" └── anthropic.messages.create [llm] input: [ { diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json index efbd4ff55..d5adea712 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.json @@ -14,37 +14,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-multiply", - "children": [] - } - ], - "input": { - "a": 15, - "b": 7, - "operation": "multiply" - }, - "output": { - "content": [ - { - "text": "multiply(15, 7) = 105", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to multiply 15 by 7. Do not answer from memory.", @@ -91,6 +61,35 @@ "tokens": 18934 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-multiply", + "children": [] + } + ], + "input": { + "a": 15, + "b": 7, + "operation": "multiply" + }, + "output": { + "content": [ + { + "text": "multiply(15, 7) = 105", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } + }, { "name": "anthropic.messages.create", "type": "llm", @@ -390,37 +389,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-add", - "children": [] - } - ], - "input": { - "a": 15, - "b": 27, - "operation": "add" - }, - "output": { - "content": [ - { - "text": "add(15, 27) = 42", - "type": "text" - } - ] - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - } - } - ], + "children": [], "input": [ { "content": [ @@ -462,6 +431,35 @@ "prompt_tokens": 22756, "tokens": 22764 } + }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-add", + "children": [] + } + ], + "input": { + "a": 15, + "b": 27, + "operation": "add" + }, + "output": { + "content": [ + { + "text": "add(15, 27) = 42", + "type": "text" + } + ] + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + } } ], "input": "Please use the calculator tool to add 15 and 27 together. Report the result.", @@ -914,31 +912,7 @@ { "name": "anthropic.messages.create", "type": "llm", - "children": [ - { - "name": "tool: calculator/calculator", - "type": "tool", - "children": [ - { - "name": "calculator-local-handler-divide", - "children": [], - "error": "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" - } - ], - "input": { - "a": 2, - "b": 0, - "operation": "divide" - }, - "metadata": { - "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", - "gen_ai.tool.name": "calculator", - "mcp.server": "calculator" - }, - "error": "division by zero" - } - ], + "children": [], "input": [ { "content": "Use the calculator tool to divide 2 by 0. Do not recover from the error.", @@ -985,6 +959,29 @@ "tokens": 19309 } }, + { + "name": "tool: calculator/calculator", + "type": "tool", + "children": [ + { + "name": "calculator-local-handler-divide", + "children": [], + "error": "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" + } + ], + "input": { + "a": 2, + "b": 0, + "operation": "divide" + }, + "metadata": { + "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", + "gen_ai.tool.name": "calculator", + "mcp.server": "calculator" + }, + "error": "division by zero" + }, { "name": "anthropic.messages.create", "type": "llm", diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt index aa20ba3e9..95084004f 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/__snapshots__/claude-agent-sdk-v0.2.81-wrapped.span-tree.txt @@ -72,27 +72,27 @@ span_tree: │ │ "prompt_tokens": 18927, │ │ "tokens": 18934 │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 7, - │ │ "operation": "multiply" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "multiply(15, 7) = 105", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ ├── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 7, + │ │ "operation": "multiply" + │ │ } + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "multiply(15, 7) = 105", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-multiply + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_0117rPYttev4aUWbEHmd8MNP", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-multiply │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -415,69 +415,69 @@ span_tree: │ │ "claude_agent_sdk.tool_use_id": "toolu_01JKWJX27jjQTVZ6zGBZETBo", │ │ "claude_agent_sdk.total_tokens": 22772 │ │ } - │ │ └── anthropic.messages.create [llm] - │ │ input: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", - │ │ "type": "text" - │ │ } - │ │ ], - │ │ "role": "user" - │ │ } - │ │ ] - │ │ output: [ - │ │ { - │ │ "content": [ - │ │ { - │ │ "caller": { - │ │ "type": "direct" - │ │ }, - │ │ "id": "", - │ │ "input": { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ }, - │ │ "name": "mcp__calculator__calculator", - │ │ "type": "tool_use" - │ │ } - │ │ ], - │ │ "role": "assistant" - │ │ } - │ │ ] - │ │ metadata: { - │ │ "model": "claude-haiku-4-5-20251001" - │ │ } - │ │ metrics: { - │ │ "completion_tokens": 8, - │ │ "prompt_cache_creation_tokens": 9514, - │ │ "prompt_cached_tokens": 13239, - │ │ "prompt_tokens": 22756, - │ │ "tokens": 22764 + │ │ ├── anthropic.messages.create [llm] + │ │ │ input: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "text": "Please use the calculator tool to add 15 and 27 together. Report the result.", + │ │ │ "type": "text" + │ │ │ } + │ │ │ ], + │ │ │ "role": "user" + │ │ │ } + │ │ │ ] + │ │ │ output: [ + │ │ │ { + │ │ │ "content": [ + │ │ │ { + │ │ │ "caller": { + │ │ │ "type": "direct" + │ │ │ }, + │ │ │ "id": "", + │ │ │ "input": { + │ │ │ "a": 15, + │ │ │ "b": 27, + │ │ │ "operation": "add" + │ │ │ }, + │ │ │ "name": "mcp__calculator__calculator", + │ │ │ "type": "tool_use" + │ │ │ } + │ │ │ ], + │ │ │ "role": "assistant" + │ │ │ } + │ │ │ ] + │ │ │ metadata: { + │ │ │ "model": "claude-haiku-4-5-20251001" + │ │ │ } + │ │ │ metrics: { + │ │ │ "completion_tokens": 8, + │ │ │ "prompt_cache_creation_tokens": 9514, + │ │ │ "prompt_cached_tokens": 13239, + │ │ │ "prompt_tokens": 22756, + │ │ │ "tokens": 22764 + │ │ │ } + │ │ └── tool: calculator/calculator [tool] + │ │ input: { + │ │ "a": 15, + │ │ "b": 27, + │ │ "operation": "add" │ │ } - │ │ └── tool: calculator/calculator [tool] - │ │ input: { - │ │ "a": 15, - │ │ "b": 27, - │ │ "operation": "add" - │ │ } - │ │ output: { - │ │ "content": [ - │ │ { - │ │ "text": "add(15, 27) = 42", - │ │ "type": "text" - │ │ } - │ │ ] - │ │ } - │ │ metadata: { - │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ │ "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", - │ │ "gen_ai.tool.name": "calculator", - │ │ "mcp.server": "calculator" + │ │ output: { + │ │ "content": [ + │ │ { + │ │ "text": "add(15, 27) = 42", + │ │ "type": "text" │ │ } - │ │ └── calculator-local-handler-add + │ │ ] + │ │ } + │ │ metadata: { + │ │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ │ "gen_ai.tool.call.id": "toolu_014XWmbx353UYpHZSZN82BU3", + │ │ "gen_ai.tool.name": "calculator", + │ │ "mcp.server": "calculator" + │ │ } + │ │ └── calculator-local-handler-add │ └── anthropic.messages.create [llm] │ input: [ │ { @@ -854,21 +854,21 @@ span_tree: │ "prompt_tokens": 19301, │ "tokens": 19309 │ } - │ └── tool: calculator/calculator [tool] - │ input: { - │ "a": 2, - │ "b": 0, - │ "operation": "divide" - │ } - │ metadata: { - │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", - │ "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", - │ "gen_ai.tool.name": "calculator", - │ "mcp.server": "calculator" - │ } - │ error: "division by zero" - │ └── calculator-local-handler-divide - │ error: "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" + ├── tool: calculator/calculator [tool] + │ input: { + │ "a": 2, + │ "b": 0, + │ "operation": "divide" + │ } + │ metadata: { + │ "claude_agent_sdk.raw_tool_name": "mcp__calculator__calculator", + │ "gen_ai.tool.call.id": "toolu_01HM53Dgu54gnzQEZMp744Ua", + │ "gen_ai.tool.name": "calculator", + │ "mcp.server": "calculator" + │ } + │ error: "division by zero" + │ └── calculator-local-handler-divide + │ error: "division by zero\n\nError: division by zero\n at result.name (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at /js/dist/index.js:0:0\n at AsyncLocalStorage.run (node::0:0)\n at BraintrustContextManager.runInContext (/js/dist/index.js:0:0)\n at withCurrent (/js/dist/index.js:0:0)\n at /js/dist/index.js:0:0\n at runCatchFinally (/js/dist/index.js:0:0)\n at traced (/js/dist/index.js:0:0)\n at (/e2e/scenarios/claude-agent-sdk-instrumentation/scenario.impl.mjs:0:0)\n at runHandler (/js/dist/index.js:0:0)" └── anthropic.messages.create [llm] input: [ { diff --git a/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts b/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts index 2c0eafe75..25f87e106 100644 --- a/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts +++ b/e2e/scenarios/claude-agent-sdk-instrumentation/assertions.ts @@ -472,11 +472,14 @@ export function defineClaudeAgentSDKInstrumentationAssertions(options: { events, "calculator-local-handler-multiply", ) ?? findToolSpanByOperation(events, "multiply"); + const toolParent = findSpanById(events, tool?.span.parentIds[0]); expect(operation).toBeDefined(); expect(task).toBeDefined(); expect(llm).toBeDefined(); expect(tool).toBeDefined(); + expect(toolParent?.span.id).toBe(task?.span.id); + expect(tool?.span.parentIds).not.toContain(llm?.span.id ?? ""); expect(operation?.span.parentIds).toEqual([root?.span.id ?? ""]); }); @@ -643,14 +646,9 @@ export function defineClaudeAgentSDKInstrumentationAssertions(options: { }); expect(nestedTaskLlmInputText).toBe(handoffToolInput?.prompt); expect(nestedTaskLlmInputText).not.toBe(taskRoot?.input); - if (tool) { - expect(tool.span.parentIds).not.toContain(taskRoot?.span.id ?? ""); - if (toolParent?.span.type === "llm") { - expect(toolParent.span.parentIds).not.toContain( - taskRoot?.span.id ?? "", - ); - } - } + expect(tool).toBeDefined(); + expect(toolParent?.span.id).toBe(nestedTask?.span.id); + expect(tool?.span.parentIds).not.toContain(nestedTaskLlm?.span.id ?? ""); }); if (options.expectTaskLifecycleDetails) { @@ -739,6 +737,7 @@ export function defineClaudeAgentSDKInstrumentationAssertions(options: { const tool = findToolSpanByLocalHandler(events, "calculator-local-handler-divide") ?? findToolSpanByOperation(events, "divide"); + const toolParent = findSpanById(events, tool?.span.parentIds[0]); expect(operation).toBeDefined(); expect(task).toBeDefined(); @@ -746,6 +745,8 @@ export function defineClaudeAgentSDKInstrumentationAssertions(options: { if (tool) { expect(tool.row.error).toBe("division by zero"); } + expect(toolParent?.span.id).toBe(task?.span.id); + expect(tool?.span.parentIds).not.toContain(llm?.span.id ?? ""); }); test("matches the shared span tree snapshot", testConfig, async () => { diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts index ef96d0efe..7262f643e 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts @@ -1,6 +1,9 @@ import iso from "../../isomorph"; -type LocalToolParentResolver = (toolUseId: string) => Promise; +type LocalToolParentResolver = ( + toolUseId: string, + context?: { agentId?: string; preferTaskSiblingParent?: boolean }, +) => Promise; export type ClaudeAgentSDKLocalToolContext = { resolveLocalToolParent?: LocalToolParentResolver; diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts index 1631cc75f..926e1c990 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts @@ -59,7 +59,9 @@ export function wrapLocalClaudeToolHandler( const runWithResolvedParent = async () => { const parent = toolUseId && localToolParentResolver - ? await localToolParentResolver(toolUseId).catch(() => undefined) + ? await localToolParentResolver(toolUseId, { + preferTaskSiblingParent: true, + }).catch(() => undefined) : undefined; const span = startSpan({ event: { diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts b/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts index e123c9693..303a64b50 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts @@ -71,10 +71,6 @@ function isSubAgentDelegationToolName(toolName: string): boolean { return toolName === "Agent" || toolName === "Task"; } -function shouldParentToolAsTaskSibling(toolName: string): boolean { - return toolName === "Agent" || toolName === "Task" || toolName === "Bash"; -} - function filterSerializableOptions( options: ClaudeAgentSDKQueryOptions, ): Record { @@ -534,7 +530,7 @@ function createToolTracingHooks( name: parsed.displayName, parent: await resolveParentSpan(toolUseID, { agentId: input.agent_id, - preferTaskSiblingParent: shouldParentToolAsTaskSibling(parsed.toolName), + preferTaskSiblingParent: true, }), spanAttributes: { type: SpanTypeAttribute.TOOL }, }); @@ -1504,12 +1500,16 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { : null); const parentKey = llmParentKey(parentToolUseId); const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey); - - if (context?.preferTaskSiblingParent) { - // Built-in Claude tools should be siblings of the driving LLM turn, - // but we still materialize that LLM span first so trace ordering - // reflects that the tool call was produced by the model. - if (!activeLlmSpan) { + const latestLlmParent = parentToolUseId + ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) + : latestRootLlmParentRef.value; + + if (context?.preferTaskSiblingParent || parentToolUseId) { + // Built-in root tools and sub-agent tools should be siblings of the + // driving LLM turn, but we still materialize that LLM span first so + // trace ordering reflects that the tool call was produced by the + // model. + if (!activeLlmSpan && !latestLlmParent) { await ensureActiveLlmSpanForParentToolUse( span, activeLlmSpansByParentToolUse, @@ -1540,10 +1540,8 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { } if (parentToolUseId) { - const parentLlm = - latestLlmParentBySubAgentToolUse.get(parentToolUseId); - if (parentLlm) { - return parentLlm; + if (latestLlmParent) { + return latestLlmParent; } const subAgentSpan = await ensureSubAgentSpan( @@ -1556,8 +1554,8 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { return subAgentSpan.export(); } - if (latestRootLlmParentRef.value) { - return latestRootLlmParentRef.value; + if (latestLlmParent) { + return latestLlmParent; } return span.export(); diff --git a/js/tests/provider-wrappers.test.ts b/js/tests/provider-wrappers.test.ts index 4683d4229..919207a49 100644 --- a/js/tests/provider-wrappers.test.ts +++ b/js/tests/provider-wrappers.test.ts @@ -514,9 +514,11 @@ describe("provider wrapper", () => { test("Claude Agent SDK keeps parented user tool results in sub-agent conversation history", async () => { const rootPrompt = "Delegate the calculation."; const subAgentPrompt = "Use the calculator to add 15 and 27."; + let capturedOptions: any; const wrappedSDK = wrapClaudeAgentSDK({ - query: () => - (async function* () { + query: (params: any) => { + capturedOptions = params.options; + return (async function* () { yield { type: "assistant", message: { @@ -590,13 +592,45 @@ describe("provider wrapper", () => { type: "result", usage: { input_tokens: 1, output_tokens: 3 }, }; - })(), + })(); + }, }); - for await (const _message of wrappedSDK.query({ + for await (const message of wrappedSDK.query({ prompt: rootPrompt, options: { model: "test-model" }, } as any)) { + if ( + message.type === "assistant" && + message.message?.id === "sub-agent-tool-call" + ) { + await new Promise((resolve) => setTimeout(resolve, 0)); + await capturedOptions.hooks.PreToolUse.at(-1).hooks[0]( + { + hook_event_name: "PreToolUse", + tool_name: "mcp__calculator__calculator", + tool_input: { a: 15, b: 27, operation: "add" }, + session_id: "test-session", + transcript_path: "/tmp/transcript", + cwd: "/tmp", + }, + "calculator-tool-use", + { signal: new AbortController().signal }, + ); + await capturedOptions.hooks.PostToolUse.at(-1).hooks[0]( + { + hook_event_name: "PostToolUse", + tool_name: "mcp__calculator__calculator", + tool_input: { a: 15, b: 27, operation: "add" }, + tool_response: "42", + session_id: "test-session", + transcript_path: "/tmp/transcript", + cwd: "/tmp", + }, + "calculator-tool-use", + { signal: new AbortController().signal }, + ); + } } const spans = await backgroundLogger.drain(); @@ -638,6 +672,115 @@ describe("provider wrapper", () => { }, ]); expect(JSON.stringify(finalSubAgentLlm?.input)).not.toContain(rootPrompt); + + const subAgentSpan = spans.find( + (span: any) => span.span_attributes.name === "Agent: math specialist", + ); + const calculatorToolSpan = spans.find( + (span: any) => + span.span_attributes.name === "tool: calculator/calculator", + ); + expect(calculatorToolSpan?.span_parents).toEqual([subAgentSpan?.span_id]); + }); + + test("Claude Agent SDK does not create duplicate LLM spans for late built-in tool hooks", async () => { + let capturedOptions: any; + const rootPrompt = "Delegate to an echo agent."; + const toolUseID = "agent-tool-use"; + const wrappedSDK = wrapClaudeAgentSDK({ + query: (params: any) => { + capturedOptions = params.options; + return (async function* () { + yield { + type: "assistant", + message: { + id: "root-assistant", + role: "assistant", + content: [ + { + id: toolUseID, + input: { + description: "echo greeting", + prompt: "Run a bash echo command.", + subagent_type: "echo", + }, + name: "Agent", + type: "tool_use", + }, + ], + usage: { input_tokens: 1, output_tokens: 1 }, + }, + }; + yield { + type: "user", + parent_tool_use_id: toolUseID, + message: { + role: "user", + content: [{ text: "Run a bash echo command.", type: "text" }], + }, + }; + yield { + type: "result", + usage: { input_tokens: 1, output_tokens: 1 }, + }; + })(); + }, + }); + + for await (const message of wrappedSDK.query({ + prompt: rootPrompt, + options: { model: "test-model" }, + } as any)) { + if (message.type === "user" && message.parent_tool_use_id === toolUseID) { + await new Promise((resolve) => setTimeout(resolve, 0)); + await capturedOptions.hooks.PreToolUse.at(-1).hooks[0]( + { + hook_event_name: "PreToolUse", + tool_name: "Agent", + tool_input: { + description: "echo greeting", + prompt: "Run a bash echo command.", + subagent_type: "echo", + }, + session_id: "test-session", + transcript_path: "/tmp/transcript", + cwd: "/tmp", + }, + toolUseID, + { signal: new AbortController().signal }, + ); + } + } + + const spans = await backgroundLogger.drain(); + const taskSpan = spans.find( + (span: any) => span.span_attributes.name === "Claude Agent", + ); + const rootLlmSpans = spans.filter( + (span: any) => + span.span_attributes.name === "anthropic.messages.create" && + span.span_parents?.includes(taskSpan?.span_id), + ); + + expect(rootLlmSpans).toHaveLength(1); + expect(rootLlmSpans[0]?.input).toEqual([ + { + content: rootPrompt, + role: "user", + }, + ]); + expect(rootLlmSpans[0]?.output).toMatchObject([ + { + content: [ + { + id: toolUseID, + name: "Agent", + type: "tool_use", + }, + ], + role: "assistant", + }, + ]); }); test("Claude Agent SDK injects tracing hooks while preserving user hooks", async () => { From 9971a7a49bdb64025f4e46f510869958099402db Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 16 Jun 2026 19:03:08 +0200 Subject: [PATCH 4/4] Review comment --- .../claude-agent-sdk-local-tool-context.ts | 5 +- .../claude-agent-sdk-local-tool-spans.ts | 4 +- .../plugins/claude-agent-sdk-plugin.ts | 57 +++++-------------- 3 files changed, 16 insertions(+), 50 deletions(-) diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts index 7262f643e..ef96d0efe 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts @@ -1,9 +1,6 @@ import iso from "../../isomorph"; -type LocalToolParentResolver = ( - toolUseId: string, - context?: { agentId?: string; preferTaskSiblingParent?: boolean }, -) => Promise; +type LocalToolParentResolver = (toolUseId: string) => Promise; export type ClaudeAgentSDKLocalToolContext = { resolveLocalToolParent?: LocalToolParentResolver; diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts index 926e1c990..1631cc75f 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts @@ -59,9 +59,7 @@ export function wrapLocalClaudeToolHandler( const runWithResolvedParent = async () => { const parent = toolUseId && localToolParentResolver - ? await localToolParentResolver(toolUseId, { - preferTaskSiblingParent: true, - }).catch(() => undefined) + ? await localToolParentResolver(toolUseId).catch(() => undefined) : undefined; const span = startSpan({ event: { diff --git a/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts b/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts index 303a64b50..175a2a05a 100644 --- a/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts +++ b/js/src/instrumentation/plugins/claude-agent-sdk-plugin.ts @@ -40,7 +40,7 @@ type ParsedToolName = { }; type ParentSpanResolver = ( toolUseID: string, - context?: { agentId?: string; preferTaskSiblingParent?: boolean }, + context?: { agentId?: string }, ) => Promise; type LLMSpanResult = { finalMessage: ClaudeConversationMessage | undefined; @@ -530,7 +530,6 @@ function createToolTracingHooks( name: parsed.displayName, parent: await resolveParentSpan(toolUseID, { agentId: input.agent_id, - preferTaskSiblingParent: true, }), spanAttributes: { type: SpanTypeAttribute.TOOL }, }); @@ -1504,46 +1503,22 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value; - if (context?.preferTaskSiblingParent || parentToolUseId) { - // Built-in root tools and sub-agent tools should be siblings of the - // driving LLM turn, but we still materialize that LLM span first so - // trace ordering reflects that the tool call was produced by the - // model. - if (!activeLlmSpan && !latestLlmParent) { - await ensureActiveLlmSpanForParentToolUse( - span, - activeLlmSpansByParentToolUse, - subAgentDetailsByToolUseId, - activeToolSpans, - subAgentSpans, - parentToolUseId, - getCurrentUnixTimestamp(), - ); - } - - if (parentToolUseId) { - const subAgentSpan = await ensureSubAgentSpan( - subAgentDetailsByToolUseId, - span, - activeToolSpans, - subAgentSpans, - parentToolUseId, - ); - return subAgentSpan.export(); - } - - return span.export(); - } - - if (activeLlmSpan) { - return activeLlmSpan.export(); + // Tool spans should be siblings of the driving LLM turn, but we still + // materialize that LLM span first so trace ordering reflects that the + // tool call was produced by the model. + if (!activeLlmSpan && !latestLlmParent) { + await ensureActiveLlmSpanForParentToolUse( + span, + activeLlmSpansByParentToolUse, + subAgentDetailsByToolUseId, + activeToolSpans, + subAgentSpans, + parentToolUseId, + getCurrentUnixTimestamp(), + ); } if (parentToolUseId) { - if (latestLlmParent) { - return latestLlmParent; - } - const subAgentSpan = await ensureSubAgentSpan( subAgentDetailsByToolUseId, span, @@ -1554,10 +1529,6 @@ export class ClaudeAgentSDKPlugin extends BasePlugin { return subAgentSpan.export(); } - if (latestLlmParent) { - return latestLlmParent; - } - return span.export(); };