Skip to main content

PyPI

The weflayr package.

Changelog

What changed in each release.

Install

Requires Python 3.13 or later.

Send metadata to the gateway

Wrap your calls in propagate_metadata 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

enable_propagate_metadata_to_gateway

Makes every OpenAI client send the current propagate_metadata and set_call_name values in the x-weflayr-metadata header, on requests to https://gateway.weflayr.com. Pass gateway_base_url to target another gateway URL. Calls sent to the gateway are recorded by the gateway itself, never twice by the SDK.

propagate_metadata

Use it as a context manager (with), a decorator (@), or call fn directly with fn=. Every LLM call made inside the scope, including nested ones, gets its values. Each scope (or each call of a decorated function) 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.
dict[str, str | int | float | bool]
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.create_feature_run_uuid(): 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.

set_call_name

A context manager or decorator naming the LLM call inside a feature (e.g. "summarize_ticket"). It keeps the enclosing propagate_metadata scope and does not start a new feature run.

auto_instrument

Records every call of the given provider SDKs: weflayr.AiSdk.OPENAI, ANTHROPIC, BEDROCK, COHERE, GOOGLE_GENAI, ELEVENLABS, MISTRAL, or a list of them. Each needs its instrumentation package, listed in Telemetry. The other parameters 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. 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. Defaults to WEFLAYR_BASE_URL when set.
dict
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.
function
Receives a copy of each call’s attributes and returns the ones to send. Returning None keeps them unchanged; raising an error drops all of them.
boolean
default:"true"
Set False to silence the warning written to stderr on each failed export.

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.

client

Returns the management API client. api_key defaults to WEFLAYR_API_KEY.

Environment variables