Skip to main content
TanStack AI provides framework-agnostic helpers — chat(), tool loops, and provider adapters — for building LLM apps in TypeScript. Arize AX captures every TanStack AI run by plugging the @arizeai/openinference-tanstack-ai middleware into the middleware option, which emits OpenInference spans for the overall run, each model turn, and each tool call.

Prerequisites

  • Node.js 18+
  • An Arize AX account (sign up)
  • An OPENAI_API_KEY from the OpenAI Platform (the example uses the OpenAI adapter)

Launch Arize AX

  1. Sign in to your Arize AX account.
  2. From Space Settings, copy your Space ID and API Key. You will set them as ARIZE_SPACE_ID and ARIZE_API_KEY below.

Install

Install TanStack AI, the OpenAI adapter, the OpenInference middleware, and the OpenTelemetry packages that export spans to Arize AX:

Configure credentials

Setup tracing

The middleware does not export spans itself — it uses your application’s OpenTelemetry tracer provider. Register a NodeTracerProvider that ships spans to Arize AX, then attach openInferenceMiddleware() at each chat() call site (see Run).
Registering the tracer provider must run before the middleware emits spans, so import instrumentation.ts first in your entry point. By default openInferenceMiddleware() uses the global tracer this file registers. To attach a request-scoped tracer instead, pass one explicitly: openInferenceMiddleware({ tracer }).

Run TanStack AI

The middleware works for both streaming and non-streaming chat() calls. This example streams the response and collects it with streamToText.

Expected output

Verify in Arize AX

  1. Open your Arize AX space and select project tanstack-ai-tracing-example.
  2. You should see a new trace within ~30 seconds containing an ai.chat AGENT span wrapping an ai.llm 1 LLM child span (with the prompt, response, model, and token usage attached). Tool loops add a TOOL span per executed tool and an additional LLM span per model turn.
  3. If no traces appear, see Troubleshooting.

Check from the skill, CLI, or SDK

Confirm spans are actually reaching your Arize AX project. Use whichever fits your workflow — the skill and CLI work for any framework; the SDK check is shown for each language.
Install the Arize Skills plugin and let your coding agent check for you:
Then prompt your agent:
Use the arize-trace skill to export and analyze recent traces from my project. Confirm spans are arriving, and summarize any errors or latency issues.

Troubleshooting

  • No traces in Arize AX. Confirm ARIZE_SPACE_ID and ARIZE_API_KEY are set in the same shell that runs example.ts, and that instrumentation.ts is imported before any chat() call. Enable OpenTelemetry debug logs with export OTEL_LOG_LEVEL=debug and re-run.
  • Spans missing but the model responds. The middleware only traces chat() calls it is attached to. Make sure middleware: [openInferenceMiddleware()] is passed to every chat() you want traced.
  • 401 from OpenAI. Verify OPENAI_API_KEY is set and has access to the model in the example. Swap openaiText("gpt-5.4-mini") for a model your key can call.
  • Process exits before spans flush. Always await provider.forceFlush() (or provider.shutdown()) before the process exits, otherwise trailing spans are dropped.

Resources

TanStack AI Documentation

OpenInference TanStack AI Middleware

TanStack AI GitHub