costopt 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. costopt-0.1.0/LICENSE +21 -0
  2. costopt-0.1.0/MANIFEST.in +6 -0
  3. costopt-0.1.0/PKG-INFO +200 -0
  4. costopt-0.1.0/README.md +178 -0
  5. costopt-0.1.0/costopt.yaml +21 -0
  6. costopt-0.1.0/dashboard/app.js +641 -0
  7. costopt-0.1.0/dashboard/index.html +401 -0
  8. costopt-0.1.0/dashboard/style.css +681 -0
  9. costopt-0.1.0/pricing/providers/anthropic.yaml +50 -0
  10. costopt-0.1.0/pricing/providers/google.yaml +35 -0
  11. costopt-0.1.0/pricing/providers/huggingface.yaml +44 -0
  12. costopt-0.1.0/pricing/providers/ollama.yaml +50 -0
  13. costopt-0.1.0/pricing/providers/openai.yaml +57 -0
  14. costopt-0.1.0/pricing/schema.yaml +35 -0
  15. costopt-0.1.0/pyproject.toml +45 -0
  16. costopt-0.1.0/setup.cfg +4 -0
  17. costopt-0.1.0/src/costopt/__init__.py +6 -0
  18. costopt-0.1.0/src/costopt/anomaly.py +110 -0
  19. costopt-0.1.0/src/costopt/api/routes.py +354 -0
  20. costopt-0.1.0/src/costopt/api/server.py +56 -0
  21. costopt-0.1.0/src/costopt/cache.py +180 -0
  22. costopt-0.1.0/src/costopt/client.py +307 -0
  23. costopt-0.1.0/src/costopt/generator.py +226 -0
  24. costopt-0.1.0/src/costopt/main.py +71 -0
  25. costopt-0.1.0/src/costopt/pricing.py +118 -0
  26. costopt-0.1.0/src/costopt/router.py +114 -0
  27. costopt-0.1.0/src/costopt/telemetry.py +136 -0
  28. costopt-0.1.0/src/costopt.egg-info/PKG-INFO +200 -0
  29. costopt-0.1.0/src/costopt.egg-info/SOURCES.txt +31 -0
  30. costopt-0.1.0/src/costopt.egg-info/dependency_links.txt +1 -0
  31. costopt-0.1.0/src/costopt.egg-info/entry_points.txt +2 -0
  32. costopt-0.1.0/src/costopt.egg-info/requires.txt +11 -0
  33. costopt-0.1.0/src/costopt.egg-info/top_level.txt +1 -0
