llmapi-tracker 1.0.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 (50) hide show
  1. llmapi_tracker-1.0.0/LICENSE +21 -0
  2. llmapi_tracker-1.0.0/PKG-INFO +225 -0
  3. llmapi_tracker-1.0.0/README.md +196 -0
  4. llmapi_tracker-1.0.0/backend/__init__.py +1 -0
  5. llmapi_tracker-1.0.0/backend/app/__init__.py +1 -0
  6. llmapi_tracker-1.0.0/backend/app/database.py +24 -0
  7. llmapi_tracker-1.0.0/backend/app/frontend.py +31 -0
  8. llmapi_tracker-1.0.0/backend/app/launcher.py +17 -0
  9. llmapi_tracker-1.0.0/backend/app/main.py +57 -0
  10. llmapi_tracker-1.0.0/backend/app/models.py +116 -0
  11. llmapi_tracker-1.0.0/backend/app/paths.py +60 -0
  12. llmapi_tracker-1.0.0/backend/app/routes/__init__.py +1 -0
  13. llmapi_tracker-1.0.0/backend/app/routes/analytics.py +91 -0
  14. llmapi_tracker-1.0.0/backend/app/routes/models.py +29 -0
  15. llmapi_tracker-1.0.0/backend/app/routes/pricing.py +44 -0
  16. llmapi_tracker-1.0.0/backend/app/routes/projects.py +55 -0
  17. llmapi_tracker-1.0.0/backend/app/routes/providers.py +25 -0
  18. llmapi_tracker-1.0.0/backend/app/routes/usage.py +27 -0
  19. llmapi_tracker-1.0.0/backend/app/schemas.py +130 -0
  20. llmapi_tracker-1.0.0/backend/app/services/__init__.py +1 -0
  21. llmapi_tracker-1.0.0/backend/app/services/cost_service.py +33 -0
  22. llmapi_tracker-1.0.0/backend/app/services/usage_service.py +119 -0
  23. llmapi_tracker-1.0.0/backend/app/static/assets/index-3YVy8dav.css +1 -0
  24. llmapi_tracker-1.0.0/backend/app/static/assets/index-jlcpybVb.js +75 -0
  25. llmapi_tracker-1.0.0/backend/app/static/index.html +13 -0
  26. llmapi_tracker-1.0.0/llmapi_tracker.egg-info/PKG-INFO +225 -0
  27. llmapi_tracker-1.0.0/llmapi_tracker.egg-info/SOURCES.txt +48 -0
  28. llmapi_tracker-1.0.0/llmapi_tracker.egg-info/dependency_links.txt +1 -0
  29. llmapi_tracker-1.0.0/llmapi_tracker.egg-info/entry_points.txt +2 -0
  30. llmapi_tracker-1.0.0/llmapi_tracker.egg-info/requires.txt +22 -0
  31. llmapi_tracker-1.0.0/llmapi_tracker.egg-info/top_level.txt +2 -0
  32. llmapi_tracker-1.0.0/pyproject.toml +45 -0
  33. llmapi_tracker-1.0.0/sdk/api_tracker/__init__.py +5 -0
  34. llmapi_tracker-1.0.0/sdk/api_tracker/client.py +49 -0
  35. llmapi_tracker-1.0.0/sdk/api_tracker/models.py +30 -0
  36. llmapi_tracker-1.0.0/sdk/api_tracker/pricing.py +79 -0
  37. llmapi_tracker-1.0.0/sdk/api_tracker/providers/__init__.py +8 -0
  38. llmapi_tracker-1.0.0/sdk/api_tracker/providers/gemini.py +36 -0
  39. llmapi_tracker-1.0.0/sdk/api_tracker/providers/openai.py +36 -0
  40. llmapi_tracker-1.0.0/sdk/api_tracker/py.typed +1 -0
  41. llmapi_tracker-1.0.0/sdk/api_tracker/tracker.py +47 -0
  42. llmapi_tracker-1.0.0/sdk/api_tracker/transport.py +25 -0
  43. llmapi_tracker-1.0.0/setup.cfg +4 -0
  44. llmapi_tracker-1.0.0/tests/test_analytics_routes.py +24 -0
  45. llmapi_tracker-1.0.0/tests/test_cost_service.py +21 -0
  46. llmapi_tracker-1.0.0/tests/test_paths.py +22 -0
  47. llmapi_tracker-1.0.0/tests/test_provider_model_routes.py +24 -0
  48. llmapi_tracker-1.0.0/tests/test_routes.py +91 -0
  49. llmapi_tracker-1.0.0/tests/test_tracker.py +34 -0
  50. llmapi_tracker-1.0.0/tests/test_usage_service.py +66 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 API Tracker contributors
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,225 @@
1
+ Metadata-Version: 2.4
2
+ Name: llmapi-tracker
3
+ Version: 1.0.0
4
+ Summary: Local-first API usage and cost tracking platform
5
+ Author: API Tracker
6
+ License: MIT
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: requests>=2.32
11
+ Requires-Dist: fastapi>=0.115
12
+ Requires-Dist: uvicorn[standard]>=0.30
13
+ Requires-Dist: sqlalchemy>=2.0
14
+ Requires-Dist: pydantic>=2.8
15
+ Provides-Extra: openai
16
+ Requires-Dist: openai>=1.40; extra == "openai"
17
+ Provides-Extra: gemini
18
+ Requires-Dist: google-genai>=0.7; extra == "gemini"
19
+ Provides-Extra: all
20
+ Requires-Dist: openai>=1.40; extra == "all"
21
+ Requires-Dist: google-genai>=0.7; extra == "all"
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=8.0; extra == "dev"
24
+ Requires-Dist: httpx>=0.27; extra == "dev"
25
+ Requires-Dist: ruff>=0.6; extra == "dev"
26
+ Requires-Dist: build>=1.2; extra == "dev"
27
+ Requires-Dist: twine>=5.1; extra == "dev"
28
+ Dynamic: license-file
29
+
30
+ # API Tracker
31
+
32
+ ## What API Tracker Is
33
+
34
+ API Tracker is a local-first API usage and cost observability tool. It records request volume, token usage, latency, status, and versioned pricing for models from providers such as OpenAI and Gemini.
35
+
36
+ The project is built to keep data local by default. It stores usage and pricing in a local SQLite database and does not persist prompts, responses, or raw API keys in the application database.
37
+
38
+ ## Features
39
+
40
+ - Local FastAPI backend with SQLite storage
41
+ - Versioned pricing records for historical cost tracking
42
+ - Usage capture for input, output, thinking, cached, and total tokens
43
+ - Cost calculation per request and across analytics views
44
+ - React + Vite dashboard for usage, analytics, providers, models, and pricing
45
+ - SDK wrappers for OpenAI and Gemini
46
+ - Packaged launcher command that starts the local app and opens the browser
47
+ - Open-source repository setup with tests, CI, and release automation
48
+
49
+ ## Installation
50
+
51
+ ### Prerequisites
52
+
53
+ - Python 3.10 or newer
54
+ - Node.js 20 or newer for frontend development
55
+
56
+ ### Local development install
57
+
58
+ ```bash
59
+ python -m venv .venv
60
+ .venv\Scripts\activate
61
+ pip install -r requirements.txt
62
+ cd frontend
63
+ npm install
64
+ ```
65
+
66
+ ### Package install
67
+
68
+ If you install this project as a package, the launcher command is:
69
+
70
+ ```bash
71
+ api-tracker
72
+ ```
73
+
74
+ ## Starting the Application
75
+
76
+ ### Backend only
77
+
78
+ ```bash
79
+ uvicorn backend.app.main:app --reload
80
+ ```
81
+
82
+ ### Frontend only
83
+
84
+ ```bash
85
+ cd frontend
86
+ npm run dev
87
+ ```
88
+
89
+ ### Packaged app
90
+
91
+ After building the frontend assets and packaging them into the backend static directory, run:
92
+
93
+ ```bash
94
+ api-tracker
95
+ ```
96
+
97
+ The launcher starts the backend server and opens the browser to the local dashboard.
98
+
99
+ ## SDK Usage
100
+
101
+ ### Initialize the SDK
102
+
103
+ ```python
104
+ import os
105
+ from sdk.api_tracker import APITracker
106
+
107
+ tracker = APITracker(
108
+ project="Pharmacy-AI",
109
+ backend_url=os.getenv("TRACKER_BACKEND_URL", "http://localhost:8000"),
110
+ openai_api_key=os.getenv("OPENAI_API_KEY"),
111
+ gemini_api_key=os.getenv("GEMINI_API_KEY"),
112
+ )
113
+ ```
114
+
115
+ ### OpenAI
116
+
117
+ ```python
118
+ response = tracker.openai.chat(
119
+ model="gpt-5",
120
+ messages=[{"role": "user", "content": "Analyze this prescription"}],
121
+ )
122
+ ```
123
+
124
+ ### Gemini
125
+
126
+ ```python
127
+ response = tracker.gemini.generate(
128
+ model="gemini-2.5-flash",
129
+ contents="Analyze this prescription",
130
+ )
131
+ ```
132
+
133
+ The SDK reads usage metadata from the provider response and sends it to the local backend automatically.
134
+
135
+ ## Database Location
136
+
137
+ By default, the SQLite database is stored in a stable user data directory so it survives uninstall and reinstall.
138
+
139
+ The path is resolved in this order:
140
+
141
+ 1. `API_TRACKER_DB_PATH` if set
142
+ 2. `DATABASE_URL` if set
143
+ 3. A user-data location based on the operating system
144
+
145
+ Typical defaults:
146
+
147
+ - Windows: `%LOCALAPPDATA%\\APITracker\\api_tracker.db`
148
+ - macOS/Linux: `~/.local/share/APITracker/api_tracker.db`
149
+
150
+ If the preferred user-data directory cannot be created, the app falls back to a writable local `.api-tracker-data/` directory in the current workspace.
151
+
152
+ ## Configuration
153
+
154
+ Environment variables:
155
+
156
+ ```bash
157
+ DATABASE_URL=sqlite:///./api_tracker.db
158
+ API_TRACKER_DATA_DIR=
159
+ API_TRACKER_DB_PATH=
160
+ API_TRACKER_HOST=127.0.0.1
161
+ API_TRACKER_PORT=8000
162
+ API_TRACKER_OPEN_BROWSER=true
163
+ OPENAI_API_KEY=
164
+ GEMINI_API_KEY=
165
+ TRACKER_BACKEND_URL=http://localhost:8000
166
+ VITE_API_BASE_URL=http://localhost:8000
167
+ ```
168
+
169
+ Notes:
170
+
171
+ - `DATABASE_URL` overrides the database connection string
172
+ - `API_TRACKER_DATA_DIR` sets the user-data root used for SQLite storage
173
+ - `API_TRACKER_DB_PATH` points directly to a specific SQLite file
174
+ - `API_TRACKER_OPEN_BROWSER=false` disables browser auto-open for the launcher
175
+
176
+ ## Development Setup
177
+
178
+ ### Backend
179
+
180
+ ```bash
181
+ python -m compileall backend sdk tests
182
+ pytest -q
183
+ ```
184
+
185
+ ### Frontend
186
+
187
+ ```bash
188
+ cd frontend
189
+ npm run build
190
+ ```
191
+
192
+ ### Packaging assets
193
+
194
+ ```bash
195
+ cd frontend
196
+ npm run build
197
+ cd ..
198
+ python scripts/package_frontend_assets.py
199
+ python scripts/verify_frontend_assets.py
200
+ ```
201
+
202
+ ## Contributing
203
+
204
+ This repository is intended to be open source and contributor friendly.
205
+
206
+ Contribution files:
207
+
208
+ - [`CONTRIBUTING.md`](L:/Personal/APITracker/CONTRIBUTING.md)
209
+ - [`CODE_OF_CONDUCT.md`](L:/Personal/APITracker/CODE_OF_CONDUCT.md)
210
+ - [`SECURITY.md`](L:/Personal/APITracker/SECURITY.md)
211
+ - [`CHANGELOG.md`](L:/Personal/APITracker/CHANGELOG.md)
212
+ - [`LICENSE`](L:/Personal/APITracker/LICENSE)
213
+
214
+ Contribution expectations:
215
+
216
+ - Open changes through pull requests
217
+ - Require review before merge
218
+ - Keep the app local-first
219
+ - Avoid storing prompts, responses, or raw API keys
220
+ - Add tests for behavior changes
221
+
222
+ ## License
223
+
224
+ API Tracker is released under the MIT License. See [`LICENSE`](L:/Personal/APITracker/LICENSE).
225
+
@@ -0,0 +1,196 @@
1
+ # API Tracker
2
+
3
+ ## What API Tracker Is
4
+
5
+ API Tracker is a local-first API usage and cost observability tool. It records request volume, token usage, latency, status, and versioned pricing for models from providers such as OpenAI and Gemini.
6
+
7
+ The project is built to keep data local by default. It stores usage and pricing in a local SQLite database and does not persist prompts, responses, or raw API keys in the application database.
8
+
9
+ ## Features
10
+
11
+ - Local FastAPI backend with SQLite storage
12
+ - Versioned pricing records for historical cost tracking
13
+ - Usage capture for input, output, thinking, cached, and total tokens
14
+ - Cost calculation per request and across analytics views
15
+ - React + Vite dashboard for usage, analytics, providers, models, and pricing
16
+ - SDK wrappers for OpenAI and Gemini
17
+ - Packaged launcher command that starts the local app and opens the browser
18
+ - Open-source repository setup with tests, CI, and release automation
19
+
20
+ ## Installation
21
+
22
+ ### Prerequisites
23
+
24
+ - Python 3.10 or newer
25
+ - Node.js 20 or newer for frontend development
26
+
27
+ ### Local development install
28
+
29
+ ```bash
30
+ python -m venv .venv
31
+ .venv\Scripts\activate
32
+ pip install -r requirements.txt
33
+ cd frontend
34
+ npm install
35
+ ```
36
+
37
+ ### Package install
38
+
39
+ If you install this project as a package, the launcher command is:
40
+
41
+ ```bash
42
+ api-tracker
43
+ ```
44
+
45
+ ## Starting the Application
46
+
47
+ ### Backend only
48
+
49
+ ```bash
50
+ uvicorn backend.app.main:app --reload
51
+ ```
52
+
53
+ ### Frontend only
54
+
55
+ ```bash
56
+ cd frontend
57
+ npm run dev
58
+ ```
59
+
60
+ ### Packaged app
61
+
62
+ After building the frontend assets and packaging them into the backend static directory, run:
63
+
64
+ ```bash
65
+ api-tracker
66
+ ```
67
+
68
+ The launcher starts the backend server and opens the browser to the local dashboard.
69
+
70
+ ## SDK Usage
71
+
72
+ ### Initialize the SDK
73
+
74
+ ```python
75
+ import os
76
+ from sdk.api_tracker import APITracker
77
+
78
+ tracker = APITracker(
79
+ project="Pharmacy-AI",
80
+ backend_url=os.getenv("TRACKER_BACKEND_URL", "http://localhost:8000"),
81
+ openai_api_key=os.getenv("OPENAI_API_KEY"),
82
+ gemini_api_key=os.getenv("GEMINI_API_KEY"),
83
+ )
84
+ ```
85
+
86
+ ### OpenAI
87
+
88
+ ```python
89
+ response = tracker.openai.chat(
90
+ model="gpt-5",
91
+ messages=[{"role": "user", "content": "Analyze this prescription"}],
92
+ )
93
+ ```
94
+
95
+ ### Gemini
96
+
97
+ ```python
98
+ response = tracker.gemini.generate(
99
+ model="gemini-2.5-flash",
100
+ contents="Analyze this prescription",
101
+ )
102
+ ```
103
+
104
+ The SDK reads usage metadata from the provider response and sends it to the local backend automatically.
105
+
106
+ ## Database Location
107
+
108
+ By default, the SQLite database is stored in a stable user data directory so it survives uninstall and reinstall.
109
+
110
+ The path is resolved in this order:
111
+
112
+ 1. `API_TRACKER_DB_PATH` if set
113
+ 2. `DATABASE_URL` if set
114
+ 3. A user-data location based on the operating system
115
+
116
+ Typical defaults:
117
+
118
+ - Windows: `%LOCALAPPDATA%\\APITracker\\api_tracker.db`
119
+ - macOS/Linux: `~/.local/share/APITracker/api_tracker.db`
120
+
121
+ If the preferred user-data directory cannot be created, the app falls back to a writable local `.api-tracker-data/` directory in the current workspace.
122
+
123
+ ## Configuration
124
+
125
+ Environment variables:
126
+
127
+ ```bash
128
+ DATABASE_URL=sqlite:///./api_tracker.db
129
+ API_TRACKER_DATA_DIR=
130
+ API_TRACKER_DB_PATH=
131
+ API_TRACKER_HOST=127.0.0.1
132
+ API_TRACKER_PORT=8000
133
+ API_TRACKER_OPEN_BROWSER=true
134
+ OPENAI_API_KEY=
135
+ GEMINI_API_KEY=
136
+ TRACKER_BACKEND_URL=http://localhost:8000
137
+ VITE_API_BASE_URL=http://localhost:8000
138
+ ```
139
+
140
+ Notes:
141
+
142
+ - `DATABASE_URL` overrides the database connection string
143
+ - `API_TRACKER_DATA_DIR` sets the user-data root used for SQLite storage
144
+ - `API_TRACKER_DB_PATH` points directly to a specific SQLite file
145
+ - `API_TRACKER_OPEN_BROWSER=false` disables browser auto-open for the launcher
146
+
147
+ ## Development Setup
148
+
149
+ ### Backend
150
+
151
+ ```bash
152
+ python -m compileall backend sdk tests
153
+ pytest -q
154
+ ```
155
+
156
+ ### Frontend
157
+
158
+ ```bash
159
+ cd frontend
160
+ npm run build
161
+ ```
162
+
163
+ ### Packaging assets
164
+
165
+ ```bash
166
+ cd frontend
167
+ npm run build
168
+ cd ..
169
+ python scripts/package_frontend_assets.py
170
+ python scripts/verify_frontend_assets.py
171
+ ```
172
+
173
+ ## Contributing
174
+
175
+ This repository is intended to be open source and contributor friendly.
176
+
177
+ Contribution files:
178
+
179
+ - [`CONTRIBUTING.md`](L:/Personal/APITracker/CONTRIBUTING.md)
180
+ - [`CODE_OF_CONDUCT.md`](L:/Personal/APITracker/CODE_OF_CONDUCT.md)
181
+ - [`SECURITY.md`](L:/Personal/APITracker/SECURITY.md)
182
+ - [`CHANGELOG.md`](L:/Personal/APITracker/CHANGELOG.md)
183
+ - [`LICENSE`](L:/Personal/APITracker/LICENSE)
184
+
185
+ Contribution expectations:
186
+
187
+ - Open changes through pull requests
188
+ - Require review before merge
189
+ - Keep the app local-first
190
+ - Avoid storing prompts, responses, or raw API keys
191
+ - Add tests for behavior changes
192
+
193
+ ## License
194
+
195
+ API Tracker is released under the MIT License. See [`LICENSE`](L:/Personal/APITracker/LICENSE).
196
+
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,24 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ from sqlalchemy import create_engine
6
+ from sqlalchemy.orm import declarative_base, sessionmaker
7
+
8
+ from .paths import get_database_url
9
+
10
+ DATABASE_URL = get_database_url()
11
+
12
+ connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
13
+
14
+ engine = create_engine(DATABASE_URL, connect_args=connect_args, future=True)
15
+ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, future=True)
16
+ Base = declarative_base()
17
+
18
+
19
+ def get_db():
20
+ db = SessionLocal()
21
+ try:
22
+ yield db
23
+ finally:
24
+ db.close()
@@ -0,0 +1,31 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ from fastapi import APIRouter
6
+ from fastapi.responses import FileResponse
7
+ from fastapi.staticfiles import StaticFiles
8
+
9
+ from .paths import get_frontend_dist_dir
10
+
11
+
12
+ def mount_frontend(app, prefix: str = "") -> None:
13
+ static_dir = get_frontend_dist_dir()
14
+ if static_dir.exists():
15
+ app.mount(prefix or "/assets", StaticFiles(directory=static_dir / "assets"), name="assets")
16
+
17
+
18
+ def register_frontend_routes(router: APIRouter) -> None:
19
+ static_dir = get_frontend_dist_dir()
20
+ index_path = static_dir / "index.html"
21
+
22
+ if not index_path.exists():
23
+ return
24
+
25
+ @router.get("/{path:path}")
26
+ @router.get("/")
27
+ def serve_frontend(path: str = ""):
28
+ target = static_dir / path
29
+ if path and target.exists() and target.is_file():
30
+ return FileResponse(target)
31
+ return FileResponse(index_path)
@@ -0,0 +1,17 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import threading
5
+ import webbrowser
6
+
7
+ import uvicorn
8
+
9
+
10
+ def launch_app() -> None:
11
+ host = os.getenv("API_TRACKER_HOST", "127.0.0.1")
12
+ port = int(os.getenv("API_TRACKER_PORT", "8000"))
13
+ open_browser = os.getenv("API_TRACKER_OPEN_BROWSER", "true").lower() not in {"0", "false", "no"}
14
+ if open_browser:
15
+ url = f"http://{host}:{port}"
16
+ threading.Timer(1.0, lambda: webbrowser.open(url)).start()
17
+ uvicorn.run("backend.app.main:app", host=host, port=port, reload=False)
@@ -0,0 +1,57 @@
1
+ from pathlib import Path
2
+
3
+ from fastapi import FastAPI
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.responses import FileResponse
6
+ from fastapi.staticfiles import StaticFiles
7
+
8
+ from .database import Base, engine
9
+ from .paths import get_frontend_dist_dir
10
+ from .routes import analytics, models, pricing, projects, providers, usage
11
+
12
+ Base.metadata.create_all(bind=engine)
13
+
14
+ app = FastAPI(title="API Tracker", version="1.0.0")
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=["*"],
18
+ allow_credentials=True,
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
+ )
22
+
23
+ app.include_router(projects.router)
24
+ app.include_router(usage.router)
25
+ app.include_router(pricing.router)
26
+ app.include_router(providers.router)
27
+ app.include_router(models.router)
28
+ app.include_router(analytics.router)
29
+
30
+ frontend_dist = get_frontend_dist_dir()
31
+ index_file = frontend_dist / "index.html"
32
+ assets_dir = frontend_dist / "assets"
33
+
34
+ if assets_dir.exists():
35
+ app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")
36
+
37
+
38
+ @app.get("/")
39
+ def root():
40
+ if index_file.exists():
41
+ return FileResponse(index_file)
42
+ return {"name": "API Tracker", "status": "running"}
43
+
44
+
45
+ @app.get("/{path:path}")
46
+ def frontend_fallback(path: str):
47
+ target = frontend_dist / path
48
+ if target.exists() and target.is_file():
49
+ return FileResponse(target)
50
+ if index_file.exists():
51
+ return FileResponse(index_file)
52
+ return {"detail": "frontend not built"}
53
+
54
+
55
+ @app.get("/health")
56
+ def health():
57
+ return {"status": "healthy"}
@@ -0,0 +1,116 @@
1
+ from __future__ import annotations
2
+
3
+ from datetime import datetime
4
+
5
+ from sqlalchemy import (
6
+ JSON,
7
+ Boolean,
8
+ Column,
9
+ DateTime,
10
+ Float,
11
+ ForeignKey,
12
+ Integer,
13
+ String,
14
+ Text,
15
+ UniqueConstraint,
16
+ )
17
+ from sqlalchemy.orm import relationship
18
+
19
+ from .database import Base
20
+
21
+
22
+ def utcnow() -> datetime:
23
+ return datetime.utcnow()
24
+
25
+
26
+ class Project(Base):
27
+ __tablename__ = "projects"
28
+
29
+ id = Column(Integer, primary_key=True, index=True)
30
+ name = Column(String(150), unique=True, nullable=False, index=True)
31
+ description = Column(Text, nullable=True)
32
+ environment = Column(String(50), default="development", nullable=False)
33
+ created_at = Column(DateTime, default=utcnow, nullable=False)
34
+
35
+ usage = relationship("ApiUsage", back_populates="project")
36
+
37
+
38
+ class Provider(Base):
39
+ __tablename__ = "providers"
40
+
41
+ id = Column(Integer, primary_key=True, index=True)
42
+ name = Column(String(100), unique=True, nullable=False, index=True)
43
+ created_at = Column(DateTime, default=utcnow, nullable=False)
44
+
45
+ models = relationship("Model", back_populates="provider", cascade="all, delete-orphan")
46
+
47
+
48
+ class Model(Base):
49
+ __tablename__ = "models"
50
+ __table_args__ = (UniqueConstraint("provider_id", "model_name", name="uq_provider_model_name"),)
51
+
52
+ id = Column(Integer, primary_key=True, index=True)
53
+ provider_id = Column(Integer, ForeignKey("providers.id"), nullable=False, index=True)
54
+ model_name = Column(String(150), nullable=False, index=True)
55
+ model_type = Column(String(50), default="text", nullable=False)
56
+ created_at = Column(DateTime, default=utcnow, nullable=False)
57
+
58
+ provider = relationship("Provider", back_populates="models")
59
+ pricing = relationship("ModelPricing", back_populates="model", cascade="all, delete-orphan")
60
+ usage = relationship("ApiUsage", back_populates="model_ref")
61
+
62
+
63
+ class ModelPricing(Base):
64
+ __tablename__ = "model_pricing"
65
+ __table_args__ = (
66
+ UniqueConstraint("model_id", "effective_from", name="uq_model_pricing_version"),
67
+ )
68
+
69
+ id = Column(Integer, primary_key=True, index=True)
70
+ model_id = Column(Integer, ForeignKey("models.id"), nullable=False, index=True)
71
+ input_price_per_1m = Column(Float, default=0.0, nullable=False)
72
+ output_price_per_1m = Column(Float, default=0.0, nullable=False)
73
+ thinking_price_per_1m = Column(Float, default=0.0, nullable=False)
74
+ cached_input_price_per_1m = Column(Float, default=0.0, nullable=False)
75
+ currency = Column(String(10), default="USD", nullable=False)
76
+ effective_from = Column(DateTime, default=utcnow, nullable=False, index=True)
77
+ effective_to = Column(DateTime, nullable=True, index=True)
78
+ created_at = Column(DateTime, default=utcnow, nullable=False)
79
+ updated_at = Column(DateTime, default=utcnow, onupdate=utcnow, nullable=False)
80
+
81
+ model = relationship("Model", back_populates="pricing")
82
+
83
+
84
+ class ApiUsage(Base):
85
+ __tablename__ = "api_usage"
86
+
87
+ id = Column(Integer, primary_key=True, index=True)
88
+ project_id = Column(Integer, ForeignKey("projects.id"), nullable=False, index=True)
89
+ provider_id = Column(Integer, ForeignKey("providers.id"), nullable=False, index=True)
90
+ model_id = Column(Integer, ForeignKey("models.id"), nullable=False, index=True)
91
+ internal_request_id = Column(String(255), nullable=False, unique=True, index=True)
92
+ provider_request_id = Column(String(255), nullable=True, index=True)
93
+ timestamp = Column(DateTime, default=utcnow, nullable=False, index=True)
94
+ input_tokens = Column(Integer, default=0, nullable=False)
95
+ output_tokens = Column(Integer, default=0, nullable=False)
96
+ thinking_tokens = Column(Integer, default=0, nullable=False)
97
+ cached_tokens = Column(Integer, default=0, nullable=False)
98
+ total_tokens = Column(Integer, default=0, nullable=False)
99
+ input_cost = Column(Float, default=0.0, nullable=False)
100
+ output_cost = Column(Float, default=0.0, nullable=False)
101
+ thinking_cost = Column(Float, default=0.0, nullable=False)
102
+ cached_cost = Column(Float, default=0.0, nullable=False)
103
+ total_cost = Column(Float, default=0.0, nullable=False)
104
+ audio_seconds = Column(Float, default=0.0, nullable=False)
105
+ characters = Column(Integer, default=0, nullable=False)
106
+ request_count = Column(Integer, default=1, nullable=False)
107
+ latency_ms = Column(Float, default=0.0, nullable=False)
108
+ status = Column(String(30), default="success", nullable=False)
109
+ http_status_code = Column(Integer, nullable=True)
110
+ error_type = Column(String(100), nullable=True)
111
+ metadata_json = Column(JSON, default=dict, nullable=False)
112
+ capture_content = Column(Boolean, default=False, nullable=False)
113
+
114
+ project = relationship("Project", back_populates="usage")
115
+ provider = relationship("Provider")
116
+ model_ref = relationship("Model", back_populates="usage")