Skip to main content
PromptLayer can auto-instrument the official OpenAI SDK and export supported calls as OpenTelemetry spans. Each supported direct SDK call appears in PromptLayer as both a trace span and an associated request log without changing how you create or use the OpenAI client.
This guide covers the OpenAI model SDK. If you use the OpenAI Agents SDK, follow the OpenAI Agents SDK integration instead.

Supported APIs

Only the API surfaces in this table are auto-instrumented. Other OpenAI SDK calls continue to work normally, but this integration does not automatically create PromptLayer traces or request logs for them.

Prerequisites

  • A PromptLayer API key for the workspace that should receive the traces
  • An OpenAI API key
  • Python 3.10 or later for the Python integration, or Node.js 20 or later for the JavaScript integration
Export the variables for your language before the application starts. The API keys are required for the setup in this guide. PromptLayer captures supported prompt, response, and tool content by default.
If your data policies require metadata-only telemetry, use the language-specific opt-out in Capture Prompts and Responses before starting the application. Disabling capture limits content-aware PromptLayer features.

Python

1. Install the SDKs

Install PromptLayer with the OpenAI tracing extra:

2. Initialize instrumentation

Call instrument_openai() before the first OpenAI request. It configures the OpenAI instrumentor and an authenticated OTLP exporter for PromptLayer.
instrument_openai() is idempotent when called again with the same tracer provider. If your application already owns an OpenTelemetry SDK tracer provider, pass it with tracer_provider=.
If your application already creates a PromptLayer client, select OpenAI when enabling tracing:
Omit tracing_providers to instrument every supported provider SDK that is installed. Use either this setup or instrument_openai() for the same tracer provider; you do not need both.

JavaScript

1. Install the SDKs

2. Preload PromptLayer instrumentation

Start Node.js with the promptlayer/register preload. The preload must run before your application imports openai.
For a deployment command that you cannot edit directly, add the preload through NODE_OPTIONS:

3. Use the OpenAI SDK normally

Call shutdownTracing() when a short-lived process finishes, not after every request in a long-running server. The preload instruments every supported provider. To instrument only OpenAI, call configureTracing({ providers: ["openai"] }) in a bootstrap module and dynamically import the application afterward. See Select Providers.

Capture Prompts and Responses

PromptLayer captures supported prompt and response content by default so search, analytics, request inspection, and debugging can use the complete LLM interaction. Model names, timing, token usage when available, and other non-content telemetry are still recorded when capture is disabled. To disable content capture, set the language-specific value before instrument_openai() or the promptlayer/register preload runs:
JavaScript can instead pass captureContent: false to configureTracing(). Restart an already-running process after changing the setting.
Content capture can send user prompts, model responses, and tool arguments to PromptLayer. Because it is enabled by default, review your privacy, retention, and compliance requirements and opt out before the first OpenAI request when necessary.

Configuration Reference

Python also accepts api_key, base_url, endpoint, and tracer_provider keyword arguments:
Configure a tracer provider only once and reuse it. If the OpenAI SDK is already instrumented with a different provider, PromptLayer rejects the mismatch instead of silently exporting incomplete traces. For multi-provider Python applications, use configure_tracing(providers=("openai", ...)) or the tracing_providers PromptLayer client option described in the auto-instrumentation overview.

Verify the Integration

Run one supported OpenAI request, flush tracing, and open Traces in PromptLayer. The OpenAI span should have an associated request log. If the call runs inside PromptLayer.run(), PromptLayer links the provider span to the existing run request log instead of creating a duplicate. If no span appears:
  • Confirm the initialization or JavaScript preload runs before the first OpenAI call.
  • Confirm the call uses an API surface listed in Supported APIs.
  • Confirm PROMPTLAYER_API_KEY belongs to the workspace you are checking.
  • Flush or shut down tracing before a short-lived process exits.
  • If only prompt or response content is missing, confirm content capture was not disabled and restart the process after changing the setting.