---
title: "OpenAI SDK"
description: "Set up Blyp tracing for OpenAI SDK calls, including OpenRouter through the OpenAI-compatible client path."
canonical_url: "https://www.blyp.dev/docs/ai/openai-sdk"
markdown_url: "https://www.blyp.dev/docs/ai/openai-sdk.md"
last_updated: "2018-10-20"
x_farming_labs_generated_preamble: true
agent:
  task: "Wrap an OpenAI-compatible client so Blyp traces Responses or Chat Completions calls."
  outcome: "OpenAI or OpenRouter calls emit normalized ai_trace records with usage and timing."
  appliesTo:
    package:
      - "@blyp/core"
      - "openai"
  prerequisites:
    - "The OpenAI-compatible client and its base URL or API key already work."
  files:
    - "blyp.config.ts"
    - "src/lib/openai.ts"
  commands:
    - "pnpm add @blyp/core openai"
  sideEffects:
    - "Opt-in content capture can persist prompts"
    - "outputs"
    - "reasoning"
    - "and tool payloads."
  verification:
    - "Call responses.create or chat.completions.create and confirm one ai_trace record."
  rollback:
    - "Restore the original unwrapped OpenAI client."
  failureModes:
    - symptom: "OpenRouter traces show the wrong provider."
      resolution: "Configure the OpenAI-compatible client base URL before wrapping it and set provider metadata where documented."
---

# OpenAI SDK
URL: /docs/ai/openai-sdk
LLM index: /llms.txt
Description: Set up Blyp tracing for OpenAI SDK calls, including OpenRouter through the OpenAI-compatible client path.
Related: /docs/ai/tracing, /docs/ai/privacy-and-capture, /docs/ai/vercel-ai-sdk

<!-- farming-labs:agent-contract:start -->
## Agent Contract

Task: Wrap an OpenAI-compatible client so Blyp traces Responses or Chat Completions calls.
Outcome: OpenAI or OpenRouter calls emit normalized ai_trace records with usage and timing.

### Applies To

- Package: `@blyp/core`, `openai`

### Prerequisites

- The OpenAI-compatible client and its base URL or API key already work.

### Files

- `blyp.config.ts`
- `src/lib/openai.ts`

### Commands

- `pnpm add @blyp/core openai`

### Side Effects

- Opt-in content capture can persist prompts
- outputs
- reasoning
- and tool payloads.

### Verification

- Call responses.create or chat.completions.create and confirm one ai_trace record.

### Rollback

- Restore the original unwrapped OpenAI client.

### Failure Modes

- OpenRouter traces show the wrong provider. — Recovery: Configure the OpenAI-compatible client base URL before wrapping it and set provider metadata where documented.
<!-- farming-labs:agent-contract:end -->

# OpenAI SDK

Wrap the configured OpenAI or OpenRouter-compatible client once, then call `responses.create()` or
`chat.completions.create()` through that wrapper. Verify a normalized `ai_trace` with model, usage,
and duration. Do not enable prompt/output capture by default; missing traces usually indicate calls
still use the original client instance.

Use `@blyp/core/ai/openai` when you want direct OpenAI SDK instrumentation without going through the Vercel AI SDK abstraction.

This same page also covers OpenRouter, because OpenRouter works through the OpenAI-compatible client path.

## Install

Install the OpenAI SDK in the app that uses the traced client:

```bash
bun add openai
```

## `wrapOpenAI()`

Use `wrapOpenAI()` to instrument an OpenAI client directly:

```ts
import OpenAI from "openai";
import { wrapOpenAI } from "@blyp/core/ai/openai";

const openai = wrapOpenAI(
  new OpenAI({ apiKey: process.env.OPENAI_API_KEY }),
  {
    operation: "draft_blog_intro",
    metadata: {
      app: "docs-site",
    },
  }
);

await openai.responses.create({
  model: "gpt-5.4-mini",
  input: "Write a short intro about release notes",
});
```

`wrapOpenAI()` covers:

- `responses.create()`
- `chat.completions.create()`

## OpenRouter through the OpenAI-compatible path

Use the same OpenAI SDK wrapper for OpenRouter by pointing the client at the OpenRouter base URL:

```ts
import OpenAI from "openai";
import { wrapOpenAI } from "@blyp/core/ai/openai";

const client = wrapOpenAI(
  new OpenAI({
    apiKey: process.env.OPENROUTER_API_KEY,
    baseURL: "https://openrouter.ai/api/v1",
  }),
  {
    provider: "openrouter",
    operation: "route_experiment",
  }
);
```

There is no separate OpenRouter Blyp page or wrapper. Use the OpenAI SDK path for both.

## `blypFetch()` for low-level transport tracing

When you want request-level transport tracing in addition to SDK method tracing, wrap the OpenAI client's `fetch` implementation:

```ts
import OpenAI from "openai";
import { blypFetch } from "@blyp/core/ai/fetch";

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  fetch: blypFetch(fetch, {
    inspectJsonBody: true,
    metadata: {
      target: "openai-http",
    },
  }),
});
```

This emits `fetch_trace` records for status codes, latency, request IDs, and optional inspected JSON responses.

## Advanced API surface

Additional public APIs:

- `createOpenAITracker`
- `BlypProviderOptions`
- `BlypSDKContext`
- `BlypLLMTrace`

Use [AI Privacy & Capture](/docs/ai/privacy-and-capture) for the shared `capture`, `exclude`, and `limits` controls.

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
