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_KEYfrom the OpenAI Platform (the example uses the OpenAI adapter)
Launch Arize AX
- Sign in to your Arize AX account.
- From Space Settings, copy your Space ID and API Key. You will set them as
ARIZE_SPACE_IDandARIZE_API_KEYbelow.
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 aNodeTracerProvider 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-streamingchat() calls. This example streams the response and collects it with streamToText.
Expected output
Verify in Arize AX
- Open your Arize AX space and select project
tanstack-ai-tracing-example. - You should see a new trace within ~30 seconds containing an
ai.chatAGENT span wrapping anai.llm 1LLM 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. - 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.- Arize skill (agent)
- AX CLI
- SDK
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_IDandARIZE_API_KEYare set in the same shell that runsexample.ts, and thatinstrumentation.tsis imported before anychat()call. Enable OpenTelemetry debug logs withexport OTEL_LOG_LEVEL=debugand re-run. - Spans missing but the model responds. The middleware only traces
chat()calls it is attached to. Make suremiddleware: [openInferenceMiddleware()]is passed to everychat()you want traced. 401from OpenAI. VerifyOPENAI_API_KEYis set and has access to the model in the example. SwapopenaiText("gpt-5.4-mini")for a model your key can call.- Process exits before spans flush. Always
await provider.forceFlush()(orprovider.shutdown()) before the process exits, otherwise trailing spans are dropped.