Skip to main content
This example demonstrates how to instrument the OpenAI JavaScript/TypeScript SDK with OpenInference tracing.

Prerequisites

  • Node.js 18+
  • OpenAI API key
  • Phoenix or another OpenTelemetry collector running

Installation

1

Install dependencies

2

Set environment variables

Instrumentation Setup

Create an instrumentation.ts file:

Complete Example

Create a chat.ts file:
Run the example:

Streaming Example

Function Calling Example

Key Features

Automatic Instrumentation

The OpenAI instrumentation automatically traces:
  • Chat completions: Standard and streaming responses
  • Embeddings: Text embedding generation
  • Function calling: Tool use and execution
  • Vision: Image inputs with multimodal models

Manual Instrumentation Check

Use isPatched() to verify instrumentation is active:

Resource Attributes

Add project metadata:

Next Steps