Skip to content

Commit 9ee8a33

Browse files
nik13claude
andcommitted
fix(examples): send gen_ai.provider.name for TanStack AI model calls
otelMiddleware copies the adapter name to gen_ai.system, so Observe showed Provider "openai-chat" (openaiChatCompletions) and no vendor logo. The recipe now adds gen_ai.provider.name, which fi-collector reads first, on the chat and model-call spans, from the adapter names of the published adapters: openai / openai-chat -> openai, anthropic, gemini / gemini-text-interactions -> google, grok -> xai, groq, ollama. Other adapters (OpenRouter, custom) get none. Live: claude-haiku-4-5-20251001 through openaiChatCompletions -> provider openai, cost 0.000851 / 0.000794 per model call (fi-collector prices from gen_ai.request.model and tokens). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 175fb97 commit 9ee8a33

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ test runs this snippet as written.
130130
| Span | Name | Kind set by the recipe | Key attributes |
131131
|---|---|---|---|
132132
| 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. |
133-
| 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` |
133+
| model call | `chat <model> #<n>` | `LLM` | `gen_ai.operation.name=chat`, `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.provider.name`, `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
136136
Usage is a span attribute, not only a metric: `gen_ai.usage.input_tokens`,
@@ -140,6 +140,15 @@ Usage is a span attribute, not only a metric: `gen_ai.usage.input_tokens`,
140140
`gen_ai.usage.cache_creation.input_tokens` and
141141
`gen_ai.usage.reasoning.output_tokens`.
142142
143+
Provider: TanStack sends the adapter name as `gen_ai.system` (`openai-chat`
144+
for `openaiChatCompletions`). The recipe adds `gen_ai.provider.name`, which
145+
Future AGI reads first, on the chat and model-call spans: `openai` /
146+
`openai-chat` → `openai`, `anthropic` → `anthropic`, `gemini` /
147+
`gemini-text-interactions` → `google`, `grok` → `xai`, `groq` → `groq`,
148+
`ollama` → `ollama`. Other adapters (OpenRouter, your own) get none. Future
149+
AGI prices each model call from `gen_ai.request.model` and its tokens, so a
150+
model its price table lists gets a cost whatever the provider.
151+
143152
The kind (`gen_ai.span.kind`) is keyed on the middleware's span scope. Only
144153
model-call spans carry `gen_ai.operation.name`. The root's kind is set in
145154
`onSpanEnd`, because `attributeEnricher` runs for the root before the number

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ function moveRootUsage(span) {
140140
}
141141

142142
export const SESSION_ID = "session.id";
143+
export const GEN_AI_PROVIDER_NAME = "gen_ai.provider.name";
144+
145+
/**
146+
* TanStack adapter name (`adapter.name`, which otelMiddleware copies to
147+
* gen_ai.system) -> gen_ai.provider.name, the key fi-collector reads first for
148+
* a span's provider. Names from the published adapters: openaiChatCompletions
149+
* is "openai-chat", openaiText "openai", @tanstack/ai-gemini "gemini" and
150+
* "gemini-text-interactions", @tanstack/ai-grok "grok". Any other adapter
151+
* (OpenRouter, a custom one) leaves the provider unset. Future AGI prices a
152+
* call from gen_ai.request.model and its tokens, not from the provider.
153+
*/
154+
const ADAPTER_PROVIDERS = {
155+
openai: "openai",
156+
"openai-chat": "openai",
157+
anthropic: "anthropic",
158+
gemini: "google",
159+
"gemini-text-interactions": "google",
160+
grok: "xai",
161+
groq: "groq",
162+
ollama: "ollama",
163+
};
143164

144165
/**
145166
* Span kinds, usage and session for the spans otelMiddleware emits.
@@ -170,6 +191,11 @@ export function futureAgiSpanKinds({ threadIdAsSession = false } = {}) {
170191
return {
171192
attributeEnricher(info) {
172193
const attributes = kindAttributes(info);
194+
const adapter = info.ctx?.provider;
195+
const provider = Object.hasOwn(ADAPTER_PROVIDERS, adapter) ? ADAPTER_PROVIDERS[adapter] : undefined;
196+
if (provider && (info.kind === "chat" || info.kind === "iteration")) {
197+
attributes[GEN_AI_PROVIDER_NAME] = provider;
198+
}
173199
if (threadIdAsSession && info.ctx?.threadId) {
174200
attributes[SESSION_ID] = info.ctx.threadId;
175201
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,31 @@ test("no session.id by default, although TanStack always sets a threadId", () =>
321321
assert.equal(span.attributes["session.id"], undefined);
322322
}
323323
});
324+
325+
test("gen_ai.provider.name from the adapter name, so Future AGI shows the vendor", () => {
326+
// TanStack copies adapter.name to gen_ai.system ("openai-chat" for openaiChatCompletions);
327+
// fi-collector reads gen_ai.provider.name first. Adapter names from the published adapters.
328+
const cases = [
329+
["openai-chat", "openai"],
330+
["openai", "openai"],
331+
["anthropic", "anthropic"],
332+
["gemini", "google"],
333+
["gemini-text-interactions", "google"],
334+
["grok", "xai"],
335+
["groq", "groq"],
336+
["ollama", "ollama"],
337+
];
338+
for (const [adapter, provider] of cases) {
339+
const { root, modelCalls } = run(futureAgiSpanKinds(), [usage(1, 1)], { ctx: { provider: adapter } });
340+
assert.equal(modelCalls[0].attributes["gen_ai.provider.name"], provider, adapter);
341+
assert.equal(root.attributes["gen_ai.provider.name"], provider, adapter);
342+
}
343+
});
344+
345+
test("an adapter name with no known vendor leaves gen_ai.provider.name unset", () => {
346+
for (const adapter of ["openrouter", "openrouter-responses", "my-adapter", "constructor", undefined]) {
347+
const { root, modelCalls } = run(futureAgiSpanKinds(), [usage(1, 1)], { ctx: { provider: adapter } });
348+
assert.equal(modelCalls[0].attributes["gen_ai.provider.name"], undefined, String(adapter));
349+
assert.equal(root.attributes["gen_ai.provider.name"], undefined, String(adapter));
350+
}
351+
});

0 commit comments

Comments
 (0)