Installation
Install the SDK from PyPI using pip:openai, anthropic, or google-genai), the base pip install costrace-sdk is all you need — Costrace will detect and patch any installed providers automatically.
Requires Python 3.8 or higher.
How It Works
Costrace uses monkey-patching to wrap your existing LLM client methods. When you callcostrace.init(), it automatically patches the clients for any installed providers — no code changes needed on your end.
Every LLM call is intercepted to capture token usage, latency, and cost, then a trace is sent to the Costrace backend in a background thread so your application is never blocked.
Quick Start
Configuration
Custom Endpoint
Point to a self-hosted or local backend:Supported Providers
OpenAI
- GPT-5 family:
gpt-5.2,gpt-5,gpt-5-mini,gpt-5-nano - GPT-4 family:
gpt-4o,gpt-4o-mini,gpt-4.1,gpt-4-turbo,gpt-4 - GPT-3.5:
gpt-3.5-turbo - Other:
o3,o4-mini
Anthropic
- Opus:
claude-opus-4-6,claude-opus-4-1-20250805,claude-opus-4-20250514 - Sonnet:
claude-sonnet-4-6,claude-sonnet-4-5-20250929,claude-sonnet-4-20250514,claude-3-7-sonnet-20250219 - Haiku:
claude-haiku-4-5-20251001,claude-3-haiku-20240307
Google Gemini
- Gemini 2.0:
gemini-2.0-flash,gemini-2.0-flash-lite - Gemini 1.5:
gemini-1.5-pro,gemini-1.5-flash,gemini-1.5-flash-8b
What Gets Tracked
Every LLM API call sends a trace containing:atexit handler to wait up to 10 seconds for any pending traces before your process exits.
Requirements
- Python: 3.8 or higher
- Core dependency:
requests— installed automatically - Provider SDKs: Optional, install only the ones you use
| Extra | Installs | Minimum Version |
|---|---|---|
openai | openai | >=2.21.0 |
anthropic | anthropic | >=0.82.0 |
gemini | google-genai | >=1.64.0 |
all | All of the above | — |
Troubleshooting
No traces appearing in dashboard
- Check that
costrace.init()is called before creating LLM clients - Verify your API key is correct
- Check for error messages in console output
Traces not being sent
The SDK silently catches network errors when sending traces. This is intentional — trace failures should never break your application. Check your network connectivity and ensure the Costrace API endpoint is reachable.Provider not being tracked
If a provider isn’t being tracked, make sure:- The provider SDK is installed (e.g.
pip install openai) costrace.init()is called before creating the client instance- You’re using a supported model from the lists above