npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@farzanhossans/agentlens-openai

v0.1.2

Published

OpenAI auto-instrumentation for AgentLens

Readme

@farzanhossans/agentlens-openai

npm License: MIT

Auto-instrumentation for the OpenAI Node.js SDK. Monkey-patches the OpenAI prototype so every API call is traced — without changing a single line of your existing code.


Install

npm install @farzanhossans/agentlens-core @farzanhossans/agentlens-openai

Setup

import { AgentLens } from '@farzanhossans/agentlens-core'
import '@farzanhossans/agentlens-openai'          // ← auto-patches on import

AgentLens.init({
  apiKey: 'proj_live_abc123',
  projectId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
})

// Nothing else needed. All calls below are now traced:
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
const res = await openai.chat.completions.create({ model: 'gpt-4o', messages })

Import order matters: @farzanhossans/agentlens-openai must be imported before creating any OpenAI client instances.


What gets captured automatically

chat.completions.create — non-streaming

| Field | Source | |-------|--------| | name | "openai.chat.completions" | | model | params.model | | provider | "openai" | | input | JSON.stringify(params.messages) | | output | choices[0].message.content | | inputTokens | usage.prompt_tokens | | outputTokens | usage.completion_tokens | | costUsd | Computed from model pricing table | | latencyMs | Wall-clock time of the HTTP call | | status | "success" or "error" | | errorMessage | Error message if the call throws |

chat.completions.create — streaming (stream: true)

The patcher wraps the returned Stream<ChatCompletionChunk> in an async generator that accumulates delta content. The span is closed when the stream is fully consumed.

Token counts are recorded from the final chunk when stream_options: { include_usage: true } is passed. Without it, token counts are omitted for streaming calls.

completions.create — legacy text completions

| Field | Source | |-------|--------| | name | "openai.completions" | | input | params.prompt (string prompts only) | | output | choices[0].text |

embeddings.create

| Field | Source | |-------|--------| | name | "openai.embeddings" | | input | params.input (serialised to string if array) | | inputTokens | usage.prompt_tokens | | costUsd | Computed from model pricing table |


Supported models and pricing

Cost is calculated automatically using the table below (USD per 1,000 tokens, as of 2024-Q2):

| Model | Input / 1k tokens | Output / 1k tokens | |-------|------------------|--------------------| | gpt-4o | $0.005 | $0.015 | | gpt-4o-mini | $0.00015 | $0.0006 | | gpt-4-turbo | $0.01 | $0.03 | | gpt-4 | $0.03 | $0.06 | | gpt-4-32k | $0.06 | $0.12 | | gpt-3.5-turbo | $0.0005 | $0.0015 | | gpt-3.5-turbo-instruct | $0.0015 | $0.002 | | text-embedding-3-small | $0.00002 | — | | text-embedding-3-large | $0.00013 | — | | text-embedding-ada-002 | $0.0001 | — |

If a model isn't in the table, costUsd is recorded as undefined. Versioned model names (e.g. gpt-4o-2024-05-13) are normalised to their base name for pricing lookups.


Unpatching (testing)

The patcher exposes unpatch() to restore the original OpenAI methods. Use this in test teardown:

import { patch, unpatch, patches } from '@farzanhossans/agentlens-openai/patcher'
import OpenAI from 'openai'

beforeEach(() => {
  patch(OpenAI)  // pass the constructor to ensure correct ESM prototype
})

afterEach(() => {
  unpatch()
})

To mock the underlying OpenAI call in unit tests, replace patches[N].original after calling patch():

import { patches } from '@farzanhossans/agentlens-openai/patcher'
import { vi } from 'vitest'

// patches[0] = chat completions, patches[1] = legacy completions, patches[2] = embeddings
patches[0]!.original = vi.fn().mockResolvedValue(mockChatCompletion)

Known limitations

  • stream_options.include_usage must be explicitly set to true to capture token counts in streaming responses. OpenAI does not emit usage by default for streams.
  • Parallel tool calls (tool_calls in the message) are recorded as part of the raw output JSON but not parsed into structured fields.
  • Images in messages (content: [{ type: 'image_url', ... }]) are included in the serialised input and subject to PII scrubbing if redactPII: true is set.
  • The patcher targets the prototype chain of the OpenAI class. If OpenAI releases a breaking SDK version that restructures the chat.completions resource, repatch may be needed.