Skip to content

Commit 175fb97

Browse files
nik13claude
andcommitted
fix(examples): mark a single-call TanStack AI chat root as CHAIN
The recipe gave the chat() root a kind only when it ran more than one model call. fi-collector stores a span without a kind as observation type "unknown", so every plain chat() (one model call, no tool round) and every failed first call showed an "unknown" root in Observe (live run: `[unknown] chat no-such-model-th8103`). The root is now AGENT with more than one model call and CHAIN otherwise. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a6c3331 commit 175fb97

4 files changed

Lines changed: 13 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ test runs this snippet as written.
129129
130130
| Span | Name | Kind set by the recipe | Key attributes |
131131
|---|---|---|---|
132-
| chat() call | `chat <model>` | `AGENT` when it ran more than one model call, else none | `gen_ai.request.model`, `tanstack.ai.iterations`, and TanStack's sum of every model call's usage, renamed: each `gen_ai.usage.<suffix>` becomes `tanstack.ai.root_usage.<suffix>` and each `gen_ai.cost.<suffix>` becomes `tanstack.ai.root_usage.cost.<suffix>`. Future AGI sums `gen_ai.usage.*` over the trace, so the root's copy would double tokens, cache, reasoning and cost. The root keeps `gen_ai.usage.*` only when no model call reported usage. |
132+
| chat() call | `chat <model>` | `AGENT` when it ran more than one model call, else `CHAIN` (a root without a kind shows as type "unknown" in Future AGI) | `gen_ai.request.model`, `tanstack.ai.iterations`, and TanStack's sum of every model call's usage, renamed: each `gen_ai.usage.<suffix>` becomes `tanstack.ai.root_usage.<suffix>` and each `gen_ai.cost.<suffix>` becomes `tanstack.ai.root_usage.cost.<suffix>`. Future AGI sums `gen_ai.usage.*` over the trace, so the root's copy would double tokens, cache, reasoning and cost. The root keeps `gen_ai.usage.*` only when no model call reported usage. |
133133
| 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` |
134134
| tool call | `execute_tool <name>` | `TOOL` | `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.tool.type` |
135135

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ export async function shutdownTraces(tracerProvider) {
9494
*
9595
* - iteration (one provider model call) -> LLM
9696
* - tool (one tool execution) -> TOOL
97-
* - chat root that ran more than one iteration -> AGENT
97+
* - chat root that ran more than one iteration -> AGENT, else CHAIN
9898
*
9999
* The root's kind is set in onSpanEnd because attributeEnricher runs for the
100-
* root when chat() starts, before the iteration count is known. A
101-
* single-iteration root and media "generation" spans get no kind.
100+
* root when chat() starts, before the iteration count is known. A root with
101+
* no kind would be stored by Future AGI as observation type "unknown". Media
102+
* "generation" spans get no kind.
102103
*
103104
* Usage: TanStack sums every model call's gen_ai.usage.* keys onto the root
104105
* (otel.ts applyRootUsage, set just before onSpanEnd): input, output and
@@ -179,9 +180,7 @@ export function futureAgiSpanKinds({ threadIdAsSession = false } = {}) {
179180
usageOnIterations.add(info.ctx);
180181
}
181182
if (info.kind === "chat") {
182-
if ((iterationsByRun.get(info.ctx) ?? 0) > 1) {
183-
span.setAttribute(GEN_AI_SPAN_KIND, "AGENT");
184-
}
183+
span.setAttribute(GEN_AI_SPAN_KIND, (iterationsByRun.get(info.ctx) ?? 0) > 1 ? "AGENT" : "CHAIN");
185184
if (usageOnIterations.has(info.ctx)) {
186185
moveRootUsage(span);
187186
}

‎typescript/examples/tanstack-ai/tests/span_kinds.test.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ test("no model call reported usage: the root keeps its usage", () => {
157157
assert.equal(traceSum(spans, "gen_ai.usage.total_tokens"), 42);
158158
});
159159

160-
test("one model call: no kind and no promoted usage on the root", () => {
160+
test("one model call: CHAIN and no promoted usage on the root", () => {
161+
// Without a kind, fi-collector stores the root as observation_type "unknown".
161162
const call = usage(11, 7);
162163
const { root, modelCalls, spans } = run(futureAgiSpanKinds(), [call]);
163-
assert.equal(root.attributes[GEN_AI_SPAN_KIND], undefined);
164+
assert.equal(root.attributes[GEN_AI_SPAN_KIND], "CHAIN");
164165
assert.equal(modelCalls[0].attributes[GEN_AI_SPAN_KIND], "LLM");
165166
assert.deepEqual(promotedKeys(root.attributes), []);
166167
assert.equal(root.attributes["tanstack.ai.root_usage.input_tokens"], 11);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ def test_failed_model_call_exports_error_spans():
393393
assert status.get("message") == "500 boom", (name, status)
394394
assert spans[ITERATION_0]["parentSpanId"] == spans[ROOT]["spanId"]
395395
assert _attributes(spans[ITERATION_0])["gen_ai.span.kind"] == "LLM"
396+
assert _attributes(spans[ROOT])["gen_ai.span.kind"] == "CHAIN"
396397
# No thread id was passed, so no session (chat() generated its own id).
397398
for name in (ROOT, ITERATION_0):
398399
assert "session.id" not in _attributes(spans[name]), name
@@ -427,9 +428,9 @@ def test_abort_mid_stream_ends_spans_as_cancelled():
427428
assert _is_error(status), status
428429
assert status.get("message") == "cancelled"
429430
assert _attributes(spans[name])["tanstack.ai.completion.reason"] == "cancelled"
430-
# One iteration: LLM on the iteration span, no kind on the root.
431+
# One iteration: LLM on the iteration span, CHAIN on the root.
431432
assert _attributes(spans[ITERATION_0])["gen_ai.span.kind"] == "LLM"
432-
assert "gen_ai.span.kind" not in _attributes(spans[ROOT])
433+
assert _attributes(spans[ROOT])["gen_ai.span.kind"] == "CHAIN"
433434

434435

435436
def _readme_recipe() -> str:
@@ -482,7 +483,7 @@ def test_readme_recipe_runs_and_exports_the_contract_spans():
482483
assert sorted(spans) == sorted([ROOT, ITERATION_0])
483484
root = _attributes(spans[ROOT])
484485
first = _attributes(spans[ITERATION_0])
485-
assert "gen_ai.span.kind" not in root
486+
assert root["gen_ai.span.kind"] == "CHAIN"
486487
assert first["gen_ai.span.kind"] == "LLM"
487488
assert not [k for k in root if k.startswith(("gen_ai.usage.", "gen_ai.cost."))], root
488489
assert root["tanstack.ai.root_usage.input_tokens"] == ANSWER_USAGE["prompt_tokens"]

0 commit comments

Comments
 (0)