npm
The
weflayr package.Changelog
What changed in each release.
Install
Requires Node.js 18 or later.Send metadata to the gateway
Wrap your calls inpropagateMetadata to tag them with their feature, customer and call name, through the gateway or with telemetry. See Name features, customers and runs.
Call the management API
weflayr.client() returns a client for every management route, with a key that has the Management scope. See Call the management API for an example and the list of routes.
Track calls made without the gateway
When your app calls the providers directly, the SDK can record each call with OpenTelemetry and send it to Weflayr. See Telemetry for the step by step.Reference
enablePropagateMetadataToGateway
propagateMetadata and setCallName values in the x-weflayr-metadata header, on requests to https://gateway.weflayr.com. Pass gatewayBaseUrl to target another gateway URL. Requires the openai package, and setup() to have run. Calls sent to the gateway are recorded by the gateway itself, never twice by the SDK.
propagateMetadata
fn, runs it and returns its result. Without, returns a decorator for a class method, or a wrapper: propagateMetadata(options)(fn). Every LLM call made inside, including nested ones, gets its values. 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 the SDK, when the client on the wire differs from it (e.g. Mistral reached through the OpenAI client). Ignored for gateway calls.
setCallName
'summarize_ticket'). 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.
autoInstrument
'openai', 'anthropic', 'bedrock', 'cohere', 'google-genai', 'elevenlabs', 'mistral', or an array of them. Each needs its instrumentation package, listed in Telemetry. The other options are those of setup.
setup
string
A key with the Management scope. Defaults to
WEFLAYR_API_KEY.string
default:"'https://api.weflayr.com'"
Where recorded calls are sent. Defaults to
WEFLAYR_BASE_URL when set.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.flush
client
weflayr.api functions. apiKey defaults to WEFLAYR_API_KEY, baseUrl to https://app.weflayr.com/api.