Python
Overview
The Python SDK is the most complete dexcost SDK, auto-instrumenting 6 LLM providers and 5 HTTP libraries with a built-in CLI.
The Python SDK is the most complete dexcost SDK. It auto-instruments 6 LLM providers, patches 5 HTTP libraries for non-LLM cost capture, and ships a CLI to scan your codebase for untracked cost points. Drop two lines into your application and every LLM call lands with customer attribution, retry tracking, and exact-precision cost — no changes to existing call sites required.
Key capabilities
- LLM cost capture — auto-patches OpenAI, Anthropic, LiteLLM, Google Gemini, AWS Bedrock, and Cohere; records provider, model, input/output tokens, latency, and computed cost for every call inside a tracked task. Model Context Protocol (MCP) tool calls are instrumented as well.
- HTTP / non-LLM cost capture — patches
requests,httpx,aiohttp,botocore, andurllib3; extracts costs from the bundled 163-service catalog covering Pinecone, Stripe, Twilio, SendGrid, Firecrawl, Exa Search, and more. - Retry tracking — detects and flags retry events, accumulates retry-waste cost, and attributes each retry to a root cause (rate limit, timeout, 5xx).
- Customer / project / task attribution —
dexcost.set_context()propagatescustomer_id,project_id, and optional metadata throughcontextvarsacrossawaitpoints andThreadPoolExecutorboundaries. - Local SQLite buffer + background sync — events are written to a local SQLite file first; a background
SyncWorkerflushes them to the Control Layer in configurable batches, so data is never lost on a crash. - Pricing engine — bundled model pricing is available offline and refreshes from the Control Layer in the background; custom per-model rates can be registered at any time.
Requirements
Python 3.10 or later.
Next steps
- Installation —
pip install dexcostand optional provider extras. - Quickstart — two-line initialization and your first tracked call.
- Instrumentation — LLM provider details, HTTP tracking, and controlling what gets patched.
- Configuration — all
dexcost.init()parameters, environment variables, and privacy options. - API Reference — full public API:
init,set_context,task,record_cost,flush, and more.