The Flaw: A Double Dip in Tool Call Logging
A subtle but significant bug has been identified in Sentry's JavaScript SDK, specifically impacting how it logs tool calls made by the @google/genai library when operating in streaming mode. The issue causes a single tool call to be recorded twice, effectively creating a "double-dip" in telemetry data. This means that for every actual tool execution initiated by a Google GenAI model, Sentry's SDK reports two entries. The attribute in question is gen_ai.response.tool_calls, which is designed to hold an array of objects, with each object representing a distinct tool call. In the case of a streamed controlLight call, this attribute incorrectly contained two entries instead of one. Compounding the problem, these two reported entries did not even conform to the same structure, suggesting a deeper issue in how the data is processed and serialized.
This bug was discovered during DEV's Summer Bug Smash event, powered by Sentry itself. The reporter, who has submitted this as a bug-hunting exercise, detailed the issue with a concrete example. While the exact output of the erroneous capture was truncated in the initial report, the implication is clear: developers relying on Sentry for monitoring their GenAI applications might be seeing inflated counts for tool usage. This could lead to misinterpretations of model behavior, inaccurate cost estimations if tool calls are metered, and flawed performance analysis.

Understanding the Mechanism: Streaming and Tool Use
Generative AI models, particularly large language models (LLMs), are increasingly being augmented with the ability to interact with external tools. This capability allows models to go beyond text generation and perform actions like searching databases, calling APIs, or executing code. When a model needs to use a tool, it typically outputs a structured request specifying the tool name and its arguments. This is often referred to as a "tool call.".
Streaming is a common technique used in AI model responses to provide users with more immediate feedback. Instead of waiting for the entire response to be generated, the model sends back parts of the response as they become available. This improves perceived performance and user experience, especially for longer or more complex outputs. When tool calls are part of a streamed response, the model might signal the intent to call a tool mid-stream, or it might stream the tool call definition itself.
The interaction between streaming responses and the logging of tool calls is where this particular bug manifests. Sentry's JavaScript SDK is designed to capture these interactions as part of its Application Performance Monitoring (APM) capabilities. It instruments various libraries and frameworks to record events, spans, and attributes, providing developers with insights into their application's performance and behavior. In this scenario, the SDK appears to be misinterpreting the streaming nature of the @google/genai library's tool call mechanism, leading to duplicate entries in the gen_ai.response.tool_calls attribute.
Implications for Developers and Sentry Users
The primary implication of this bug is data inaccuracy. Developers using Sentry to monitor applications that leverage Google's GenAI models, especially those employing streaming and tool-calling features, will have a distorted view of their system's activity. This can have several downstream effects:
- Misleading Metrics: The number of tool calls recorded will be twice what actually occurred. This inflates performance metrics and can lead to incorrect conclusions about the model's reliance on external tools.
- Inaccurate Cost Analysis: If tool usage is tied to billing (either by Google or by a third-party orchestrator), developers might incorrectly estimate costs based on Sentry's inflated logs.
- Debugging Challenges: When investigating issues related to tool execution or model behavior, developers might be led astray by duplicate log entries, making it harder to pinpoint the root cause.
- Resource Allocation: Overestimating tool usage might lead to inefficient resource allocation or provisioning if automated systems rely on these metrics.
The fact that the two reported tool calls did not even agree on their shape is particularly concerning. It suggests that the SDK might not only be duplicating entries but also corrupting the data in the process. This could stem from how the SDK handles incremental updates in a streaming context, potentially processing partial data multiple times or failing to correctly de-duplicate identical or similar data chunks as they arrive.
The Broader Context: AI Observability
As AI models become more integrated into applications, the need for robust observability tools like Sentry becomes paramount. Monitoring AI-specific metrics, such as token usage, prompt complexity, tool calls, and response latency, is crucial for understanding and optimizing AI-powered features. This bug highlights a common challenge in AI observability: the complexity of new interaction patterns, like streaming and dynamic tool use, which can be difficult for traditional APM tools to instrument accurately without specific adaptations.
The incident underscores the importance of thorough testing and validation of observability tools, especially as they extend their capabilities to cover emerging AI paradigms. Developers building with these powerful AI tools need assurance that their monitoring solutions are providing a true reflection of their application's behavior. For Sentry, this presents an opportunity to refine its instrumentation for AI-specific features, ensuring it can accurately capture the nuances of streaming responses and complex tool interactions.
What remains to be seen is how quickly Sentry can address this issue and what other subtle bugs might exist in the instrumentation of other AI-specific features. As the AI landscape evolves at a breakneck pace, observability platforms must keep stride, adapting their tools to provide reliable insights into these increasingly sophisticated systems. For now, developers using Sentry with Google GenAI in streaming mode should be aware of this potential data discrepancy and consider manual verification or alternative logging methods for critical metrics related to tool calls.
