Skip to main content
The gateway is designed to be a drop-in replacement for provider base URLs, every Python LLM SDK works by setting its base URL env var and using a LangWatch virtual key as the API key. This page covers the standard setup + how to propagate trace ids through the gateway so you don’t double-count cost.

OpenAI Python SDK

Minimal setup

That’s the whole integration. Everything else (streaming, tools, vision, embeddings, images, audio) works unchanged.

Trace propagation

If your application is already traced with the LangWatch SDK, pass the current trace context so the gateway’s span nests inside yours, otherwise you’ll have two disconnected traces and double-counted cost in dashboards.
langwatch.get_gateway_headers() returns a dict with:
  • traceparent (W3C format), set from the active LangWatch trace.
  • X-LangWatch-Trace-Id, LangWatch-native trace id override.
  • X-LangWatch-Parent-Span-Id, parent span.
  • X-LangWatch-Thread-Id, if the active trace has a thread id.
The gateway reads these on each request and attaches its own span as a child of your trace. The LangWatch UI shows the LLM call nested under your agent span with no cost duplication.
get_gateway_headers() ships in LangWatch Python SDK ≥ v0.22.0 alongside the gateway GA. Check your installed version with pip show langwatch.

Response headers for correlation

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

Without the LangWatch SDK: raw traceparent

If you don’t use the LangWatch Python SDK but still want trace continuity (e.g. you’re traced via OpenTelemetry directly):
The gateway honours the standard W3C traceparent contract, any OTel-instrumented app already emits this; no LangWatch-specific code needed.

Per-call overrides

Every OpenAI SDK method accepts extra_headers={}:
These layer on top of default_headers without replacing them.

Response inspection

Grab the X-LangWatch-Request-Id for support tickets or log correlation:

Anthropic Python SDK

Minimal setup

Trace propagation (Anthropic)

Works identically to the OpenAI SDK.

LangChain, LangGraph

LangChain’s ChatOpenAI and ChatAnthropic accept a base_url or openai_api_base:
For LangGraph agents, set the headers on the LLM node, it propagates to every call the agent makes.

LlamaIndex

PydanticAI, OpenInference, other OTel-aware frameworks

Frameworks that already emit OpenTelemetry spans will set traceparent automatically on outbound HTTP requests if the opentelemetry-instrumentation-requests, httpx, aiohttp packages are installed. No LangWatch SDK involvement needed.

Self-hosted gateway

Replace the hostname:
Rest of the setup is identical.

Troubleshooting

  • 401 invalid_api_key, wrong VK or VK revoked. Check the first 12 characters against the LangWatch UI.
  • Cost double-counted: trace propagation not working. Verify default_headers contains traceparent at request time (client._custom_headers on the OpenAI SDK).
  • Anthropic auth header mismatch: the Python Anthropic SDK sets x-api-key automatically from api_key. The gateway accepts all three (Bearer, x-api-key, api-key), so either works.
See API: Errors for the full error-code list.