Skip to main content


CostOpt Logo Badge

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.

Build Status # CostOpt — Cost-Aware Local Development & Feature-Level LLM Cost Attribution ⚡

PyPI Version License: MIT Build Status Python 3.10+

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:

  1. Feature-Level Cost Attribution: Group LLM calls by feature or component (feature="rag_summarizer") to track feature unit economics before shipping.
  2. Local Lexical Cache: High-speed token & n-gram similarity cache returning sub-2ms latency and $0.00 cost on repeated prompts.
  3. Silent Loop Circuit Breaker: Detects rapid call loops (>15 calls in 30s) and trips CostOptCircuitBreakerError locally to stop runaway billing leaks before they happen.
  4. Zero-Downtime Outage Failover: Automatically reroutes queries to multi-provider fallbacks (gpt-4oclaude-3-5-sonnet or local ollama/llama3) when primary providers return 429 Rate Limits or 503 Outages.
  5. Zero-Churn 1-Line SDK Interception: Patches standard OpenAI client calls with zero architectural refactoring.

🖥️ Developer Observability Console

CostOpt Developer Observability Console

Live System Overview displaying spend metrics, vector cache hits, optimization recommendations, and prompt interception logs.


Full-Screen Trace Explorer

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:

  1. Install CostOpt & VS Code Extension:

    pip install costopt
    

    Install CostOpt from the VS Code Extensions Marketplace or load vscode-extension/.

  2. Start Local Service:

    costopt dashboard
    
  3. 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") or feature="summarizer").
    • Cost Forecast & Warnings: Spend forecasting and cost drift warnings directly in the Problems panel.

🔧 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

costopt-0.1.2.tar.gz (46.8 kB view details)

Uploaded Source

Built Distribution

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

costopt-0.1.2-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

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

Hashes for costopt-0.1.2.tar.gz
Algorithm Hash digest
SHA256 35794f62ec31bad446f1beec64a3f90127c3ded934aa722d8a8c46d680872e15
MD5 752a2a6717d8c0245c40c78fb332da4a
BLAKE2b-256 32d138ad5ee3fad79f9815006840e8555d2d13840c43298a53672cc59eef146a

See more details on using hashes here.

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

Hashes for costopt-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e223bd879ceccaa88f90f10eb406fa7823ee34a075a74419cb9553d65ebc07b2
MD5 b47c7c5e20e1080b4289f303b255cd5e
BLAKE2b-256 b280d0665b17a5614b65e154e510fab977d374cedbfef6fd7393105336e3a9c4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page