modelcost-sdk 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 (48) hide show
  1. modelcost_sdk-0.1.0/.gitignore +113 -0
  2. modelcost_sdk-0.1.0/CHANGELOG.md +18 -0
  3. modelcost_sdk-0.1.0/LICENSE +21 -0
  4. modelcost_sdk-0.1.0/PKG-INFO +106 -0
  5. modelcost_sdk-0.1.0/README.md +75 -0
  6. modelcost_sdk-0.1.0/pyproject.toml +83 -0
  7. modelcost_sdk-0.1.0/src/modelcost/__init__.py +469 -0
  8. modelcost_sdk-0.1.0/src/modelcost/_version.py +1 -0
  9. modelcost_sdk-0.1.0/src/modelcost/async_client.py +189 -0
  10. modelcost_sdk-0.1.0/src/modelcost/budget.py +132 -0
  11. modelcost_sdk-0.1.0/src/modelcost/client.py +250 -0
  12. modelcost_sdk-0.1.0/src/modelcost/config.py +77 -0
  13. modelcost_sdk-0.1.0/src/modelcost/exceptions.py +109 -0
  14. modelcost_sdk-0.1.0/src/modelcost/models/__init__.py +32 -0
  15. modelcost_sdk-0.1.0/src/modelcost/models/budget.py +59 -0
  16. modelcost_sdk-0.1.0/src/modelcost/models/common.py +42 -0
  17. modelcost_sdk-0.1.0/src/modelcost/models/cost.py +17 -0
  18. modelcost_sdk-0.1.0/src/modelcost/models/governance.py +66 -0
  19. modelcost_sdk-0.1.0/src/modelcost/models/session.py +98 -0
  20. modelcost_sdk-0.1.0/src/modelcost/models/track.py +37 -0
  21. modelcost_sdk-0.1.0/src/modelcost/pii.py +324 -0
  22. modelcost_sdk-0.1.0/src/modelcost/providers/__init__.py +13 -0
  23. modelcost_sdk-0.1.0/src/modelcost/providers/anthropic.py +255 -0
  24. modelcost_sdk-0.1.0/src/modelcost/providers/base.py +35 -0
  25. modelcost_sdk-0.1.0/src/modelcost/providers/google.py +245 -0
  26. modelcost_sdk-0.1.0/src/modelcost/providers/openai.py +268 -0
  27. modelcost_sdk-0.1.0/src/modelcost/py.typed +0 -0
  28. modelcost_sdk-0.1.0/src/modelcost/rate_limiter.py +80 -0
  29. modelcost_sdk-0.1.0/src/modelcost/session.py +188 -0
  30. modelcost_sdk-0.1.0/src/modelcost/tracking.py +245 -0
  31. modelcost_sdk-0.1.0/tests/__init__.py +0 -0
  32. modelcost_sdk-0.1.0/tests/conftest.py +92 -0
  33. modelcost_sdk-0.1.0/tests/fixtures/budget_check_allowed.json +6 -0
  34. modelcost_sdk-0.1.0/tests/fixtures/budget_check_blocked.json +6 -0
  35. modelcost_sdk-0.1.0/tests/fixtures/budget_status.json +25 -0
  36. modelcost_sdk-0.1.0/tests/fixtures/error_response.json +5 -0
  37. modelcost_sdk-0.1.0/tests/fixtures/governance_scan_clean.json +6 -0
  38. modelcost_sdk-0.1.0/tests/fixtures/governance_scan_pii.json +21 -0
  39. modelcost_sdk-0.1.0/tests/fixtures/track_request.json +15 -0
  40. modelcost_sdk-0.1.0/tests/fixtures/track_response.json +3 -0
  41. modelcost_sdk-0.1.0/tests/test_budget.py +98 -0
  42. modelcost_sdk-0.1.0/tests/test_client.py +127 -0
  43. modelcost_sdk-0.1.0/tests/test_config.py +77 -0
  44. modelcost_sdk-0.1.0/tests/test_pii.py +197 -0
  45. modelcost_sdk-0.1.0/tests/test_providers/__init__.py +0 -0
  46. modelcost_sdk-0.1.0/tests/test_providers/test_openai.py +87 -0
  47. modelcost_sdk-0.1.0/tests/test_rate_limiter.py +55 -0
  48. modelcost_sdk-0.1.0/tests/test_tracking.py +100 -0
