agentspend-tracker 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,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,66 @@
1
+ # AgentSpend SDK
2
+
3
+ Track LLM costs across your AI agents in real time.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pip install agentspend
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```python
14
+ import agentspend
15
+
16
+ agentspend.init("your-api-key") # Get your key from the AgentSpend app
17
+ ```
18
+
19
+ ## Track Anthropic Claude
20
+
21
+ ```python
22
+ import anthropic
23
+ import agentspend
24
+
25
+ agentspend.init("as_your_key_here")
26
+
27
+ client = anthropic.Anthropic(api_key="sk-ant-...")
28
+ response = client.messages.create(
29
+ model="claude-sonnet-4-6",
30
+ max_tokens=1024,
31
+ messages=[{"role": "user", "content": "Hello!"}]
32
+ )
33
+
34
+ agentspend.track_anthropic(response, agent_name="my-agent")
35
+ ```
36
+
37
+ ## Track OpenAI
38
+
39
+ ```python
40
+ import openai
41
+ import agentspend
42
+
43
+ agentspend.init("as_your_key_here")
44
+
45
+ client = openai.OpenAI(api_key="sk-...")
46
+ response = client.chat.completions.create(
47
+ model="gpt-4o",
48
+ messages=[{"role": "user", "content": "Hello!"}]
49
+ )
50
+
51
+ agentspend.track_openai(response, agent_name="my-agent")
52
+ ```
53
+
54
+ ## Manual Tracking
55
+
56
+ ```python
57
+ agentspend.track(
58
+ agent_name="my-agent",
59
+ model="claude-sonnet-4-6",
60
+ input_tokens=1000,
61
+ output_tokens=500,
62
+ project="my-project" # optional
63
+ )
64
+ ```
65
+
66
+ Get your API key at agentspend.app
@@ -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,8 @@
1
+ README.md
2
+ setup.py
3
+ agentspend/__init__.py
4
+ agentspend_tracker.egg-info/PKG-INFO
5
+ agentspend_tracker.egg-info/SOURCES.txt
6
+ agentspend_tracker.egg-info/dependency_links.txt
7
+ agentspend_tracker.egg-info/requires.txt
8
+ agentspend_tracker.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ requests>=2.28.0
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,11 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="agentspend-tracker",
5
+ version="0.1.0",
6
+ description="Track LLM costs across your AI agents",
7
+ author="Sankar",
8
+ packages=find_packages(),
9
+ install_requires=["requests>=2.28.0"],
10
+ python_requires=">=3.8",
11
+ )