ai icon indicating copy to clipboard operation
ai copied to clipboard

WorkflowAgent emits no tool-call telemetry inside a workflow (model calls fixed in #22378, tools still missing)

Open robertyates opened this issue 1 month ago • 5 comments

Description

#22378 fixed model-call telemetry for WorkflowAgent: model-call-telemetry.ts now dispatches the model call's lifecycle from inside the model step, where globally registered integrations are visible. Tool calls have the same gap and were not covered.

executeToolWithCallbacks in workflow-agent.ts calls telemetryDispatcher.onToolExecutionStart, executeTool and onToolExecutionEnd from the workflow body. The body cannot see AI_SDK_TELEMETRY_INTEGRATIONS, so the dispatcher is a no-op there. With registerTelemetry(new OpenTelemetry()) in instrumentation.ts, a durable WorkflowAgent run exports chat spans for every model call and no execute_tool spans for any tool call. A plain streamText in the same process exports both.

Versions: @ai-sdk/[email protected] (also checked against main), [email protected], @ai-sdk/[email protected], [email protected].

Reproduction

Same harness as #22378, with one tool on the agent:

export async function agentWithTool(prompt: string) {
  'use workflow';
  const agent = new WorkflowAgent({
    model: fakeModel, // first call returns a tool call to `echo`, second returns text
    tools: { echo: tool({ inputSchema: z.object({ text: z.string() }), execute: echoStep }) },
  });
  return agent.stream({ prompt, writable: getWritable() });
}

async function echoStep({ text }: { text: string }) {
  'use step';
  return text;
}

With an OpenTelemetry integration registered globally:

  • Expected: a chat span per model call and an execute_tool echo span for the tool call.
  • Actual: the chat spans arrive; no tool span, and onToolExecutionStart / onToolExecutionEnd on the integration are never called.

Notes

  • Tool execute functions are commonly 'use step' functions, so the fix probably can't wrap execution in a library step. A step that dispatches start/end after the tool returns would work, if it carries the measured start time and duration rather than stamping the span when it runs.
  • Today we rebuild tool spans from the turn's saved output when the turn ends. That loses real timings, shows a running turn with no tools, and doesn't cover agents run outside our chat path.

robertyates avatar Oct 20 '26 13:10 robertyates

Identify issue type

Identified as Bug
Confidence: High

Rationale

This reports a concrete telemetry failure in existing WorkflowAgent behavior: tool execution lifecycle events are dispatched where registered integrations are unavailable, so expected tool spans are missing.

Evidence

  • WorkflowAgent exports model-call chat spans but omits execute_tool spans for actual tool calls.
  • The globally registered telemetry integration's onToolExecutionStart and onToolExecutionEnd callbacks are never invoked inside the workflow.
  • The same process correctly exports both model and tool spans when using plain streamText, providing a concrete behavioral comparison and reproduction.

ai-sdk-factory[bot] avatar Oct 20 '26 13:10 ai-sdk-factory[bot]

Identify bug discovery

Discovery source: discovery:audit (medium confidence).

The report appears to originate from inspecting the prior telemetry fix and the analogous tool-execution path in AI SDK source. It includes runtime validation, but does not establish actual production use.

  • The author identifies specific source locations and calls in workflow-agent.ts, including executeToolWithCallbacks and the telemetry dispatcher lifecycle methods.
  • The report explicitly compares tool handling with the fix in #22378 and states that tool calls were not covered by that change.
  • The author explains the source-level cause: workflow bodies cannot see AI_SDK_TELEMETRY_INTEGRATIONS, making the dispatcher a no-op.
  • The behavior was checked against main, consistent with source review and validation.
  • No statement establishes that the bug was discovered in a production application; the durable run and harness could be a reproduction or test.

ai-sdk-factory[bot] avatar Oct 20 '26 13:10 ai-sdk-factory[bot]

Related: #23618 — the run itself has no root span either, from the same cause (the run's start and end are dispatched from the workflow body).

robertyates avatar Oct 20 '26 16:10 robertyates

Thanks @robertyates for the precise report. The realm diagnosis is right, but I found two more things that keep tool spans missing even after moving the dispatch into a step:

  1. executeToolWithCallbacks sends tool events in the WorkflowAgent callback shape (durationMs, success, no callId), not the ToolExecution*Event contract. OpenTelemetry looks up call state by callId and returns early, so tool spans are missing even with explicit telemetry.integrations.
  2. Since #22480 each model call is its own operation and is closed before tools run, so there's no live parent for a tool span.

I opened #24334 with failing tests for each, plus a fix that reports each tool call as its own short operation from a step after the tool returns, the same pattern #22480 uses for model calls. 'use step' tools stay untouched.

@aayush-kapoor this shares a root cause with #23618 (run-level root span), which is assigned to you. I kept it to tool events only and shaped it so a future root span can parent these. Happy to adjust it, or to have you fold it into the #23618 work if that's simpler.

jadibdev avatar Nov 01 '26 14:11 jadibdev

Resolved by #24408, released in @ai-sdk/[email protected]. With the durable bridge (OpenTelemetryIdGenerator + new OpenTelemetry({ durable })), a durable WorkflowAgent run now exports an execute_tool span for each tool call, with the original start and end times, under its step in the run's invoke_agent tree. We've confirmed this in our traces and removed our workaround. Closing.

robertyates avatar Nov 11 '26 12:11 robertyates