@@ -0,0 +1,113 @@
1
+ # OS
2
+ .DS_Store
3
+
4
+ # Environment
5
+ .env*
6
+
7
+ # Local dev config (contains secrets)
8
+ server/src/main/resources/application-local.yml
9
+
10
+ ### Client (Next.js) ###
11
+ client/node_modules/
12
+ client/.next/
13
+ client/out/
14
+ client/.pnp
15
+ client/.pnp.*
16
+ client/.yarn/*
17
+ !client/.yarn/patches
18
+ !client/.yarn/plugins
19
+ !client/.yarn/releases
20
+ !client/.yarn/versions
21
+ client/coverage/
22
+ client/build/
23
+ client/.vercel
24
+ client/*.tsbuildinfo
25
+ client/next-env.d.ts
26
+ npm-debug.log*
27
+ yarn-debug.log*
28
+ yarn-error.log*
29
+
30
+ ### Server (Spring Boot / Maven) ###
31
+ server/target/
32
+ server/.mvn/wrapper/maven-wrapper.jar
33
+ !server/**/src/main/**/target/
34
+ !server/**/src/test/**/target/
35
+
36
+ # IDE - Eclipse/STS
37
+ server/.apt_generated
38
+ server/.classpath
39
+ server/.factorypath
40
+ server/.project
41
+ server/.settings
42
+ server/.springBeans
43
+ server/.sts4-cache
44
+
45
+ # IDE - IntelliJ
46
+ .idea/
47
+ *.iws
48
+ *.iml
49
+ *.ipr
50
+
51
+ # IDE - NetBeans
52
+ /nbproject/private/
53
+ /nbbuild/
54
+ /dist/
55
+ /nbdist/
56
+ /.nb-gradle/
57
+
58
+ # IDE - VS Code
59
+ .vscode/
60
+
61
+ # Misc
62
+ *.pem
63
+
64
+ ### SDK - Python ###
65
+ sdk/python/.venv/
66
+ sdk/python/venv/
67
+ sdk/python/__pycache__/
68
+ sdk/python/**/__pycache__/
69
+ sdk/python/*.egg-info/
70
+ sdk/python/src/*.egg-info/
71
+ sdk/python/dist/
72
+ sdk/python/build/
73
+ sdk/python/.mypy_cache/
74
+ sdk/python/.ruff_cache/
75
+ sdk/python/.pytest_cache/
76
+ sdk/python/htmlcov/
77
+ sdk/python/.coverage
78
+
79
+ ### SDK - Node.js ###
80
+ sdk/node/node_modules/
81
+ sdk/node/dist/
82
+ sdk/node/coverage/
83
+ sdk/node/*.tsbuildinfo
84
+
85
+ ### SDK - Java ###
86
+ sdk/java/target/
87
+ sdk/java/.mvn/wrapper/maven-wrapper.jar
88
+ !sdk/java/**/src/main/**/target/
89
+ !sdk/java/**/src/test/**/target/
90
+
91
+ ### Marketing Site (Next.js) ###
92
+ marketing-site/node_modules/
93
+ marketing-site/.next/
94
+ marketing-site/out/
95
+
96
+ ### Demo App ###
97
+ demo/node_modules/
98
+ demo/dist/
99
+
100
+ ### SDK Examples ###
101
+ sdk-examples/node_modules/
102
+ sdk-examples/dist/
103
+
104
+ ### Terraform ###
105
+ infra/.terraform/
106
+ infra/*.tfstate
107
+ infra/*.tfstate.backup
108
+ infra/.terraform.lock.hcl
109
+ infra/terraform.tfvars
110
+ infra/dev/.terraform/
111
+ infra/dev/*.tfstate
112
+ infra/dev/*.tfstate.backup
113
+ infra/dev/.terraform.lock.hcl
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to `modelcost` will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-03-01
9
+
10
+ ### Added
11
+ - Initial alpha release
12
+ - Cost tracking with automatic provider wrapping (OpenAI, Anthropic, Google)
13
+ - Budget enforcement with configurable actions (alert, throttle, block)
14
+ - PII detection scanning for SSN, email, phone, API keys, and credit cards
15
+ - Token bucket rate limiting
16
+ - Automatic background telemetry flushing
17
+ - Sync and async client support
18
+ - Full type annotations with PEP 561 marker
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2026 ModelCost
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,106 @@
1
+ Metadata-Version: 2.4
2
+ Name: modelcost-sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for the ModelCost API — track, govern, and optimize AI model spending.
5
+ Project-URL: Homepage, https://modelcost.ai
6
+ Project-URL: Documentation, https://modelcost.ai/docs
7
+ Author-email: ModelCost <support@modelcost.ai>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: ai,budget,cost,governance,llm,tracking
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.9
22
+ Requires-Dist: httpx>=0.27
23
+ Requires-Dist: pydantic>=2.0
24
+ Provides-Extra: dev
25
+ Requires-Dist: mypy; extra == 'dev'
26
+ Requires-Dist: pytest; extra == 'dev'
27
+ Requires-Dist: pytest-asyncio; extra == 'dev'
28
+ Requires-Dist: respx; extra == 'dev'
29
+ Requires-Dist: ruff; extra == 'dev'
30
+ Description-Content-Type: text/markdown
31
+
32
+ # ModelCost Python SDK
33
+
34
+ Python SDK for the ModelCost API. Track, govern, and optimize your AI model spending in real time.
35
+
36
+ ## Installation
37
+
38
+ ```bash
39
+ pip install modelcost
40
+ ```
41
+
42
+ For development:
43
+
44
+ ```bash
45
+ pip install modelcost[dev]
46
+ ```
47
+
48
+ ## Quick Start
49
+
50
+ ```python
51
+ import modelcost
52
+
53
+ # Initialize the SDK
54
+ modelcost.init(api_key="mc_your_api_key", org_id="org_123")
55
+
56
+ # Wrap your OpenAI client for automatic tracking
57
+ import openai
58
+ client = openai.OpenAI()
59
+ wrapped = modelcost.wrap(client)
60
+
61
+ # All calls are now tracked automatically
62
+ response = wrapped.chat.completions.create(
63
+ model="gpt-4o",
64
+ messages=[{"role": "user", "content": "Hello!"}],
65
+ )
66
+
67
+ # Or track costs manually
68
+ modelcost.track_cost(
69
+ provider="openai",
70
+ model="gpt-4o",
71
+ input_tokens=150,
72
+ output_tokens=50,
73
+ feature="chatbot",
74
+ )
75
+
76
+ # Check budget before expensive operations
77
+ budget = modelcost.check_budget(feature="chatbot", estimated_cost=0.50)
78
+ if not budget.allowed:
79
+ print(f"Budget exceeded: {budget.reason}")
80
+
81
+ # Scan text for PII before sending to models
82
+ result = modelcost.scan_pii("Contact me at test@example.com")
83
+ if result.detected:
84
+ print(f"PII found: {result.entities}")
85
+
86
+ # Get current usage summary
87
+ usage = modelcost.get_usage()
88
+
89
+ # Flush any buffered events and shut down
90
+ modelcost.shutdown()
91
+ ```
92
+
93
+ ## Configuration
94
+
95
+ You can configure the SDK via environment variables:
96
+
97
+ | Variable | Description |
98
+ |---|---|
99
+ | `MODELCOST_API_KEY` | Your API key (must start with `mc_`) |
100
+ | `MODELCOST_ORG_ID` | Your organization ID |
101
+ | `MODELCOST_ENV` | Environment name (default: `production`) |
102
+ | `MODELCOST_BASE_URL` | API base URL (default: `https://api.modelcost.ai`) |
103
+
104
+ ## License
105
+
106
+ MIT
@@ -0,0 +1,75 @@
1
+ # ModelCost Python SDK
2
+
3
+ Python SDK for the ModelCost API. Track, govern, and optimize your AI model spending in real time.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install modelcost
9
+ ```
10
+
11
+ For development:
12
+
13
+ ```bash
14
+ pip install modelcost[dev]
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ```python
20
+ import modelcost
21
+
22
+ # Initialize the SDK
23
+ modelcost.init(api_key="mc_your_api_key", org_id="org_123")
24
+
25
+ # Wrap your OpenAI client for automatic tracking
26
+ import openai
27
+ client = openai.OpenAI()
28
+ wrapped = modelcost.wrap(client)
29
+
30
+ # All calls are now tracked automatically
31
+ response = wrapped.chat.completions.create(
32
+ model="gpt-4o",
33
+ messages=[{"role": "user", "content": "Hello!"}],
34
+ )
35
+
36
+ # Or track costs manually
37
+ modelcost.track_cost(
38
+ provider="openai",
39
+ model="gpt-4o",
40
+ input_tokens=150,
41
+ output_tokens=50,
42
+ feature="chatbot",
43
+ )
44
+
45
+ # Check budget before expensive operations
46
+ budget = modelcost.check_budget(feature="chatbot", estimated_cost=0.50)
47
+ if not budget.allowed:
48
+ print(f"Budget exceeded: {budget.reason}")
49
+
50
+ # Scan text for PII before sending to models
51
+ result = modelcost.scan_pii("Contact me at test@example.com")
52
+ if result.detected:
53
+ print(f"PII found: {result.entities}")
54
+
55
+ # Get current usage summary
56
+ usage = modelcost.get_usage()
57
+
58
+ # Flush any buffered events and shut down
59
+ modelcost.shutdown()
60
+ ```
61
+
62
+ ## Configuration
63
+
64
+ You can configure the SDK via environment variables:
65
+
66
+ | Variable | Description |
67
+ |---|---|
68
+ | `MODELCOST_API_KEY` | Your API key (must start with `mc_`) |
69
+ | `MODELCOST_ORG_ID` | Your organization ID |
70
+ | `MODELCOST_ENV` | Environment name (default: `production`) |
71
+ | `MODELCOST_BASE_URL` | API base URL (default: `https://api.modelcost.ai`) |
72
+
73
+ ## License
74
+
75
+ MIT
@@ -0,0 +1,83 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "modelcost-sdk"
7
+ version = "0.1.0"
8
+ description = "Python SDK for the ModelCost API — track, govern, and optimize AI model spending."
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [
14
+ { name = "ModelCost", email = "support@modelcost.ai" },
15
+ ]
16
+ keywords = ["ai", "llm", "cost", "tracking", "budget", "governance"]
17
+ classifiers = [
18
+ "Development Status :: 3 - Alpha",
19
+ "Intended Audience :: Developers",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ "Programming Language :: Python :: 3.13",
27
+ "Typing :: Typed",
28
+ ]
29
+ dependencies = [
30
+ "httpx>=0.27",
31
+ "pydantic>=2.0",
32
+ ]
33
+
34
+ [project.optional-dependencies]
35
+ dev = [
36
+ "pytest",
37
+ "pytest-asyncio",
38
+ "ruff",
39
+ "mypy",
40
+ "respx",
41
+ ]
42
+
43
+ [project.urls]
44
+ Homepage = "https://modelcost.ai"
45
+ Documentation = "https://modelcost.ai/docs"
46
+
47
+ [project.scripts]
48
+ test = "pytest:main"
49
+
50
+ [tool.hatch.build.targets.wheel]
51
+ packages = ["src/modelcost"]
52
+
53
+ [tool.pytest.ini_options]
54
+ testpaths = ["tests"]
55
+ addopts = "-v"
56
+
57
+ [tool.ruff]
58
+ target-version = "py39"
59
+ line-length = 100
60
+
61
+ [tool.ruff.lint]
62
+ select = [
63
+ "E", # pycodestyle errors
64
+ "W", # pycodestyle warnings
65
+ "F", # pyflakes
66
+ "I", # isort
67
+ "UP", # pyupgrade
68
+ "B", # flake8-bugbear
69
+ "SIM", # flake8-simplify
70
+ "TCH", # flake8-type-checking
71
+ ]
72
+ ignore = [
73
+ "E501", # line too long (handled by formatter)
74
+ ]
75
+
76
+ [tool.ruff.lint.isort]
77
+ known-first-party = ["modelcost"]
78
+
79
+ [tool.mypy]
80
+ python_version = "3.9"
81
+ strict = true
82
+ warn_return_any = true
83
+ warn_unused_configs = true