agentcap 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.
@@ -0,0 +1,50 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ build/
11
+ dist/
12
+ *.egg-info/
13
+ *.egg
14
+
15
+ # Installer logs
16
+ pip-log.txt
17
+ pip-delete-this-directory.txt
18
+
19
+ # Unit test / coverage reports
20
+ htmlcov/
21
+ .tox/
22
+ .nox/
23
+ .coverage
24
+ .coverage.*
25
+ .cache
26
+ nosetests.xml
27
+ coverage.xml
28
+ *.cover
29
+ *.py,cover
30
+ .hypothesis/
31
+
32
+ # Virtual environments
33
+ venv/
34
+ .env/
35
+ ENV/
36
+ env/
37
+ .venv/
38
+ .env
39
+
40
+ # IDE / editor configs
41
+ .vscode/
42
+ .idea/
43
+ *.swp
44
+ *.swo
45
+
46
+ # MacOS / system files
47
+ .DS_Store
48
+
49
+ # Logs
50
+ *.log
agentcap-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OliverIida
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,57 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentcap
3
+ Version: 0.1.0
4
+ Summary: Cost guardrails for LLM agent runs.
5
+ Project-URL: Homepage, https://github.com/OliverIida/agentcap
6
+ Author: Oliver Iida
7
+ License: MIT License
8
+
9
+ Copyright (c) 2026 OliverIida
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ License-File: LICENSE
29
+ Keywords: agents,anthropic,cost-control,gemini,llm,openai
30
+ Classifier: License :: OSI Approved :: MIT License
31
+ Classifier: Operating System :: OS Independent
32
+ Classifier: Programming Language :: Python :: 3
33
+ Requires-Python: >=3.10
34
+ Description-Content-Type: text/markdown
35
+
36
+ # agentcap
37
+ `agentcap` is a cost guardrail for LLM agent runs. It calculates safe token limits before execution and enforces strict budget ceilings using real usage data.
38
+
39
+ ## Install
40
+
41
+ ```bash
42
+ pip install agentcap
43
+ ```
44
+
45
+ ## Documentation
46
+ See [DOCUMENTATION.md](DOCUMENTATION.md) for full usage details.
47
+
48
+ ## Supported models
49
+ See [agentcap/models.json](agentcap/models.json) for the list of currently supported models.
50
+ To add more models, open a PR.
51
+
52
+ Note: Prices may change. Please verify with the provider.
53
+
54
+ ## Model pricing sources
55
+ OpenAI pricing: https://developers.openai.com/api/docs/pricing?latest-pricing=standard
56
+ Anthropic pricing: https://platform.claude.com/docs/en/about-claude/pricing
57
+ Gemini pricing: https://ai.google.dev/gemini-api/docs/pricing#standard
@@ -0,0 +1,22 @@
1
+ # agentcap
2
+ `agentcap` is a cost guardrail for LLM agent runs. It calculates safe token limits before execution and enforces strict budget ceilings using real usage data.
3
+
4
+ ## Install
5
+
6
+ ```bash
7
+ pip install agentcap
8
+ ```
9
+
10
+ ## Documentation
11
+ See [DOCUMENTATION.md](DOCUMENTATION.md) for full usage details.
12
+
13
+ ## Supported models
14
+ See [agentcap/models.json](agentcap/models.json) for the list of currently supported models.
15
+ To add more models, open a PR.
16
+
17
+ Note: Prices may change. Please verify with the provider.
18
+
19
+ ## Model pricing sources
20
+ OpenAI pricing: https://developers.openai.com/api/docs/pricing?latest-pricing=standard
21
+ Anthropic pricing: https://platform.claude.com/docs/en/about-claude/pricing
22
+ Gemini pricing: https://ai.google.dev/gemini-api/docs/pricing#standard
@@ -0,0 +1,9 @@
1
+ from .core import AgentCap
2
+ from .exceptions import BudgetExceeded, InvalidArguments, UnknownModelError
3
+
4
+ __all__ = [
5
+ "AgentCap",
6
+ "BudgetExceeded",
7
+ "InvalidArguments",
8
+ "UnknownModelError"
9
+ ]
@@ -0,0 +1,100 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from math import floor
5
+
6
+ from .exceptions import BudgetExceeded, InvalidArguments
7
+ from .pricing import get_model_price
8
+ from .token_estimator import estimate_tokens
9
+
10
+ @dataclass(slots=True)
11
+ class PlanResult:
12
+ model: str
13
+ budget_usd: float
14
+ effective_budget_usd: float
15
+ prompt_tokens: int
16
+ estimated_input_cost_usd: float
17
+ remaining_budget_usd: float
18
+ max_output_tokens: int
19
+ warnings: list[str]
20
+
21
+ class AgentCap:
22
+ def __init__(
23
+ self,
24
+ model: str,
25
+ budget_usd: float,
26
+ safety_margin: float = 0.90,
27
+ min_output_tokens: int = 16,
28
+ low_budget_warning_threshold: float = 0.15,
29
+ ) -> None:
30
+ if not isinstance(model, str) or not model.strip():
31
+ raise InvalidArguments("model must be a non-empty string")
32
+ if budget_usd <= 0:
33
+ raise InvalidArguments("budget_usd must be greater than 0")
34
+ if not (0 < safety_margin <= 1):
35
+ raise InvalidArguments("safety_margin must be in the range (0, 1]")
36
+ if min_output_tokens < 1:
37
+ raise InvalidArguments("min_output_tokens must be >= 1")
38
+ if not (0 <= low_budget_warning_threshold <= 1):
39
+ raise InvalidArguments(
40
+ "low_budget_warning_threshold must be in the range [0, 1]"
41
+ )
42
+
43
+ self.model = model.strip()
44
+ self.budget_usd = float(budget_usd)
45
+ self.safety_margin = float(safety_margin)
46
+ self.min_output_tokens = int(min_output_tokens)
47
+ self.low_budget_warning_threshold = float(low_budget_warning_threshold)
48
+ self._price = get_model_price(self.model)
49
+
50
+ def estimate_cost(self, prompt_tokens: int, output_tokens: int) -> float:
51
+ if prompt_tokens < 0 or output_tokens < 0:
52
+ raise InvalidArguments("prompt_tokens and output_tokens must be >= 0")
53
+
54
+ input_cost = (prompt_tokens / 1_000_000) * self._price.input_per_1m
55
+ output_cost = (output_tokens / 1_000_000) * self._price.output_per_1m
56
+ return input_cost + output_cost
57
+
58
+ def plan(self, prompt_text: str) -> PlanResult:
59
+ if not isinstance(prompt_text, str) or not prompt_text.strip():
60
+ raise InvalidArguments("prompt_text must be a non-empty string")
61
+
62
+ prompt_tokens = estimate_tokens(prompt_text)
63
+ effective_budget = self.budget_usd * self.safety_margin
64
+ input_cost = (prompt_tokens / 1_000_000) * self._price.input_per_1m
65
+
66
+ if input_cost >= effective_budget:
67
+ raise BudgetExceeded("Prompt alone exceeds budget.")
68
+
69
+ remaining = effective_budget - input_cost
70
+ cost_per_output_token = self._price.output_per_1m / 1_000_000
71
+ if cost_per_output_token <= 0:
72
+ raise InvalidArguments("model output pricing must be greater than 0")
73
+
74
+ max_output_tokens = floor(remaining / cost_per_output_token)
75
+ if max_output_tokens < self.min_output_tokens:
76
+ raise BudgetExceeded("Not enough budget for minimum output tokens.")
77
+
78
+ warnings: list[str] = []
79
+ if remaining <= self.budget_usd * self.low_budget_warning_threshold:
80
+ warnings.append(
81
+ "Low remaining budget after prompt. Consider increasing budget or "
82
+ "shortening prompt."
83
+ )
84
+
85
+ return PlanResult(
86
+ model=self.model,
87
+ budget_usd=self.budget_usd,
88
+ effective_budget_usd=effective_budget,
89
+ prompt_tokens=prompt_tokens,
90
+ estimated_input_cost_usd=input_cost,
91
+ remaining_budget_usd=remaining,
92
+ max_output_tokens=max_output_tokens,
93
+ warnings=warnings,
94
+ )
95
+
96
+ def finalize(self, prompt_tokens: int, output_tokens: int) -> float:
97
+ actual_cost = self.estimate_cost(prompt_tokens, output_tokens)
98
+ if actual_cost > self.budget_usd:
99
+ raise BudgetExceeded("Run exceeded budget.")
100
+ return actual_cost
@@ -0,0 +1,8 @@
1
+ class UnknownModelError(ValueError):
2
+ """Raised when a model name is not found in pricing data."""
3
+
4
+ class BudgetExceeded(RuntimeError):
5
+ """Raised when a planned or actual run exceeds budget constraints."""
6
+
7
+ class InvalidArguments(ValueError):
8
+ """Raised when inputs are invalid."""
@@ -0,0 +1,73 @@
1
+ {
2
+ "claude-opus-4.6": {
3
+ "input_per_1m": 5.00,
4
+ "output_per_1m": 25.00
5
+ },
6
+ "claude-opus-4.5": {
7
+ "input_per_1m": 5.00,
8
+ "output_per_1m": 25.00
9
+ },
10
+ "claude-sonnet-4.6": {
11
+ "input_per_1m": 3.00,
12
+ "output_per_1m": 15.00
13
+ },
14
+ "claude-sonnet-4.5": {
15
+ "input_per_1m": 3.00,
16
+ "output_per_1m": 15.00
17
+ },
18
+ "claude-haiku-4.5": {
19
+ "input_per_1m": 1.00,
20
+ "output_per_1m": 5.00
21
+ },
22
+ "claude-haiku-3.5": {
23
+ "input_per_1m": 0.80,
24
+ "output_per_1m": 4.00
25
+ },
26
+
27
+ "gpt-5.3-codex": {
28
+ "input_per_1m": 1.75,
29
+ "output_per_1m": 14.00
30
+ },
31
+ "gpt-5.2-codex": {
32
+ "input_per_1m": 1.75,
33
+ "output_per_1m": 14.00
34
+ },
35
+ "gpt-5.1-codex": {
36
+ "input_per_1m": 1.25,
37
+ "output_per_1m": 10.00
38
+ },
39
+ "gpt-5-codex": {
40
+ "input_per_1m": 1.25,
41
+ "output_per_1m": 10.00
42
+ },
43
+
44
+ "gpt-5.2": {
45
+ "input_per_1m": 1.75,
46
+ "output_per_1m": 14.00
47
+ },
48
+ "gpt-5.1": {
49
+ "input_per_1m": 1.25,
50
+ "output_per_1m": 10.00
51
+ },
52
+ "gpt-5": {
53
+ "input_per_1m": 1.25,
54
+ "output_per_1m": 10.00
55
+ },
56
+
57
+ "gemini-3.1-pro-preview": {
58
+ "input_per_1m": 2.00,
59
+ "output_per_1m": 12.00
60
+ },
61
+ "gemini-3-flash-preview": {
62
+ "input_per_1m": 0.50,
63
+ "output_per_1m": 3.00
64
+ },
65
+ "gemini-2.5-pro": {
66
+ "input_per_1m": 1.25,
67
+ "output_per_1m": 10.00
68
+ },
69
+ "gemini-2.5-flash": {
70
+ "input_per_1m": 0.30,
71
+ "output_per_1m": 2.50
72
+ }
73
+ }
@@ -0,0 +1,38 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ import json
5
+ import importlib.resources as resources
6
+
7
+ from .exceptions import InvalidArguments, UnknownModelError
8
+
9
+ @dataclass(frozen=True, slots=True)
10
+ class ModelPrice:
11
+ input_per_1m: float
12
+ output_per_1m: float
13
+
14
+ def _load_model_prices() -> dict[str, dict[str, float]]:
15
+ data_file = resources.files(__package__).joinpath("models.json")
16
+ with data_file.open("r", encoding="utf-8") as handle:
17
+ raw = json.load(handle)
18
+ if not isinstance(raw, dict):
19
+ raise InvalidArguments("models.json must contain a JSON object at the root")
20
+ return raw
21
+
22
+ def get_model_price(model: str) -> ModelPrice:
23
+ if not isinstance(model, str) or not model.strip():
24
+ raise InvalidArguments("model must be a non-empty string")
25
+
26
+ models = _load_model_prices()
27
+ model_key = model.strip()
28
+ if model_key not in models:
29
+ raise UnknownModelError(f"Unknown model: {model_key}")
30
+
31
+ row = models[model_key]
32
+ try:
33
+ input_per_1m = float(row["input_per_1m"])
34
+ output_per_1m = float(row["output_per_1m"])
35
+ except (TypeError, KeyError, ValueError) as exc:
36
+ raise InvalidArguments(f"Invalid pricing entry for model: {model_key}") from exc
37
+
38
+ return ModelPrice(input_per_1m=input_per_1m, output_per_1m=output_per_1m)
@@ -0,0 +1,6 @@
1
+ from math import ceil
2
+
3
+ def estimate_tokens(text: str) -> int:
4
+ if not text:
5
+ return 0
6
+ return ceil(len(text) / 4) + 30
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "agentcap"
7
+ version = "0.1.0"
8
+ description = "Cost guardrails for LLM agent runs."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = { file = "LICENSE" }
12
+ authors = [
13
+ { name = "Oliver Iida" }
14
+ ]
15
+ keywords = ["llm", "agents", "cost-control", "openai", "anthropic", "gemini"]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Operating System :: OS Independent",
20
+ ]
21
+
22
+ dependencies = []
23
+
24
+ [project.urls]
25
+ Homepage = "https://github.com/OliverIida/agentcap"
26
+
27
+ [tool.hatch.build]
28
+ include = [
29
+ "agentcap/**",
30
+ "README.md",
31
+ "LICENSE",
32
+ ]