Skip to Content
SDK ReferencewithNeuraMeter()

withNeuraMeter()

Wraps an OpenAI client so that every chat.completions.create() call is automatically tracked in NeuraMeter. No proxy required.

Usage

import OpenAI from 'openai'; import { withNeuraMeter } from '@neurameter/core'; const openai = withNeuraMeter(new OpenAI(), { apiKey: 'nm_xxx', projectId: 'proj_xxx', agentName: 'MyAgent', });

Config

interface WithNeuraMeterConfig { /** NeuraMeter API key (format: nm_{orgId}_{secret}) */ apiKey: string; /** NeuraMeter project ID */ projectId: string; /** Agent name for cost events (default: 'default') */ agentName?: string; /** NeuraMeter ingestion endpoint (default: 'https://meter.neuria.tech') */ endpoint?: string; }
FieldRequiredDefaultDescription
apiKeyYesYour NeuraMeter API key
projectIdYesYour NeuraMeter project ID
agentNameNo'default'Name to label this agent’s events
endpointNo'https://meter.neuria.tech'Ingestion API URL

How It Works

Non-streaming

  1. Intercepts chat.completions.create() calls
  2. Starts a timer (Date.now())
  3. Calls the original OpenAI method
  4. Extracts response.usage (prompt_tokens, completion_tokens, etc.)
  5. Calculates cost using built-in pricing tables
  6. Records the event via NeuraMeter.record() (batched, async)
  7. Returns the response unchanged

Streaming

  1. Detects stream: true in params
  2. Auto-injects stream_options: { include_usage: true } (so OpenAI includes usage in the final chunk)
  3. Wraps the async iterable stream
  4. Yields every chunk unchanged to your code
  5. Captures usage from the final chunk
  6. Records the event after stream completion

What Gets Tracked

Each call records:

FieldSource
modelFrom request params
inputTokensusage.prompt_tokens
outputTokensusage.completion_tokens
reasoningTokensusage.completion_tokens_details.reasoning_tokens
cachedTokensusage.prompt_tokens_details.cached_tokens
costMicrodollarsCalculated from built-in pricing tables
latencyMsDate.now() - startTime
provider'openai'
agentNameFrom config

Cleanup

The wrapper attaches a __neurameter_destroy method for cleanup:

// When shutting down your application await (openai as any).__neurameter_destroy();

This flushes any remaining buffered events before shutdown.

Notes

  • The wrapper has zero dependency on the openai npm package — it uses minimal interfaces internally, so it works with any version of the OpenAI SDK
  • Events are batched (50 events or 5s interval) and sent asynchronously — no impact on your API call latency
  • If the NeuraMeter ingestion endpoint is unreachable, events are silently dropped (fire-and-forget)
  • The wrapper only intercepts chat.completions.create(). Other OpenAI methods (embeddings, images, etc.) pass through unchanged.