Skip to main content

PyPI

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 Python 3.13 or later.

Reference

Send extra metadata to the gateway

enable_propagate_metadata_to_gateway

Makes every OpenAI client send the current propagate_metadata values in the x-weflayr-metadata header of its gateway requests.
string
default:"https://gateway.weflayr.com"
The gateway URL whose requests get the header. Requests to any other host are left unchanged.

propagate_metadata

Tags every LLM call made inside its scope, including nested ones, through the gateway or recorded by telemetry. 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.
function
Runs this function inside the scope and returns its result.
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.

Track calls with telemetry only

auto_instrument

Records every call of the given provider SDKs. Each needs its instrumentation package, listed in Track calls with telemetry only.
weflayr.AiSdk | list[weflayr.AiSdk]
required
OPENAI, ANTHROPIC, BEDROCK, COHERE, GOOGLE_GENAI, ELEVENLABS or MISTRAL, or a list of them.
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.
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.

set_call_name

A context manager or decorator naming the LLM call inside a feature. It keeps the enclosing propagate_metadata 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.

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.
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