Skip to main content
Every TypeScript LLM SDK works by overriding its base URL and using a LangWatch virtual key. The gateway handles the rest. This page shows the standard setup plus how to propagate trace ids so you don’t double-count cost.

OpenAI TypeScript SDK

Minimal setup

Trace propagation

getGatewayHeaders() reads the active LangWatch trace (via the SDK’s AsyncLocalStorage context) and returns a header object with traceparent, X-LangWatch-Trace-Id, X-LangWatch-Parent-Span-Id, X-LangWatch-Thread-Id. The gateway uses these to parent its span under your trace, no duplicate cost.
getGatewayHeaders() ships in langwatch npm ≥ v0.26.0 alongside the gateway GA. Check with npm ls langwatch.

Response headers for correlation

Every gateway response carries these headers so clients can stitch the gateway span into their own trace tooling without the LangWatch SDK:

Without the LangWatch SDK: raw traceparent

Using OpenTelemetry JS directly:
The gateway honours W3C traceparent, any OTel-instrumented Node.js app already emits this.

Per-call overrides

Response inspection

Anthropic TypeScript SDK

Vercel AI SDK

Custom fetch lets the AI SDK pass gateway-specific headers on every request without the SDK having a defaultHeaders option.

LangChain.js

Self-hosted gateway

Replace the hostname:

Troubleshooting

  • 401 invalid_api_key, wrong VK or revoked. Verify the first 12 chars in the LangWatch UI.
  • Cost double-counted: trace propagation not working. Check that getGatewayHeaders() returned non-empty (log the dict). If the active trace is null, no headers are set.
  • CORS errors in browser: the gateway does not expose CORS for the /v1 routes by default. Use a server-side proxy; don’t call the gateway from a browser with a production VK (it would expose the VK to users).
See API: Errors.