agentspend-tracker 0.1.0__py3-none-any.whl

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.
agentspend/__init__.py ADDED
@@ -0,0 +1,99 @@
1
+ import requests
2
+ import threading
3
+
4
+ _api_key = None
5
+ _base_url = "https://agentspend-backend-production.up.railway.app"
6
+
7
+
8
+ def init(api_key: str):
9
+ """Initialize AgentSpend with your API key."""
10
+ global _api_key
11
+ _api_key = api_key
12
+
13
+
14
+ def track(
15
+ agent_name: str,
16
+ model: str,
17
+ input_tokens: int,
18
+ output_tokens: int,
19
+ cost_usd: float = None,
20
+ project: str = None,
21
+ ):
22
+ """Track an LLM call. Call this after every Claude/OpenAI/etc. request."""
23
+ if _api_key is None:
24
+ raise ValueError("Call agentspend.init('your-api-key') first.")
25
+
26
+ if cost_usd is None:
27
+ cost_usd = _estimate_cost(model, input_tokens, output_tokens)
28
+
29
+ payload = {
30
+ "agent_name": agent_name,
31
+ "model": model,
32
+ "input_tokens": input_tokens,
33
+ "output_tokens": output_tokens,
34
+ "cost_usd": round(cost_usd, 6),
35
+ "project": project,
36
+ }
37
+
38
+ # Send in background so it doesn't slow down the agent
39
+ thread = threading.Thread(target=_send, args=(payload,), daemon=True)
40
+ thread.start()
41
+
42
+
43
+ def track_anthropic(response, agent_name: str, project: str = None):
44
+ """Auto-track an Anthropic Claude response object."""
45
+ model = response.model
46
+ input_tokens = response.usage.input_tokens
47
+ output_tokens = response.usage.output_tokens
48
+ cost_usd = _estimate_cost(model, input_tokens, output_tokens)
49
+ track(
50
+ agent_name=agent_name,
51
+ model=model,
52
+ input_tokens=input_tokens,
53
+ output_tokens=output_tokens,
54
+ cost_usd=cost_usd,
55
+ project=project,
56
+ )
57
+
58
+
59
+ def track_openai(response, agent_name: str, project: str = None):
60
+ """Auto-track an OpenAI response object."""
61
+ model = response.model
62
+ input_tokens = response.usage.prompt_tokens
63
+ output_tokens = response.usage.completion_tokens
64
+ cost_usd = _estimate_cost(model, input_tokens, output_tokens)
65
+ track(
66
+ agent_name=agent_name,
67
+ model=model,
68
+ input_tokens=input_tokens,
69
+ output_tokens=output_tokens,
70
+ cost_usd=cost_usd,
71
+ project=project,
72
+ )
73
+
74
+
75
+ def _estimate_cost(model: str, input_tokens: int, output_tokens: int) -> float:
76
+ pricing = {
77
+ "claude-opus-4-8": (15.0, 75.0),
78
+ "claude-sonnet-4-6": (3.0, 15.0),
79
+ "claude-haiku-4-5-20251001": (0.8, 4.0),
80
+ "claude-3-5-sonnet-20241022":(3.0, 15.0),
81
+ "claude-3-5-haiku-20241022": (0.8, 4.0),
82
+ "gpt-4o": (2.5, 10.0),
83
+ "gpt-4o-mini": (0.15, 0.6),
84
+ "gpt-4-turbo": (10.0, 30.0),
85
+ }
86
+ input_price, output_price = pricing.get(model, (3.0, 15.0))
87
+ return (input_tokens * input_price / 1_000_000) + (output_tokens * output_price / 1_000_000)
88
+
89
+
90
+ def _send(payload: dict):
91
+ try:
92
+ requests.post(
93
+ f"{_base_url}/events",
94
+ headers={"Authorization": f"Bearer {_api_key}"},
95
+ json=payload,
96
+ timeout=5,
97
+ )
98
+ except Exception:
99
+ pass # Never crash the user's agent
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentspend-tracker
3
+ Version: 0.1.0
4
+ Summary: Track LLM costs across your AI agents
5
+ Author: Sankar
6
+ Requires-Python: >=3.8
7
+ Requires-Dist: requests>=2.28.0
8
+ Dynamic: author
9
+ Dynamic: requires-dist
10
+ Dynamic: requires-python
11
+ Dynamic: summary
@@ -0,0 +1,5 @@
1
+ agentspend/__init__.py,sha256=DZfJLRPN_TmPod-oldf6XGjublKuWBMy22GAHpHPx68,3008
2
+ agentspend_tracker-0.1.0.dist-info/METADATA,sha256=_ZyLrAMdj2q_TCH77UDHgr_yXUJk1CsUZJJVoXDZ0l8,271
3
+ agentspend_tracker-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
4
+ agentspend_tracker-0.1.0.dist-info/top_level.txt,sha256=NxRmonI3i5h_eqUQt0XwwkEkl9AFWHQn6NkvoAxgiUw,11
5
+ agentspend_tracker-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ agentspend