Skip to main content
LangWatch supports tracing Grok (xAI) API calls through xAI’s OpenAI-compatible endpoint. Reuse the OpenAI otelopenai middleware: point the client at the xAI base URL and label the spans with WithGenAIProvider.

Installation

Usage

Set LANGWATCH_API_KEY and XAI_API_KEY environment variables before running.
The middleware captures input and output content by default. Pass otelopenai.WithDataCapture(langwatch.DataCaptureNone) to opt out.
The defer tp.Shutdown(ctx) call is essential. Without it, traces buffered in memory will be lost when your application exits.

Check it worked

Run the program with go run .. The deferred tp.Shutdown(ctx) flushes the spans when main returns. Then open your project in LangWatch: a new trace appears in the traces list within a few seconds, with the Grok call as an LLM span carrying its input, output and token counts. If no trace appears, check that LANGWATCH_API_KEY is set in the environment the program runs in.
Last modified on October 29, 2026