costopt-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 LLM CostOpt Authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ include LICENSE
2
+ include README.md
3
+ include costopt.yaml
4
+ recursive-include pricing *.yaml
5
+ recursive-include dashboard *.html *.css *.js
6
+ recursive-include src *.py
costopt-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,200 @@
1
+ Metadata-Version: 2.4
2
+ Name: costopt
3
+ Version: 0.1.0
4
+ Summary: Drop-in LLM API cost optimization SDK with lexical similarity caching, smart model routing, and local observability dashboard
5
+ License-Expression: MIT
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: openai>=1.0.0
12
+ Requires-Dist: fastapi>=0.100.0
13
+ Requires-Dist: uvicorn>=0.20.0
14
+ Requires-Dist: pyyaml>=6.0.0
15
+ Requires-Dist: pydantic>=2.0.0
16
+ Requires-Dist: tiktoken>=0.5.0
17
+ Provides-Extra: dev
18
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
19
+ Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
20
+ Requires-Dist: requests>=2.31.0; extra == "dev"
21
+ Dynamic: license-file
22
+
23
+ <p align="center">
24
+ <br />
25
+ <img src="docs/images/costopt_logo.png" width="140" alt="CostOpt Logo Badge" />
26
+ <br />
27
+ <br />
28
+ <p align="center">
29
+ <strong>Drop-in LLM API cost optimization SDK & local developer observability platform.</strong>
30
+ <br />
31
+ Stop paying for redundant LLM calls. Intercept, route, cache, and optimize prompt spend <em>before</em> requests hit paid APIs.
32
+ </p>
33
+ </p>
34
+
35
+ <p align="center">
36
+ <a href="https://github.com/khusshdesai/CostOpt/actions"><img src="https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square" alt="Build Status"></a>
37
+ <a href="https://github.com/khusshdesai/CostOpt/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square" alt="License"></a>
38
+ <a href="https://pypi.org/project/costopt/"><img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue.svg?style=flat-square" alt="Python Versions"></a>
39
+ <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square" alt="Code Style"></a>
40
+ </p>
41
+
42
+ <p align="center">
43
+ <img src="https://img.shields.io/badge/OpenAI-supported-412991?style=for-the-badge&logo=openai&logoColor=white" />
44
+ <img src="https://img.shields.io/badge/Anthropic-supported-D97706?style=for-the-badge&logo=anthropic&logoColor=white" />
45
+ <img src="https://img.shields.io/badge/Google_Gemini-supported-4285F4?style=for-the-badge&logo=google&logoColor=white" />
46
+ <img src="https://img.shields.io/badge/Hugging_Face-supported-FFD21E?style=for-the-badge&logo=huggingface&logoColor=black" />
47
+ <img src="https://img.shields.io/badge/Ollama_(Local)-supported-000000?style=for-the-badge&logo=ollama&logoColor=white" />
48
+ <img src="https://img.shields.io/badge/FastAPI-supported-009688?style=for-the-badge&logo=fastapi&logoColor=white" />
49
+ </p>
50
+
51
+ ---
52
+
53
+ ## 📌 Key Capabilities
54
+
55
+ - **Zero-Churn 1-Line SDK Interception**: Drop-in wrapper patches standard completion clients (`client = CostOpt(OpenAI())`) with zero modifications to existing calling code.
56
+ - **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.
57
+ - **Lexical Similarity Cache**: High-speed token & character n-gram similarity cache returns **sub-2ms latency, $0.00 cost** on repeated or similar prompts.
58
+ - **Local & Offline Model Support**: Seamlessly route to local **Ollama** models (`llama3`, `mistral`, `deepseek-r1`, `qwen2.5`) for 100% free offline execution.
59
+ - **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.
60
+
61
+ ---
62
+
63
+ ## 🖥️ Developer Observability Console
64
+
65
+ <p align="center">
66
+ <img src="docs/images/dashboard_overview.png" width="100%" alt="CostOpt Developer Observability Console" />
67
+ </p>
68
+
69
+ <p align="center">
70
+ <em>Live System Overview displaying spend metrics, vector cache hits, optimization recommendations, and prompt interception logs.</em>
71
+ </p>
72
+
73
+ <br />
74
+
75
+ <p align="center">
76
+ <img src="docs/images/trace_explorer.png" width="100%" alt="Full-Screen Trace Explorer" />
77
+ </p>
78
+
79
+ <p align="center">
80
+ <em>Dedicated Trace Explorer auditing prompt MD5 hashes, response latencies, model rerouting decisions, and status code badges.</em>
81
+ </p>
82
+
83
+ ## 🏗️ Architecture & Request Flow
84
+
85
+ ```mermaid
86
+ graph TD
87
+ App["💻 Application Code"] -->|client.chat.completions.create| Interceptor["⚡ CostOpt Middleware"]
88
+
89
+ Interceptor -->|1. Vector Cosine Lookup| Cache{"💾 SQLite Vector Cache"}
90
+ Cache -->|Cache HIT 0ms / $0.0| App
91
+
92
+ Cache -->|Cache MISS| Router{"🧠 Complexity Router"}
93
+ Router -->|Simple Query| MiniModel["🚀 Mini / Local Ollama ($0.0)"]
94
+ Router -->|Complex Query| OriginalModel["🌐 Cloud Provider API ($$$)"]
95
+
96
+ MiniModel --> Telemetry["📊 Local SQLite Telemetry Logger"]
97
+ OriginalModel --> Telemetry
98
+ Telemetry --> Dashboard["🖥️ Local Observability Dashboard (Port 8000)"]
99
+ ```
100
+
101
+ ---
102
+
103
+ ## 🚀 Quickstart
104
+
105
+ ### 1. Installation
106
+
107
+ ```bash
108
+ pip install git+https://github.com/khusshdesai/CostOpt.git
109
+ ```
110
+
111
+ > **PyPI release coming soon** — track progress in [Issues](https://github.com/khusshdesai/CostOpt/issues).
112
+
113
+ ### 2. Basic Integration
114
+
115
+ ```python
116
+ from openai import OpenAI
117
+ from costopt import CostOpt
118
+
119
+ # Wrap standard client in one line
120
+ client = CostOpt(OpenAI(api_key="your-api-key"))
121
+
122
+ # Requests are automatically intercepted, cached, and optimized!
123
+ response = client.chat.completions.create(
124
+ model="gpt-4o",
125
+ messages=[{"role": "user", "content": "Classify sentiment: I love python!"}]
126
+ )
127
+ ```
128
+
129
+ ### 3. Launch Observability Dashboard
130
+
131
+ ```bash
132
+ costopt dashboard
133
+ ```
134
+
135
+ Open **`http://localhost:8000`** in your browser to view real-time spend analytics, trace logs, and policy rules!
136
+
137
+ ### 4. Integration with Popular Frameworks
138
+
139
+ CostOpt wraps standard OpenAI-compatible client instances in 1 line:
140
+
141
+ **LangChain**:
142
+ ```python
143
+ from langchain_openai import ChatOpenAI
144
+ from costopt import CostOpt
145
+
146
+ # Wrap underlying client
147
+ llm = ChatOpenAI(client=CostOpt(OpenAI()).client)
148
+ ```
149
+
150
+ **LlamaIndex**:
151
+ ```python
152
+ from llama_index.llms.openai import OpenAI as LlamaOpenAI
153
+ from costopt import CostOpt
154
+
155
+ llm = LlamaOpenAI(client=CostOpt(OpenAI()).client)
156
+ ```
157
+
158
+ **FastAPI Middleware Integration**:
159
+ ```python
160
+ from fastapi import FastAPI
161
+ from openai import OpenAI
162
+ from costopt import CostOpt
163
+
164
+ app = FastAPI()
165
+ ai_client = CostOpt(OpenAI())
166
+ ```
167
+
168
+ ---
169
+
170
+ ## 🔧 Configuration Guide
171
+
172
+ ### Custom Models & User Local Overrides
173
+
174
+ Track custom, fine-tuned, or local models by dropping a `.yaml` file into your project:
175
+
176
+ ```yaml
177
+ provider: "ollama"
178
+ models:
179
+ deepseek-r1:
180
+ input_cost_per_1m: 0.0
181
+ output_cost_per_1m: 0.0
182
+ ```
183
+
184
+ Pass the pricing directory:
185
+
186
+ ```python
187
+ client = CostOpt(OpenAI(), pricing_dir="./my_pricing")
188
+ ```
189
+
190
+ ---
191
+
192
+ ## 🛡️ Security Audit
193
+
194
+ 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`](docs/SECURITY_AUDIT.md).
195
+
196
+ ---
197
+
198
+ ## 📄 License
199
+
200
+ This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
@@ -0,0 +1,178 @@
1
+ <p align="center">
2
+ <br />
3
+ <img src="docs/images/costopt_logo.png" width="140" alt="CostOpt Logo Badge" />
4
+ <br />
5
+ <br />
6
+ <p align="center">
7
+ <strong>Drop-in LLM API cost optimization SDK & local developer observability platform.</strong>
8
+ <br />
9
+ Stop paying for redundant LLM calls. Intercept, route, cache, and optimize prompt spend <em>before</em> requests hit paid APIs.
10
+ </p>
11
+ </p>
12
+
13
+ <p align="center">
14
+ <a href="https://github.com/khusshdesai/CostOpt/actions"><img src="https://img.shields.io/badge/build-passing-brightgreen.svg?style=flat-square" alt="Build Status"></a>
15
+ <a href="https://github.com/khusshdesai/CostOpt/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square" alt="License"></a>
16
+ <a href="https://pypi.org/project/costopt/"><img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue.svg?style=flat-square" alt="Python Versions"></a>
17
+ <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square" alt="Code Style"></a>
18
+ </p>
19
+
20
+ <p align="center">
21
+ <img src="https://img.shields.io/badge/OpenAI-supported-412991?style=for-the-badge&logo=openai&logoColor=white" />
22
+ <img src="https://img.shields.io/badge/Anthropic-supported-D97706?style=for-the-badge&logo=anthropic&logoColor=white" />
23
+ <img src="https://img.shields.io/badge/Google_Gemini-supported-4285F4?style=for-the-badge&logo=google&logoColor=white" />
24
+ <img src="https://img.shields.io/badge/Hugging_Face-supported-FFD21E?style=for-the-badge&logo=huggingface&logoColor=black" />
25
+ <img src="https://img.shields.io/badge/Ollama_(Local)-supported-000000?style=for-the-badge&logo=ollama&logoColor=white" />
26
+ <img src="https://img.shields.io/badge/FastAPI-supported-009688?style=for-the-badge&logo=fastapi&logoColor=white" />
27
+ </p>
28
+
29
+ ---
30
+
31
+ ## 📌 Key Capabilities
32
+
33
+ - **Zero-Churn 1-Line SDK Interception**: Drop-in wrapper patches standard completion clients (`client = CostOpt(OpenAI())`) with zero modifications to existing calling code.
34
+ - **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.
35
+ - **Lexical Similarity Cache**: High-speed token & character n-gram similarity cache returns **sub-2ms latency, $0.00 cost** on repeated or similar prompts.
36
+ - **Local & Offline Model Support**: Seamlessly route to local **Ollama** models (`llama3`, `mistral`, `deepseek-r1`, `qwen2.5`) for 100% free offline execution.
37
+ - **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.
38
+
39
+ ---
40
+
41
+ ## 🖥️ Developer Observability Console
42
+
43
+ <p align="center">
44
+ <img src="docs/images/dashboard_overview.png" width="100%" alt="CostOpt Developer Observability Console" />
45
+ </p>
46
+
47
+ <p align="center">
48
+ <em>Live System Overview displaying spend metrics, vector cache hits, optimization recommendations, and prompt interception logs.</em>
49
+ </p>
50
+
51
+ <br />
52
+
53
+ <p align="center">
54
+ <img src="docs/images/trace_explorer.png" width="100%" alt="Full-Screen Trace Explorer" />
55
+ </p>
56
+
57
+ <p align="center">
58
+ <em>Dedicated Trace Explorer auditing prompt MD5 hashes, response latencies, model rerouting decisions, and status code badges.</em>
59
+ </p>
60
+
61
+ ## 🏗️ Architecture & Request Flow
62
+
63
+ ```mermaid
64
+ graph TD
65
+ App["💻 Application Code"] -->|client.chat.completions.create| Interceptor["⚡ CostOpt Middleware"]
66
+
67
+ Interceptor -->|1. Vector Cosine Lookup| Cache{"💾 SQLite Vector Cache"}
68
+ Cache -->|Cache HIT 0ms / $0.0| App
69
+
70
+ Cache -->|Cache MISS| Router{"🧠 Complexity Router"}
71
+ Router -->|Simple Query| MiniModel["🚀 Mini / Local Ollama ($0.0)"]
72
+ Router -->|Complex Query| OriginalModel["🌐 Cloud Provider API ($$$)"]
73
+
74
+ MiniModel --> Telemetry["📊 Local SQLite Telemetry Logger"]
75
+ OriginalModel --> Telemetry
76
+ Telemetry --> Dashboard["🖥️ Local Observability Dashboard (Port 8000)"]
77
+ ```
78
+
79
+ ---
80
+
81
+ ## 🚀 Quickstart
82
+
83
+ ### 1. Installation
84
+
85
+ ```bash
86
+ pip install git+https://github.com/khusshdesai/CostOpt.git
87
+ ```
88
+
89
+ > **PyPI release coming soon** — track progress in [Issues](https://github.com/khusshdesai/CostOpt/issues).
90
+
91
+ ### 2. Basic Integration
92
+
93
+ ```python
94
+ from openai import OpenAI
95
+ from costopt import CostOpt
96
+
97
+ # Wrap standard client in one line
98
+ client = CostOpt(OpenAI(api_key="your-api-key"))
99
+
100
+ # Requests are automatically intercepted, cached, and optimized!
101
+ response = client.chat.completions.create(
102
+ model="gpt-4o",
103
+ messages=[{"role": "user", "content": "Classify sentiment: I love python!"}]
104
+ )
105
+ ```
106
+
107
+ ### 3. Launch Observability Dashboard
108
+
109
+ ```bash
110
+ costopt dashboard
111
+ ```
112
+
113
+ Open **`http://localhost:8000`** in your browser to view real-time spend analytics, trace logs, and policy rules!
114
+
115
+ ### 4. Integration with Popular Frameworks
116
+
117
+ CostOpt wraps standard OpenAI-compatible client instances in 1 line:
118
+
119
+ **LangChain**:
120
+ ```python
121
+ from langchain_openai import ChatOpenAI
122
+ from costopt import CostOpt
123
+
124
+ # Wrap underlying client
125
+ llm = ChatOpenAI(client=CostOpt(OpenAI()).client)
126
+ ```
127
+
128
+ **LlamaIndex**:
129
+ ```python
130
+ from llama_index.llms.openai import OpenAI as LlamaOpenAI
131
+ from costopt import CostOpt
132
+
133
+ llm = LlamaOpenAI(client=CostOpt(OpenAI()).client)
134
+ ```
135
+
136
+ **FastAPI Middleware Integration**:
137
+ ```python
138
+ from fastapi import FastAPI
139
+ from openai import OpenAI
140
+ from costopt import CostOpt
141
+
142
+ app = FastAPI()
143
+ ai_client = CostOpt(OpenAI())
144
+ ```
145
+
146
+ ---
147
+
148
+ ## 🔧 Configuration Guide
149
+
150
+ ### Custom Models & User Local Overrides
151
+
152
+ Track custom, fine-tuned, or local models by dropping a `.yaml` file into your project:
153
+
154
+ ```yaml
155
+ provider: "ollama"
156
+ models:
157
+ deepseek-r1:
158
+ input_cost_per_1m: 0.0
159
+ output_cost_per_1m: 0.0
160
+ ```
161
+
162
+ Pass the pricing directory:
163
+
164
+ ```python
165
+ client = CostOpt(OpenAI(), pricing_dir="./my_pricing")
166
+ ```
167
+
168
+ ---
169
+
170
+ ## 🛡️ Security Audit
171
+
172
+ 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`](docs/SECURITY_AUDIT.md).
173
+
174
+ ---
175
+
176
+ ## 📄 License
177
+
178
+ This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
@@ -0,0 +1,21 @@
1
+ routing:
2
+ fallbacks:
3
+ claude-3-5-sonnet:
4
+ - gpt-4o
5
+ - claude-3-haiku
6
+ - llama3
7
+ gpt-4o:
8
+ - gpt-4o-mini
9
+ - llama3
10
+ - deepseek-r1
11
+ rules:
12
+ - name: "Simple text classification"
13
+ keywords: ["classify", "yes/no", "sentiment", "label", "extract"]
14
+ max_prompt_length: 500
15
+ original_model: "gpt-4o"
16
+ target_model: "gpt-4o-mini"
17
+ - name: "Local zero-cost offline processing"
18
+ keywords: ["local", "offline", "private", "draft", "summary"]
19
+ max_prompt_length: 2000
20
+ original_model: "gpt-4o"
21
+ target_model: "llama3"