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 examplescustomer_attribution.py- Customer attribution examplesanthropic_usage.py- Anthropic integrationbedrock_usage.py- AWS Bedrock integrationflask_integration.py- Flask web app integrationfastapi_integration.py- FastAPI integrationstreaming_example.py- Streaming responsesmulti_provider.py- Multiple providersnextjs-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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
657b642ec844cd8b502240af28e21c6d395a06ae0e1b89d8f1cb4afaaa7c2ec6
|
|
| MD5 |
4b13623484b5eb6f47b41e986c5bb0f1
|
|
| BLAKE2b-256 |
87019400ab452766a593763e2a73a979dd740e3ff19737f0527343aa94941b7c
|
Provenance
The following attestation bundles were made for langspend-1.0.0.tar.gz:
Publisher:
publish.yml on LangSpend/langspend-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langspend-1.0.0.tar.gz -
Subject digest:
657b642ec844cd8b502240af28e21c6d395a06ae0e1b89d8f1cb4afaaa7c2ec6 - Sigstore transparency entry: 653441810
- Sigstore integration time:
-
Permalink:
LangSpend/langspend-python-sdk@7d34c6724781c9651aa0dc45e582bbe010f3f98f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/LangSpend
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7d34c6724781c9651aa0dc45e582bbe010f3f98f -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00ac5442d84e8533201668372e00338be81ebaf176f99e5d5e099cb966e16e5e
|
|
| MD5 |
f086e6faefae538fe8618b15e434db69
|
|
| BLAKE2b-256 |
c4f1da1fedaa487e88102c67b9cde69a2569a67dd714580a3c43126738bb9612
|
Provenance
The following attestation bundles were made for langspend-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on LangSpend/langspend-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langspend-1.0.0-py3-none-any.whl -
Subject digest:
00ac5442d84e8533201668372e00338be81ebaf176f99e5d5e099cb966e16e5e - Sigstore transparency entry: 653441823
- Sigstore integration time:
-
Permalink:
LangSpend/langspend-python-sdk@7d34c6724781c9651aa0dc45e582bbe010f3f98f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/LangSpend
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7d34c6724781c9651aa0dc45e582bbe010f3f98f -
Trigger Event:
push
-
Statement type: