Drop-in LLM API cost optimization SDK & local developer observability platform.
Stop paying for redundant LLM calls. Intercept, route, cache, and optimize prompt spend before requests hit paid APIs.
# CostOpt — Cost-Aware Local Development & Feature-Level LLM Cost Attribution ⚡
Don't wait for a $500 monthly cloud bill to figure out where your LLM budget went. CostOpt puts real-time cost attribution directly into your local development loop—giving you feature-level spend visibility, lexical response caching, and automated optimization recommendations before code ships.
💡 Why CostOpt?
Most LLM gateways operate in production infrastructure after code is shipped. CostOpt operates in your local development loop:
- Feature-Level Cost Attribution: Group LLM calls by feature or component (
feature="rag_summarizer") to track feature unit economics before shipping. - Local Lexical Cache: High-speed token & n-gram similarity cache returning sub-2ms latency and $0.00 cost on repeated prompts.
- Silent Loop Circuit Breaker: Detects rapid call loops (>15 calls in 30s) and trips
CostOptCircuitBreakerErrorlocally to stop runaway billing leaks before they happen. - Zero-Downtime Outage Failover: Automatically reroutes queries to multi-provider fallbacks (
gpt-4o→claude-3-5-sonnetor localollama/llama3) when primary providers return 429 Rate Limits or 503 Outages. - Zero-Churn 1-Line SDK Interception: Patches standard OpenAI client calls with zero architectural refactoring.
🖥️ Developer Observability Console
Live System Overview displaying spend metrics, vector cache hits, optimization recommendations, and prompt interception logs.
Dedicated Trace Explorer auditing prompt MD5 hashes, response latencies, model rerouting decisions, and status code badges.
🏗️ Architecture & Request Flow
graph TD
App["💻 Application Code"] -->|client.chat.completions.create| Interceptor["⚡ CostOpt Middleware"]
Interceptor -->|1. Vector Cosine Lookup| Cache{"💾 SQLite Vector Cache"}
Cache -->|Cache HIT 0ms / $0.0| App
Cache -->|Cache MISS| Router{"🧠 Complexity Router"}
Router -->|Simple Query| MiniModel["🚀 Mini / Local Ollama ($0.0)"]
Router -->|Complex Query| OriginalModel["🌐 Cloud Provider API ($$$)"]
MiniModel --> Telemetry["📊 Local SQLite Telemetry Logger"]
OriginalModel --> Telemetry
Telemetry --> Dashboard["🖥️ Local Observability Dashboard (Port 8000)"]
🚀 Quickstart
1. Installation
pip install costopt
2. Basic Integration
from openai import OpenAI
from costopt import CostOpt
# Wrap standard client in one line
client = CostOpt(OpenAI(api_key="your-api-key"))
# Requests are automatically intercepted, cached, and optimized!
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Classify sentiment: I love python!"}]
)
3. Launch Observability Dashboard
costopt dashboard
Open http://localhost:8000 in your browser to view real-time spend analytics, trace logs, and policy rules!
4. Integration with Popular Frameworks
CostOpt wraps standard OpenAI-compatible client instances in 1 line:
LangChain:
from langchain_openai import ChatOpenAI
from costopt import CostOpt
# Wrap underlying client
llm = ChatOpenAI(client=CostOpt(OpenAI()).client)
LlamaIndex:
from llama_index.llms.openai import OpenAI as LlamaOpenAI
from costopt import CostOpt
llm = LlamaOpenAI(client=CostOpt(OpenAI()).client)
FastAPI Middleware Integration:
from fastapi import FastAPI
from openai import OpenAI
from costopt import CostOpt
app = FastAPI()
ai_client = CostOpt(OpenAI())
🔌 VS Code Extension — Cost Intelligence While You Code
CostOpt includes a native VS Code Extension that brings real-time LLM cost intelligence directly into your editor:
-
Install CostOpt & VS Code Extension:
pip install costopt
Install
CostOptfrom the VS Code Extensions Marketplace or loadvscode-extension/. -
Start Local Service:
costopt dashboard -
Make LLM Calls: As you call your LLM code locally or run test suites, CostOpt displays:
- Live CodeLens: Line-level cost (
CostOpt: ~$0.012 / request | Avg tokens: 3,421 | Calls: 184) directly above your code. - Hover Panels: Rich, compact cost breakdowns when hovering over
client.chat.completions.create(). - Status Bar Item: Compact daily spend tracker (
CostOpt: $8.42 today). - Feature Attribution Sidebar: Breakdown of spend grouped by feature (
@costopt.track("customer_support")orfeature="summarizer"). - Cost Forecast & Warnings: Spend forecasting and cost drift warnings directly in the Problems panel.
- Live CodeLens: Line-level cost (
🔧 Configuration Guide
Custom Models & User Local Overrides
Track custom, fine-tuned, or local models by dropping a .yaml file into your project:
provider: "ollama"
models:
deepseek-r1:
input_cost_per_1m: 0.0
output_cost_per_1m: 0.0
Pass the pricing directory:
client = CostOpt(OpenAI(), pricing_dir="./my_pricing")
🛡️ Security Audit
CostOpt has undergone automated penetration testing for SQL injections, CORS misconfigurations, and rate-limiting DB locks. See the full audit report at docs/SECURITY_AUDIT.md.
📄 License
This project is licensed under the MIT License. See LICENSE for details.
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 costopt-0.1.2.tar.gz.
File metadata
- Download URL: costopt-0.1.2.tar.gz
- Upload date:
- Size: 46.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35794f62ec31bad446f1beec64a3f90127c3ded934aa722d8a8c46d680872e15
|
|
| MD5 |
752a2a6717d8c0245c40c78fb332da4a
|
|
| BLAKE2b-256 |
32d138ad5ee3fad79f9815006840e8555d2d13840c43298a53672cc59eef146a
|
File details
Details for the file costopt-0.1.2-py3-none-any.whl.
File metadata
- Download URL: costopt-0.1.2-py3-none-any.whl
- Upload date:
- Size: 32.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e223bd879ceccaa88f90f10eb406fa7823ee34a075a74419cb9553d65ebc07b2
|
|
| MD5 |
b47c7c5e20e1080b4289f303b255cd5e
|
|
| BLAKE2b-256 |
b280d0665b17a5614b65e154e510fab977d374cedbfef6fd7393105336e3a9c4
|