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 License Python Versions Code Style


📌 Key Capabilities

  • Zero-Churn 1-Line SDK Interception: Drop-in wrapper patches standard completion clients (client = CostOpt(OpenAI())) with zero modifications to existing calling code.
  • Automated Cost Optimization: Intelligent intent detection automatically routes lightweight queries (like sentiment or text formatting) from expensive models (gpt-4o) to low-cost models (gpt-4o-mini or local llama3), preserving response quality while cutting spend.
  • Lexical Similarity Cache: High-speed token & character n-gram similarity cache returns sub-2ms latency, $0.00 cost on repeated or similar prompts.
  • Local & Offline Model Support: Seamlessly route to local Ollama models (llama3, mistral, deepseek-r1, qwen2.5) for 100% free offline execution.
  • 100% Private Local Telemetry: Logs financial metrics, latency distributions, and MD5 trace hashes to a local SQLite database—zero data shared with third-party servers.

🖥️ 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 git+https://github.com/khusshdesai/CostOpt.git

PyPI release coming soon — track progress in Issues.

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())

🔧 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.0.tar.gz (41.5 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.0-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file costopt-0.1.0.tar.gz.

File metadata

  • Download URL: costopt-0.1.0.tar.gz
  • Upload date:
  • Size: 41.5 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.0.tar.gz
Algorithm Hash digest
SHA256 eabb08830667f754cfcc5a68beeb75720eb18e09d71fc1ef9ee61eae23fe19e9
MD5 de8b09283658a763ab85d1709ffdb9eb
BLAKE2b-256 240118497885450d834f4945bba58e5684b4ce80ed30c2f21db982e3544ca5ea

See more details on using hashes here.

File details

Details for the file costopt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: costopt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42f5aaec5f55275a60cfe7792adabd7879e206e5a5814208ff9407963d10b7d2
MD5 3812b67d1cab108f2293329897033214
BLAKE2b-256 25861a70acb3baec7678e77af05a513699151f3e1ffadf988cf9eb723f864284

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