Skip to main content

Python SDK for SpendLens AI.

Project description

SpendLens AI Python SDK

Track OpenAI and Anthropic usage in SpendLens AI without proxying your LLM traffic.

For full setup guides, examples, and dashboard documentation, visit https://spendlensai.dev/docs.

The SDK preserves the original provider response while capturing usage metadata such as provider, model, token counts, latency, callsite, endpoint labels, and privacy-safe prompt-template metadata. When returned by the provider, it also captures OpenAI and Anthropic prompt-cache counters for the Cache Efficiency report.

SpendLens classifies each stable workload on the backend from its endpoint label, Python function, file path, token shape, and optional system template. You do not need to hard-code task labels.

Install

pip install spendlensai

OpenAI example

from openai import OpenAI
from spendlensai import track

openai_client = OpenAI(api_key="OPENAI_API_KEY")

client = track(
    openai_client,
    project="shopping-assistant",
    api_key="sli_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    api_base_url="https://spendlensai.dev/backend",
)

def recommend_products(shopping_request: str):
    with client.tag(endpoint="recommend-products"):
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[
                {
                    "role": "system",
                    "content": "Recommend three products that match the shopper's budget and intended use.",
                },
                {"role": "user", "content": shopping_request},
            ],
        )
    return response.choices[0].message.content

Anthropic / Claude example

from anthropic import Anthropic
from spendlensai import track

anthropic_client = Anthropic(api_key="ANTHROPIC_API_KEY")

client = track(
    anthropic_client,
    project="shopping-assistant",
    api_key="sli_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    api_base_url="https://spendlensai.dev/backend",
)

def recommend_products(shopping_request: str):
    with client.tag(endpoint="recommend-products"):
        response = client.messages.create(
            model="claude-sonnet-4-6",
            max_tokens=300,
            system="Recommend three products that match the shopper's budget and intended use.",
            messages=[{"role": "user", "content": shopping_request}],
        )
    return response.content[0].text

track(...) options

client = track(
    openai_client,
    project="shopping-assistant",
    api_key="sli_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    api_base_url="https://spendlensai.dev/backend",
    provider="openai",
    prompt_sampling="template_only",
    debug=False,
)
Parameter Required? Description
project Recommended Groups events under a SpendLens dashboard project.
api_key Yes Your SpendLens API key. You can pass it directly or set SPENDLENS_API_KEY.
api_base_url Recommended SpendLens API URL. Use https://spendlensai.dev/backend for production. Do not add /v1.
provider Optional Use only if auto-detection fails. Valid values are openai and anthropic.
prompt_sampling Optional Controls prompt metadata. Valid values are off, template_only, and redacted_sample.
debug Optional Emits generic SDK warnings while troubleshooting. API keys and full prompts are not logged.

Environment variables

You can configure SpendLens without hard-coding settings:

SPENDLENS_API_KEY=sli_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SPENDLENS_PROJECT=shopping-assistant
SPENDLENS_API_BASE_URL=https://spendlensai.dev/backend
SPENDLENS_DISABLE=0

Set SPENDLENS_DISABLE=1 to return the original OpenAI or Anthropic client unchanged and disable SpendLens network calls.

Endpoint labels

Add a stable business endpoint when you want cleaner dashboard grouping:

with client.tag(endpoint="recommend-products"):
    response = client.chat.completions.create(...)

with client.tag(endpoint="recommend-products"):
    response = client.messages.create(...)

Good endpoint names are short workload names such as recommend-products, classify-review, summarize-ticket, or support-rag-answer.

Task overrides

Automatic workload classification is the default. Advanced users can override a task when they already know the correct category:

with client.tag(endpoint="recommend-products", task="generation"):
    response = client.chat.completions.create(...)

Task overrides are optional. Without an endpoint, the SDK captures the Python function name, file name, and call line so SpendLens can form a stable workload key.

Metadata

Use metadata for low-risk operational labels:

with client.tag(
    endpoint="recommend-products",
    metadata={"customer_tier": "enterprise", "region": "us"}
):
    response = client.chat.completions.create(...)

Do not put prompts, API keys, emails, or personal data in metadata. Nested tags are supported; inner endpoint, task, and metadata values take precedence.

Cache Efficiency

No manual cache calculation is needed. SpendLens reads provider cache counters when they are returned:

  • OpenAI: usage.prompt_tokens_details.cached_tokens
  • Anthropic: usage.cache_read_input_tokens and usage.cache_creation_input_tokens

After sending events, open the Cache Efficiency report in your SpendLens dashboard. If provider responses do not contain cache fields, SpendLens shows that cache metrics are unavailable instead of inventing values.

For short scripts and one-off tests, call client.flush() before the process exits so queued events are sent immediately.

Prompt privacy

prompt_sampling="template_only" is the default:

client = track(
    openai_client,
    project="shopping-assistant",
    api_key="sli_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    api_base_url="https://spendlensai.dev/backend",
    prompt_sampling="template_only",
)
  • off: sends no prompt content.
  • template_only: sends only OpenAI system messages or Anthropic's system argument.
  • redacted_sample: sends privacy-filtered prompt metadata where supported.

Full user messages and full message arrays are never sent by default. Prompt content and API keys are never written to SDK logs.

Failure behavior

Tracking failures are silent by default and never replace a provider response. Set debug=True to emit generic SDK warnings without API keys or full prompt content.

SpendLens AI is an observability and cost-insight layer. It does not proxy LLM traffic, rewrite prompts, route model calls, or automatically change your production models.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spendlensai-0.1.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spendlensai-0.1.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file spendlensai-0.1.1.tar.gz.

File metadata

  • Download URL: spendlensai-0.1.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for spendlensai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 71876583f6e7a62549df76a605b333443e8fca8cce11c70790745812df367930
MD5 e1d42ffa343e084c514ae80f5adb6221
BLAKE2b-256 b73757d3ec57baea0ecc0e4460c9cbe8ff7088bd67dfc8578c470d786a781696

See more details on using hashes here.

File details

Details for the file spendlensai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: spendlensai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for spendlensai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c79096a8475fe36e971f2e56eed3d151d82d67b24759e1c0f06c6f45bd8726d8
MD5 94da057639e88cf47833d644da300257
BLAKE2b-256 3d4061a9f421f796a8f7f1e1abe33ea0548d5c839677515447c9992128964781

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page