Skip to main content

LangSpend Python SDK - Track LLM costs and usage with customer attribution. Requires LangSpend API key for production use.

Project description

LangSpend Python SDK

Track LLM costs and usage with customer attribution for OpenAI, Anthropic, AWS Bedrock, and Google Vertex AI.

Features

  • Customer Attribution: Track costs per customer and feature
  • Zero Code Changes: Wrap your existing LLM clients
  • Multi-Provider Support: OpenAI, Anthropic, AWS Bedrock, Google Vertex AI
  • Streaming Support: Works with streaming responses
  • Async Support: Full async/await compatibility
  • Error Resilience: Never breaks your LLM calls

Installation

Using pip

pip install langspend

Using uv (recommended)

uv add langspend

For specific providers

# Using pip
pip install langspend[openai]      # OpenAI support
pip install langspend[anthropic]   # Anthropic support
pip install langspend[bedrock]     # AWS Bedrock support
pip install langspend[vertex]      # Google Vertex AI support
pip install langspend[all]         # All providers

# Using uv
uv add "langspend[openai]"         # OpenAI support
uv add "langspend[anthropic]"      # Anthropic support
uv add "langspend[bedrock]"        # AWS Bedrock support
uv add "langspend[vertex]"         # Google Vertex AI support
uv add "langspend[all]"            # All providers

Get Your API Key

LangSpend is a commercial SaaS product with a free tier available.

Get your LangSpend API key at langspend.com - Sign up here

Note: An API key is required for production use. The SDK includes a free tier for development and evaluation.

Quick Start

Option 1: Customer and Feature Cost Tracking (Recommended)

from langspend import LangSpend, wrap_openai
from openai import OpenAI

# Initialize LangSpend
langspend = LangSpend(api_key="lsp_your_api_key_here")

# Wrap your OpenAI client
openai = wrap_openai(
    OpenAI(api_key="your_openai_api_key"),
    langspend
)

# Use as normal - tracking happens automatically
response = openai.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    langspend_tags={
        "customer": {
            "id": "user_123",
            "name": "John Doe",
            "email": "john@example.com"
        },
        "feature": {
            "name": "chat-api"
        }
    }
)

Option 2: Zero Code Setup (Quick, no customer level tracking)

from langspend import LangSpend, wrap_openai
from openai import OpenAI

# Initialize LangSpend
langspend = LangSpend(api_key="lsp_your_api_key_here")

# Wrap your OpenAI client
openai = wrap_openai(OpenAI(), langspend)

# Use as normal - tracking happens automatically
response = openai.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

Supported Providers

LangSpend supports OpenAI, Anthropic, and AWS Bedrock. See the examples directory for integration examples with each provider.

Demo Application

A complete Next.js + FastAPI demo is available in examples/nextjs-chat-demo/:

cd examples/nextjs-chat-demo/backend
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt
cp env.example .env
# Edit .env with your API keys
python main.py

Examples

See the examples/ directory for comprehensive usage examples:

  • basic_usage.py - Basic usage examples
  • customer_attribution.py - Customer attribution examples
  • anthropic_usage.py - Anthropic integration
  • bedrock_usage.py - AWS Bedrock integration
  • flask_integration.py - Flask web app integration
  • fastapi_integration.py - FastAPI integration
  • streaming_example.py - Streaming responses
  • multi_provider.py - Multiple providers
  • nextjs-chat-demo/ - Complete Next.js + FastAPI demo

Configuration

from langspend import LangSpend

langspend = LangSpend(
    api_key="lsp_your_api_key",
    base_url="https://platform.langspend.com",  # Optional
    debug=False,  # Enable debug logging
    max_retries=3  # Max retry attempts
)

Error Handling

The SDK never throws errors for tracking failures. All tracking happens asynchronously and failures are silent by default.

Enable debug mode to log tracking errors:

langspend = LangSpend(api_key="lsp_your_api_key", debug=True)

License

MIT License - see LICENSE file for details.

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

langspend-1.0.0.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

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

langspend-1.0.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file langspend-1.0.0.tar.gz.

File metadata

  • Download URL: langspend-1.0.0.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for langspend-1.0.0.tar.gz
Algorithm Hash digest
SHA256 657b642ec844cd8b502240af28e21c6d395a06ae0e1b89d8f1cb4afaaa7c2ec6
MD5 4b13623484b5eb6f47b41e986c5bb0f1
BLAKE2b-256 87019400ab452766a593763e2a73a979dd740e3ff19737f0527343aa94941b7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for langspend-1.0.0.tar.gz:

Publisher: publish.yml on LangSpend/langspend-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langspend-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: langspend-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for langspend-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00ac5442d84e8533201668372e00338be81ebaf176f99e5d5e099cb966e16e5e
MD5 f086e6faefae538fe8618b15e434db69
BLAKE2b-256 c4f1da1fedaa487e88102c67b9cde69a2569a67dd714580a3c43126738bb9612

See more details on using hashes here.

Provenance

The following attestation bundles were made for langspend-1.0.0-py3-none-any.whl:

Publisher: publish.yml on LangSpend/langspend-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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