Skip to main content

npm

The weflayr package.

Changelog

What changed in each release.
The SDK does three separate things, each usable on its own:

Send extra metadata to the gateway

Group the gateway calls of the same feature, and optionally tag their customer and your own dimensions.

Track calls with telemetry only

Record the calls your app makes to Anthropic, OpenAI, Bedrock, Vertex AI and other providers directly.

Call the management API

Read your costs and margin, and push revenue and customer tags.

Install

Requires Node.js 22.15 or later.

Reference

Send extra metadata to the gateway

enablePropagateMetadataToGateway

Makes every OpenAI client send the current propagateMetadata values in the x-weflayr-metadata header of its gateway requests. Requires the openai package, and setup() to have run.
string
default:"https://gateway.weflayr.com"
The gateway URL whose requests get the header. Requests to any other host are left unchanged.

propagateMetadata

Tags every LLM call made inside, including nested ones, through the gateway or recorded by telemetry. With fn, runs it and returns its result. Without, returns a decorator for a class method, or a wrapper: propagateMetadata(options)(fn). Each call of propagateMetadata (or of a decorated method) is one feature run.
string
required
The product feature making the call (e.g. 'support-chat').
string
The customer the call is made for. It joins your AI cost to your revenue.
Record<string, string | number | boolean>
Your own dimensions to filter on.
string
Records the calls under this run instead of a new one, to group the calls of one run made in several scopes or processes. Create it with weflayr.createFeatureRunUuid(): 32 lowercase hexadecimal characters.
weflayr.AIProviderName
The provider to bill a call recorded by telemetry, when the client on the wire differs from it (e.g. Mistral reached through the OpenAI client). Ignored for gateway calls.
function
Runs this function inside the scope and returns its result.

Track calls with telemetry only

autoInstrument

Records every call of the given provider SDKs. Each needs its instrumentation package, listed in Track calls with telemetry only.
string | string[]
required
'openai', 'anthropic', 'bedrock', 'cohere', 'google-genai', 'elevenlabs' or 'mistral', or an array of them.
The other options are those of setup.

setup

Configures where recorded calls are sent, without instrumenting any provider SDK: use it when your app already runs its own OpenTelemetry instrumentation, or to send metadata to the gateway. Calling it again replaces the previous configuration.
string
A key with the Management scope. Defaults to WEFLAYR_API_KEY.
string
default:"'https://api.weflayr.com'"
Where recorded calls are sent.
object
Tags added to every recorded call.
'batch' | 'immediate'
default:"'batch'"
'immediate' sends each call as soon as it ends, instead of in batches.
boolean
default:"true"
Set false to keep prompts, answers and tool definitions in your process: only tokens, cost and latency are sent.
(attributes) => attributes
Receives each call’s attributes and returns the ones to send. Returning null keeps them unchanged; throwing an error drops all of them.
boolean
default:"true"
Set false to silence the warning written to stderr on each failed export.

setCallName

Names the LLM call inside a feature. With fn, runs it and returns its result; without, returns a decorator. It keeps the enclosing propagateMetadata scope and does not start a new feature run.
string
required
The name of the call (e.g. 'summarize_ticket'). A benchmark is set up per call name.
function
Runs this function under the call name and returns its result.

flush

Sends the recorded calls still in memory. Call it before a short-lived process (script, Lambda) exits; a long-running server flushes on its own.

Call the management API

Every management API route, with its SDK method, is listed in Call the management API.

client

Returns the management API client, to pass to the weflayr.api functions.
string
A key with the Management scope. Defaults to WEFLAYR_API_KEY.
string
default:"'https://app.weflayr.com/api'"
The management API URL.

Environment variables