What changes in your code
Two settings change: the base URL points at the gateway, and the API key is a virtual key. The request body, streaming, tools and every other SDK feature stay the same. Name the model asprovider/model on the OpenAI-shaped and Anthropic-shaped routes; see Model naming.
Keep the trailing slash on the base URL: the Go SDKs append the route to it.
Install
vk-lw-...) is not your LangWatch API key. LANGWATCH_API_KEY is only needed by the LangWatch SDK, to trace your own code.
OpenAI SDK
Anthropic SDK
Check it worked
Run the OpenAI SDK sample. It prints the model’s answer. Then open the project the virtual key traces to in LangWatch: the gateway’sgen_ai.<request type> span appears in the traces list within a few seconds, as its own trace, with the model, the tokens and the cost.
Link the gateway span to your trace
The gateway records onegen_ai.<request type> span per request in the trace project of the virtual key. Send a W3C traceparent header and the gateway nests that span under your span. Without it, each request is its own trace.
Any OpenTelemetry-instrumented application has the current context. Inject it into the request headers, per request, inside the span you want the gateway span under:
otel.GetTextMapPropagator() returns a no-op until you register a propagator, for example otel.SetTextMapPropagator(propagation.TraceContext{}). The LangWatch Go SDK is built on OpenTelemetry, so the same code works inside a langwatch span. Do not also add the LangWatch OpenAI middleware to clients that call the gateway: the gateway already records the span with the model, the tokens and the cost.
Headers you can send
Pass them per request with
option.WithHeader("X-LangWatch-End-User-Id", "cust_8841") as an extra argument to the call. The gateway removes them before it forwards the request to the provider. See Billing and spend events.
Read the response headers
Manage keys and budgets from Go
The LangWatch Go SDK has no helpers for virtual keys, budgets or spend events. Call the management routes over REST with a LangWatch API key. See Virtual keys and Billing and spend events.Self-hosted gateway
Replace the host with your gateway’s URL. Everything else is the same.Troubleshooting
See Errors for every error code.