Skip to content

Commit 04409aa

Browse files
committed
fix(examples): count TanStack AI model-call tokens once per trace
otelMiddleware sums every model call's usage onto the chat root (@tanstack/ai 0.64.0 otel.ts applyRootUsage, set just before onSpanEnd). fi-collector promotes gen_ai.usage.* into the token columns on any span (adapter.go) and Observe sums total_tokens over a trace, so totals doubled. The recipe's onSpanEnd now moves the root's sum to tanstack.ai.root_usage.* when the model-call spans carry usage, and leaves it when they do not (then the root is the only copy). Test first: the contract test now asserts the summed promoted input tokens equal the two model calls. 7 passed. approved: Nikhil 2026-10-03 blanket Refs: TH-8238
1 parent 9298bd5 commit 04409aa

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎typescript/examples/tanstack-ai/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const middleware = otelMiddleware({
5757

5858
| Span | Name | Kind set by the recipe | Key attributes |
5959
|---|---|---|---|
60-
| chat() call | `chat <model>` | `AGENT` when it ran more than one model call, else none | `gen_ai.request.model`, `tanstack.ai.iterations`, `gen_ai.usage.*` summed |
60+
| chat() call | `chat <model>` | `AGENT` when it ran more than one model call, else none | `gen_ai.request.model`, `tanstack.ai.iterations`, summed usage as `tanstack.ai.root_usage.*` (Future AGI sums `gen_ai.usage.*` over the trace, so the root's copy would double it; kept on the root only when no model call reported usage) |
6161
| model call | `chat <model> #<n>` | `LLM` | `gen_ai.operation.name=chat`, `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `tanstack.ai.iteration` |
6262
| tool call | `execute_tool <name>` | `TOOL` | `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.tool.type` |
6363

‎typescript/examples/tanstack-ai/src/tracing.mjs‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,36 @@ export async function shutdownTraces(tracerProvider) {
6767
* The root's kind is set in onSpanEnd because attributeEnricher runs for the
6868
* root when chat() starts, before the iteration count is known. A
6969
* single-iteration root and media "generation" spans get no kind.
70+
*
71+
* Usage: TanStack sums every model call's usage onto the root
72+
* (otel.ts applyRootUsage, set just before onSpanEnd). Future AGI promotes
73+
* gen_ai.usage.* into its token columns on any span and sums them over the
74+
* whole trace, so leaving the root's copy would count each call twice. When
75+
* the model-call spans carry usage, the root's sum moves to
76+
* tanstack.ai.root_usage.*. If no model-call span reported usage, the root
77+
* keeps it, because then it is the only copy.
7078
*/
79+
const PROMOTED_USAGE_KEYS = [
80+
"gen_ai.usage.input_tokens",
81+
"gen_ai.usage.output_tokens",
82+
"gen_ai.usage.total_tokens",
83+
];
84+
85+
function moveRootUsage(span) {
86+
// The SDK span's attributes object is what the exporter reads at end().
87+
const attributes = span?.attributes;
88+
if (!attributes) return;
89+
for (const key of PROMOTED_USAGE_KEYS) {
90+
if (key in attributes) {
91+
span.setAttribute(`tanstack.ai.root_usage.${key.split(".").pop()}`, attributes[key]);
92+
delete attributes[key];
93+
}
94+
}
95+
}
96+
7197
export function futureAgiSpanKinds() {
7298
const iterationsByRun = new WeakMap();
99+
const usageOnIterations = new WeakSet();
73100
return {
74101
attributeEnricher(info) {
75102
if (info.kind === "iteration") {
@@ -82,8 +109,16 @@ export function futureAgiSpanKinds() {
82109
return {};
83110
},
84111
onSpanEnd(info, span) {
85-
if (info.kind === "chat" && (iterationsByRun.get(info.ctx) ?? 0) > 1) {
86-
span.setAttribute(GEN_AI_SPAN_KIND, "AGENT");
112+
if (info.kind === "iteration" && span?.attributes?.["gen_ai.usage.input_tokens"] !== undefined) {
113+
usageOnIterations.add(info.ctx);
114+
}
115+
if (info.kind === "chat") {
116+
if ((iterationsByRun.get(info.ctx) ?? 0) > 1) {
117+
span.setAttribute(GEN_AI_SPAN_KIND, "AGENT");
118+
}
119+
if (usageOnIterations.has(info.ctx)) {
120+
moveRootUsage(span);
121+
}
87122
}
88123
},
89124
};

‎typescript/examples/tanstack-ai/tests/test_tanstack_ai_contract.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,26 @@ def test_spans_carry_model_usage_and_span_kinds(default_run):
174174
assert second["gen_ai.usage.cache_read.input_tokens"] == 3
175175
assert second["gen_ai.usage.reasoning.output_tokens"] == 2
176176

177-
# Root: the whole chat() call. No operation name; usage summed over calls.
177+
# Root: the whole chat() call. No operation name. TanStack sums usage over
178+
# calls onto the root; Future AGI sums promoted gen_ai.usage.* over every
179+
# span in a trace, so the recipe keeps the root's sum under
180+
# tanstack.ai.root_usage.* and each model call counts once.
178181
assert root["gen_ai.request.model"] == MODEL
179182
assert "gen_ai.operation.name" not in root
180183
assert root["tanstack.ai.iterations"] == 2
181-
assert root["gen_ai.usage.input_tokens"] == (
184+
assert "gen_ai.usage.input_tokens" not in root
185+
assert "gen_ai.usage.output_tokens" not in root
186+
assert "gen_ai.usage.total_tokens" not in root
187+
assert root["tanstack.ai.root_usage.input_tokens"] == (
182188
TOOL_CALL_USAGE["prompt_tokens"] + ANSWER_USAGE["prompt_tokens"]
183189
)
184-
assert root["gen_ai.usage.output_tokens"] == (
190+
assert root["tanstack.ai.root_usage.output_tokens"] == (
185191
TOOL_CALL_USAGE["completion_tokens"] + ANSWER_USAGE["completion_tokens"]
186192
)
193+
promoted_input = sum(
194+
_attributes(span).get("gen_ai.usage.input_tokens", 0) for span in spans.values()
195+
)
196+
assert promoted_input == TOOL_CALL_USAGE["prompt_tokens"] + ANSWER_USAGE["prompt_tokens"]
187197
assert root["gen_ai.response.finish_reasons"] == ["stop"]
188198
assert root["gen_ai.span.kind"] == "AGENT"
189199

0 commit comments

Comments
 (0)