agentcogs 0.2.0__tar.gz → 0.2.1__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.
- {agentcogs-0.2.0 → agentcogs-0.2.1}/PKG-INFO +1 -1
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/_shekel_compat.py +19 -5
- agentcogs-0.2.1/src/agentcogs/_version.py +1 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/budget.py +44 -33
- agentcogs-0.2.1/src/agentcogs/provider_watch.py +213 -0
- agentcogs-0.2.0/src/agentcogs/_version.py +0 -1
- {agentcogs-0.2.0 → agentcogs-0.2.1}/.gitignore +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/LICENSE +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/PYPI.md +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/pyproject.toml +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/__init__.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/__main__.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/assistant.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/cli.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/client.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/config.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/context.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/data/price_book_fixture_v1.json +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/errors.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/instrument.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/integrations/__init__.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/integrations/fastapi.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/integrations/langgraph.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/integrations/litellm_event.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/kernel/__init__.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/kernel/attribute.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/kernel/money.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/localhttp.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/localstore.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/outbox.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/ping.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/reports.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/shutdown.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/star.py +0 -0
- {agentcogs-0.2.0 → agentcogs-0.2.1}/src/agentcogs/tokens.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: agentcogs
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: Customer-level AI unit economics. Compare each customer's LLM cost with their monthly fee
|
|
5
5
|
Project-URL: Homepage, https://agentcogs.vercel.app
|
|
6
6
|
Project-URL: Repository, https://github.com/vaibhav11123/agentcogs
|
|
@@ -43,19 +43,33 @@ def lanes_from_usage(provider: str, usage: dict) -> dict:
|
|
|
43
43
|
"""
|
|
44
44
|
name = (provider or "").lower()
|
|
45
45
|
if name == "openai":
|
|
46
|
-
|
|
46
|
+
# Chat Completions reports prompt_tokens. The Responses API reports
|
|
47
|
+
# input_tokens. Cached and cache-write tokens are subsets of that total.
|
|
48
|
+
details = usage.get("input_tokens_details") or usage.get("prompt_tokens_details") or {}
|
|
47
49
|
cache_read = int(details.get("cached_tokens") or 0)
|
|
48
50
|
write_details = usage.get("cache_write_tokens_details") or {}
|
|
49
51
|
if "ttl_30m_tokens" in write_details or "ttl_12h_tokens" in write_details:
|
|
50
52
|
write_30m = int(write_details.get("ttl_30m_tokens") or 0)
|
|
51
53
|
write_12h = int(write_details.get("ttl_12h_tokens") or 0)
|
|
52
|
-
|
|
54
|
+
elif "cache_write_tokens" in usage:
|
|
53
55
|
write_30m = int(usage.get("cache_write_tokens") or 0)
|
|
54
56
|
write_12h = 0
|
|
55
|
-
|
|
57
|
+
else:
|
|
58
|
+
write_30m = int(details.get("cache_write_tokens") or 0)
|
|
59
|
+
write_12h = 0
|
|
60
|
+
if "input_tokens" in usage and usage.get("input_tokens") is not None:
|
|
61
|
+
reported = int(usage.get("input_tokens") or 0)
|
|
62
|
+
else:
|
|
63
|
+
reported = int(usage.get("prompt_tokens") or 0)
|
|
64
|
+
uncached = reported - cache_read - write_30m - write_12h
|
|
56
65
|
if uncached < 0:
|
|
57
66
|
raise ValueError("LANE_NEGATIVE")
|
|
58
|
-
|
|
67
|
+
if "output_tokens" in usage and usage.get("output_tokens") is not None:
|
|
68
|
+
output = int(usage.get("output_tokens") or 0)
|
|
69
|
+
reasoning = usage.get("output_tokens_details") or {}
|
|
70
|
+
else:
|
|
71
|
+
output = int(usage.get("completion_tokens") or 0)
|
|
72
|
+
reasoning = usage.get("completion_tokens_details") or {}
|
|
59
73
|
return {
|
|
60
74
|
"uncached_input": uncached,
|
|
61
75
|
"cache_read": cache_read,
|
|
@@ -63,7 +77,7 @@ def lanes_from_usage(provider: str, usage: dict) -> dict:
|
|
|
63
77
|
"cache_write_1h": 0,
|
|
64
78
|
"cache_write_30m": write_30m,
|
|
65
79
|
"cache_write_12h": write_12h,
|
|
66
|
-
"output":
|
|
80
|
+
"output": output,
|
|
67
81
|
"reasoning": int(reasoning.get("reasoning_tokens") or 0),
|
|
68
82
|
}
|
|
69
83
|
if name in {"anthropic", "claude"}:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.1"
|
|
@@ -12,6 +12,7 @@ from .client import emit_event, emit_event_sync, fetch_budget
|
|
|
12
12
|
from .config import get_config
|
|
13
13
|
from .context import resolve_customer, resolve_workflow
|
|
14
14
|
from .errors import ConfigurationError, CustomerBudgetExceededError
|
|
15
|
+
from .provider_watch import pop as pop_watch, priced_calls, push as push_watch, snapshot
|
|
15
16
|
|
|
16
17
|
log = logging.getLogger("agentcogs")
|
|
17
18
|
|
|
@@ -124,39 +125,49 @@ def run(
|
|
|
124
125
|
run_ctx: Optional[RunContext] = None
|
|
125
126
|
|
|
126
127
|
with shekel_budget(**kwargs) as ctx:
|
|
128
|
+
watch = push_watch()
|
|
127
129
|
try:
|
|
128
|
-
run_ctx = RunContext(ctx, run_id, resolved_customer, resolved_workflow)
|
|
129
|
-
yield run_ctx
|
|
130
|
-
except CustomerBudgetExceededError:
|
|
131
|
-
status = "budget_exceeded"
|
|
132
|
-
raise
|
|
133
|
-
except Exception as e:
|
|
134
|
-
status = "error"
|
|
135
|
-
error_msg = str(e)[:500]
|
|
136
|
-
raise
|
|
137
|
-
finally:
|
|
138
130
|
try:
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
"ts": int(time.time()),
|
|
145
|
-
"status": status,
|
|
146
|
-
"total_usd": float(summary.get("total_cost") or 0),
|
|
147
|
-
"models": normalize_summary(summary.get("by_model", {})),
|
|
148
|
-
"node_costs": getattr(ctx, "node_costs", {}),
|
|
149
|
-
"metadata": _cap_metadata(metadata),
|
|
150
|
-
"error": error_msg,
|
|
151
|
-
}
|
|
152
|
-
if run_ctx is not None:
|
|
153
|
-
run_ctx._event = event
|
|
154
|
-
emit_event(event)
|
|
131
|
+
run_ctx = RunContext(ctx, run_id, resolved_customer, resolved_workflow)
|
|
132
|
+
yield run_ctx
|
|
133
|
+
except CustomerBudgetExceededError:
|
|
134
|
+
status = "budget_exceeded"
|
|
135
|
+
raise
|
|
155
136
|
except Exception as e:
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
137
|
+
status = "error"
|
|
138
|
+
error_msg = str(e)[:500]
|
|
139
|
+
raise
|
|
140
|
+
finally:
|
|
141
|
+
try:
|
|
142
|
+
summary = normalize_summary_data(ctx.summary_data())
|
|
143
|
+
event = {
|
|
144
|
+
"run_id": run_id,
|
|
145
|
+
"customer_id": resolved_customer,
|
|
146
|
+
"workflow_id": resolved_workflow,
|
|
147
|
+
"ts": int(time.time()),
|
|
148
|
+
"status": status,
|
|
149
|
+
"total_usd": float(summary.get("total_cost") or 0),
|
|
150
|
+
"models": normalize_summary(summary.get("by_model", {})),
|
|
151
|
+
"node_costs": getattr(ctx, "node_costs", {}),
|
|
152
|
+
"metadata": _cap_metadata(metadata),
|
|
153
|
+
"error": error_msg,
|
|
154
|
+
}
|
|
155
|
+
priced = priced_calls(snapshot())
|
|
156
|
+
if priced is not None:
|
|
157
|
+
event["calls"] = priced["calls"]
|
|
158
|
+
event["book_id"] = priced["book_id"]
|
|
159
|
+
event["total_usd"] = priced["total_usd"]
|
|
160
|
+
event["models"] = priced["models"]
|
|
161
|
+
if run_ctx is not None:
|
|
162
|
+
run_ctx._event = event
|
|
163
|
+
emit_event(event)
|
|
164
|
+
except Exception as e:
|
|
165
|
+
log.warning("telemetry failed", exc_info=True)
|
|
166
|
+
cb = cfg.on_telemetry_error
|
|
167
|
+
if cb is not None:
|
|
168
|
+
try:
|
|
169
|
+
cb(e)
|
|
170
|
+
except Exception:
|
|
171
|
+
pass
|
|
172
|
+
finally:
|
|
173
|
+
pop_watch(watch)
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""Record provider usage inside run() and price it with the shipped book.
|
|
2
|
+
|
|
3
|
+
Shekel keeps prompt and completion totals. The chat-completions response also
|
|
4
|
+
carries cache reads and cache writes. Those lanes are what the server prices.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
import threading
|
|
11
|
+
from contextvars import ContextVar
|
|
12
|
+
from decimal import Decimal
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from ._shekel_compat import lanes_from_usage
|
|
16
|
+
from .kernel.attribute import price_call
|
|
17
|
+
from .localstore import price_book
|
|
18
|
+
|
|
19
|
+
log = logging.getLogger("agentcogs")
|
|
20
|
+
|
|
21
|
+
_bucket: ContextVar[list | None] = ContextVar("agentcogs_provider_calls", default=None)
|
|
22
|
+
_lock = threading.Lock()
|
|
23
|
+
_depth = 0
|
|
24
|
+
_saved: dict[str, Any] = {}
|
|
25
|
+
|
|
26
|
+
_INPUT_LANES = (
|
|
27
|
+
"uncached_input",
|
|
28
|
+
"cache_read",
|
|
29
|
+
"cache_write_5m",
|
|
30
|
+
"cache_write_1h",
|
|
31
|
+
"cache_write_30m",
|
|
32
|
+
"cache_write_12h",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def push() -> Any:
|
|
37
|
+
_install()
|
|
38
|
+
box: list = []
|
|
39
|
+
return _bucket.set(box)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def pop(token: Any) -> None:
|
|
43
|
+
_bucket.reset(token)
|
|
44
|
+
_remove()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def snapshot() -> list[dict]:
|
|
48
|
+
box = _bucket.get()
|
|
49
|
+
if not box:
|
|
50
|
+
return []
|
|
51
|
+
return list(box)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def micro_as_usd(micro: int) -> float:
|
|
55
|
+
"""Six-decimal dollars. The server parses this back to the same micros."""
|
|
56
|
+
text = format(Decimal(int(micro)) / Decimal(1_000_000), "f")
|
|
57
|
+
return float(text)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def priced_calls(records: list[dict]) -> dict | None:
|
|
61
|
+
"""Return ingest fields for these provider responses, or None when empty."""
|
|
62
|
+
if not records:
|
|
63
|
+
return None
|
|
64
|
+
book = price_book()
|
|
65
|
+
calls = []
|
|
66
|
+
by_model: dict[str, dict] = {}
|
|
67
|
+
total = 0
|
|
68
|
+
try:
|
|
69
|
+
for index, record in enumerate(records):
|
|
70
|
+
lanes = lanes_from_usage(record["provider"], record["usage"])
|
|
71
|
+
_priced, micro = price_call(lanes, book)
|
|
72
|
+
model = record.get("model") or "unknown"
|
|
73
|
+
call = {"call_id": f"c{index}", "model": model, "lanes": lanes}
|
|
74
|
+
if record["provider"] == "openai":
|
|
75
|
+
usage = record["usage"]
|
|
76
|
+
if "input_tokens" in usage and usage.get("input_tokens") is not None:
|
|
77
|
+
call["provider_input_tokens"] = int(usage.get("input_tokens") or 0)
|
|
78
|
+
elif "prompt_tokens" in usage:
|
|
79
|
+
call["provider_input_tokens"] = int(usage.get("prompt_tokens") or 0)
|
|
80
|
+
calls.append(call)
|
|
81
|
+
total += micro
|
|
82
|
+
row = by_model.setdefault(
|
|
83
|
+
model, {"input_tokens": 0, "output_tokens": 0, "usd": 0.0, "_micro": 0}
|
|
84
|
+
)
|
|
85
|
+
row["input_tokens"] += sum(int(lanes.get(name, 0)) for name in _INPUT_LANES)
|
|
86
|
+
row["output_tokens"] += int(lanes.get("output", 0))
|
|
87
|
+
row["_micro"] += micro
|
|
88
|
+
except ValueError as exc:
|
|
89
|
+
log.warning("provider usage was not priced (%s)", exc)
|
|
90
|
+
return None
|
|
91
|
+
models = {
|
|
92
|
+
model: {
|
|
93
|
+
"input_tokens": row["input_tokens"],
|
|
94
|
+
"output_tokens": row["output_tokens"],
|
|
95
|
+
"usd": micro_as_usd(row["_micro"]),
|
|
96
|
+
}
|
|
97
|
+
for model, row in by_model.items()
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
"calls": calls,
|
|
101
|
+
"book_id": book["book_id"],
|
|
102
|
+
"total_usd": micro_as_usd(total),
|
|
103
|
+
"models": models,
|
|
104
|
+
"total_micro": total,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _remember(provider: str, model: str | None, usage: Any) -> None:
|
|
109
|
+
box = _bucket.get()
|
|
110
|
+
if box is None or usage is None:
|
|
111
|
+
return
|
|
112
|
+
if hasattr(usage, "model_dump"):
|
|
113
|
+
payload = usage.model_dump()
|
|
114
|
+
elif isinstance(usage, dict):
|
|
115
|
+
payload = dict(usage)
|
|
116
|
+
else:
|
|
117
|
+
return
|
|
118
|
+
box.append({"provider": provider, "model": model, "usage": payload})
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _install() -> None:
|
|
122
|
+
global _depth
|
|
123
|
+
with _lock:
|
|
124
|
+
_depth += 1
|
|
125
|
+
if _depth == 1:
|
|
126
|
+
_wrap()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _remove() -> None:
|
|
130
|
+
global _depth
|
|
131
|
+
with _lock:
|
|
132
|
+
_depth -= 1
|
|
133
|
+
if _depth == 0:
|
|
134
|
+
_unwrap()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _wrap() -> None:
|
|
138
|
+
try:
|
|
139
|
+
import openai.resources.chat.completions as oai
|
|
140
|
+
|
|
141
|
+
_saved["openai"] = oai.Completions.create
|
|
142
|
+
oai.Completions.create = _openai_create # type: ignore[method-assign]
|
|
143
|
+
except ImportError:
|
|
144
|
+
pass
|
|
145
|
+
try:
|
|
146
|
+
import anthropic.resources.messages as ant
|
|
147
|
+
|
|
148
|
+
_saved["anthropic"] = ant.Messages.create
|
|
149
|
+
ant.Messages.create = _anthropic_create # type: ignore[method-assign]
|
|
150
|
+
except ImportError:
|
|
151
|
+
pass
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _unwrap() -> None:
|
|
155
|
+
if "openai" in _saved:
|
|
156
|
+
import openai.resources.chat.completions as oai
|
|
157
|
+
|
|
158
|
+
oai.Completions.create = _saved.pop("openai")
|
|
159
|
+
if "anthropic" in _saved:
|
|
160
|
+
import anthropic.resources.messages as ant
|
|
161
|
+
|
|
162
|
+
ant.Messages.create = _saved.pop("anthropic")
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _openai_create(self: Any, *args: Any, **kwargs: Any) -> Any:
|
|
166
|
+
original = _saved.get("openai")
|
|
167
|
+
response = original(self, *args, **kwargs)
|
|
168
|
+
if kwargs.get("stream") is True:
|
|
169
|
+
return _openai_stream(response)
|
|
170
|
+
_remember("openai", getattr(response, "model", None), getattr(response, "usage", None))
|
|
171
|
+
return response
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _openai_stream(stream: Any):
|
|
175
|
+
last = None
|
|
176
|
+
model = None
|
|
177
|
+
for chunk in stream:
|
|
178
|
+
usage = getattr(chunk, "usage", None)
|
|
179
|
+
if usage is not None:
|
|
180
|
+
last = usage
|
|
181
|
+
model = getattr(chunk, "model", None) or model
|
|
182
|
+
yield chunk
|
|
183
|
+
if last is not None:
|
|
184
|
+
_remember("openai", model, last)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _anthropic_create(self: Any, *args: Any, **kwargs: Any) -> Any:
|
|
188
|
+
original = _saved.get("anthropic")
|
|
189
|
+
response = original(self, *args, **kwargs)
|
|
190
|
+
if kwargs.get("stream") is True:
|
|
191
|
+
return _anthropic_stream(response)
|
|
192
|
+
_remember("anthropic", getattr(response, "model", None), getattr(response, "usage", None))
|
|
193
|
+
return response
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _anthropic_stream(stream: Any):
|
|
197
|
+
merged: dict = {}
|
|
198
|
+
model = None
|
|
199
|
+
for event in stream:
|
|
200
|
+
kind = getattr(event, "type", None)
|
|
201
|
+
if kind == "message_start":
|
|
202
|
+
message = getattr(event, "message", None)
|
|
203
|
+
model = getattr(message, "model", None) or model
|
|
204
|
+
usage = getattr(message, "usage", None)
|
|
205
|
+
if usage is not None and hasattr(usage, "model_dump"):
|
|
206
|
+
merged.update(usage.model_dump())
|
|
207
|
+
elif kind == "message_delta":
|
|
208
|
+
usage = getattr(event, "usage", None)
|
|
209
|
+
if usage is not None and hasattr(usage, "model_dump"):
|
|
210
|
+
merged.update(usage.model_dump())
|
|
211
|
+
yield event
|
|
212
|
+
if merged:
|
|
213
|
+
_remember("anthropic", model, merged)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.0"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|