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

@optimai/sdk

v0.2.0

Published

Optim SDK - Track and optimize AI costs automatically

Downloads

19

Readme

@optimai/sdk

Track and optimize your AI costs automatically. Wrap any OpenAI-compatible client and telemetry flows to Optim with zero code changes.

Automatically detects providers: OpenAI, Groq, Anthropic, Mistral, DeepSeek, Together, Fireworks, Perplexity, Cohere, OpenRouter.

Installation

npm install @optimai/sdk openai

Quick Start (OpenAI)

import OpenAI from 'openai'
import { initOptim, wrapOpenAI } from '@optimai/sdk'

initOptim({
  projectKey: 'opt_proj_your_key_here',
  baseUrl: 'https://optim.dev',
})

const openai = wrapOpenAI(new OpenAI())

const completion = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
})

Using with Groq

Groq uses an OpenAI-compatible API, so just point the OpenAI client at Groq:

import OpenAI from 'openai'
import { initOptim, wrapOpenAI } from '@optimai/sdk'

initOptim({
  projectKey: 'opt_proj_your_key_here',
  baseUrl: 'https://optim.dev',
})

const groq = wrapOpenAI(new OpenAI({
  baseURL: 'https://api.groq.com/openai/v1',
  apiKey: process.env.GROQ_API_KEY,
}))

// Use as normal — provider is auto-detected as "groq"
const res = await groq.chat.completions.create({
  model: 'llama-3.3-70b-versatile',
  messages: [{ role: 'user', content: 'Hello!' }],
})

Using with Other Providers

Any OpenAI-compatible provider works the same way:

// Together AI
const together = wrapOpenAI(new OpenAI({
  baseURL: 'https://api.together.xyz/v1',
  apiKey: process.env.TOGETHER_API_KEY,
}))

// Mistral
const mistral = wrapOpenAI(new OpenAI({
  baseURL: 'https://api.mistral.ai/v1',
  apiKey: process.env.MISTRAL_API_KEY,
}))

Manual Tracking

For providers without an OpenAI-compatible API, use trackRequest directly:

import { initOptim, trackRequest } from '@optimai/sdk'

initOptim({
  projectKey: 'opt_proj_your_key_here',
  baseUrl: 'https://optim.dev',
})

trackRequest({
  provider: 'anthropic',
  model: 'claude-sonnet-4-20250514',
  promptTokens: 150,
  completionTokens: 300,
  latencyMs: 1200,
})

Configuration

initOptim({
  projectKey: 'opt_proj_...',    // Required — your project key
  baseUrl: 'https://optim.dev',  // Required — Optim API endpoint (HTTPS only)
  batchSize: 10,                  // Events per batch (default: 10)
  flushInterval: 5000,            // Flush interval in ms (default: 5000)
  debug: true,                    // Enable debug logging to see SDK activity
})

Security

  • HTTPS enforcedbaseUrl must use HTTPS (http://localhost allowed for local development only)
  • No hardcoded URLs — you explicitly provide the API endpoint
  • Input sanitization — all event fields are validated and bounded before transmission
  • Origin verification — outbound requests are verified against the configured base URL origin
  • Credential-free URLsbaseUrl with embedded credentials is rejected

Streaming Support

Streaming is fully supported. The SDK automatically injects stream_options.include_usage: true so token counts are captured:

const stream = await groq.chat.completions.create({
  model: 'llama-3.3-70b-versatile',
  messages: [{ role: 'user', content: 'Hello!' }],
  stream: true,
})

for await (const chunk of stream) {
  // process chunks...
}
// Telemetry is sent automatically when the stream ends

Graceful Shutdown

Call flushAll() before your process exits to ensure all events are sent:

import { flushAll } from '@optimai/sdk'

process.on('beforeExit', async () => {
  await flushAll()
})