Python SDK for SpendLens AI.
Project description
SpendLens AI Python SDK
Track supported OpenAI and Anthropic calls without proxying model traffic. SpendLens records operational metadata, token usage, latency, cache counters, workload context, and privacy-safe prompt-template context.
Install
pip install spendlensai
Configure the SDK through environment variables:
SPENDLENS_API_KEY=sli_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SPENDLENS_PROJECT=production
SPENDLENS_API_BASE_URL=https://spendlensai.dev/backend
SPENDLENS_PROMPT_SAMPLING=template_only
SPENDLENS_DISABLE=0
The SDK reads process environment variables. A local .env file must be loaded by your application or runtime; production secrets should be injected by your deployment platform.
Choose your integration
Existing application: add one decorator
Keep your standard provider client and mark the business workflow:
import spendlensai
from openai import OpenAI
client = OpenAI()
@spendlensai.observe(workload="customer-support-reply")
def generate_customer_reply(message: str):
return client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": "You are a customer-support assistant. Give concise, accurate billing help.",
},
{"role": "user", "content": message},
],
)
The bare form derives a stable endpoint from the module and qualified function name:
@spendlensai.observe
async def generate_customer_reply(message: str):
return await client.chat.completions.create(...)
Decorator mode currently instruments supported official Python SDK operations:
- OpenAI
chat.completions.create - OpenAI
responses.create - Anthropic
messages.create - Supported sync and async variants
Direct REST calls and arbitrary HTTP traffic are not automatically captured.
Precision tracking for new or complex applications
Use the existing tracked client when one function makes several unrelated calls or you need exact task and metadata attribution:
from openai import OpenAI
from spendlensai import track
client = track(OpenAI())
with client.tag(
endpoint="customer-support-reply",
task="generation",
metadata={"feature": "support"},
):
response = client.chat.completions.create(...)
When both integrations are used, attribution precedence is:
client.tag() → @spendlensai.observe → inferred callsite
The SDK prevents the same tracked call from being recorded twice.
Environment variables
| Variable | Required? | Description |
|---|---|---|
SPENDLENS_API_KEY |
Yes | SpendLens test or live ingestion key. |
SPENDLENS_PROJECT |
Yes for decorator mode | Dashboard project name. |
SPENDLENS_API_BASE_URL |
No | Defaults to https://spendlensai.dev/backend. Do not append /v1. |
SPENDLENS_PROMPT_SAMPLING |
No | template_only by default; may be off or redacted_sample. |
SPENDLENS_DISABLE |
No | Set to 1 to disable tracking without changing application code. |
Existing explicit track(...) options remain supported for backwards compatibility.
Tags and metadata
Use short, stable endpoint names such as recommend-products, classify-review, summarize-ticket, or support-rag-answer.
with client.tag(
endpoint="recommend-products",
task="generation",
metadata={"customer_tier": "enterprise", "region": "us"},
):
response = client.chat.completions.create(...)
Do not put prompts, API keys, emails, or personal information in metadata. Nested tags are supported; inner values take precedence.
Prompt privacy
SPENDLENS_PROMPT_SAMPLING=template_only is the recommended default. It sends reusable OpenAI system/developer instructions or Anthropic system content to improve workload classification. It does not send user messages, full message arrays, or model responses.
Set this for metadata-only tracking:
SPENDLENS_PROMPT_SAMPLING=off
Prompt templates are truncated conservatively. API keys and prompt content are never written to SDK logs.
Cache efficiency
When providers return cache usage, SpendLens records it automatically:
- OpenAI:
usage.prompt_tokens_details.cached_tokens - Anthropic:
usage.cache_read_input_tokensandusage.cache_creation_input_tokens
SpendLens reports cache metrics as unavailable when the provider does not return them rather than inventing zero values.
Short processes
Tracked clients expose flush() for scripts, jobs, tests, and notebooks:
client.flush()
Long-running applications normally flush queued events automatically.
Failure behavior
Telemetry delivery is asynchronous and fail-open. SpendLens failures do not replace provider responses. Invalid or missing decorator configuration raises a clear configuration error on the first observed provider call. Set SPENDLENS_DISABLE=1 to disable all SpendLens activity.
SpendLens is an observability and cost-insight layer. It does not proxy model traffic, rewrite prompts, route requests, or automatically change production models.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spendlensai-0.1.2.tar.gz.
File metadata
- Download URL: spendlensai-0.1.2.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df137bf119dc4577f37d77b046f28aa83799e8bbd38ee3f464270cb825ede838
|
|
| MD5 |
54a544e278852dc3a3afb98cd4cc30c2
|
|
| BLAKE2b-256 |
c7337c5d1c935778554b56450a32be999715518a1c310cc439060df74d29fcd3
|
File details
Details for the file spendlensai-0.1.2-py3-none-any.whl.
File metadata
- Download URL: spendlensai-0.1.2-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a61a264ac5fc45f835292546597a1a8965b334618c1e5d8d4e1ca18639f1bc1d
|
|
| MD5 |
95de29177fafb653064092cf7d452fa2
|
|
| BLAKE2b-256 |
21cbe170e538460352ca55b79d22d46a0f02dafc13c962cc1b647059e2210a90
|