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

🔧 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.1.tar.gz (42.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.1-py3-none-any.whl (28.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: costopt-0.1.1.tar.gz
  • Upload date:
  • Size: 42.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.1.tar.gz
Algorithm Hash digest
SHA256 4377740fc6401d6bbd9d6c6045b92afee00cedb75c83dfcba8b66ca36e189a33
MD5 accbec86ebdb597b9ede043582723acf
BLAKE2b-256 9fe2069c76bc86f66e5b02dadcedd35eb8b5e98ceea86c11c83bc2aa130bcad0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: costopt-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48c5a42d3222e535b49dc870beee3d4a112c3f9c93ed346fe7bd7a9de3ec0a16
MD5 fc67c9f09b148b2552624f8e22525000
BLAKE2b-256 e3db725cb0e1aa740f548c9af1f75894b44399995b87721de063d34865d967e7

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