agentbudget 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.
- agentbudget-0.1.0/LICENSE +201 -0
- agentbudget-0.1.0/PKG-INFO +288 -0
- agentbudget-0.1.0/README.md +255 -0
- agentbudget-0.1.0/agentbudget/__init__.py +17 -0
- agentbudget-0.1.0/agentbudget/budget.py +126 -0
- agentbudget-0.1.0/agentbudget/circuit_breaker.py +72 -0
- agentbudget-0.1.0/agentbudget/exceptions.py +24 -0
- agentbudget-0.1.0/agentbudget/integrations/__init__.py +1 -0
- agentbudget-0.1.0/agentbudget/integrations/crewai.py +63 -0
- agentbudget-0.1.0/agentbudget/integrations/langchain.py +89 -0
- agentbudget-0.1.0/agentbudget/ledger.py +87 -0
- agentbudget-0.1.0/agentbudget/pricing.py +78 -0
- agentbudget-0.1.0/agentbudget/py.typed +0 -0
- agentbudget-0.1.0/agentbudget/session.py +299 -0
- agentbudget-0.1.0/agentbudget/types.py +53 -0
- agentbudget-0.1.0/agentbudget/webhook.py +58 -0
- agentbudget-0.1.0/agentbudget.egg-info/PKG-INFO +288 -0
- agentbudget-0.1.0/agentbudget.egg-info/SOURCES.txt +33 -0
- agentbudget-0.1.0/agentbudget.egg-info/dependency_links.txt +1 -0
- agentbudget-0.1.0/agentbudget.egg-info/requires.txt +10 -0
- agentbudget-0.1.0/agentbudget.egg-info/top_level.txt +1 -0
- agentbudget-0.1.0/pyproject.toml +49 -0
- agentbudget-0.1.0/setup.cfg +4 -0
- agentbudget-0.1.0/tests/test_async.py +92 -0
- agentbudget-0.1.0/tests/test_budget.py +76 -0
- agentbudget-0.1.0/tests/test_circuit_breaker.py +82 -0
- agentbudget-0.1.0/tests/test_exceptions.py +37 -0
- agentbudget-0.1.0/tests/test_integration.py +134 -0
- agentbudget-0.1.0/tests/test_integrations.py +49 -0
- agentbudget-0.1.0/tests/test_ledger.py +83 -0
- agentbudget-0.1.0/tests/test_nested.py +84 -0
- agentbudget-0.1.0/tests/test_pricing.py +99 -0
- agentbudget-0.1.0/tests/test_session.py +195 -0
- agentbudget-0.1.0/tests/test_types.py +69 -0
- agentbudget-0.1.0/tests/test_webhook.py +79 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentbudget
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Real-time cost enforcement for AI agent sessions
|
|
5
|
+
Author: Sahil Jagtap
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/sahiljagtap08/agentbudget
|
|
8
|
+
Project-URL: Repository, https://github.com/sahiljagtap08/agentbudget
|
|
9
|
+
Project-URL: Issues, https://github.com/sahiljagtap08/agentbudget/issues
|
|
10
|
+
Keywords: ai,agents,budget,cost-tracking,llm
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: langchain
|
|
25
|
+
Requires-Dist: langchain-core>=0.1.0; extra == "langchain"
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
|
|
30
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
31
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# AgentBudget SDK
|
|
35
|
+
|
|
36
|
+
### Real-time cost enforcement for AI agent sessions.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## What is AgentBudget?
|
|
41
|
+
|
|
42
|
+
AgentBudget is an open-source SDK that gives developers a dead-simple way to put a dollar limit on any AI agent session. It wraps LLM calls, tool calls, and external API requests with real-time cost tracking and automatic circuit breaking — so your agent can never silently burn through your budget.
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from agentbudget import AgentBudget
|
|
46
|
+
|
|
47
|
+
budget = AgentBudget(max_spend="$5.00")
|
|
48
|
+
|
|
49
|
+
with budget.session() as session:
|
|
50
|
+
# Every LLM call is tracked and costed automatically
|
|
51
|
+
response = session.wrap(openai.chat.completions.create(
|
|
52
|
+
model="gpt-4o",
|
|
53
|
+
messages=[{"role": "user", "content": "Research competitors in the CRM space"}]
|
|
54
|
+
))
|
|
55
|
+
|
|
56
|
+
# Tool calls and external APIs get custom cost annotations
|
|
57
|
+
result = session.track(call_serp_api(query="CRM market"), cost=0.01)
|
|
58
|
+
|
|
59
|
+
# When the $5 limit is hit, the session raises BudgetExhausted
|
|
60
|
+
# No silent overruns. No surprise bills.
|
|
61
|
+
|
|
62
|
+
print(session.report())
|
|
63
|
+
# Total spend: $3.42 | Remaining: $1.58 | LLM: $3.12 | Tools: $0.30 | Calls: 14
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**One line to set a budget. Zero infrastructure to manage. Works with any LLM provider.**
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## The Problem
|
|
71
|
+
|
|
72
|
+
AI agents are unpredictable by design. Unlike traditional software where you know exactly how many API calls a function makes, an agent decides at runtime how many LLM calls to make, which tools to invoke, and how many retries to attempt. This creates a class of cost failures that didn't exist before:
|
|
73
|
+
|
|
74
|
+
### The Loop Problem
|
|
75
|
+
An agent gets stuck in a reasoning loop — retrying the same failed tool call, rephrasing the same query, or recursing through subtasks. A single stuck agent can make hundreds of LLM calls in minutes. At $0.03–$0.15 per call, a 10-minute loop can cost $50–$200 before anyone notices.
|
|
76
|
+
|
|
77
|
+
### The Invisible Spend Problem
|
|
78
|
+
Most agent frameworks track token counts, but tokens aren't dollars. A GPT-4o call costs 15x more than a GPT-4o-mini call for similar token counts. A Serp API call costs $0.01. A Stripe API call is free. A premium data enrichment call costs $0.50. Agents chain all of these together, and no single system tracks the combined real-dollar cost of a session.
|
|
79
|
+
|
|
80
|
+
### The Multi-Provider Problem
|
|
81
|
+
A single agent session might call OpenAI for reasoning, Anthropic for analysis, Google for search, and three different SaaS APIs for data. Each provider has its own pricing model, its own billing dashboard, and its own latency before costs appear. There's no unified real-time view of what a single agent run actually cost.
|
|
82
|
+
|
|
83
|
+
### The Scaling Problem
|
|
84
|
+
When you go from 1 agent to 1,000 concurrent agent sessions serving customers, a 5% failure rate on cost control means 50 runaway sessions. Without per-session budget enforcement, your monthly bill becomes a function of your worst-case agent behavior multiplied by your user count.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Who is AgentBudget For?
|
|
89
|
+
|
|
90
|
+
### 1. Developers building AI agent products
|
|
91
|
+
|
|
92
|
+
If you're shipping an AI agent to customers — a support bot, a research assistant, a coding agent, a workflow automation tool — you need to guarantee that each customer session stays within a cost envelope. Your margins depend on it.
|
|
93
|
+
|
|
94
|
+
**Your pain today:** You set `max_tokens` on LLM calls and hope for the best. You find out about cost overruns from your cloud bill 24 hours later. You've been burned at least once by a stuck agent loop.
|
|
95
|
+
|
|
96
|
+
**With AgentBudget:** Every customer session gets a budget. If a session costs more than expected, it's killed automatically. You see per-session cost breakdowns in real time. Your unit economics become predictable.
|
|
97
|
+
|
|
98
|
+
### 2. Teams deploying internal AI agents
|
|
99
|
+
|
|
100
|
+
Engineering teams using AI agents for code review, data analysis, document processing, or internal tooling. The agents work great 95% of the time, but the other 5% produce surprise bills that make finance teams nervous.
|
|
101
|
+
|
|
102
|
+
**Your pain today:** No one knows which internal agent costs what. The shared OpenAI API key gets a $2,000 bill and nobody can attribute it. Leadership asks "what's the ROI on our AI spend?" and you can't answer per-workflow.
|
|
103
|
+
|
|
104
|
+
**With AgentBudget:** Each workflow gets a budget. Costs are attributed to specific tasks. Monthly spend becomes forecastable. You can finally answer "this document processing agent costs $0.12 per document on average, with a hard cap at $0.50."
|
|
105
|
+
|
|
106
|
+
### 3. AI platform builders (the AirStitch / Zapier / n8n use case)
|
|
107
|
+
|
|
108
|
+
If you're building a platform where users create their own agents or automations, you need to prevent any single user's workflow from consuming disproportionate resources — and you need to meter their usage for billing.
|
|
109
|
+
|
|
110
|
+
**Your pain today:** User-created agents are black boxes. A user builds a workflow that accidentally loops and it eats your infrastructure budget. You can't meter individual workflow costs for usage-based billing because cost data is fragmented across providers.
|
|
111
|
+
|
|
112
|
+
**With AgentBudget:** Each user workflow gets a budget envelope. If a user's automation exceeds their plan limit, it's halted gracefully. You get structured cost events you can pipe into your billing system.
|
|
113
|
+
|
|
114
|
+
### 4. Researchers and hobbyists
|
|
115
|
+
|
|
116
|
+
Anyone experimenting with agents who has accidentally left a LangChain agent running overnight and woke up to a $300 OpenAI bill. You just want a safety net.
|
|
117
|
+
|
|
118
|
+
**With AgentBudget:** `budget = AgentBudget("$2.00")` — that's it. Two-dollar hard cap. Sleep peacefully.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Why Existing Tools Don't Solve This
|
|
123
|
+
|
|
124
|
+
| Tool | What it does | What's missing |
|
|
125
|
+
|---|---|---|
|
|
126
|
+
| **LangSmith** | Tracks token usage and cost *after the fact*. Dashboard shows what you spent. | No real-time enforcement. It's observability, not prevention. By the time you see the cost, you've already paid it. |
|
|
127
|
+
| **LangChain ModelCallLimitMiddleware** | Limits the *number* of LLM calls per session. | Counts calls, not dollars. 10 cheap calls and 10 expensive calls look the same. Doesn't track tool/API costs at all. |
|
|
128
|
+
| **Langfuse** | Open-source LLM observability. Tracks cost per trace. | Same as LangSmith — it's a monitoring tool, not an enforcement tool. No circuit breaking. |
|
|
129
|
+
| **Lava AI Spend** | Per-API-key spend caps with auto-cutoff. | Operates at the API key level, not the session level. Can't give User A a $5 budget and User B a $10 budget on the same key. Doesn't track non-LLM costs (tool calls, external APIs). |
|
|
130
|
+
| **OpenAI/Anthropic usage limits** | Monthly org-level spend caps. | Way too coarse. A monthly cap doesn't help when one session burns $200 in 10 minutes. No per-session granularity. |
|
|
131
|
+
| **DIY callbacks** | Custom LangChain callbacks that track `total_cost` and raise exceptions. | Everyone rebuilds this from scratch. No standard interface. Usually OpenAI-only. Doesn't handle tool costs, multi-provider, or async agents. Breaks when you switch frameworks. |
|
|
132
|
+
|
|
133
|
+
**The gap AgentBudget fills:** Real-time, dollar-denominated, per-session budget enforcement that spans LLM calls + tool calls + external APIs, works across providers, and kills runaway sessions automatically.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Core Concepts
|
|
138
|
+
|
|
139
|
+
### Budget Envelope
|
|
140
|
+
A budget envelope is a dollar amount assigned to a unit of work — a session, a task, a user request, or a workflow run. Every cost incurred within that envelope is tracked in real time. When the budget is exhausted, the session terminates.
|
|
141
|
+
|
|
142
|
+
### Cost Sources
|
|
143
|
+
AgentBudget tracks three categories of cost:
|
|
144
|
+
|
|
145
|
+
1. **LLM calls** — Automatically costed using a built-in pricing table that maps model names to per-token input/output prices. Updated regularly. Supports OpenAI, Anthropic, Google, Mistral, Cohere, and open-source models via common inference providers.
|
|
146
|
+
|
|
147
|
+
2. **Tracked tool calls** — External API calls where the developer annotates the cost. Example: `session.track(serp_api_call(), cost=0.01)`. This is for APIs with known per-call pricing.
|
|
148
|
+
|
|
149
|
+
3. **Estimated costs** — For tool calls where cost isn't deterministic, developers can provide cost estimation functions. Example: a database query where cost depends on rows scanned.
|
|
150
|
+
|
|
151
|
+
### Circuit Breaker
|
|
152
|
+
When remaining budget drops below a configurable threshold, the circuit breaker activates:
|
|
153
|
+
|
|
154
|
+
- **Soft limit** (default 90% spent): Emits a warning event. Agent can choose to wrap up gracefully.
|
|
155
|
+
- **Hard limit** (default 100% spent): Raises `BudgetExhausted` exception. No more calls allowed.
|
|
156
|
+
- **Loop detection**: If the same tool is called N times with similar arguments within a time window, the circuit breaker trips early — even if budget remains. This catches infinite loops before they drain the budget.
|
|
157
|
+
|
|
158
|
+
### Cost Report
|
|
159
|
+
Every session produces a structured cost report:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
{
|
|
163
|
+
"session_id": "sess_abc123",
|
|
164
|
+
"budget": 5.00,
|
|
165
|
+
"total_spent": 3.42,
|
|
166
|
+
"remaining": 1.58,
|
|
167
|
+
"breakdown": {
|
|
168
|
+
"llm": {"total": 3.12, "calls": 8, "by_model": {"gpt-4o": 2.80, "gpt-4o-mini": 0.32}},
|
|
169
|
+
"tools": {"total": 0.30, "calls": 6, "by_tool": {"serp_api": 0.05, "scrape": 0.25}},
|
|
170
|
+
},
|
|
171
|
+
"duration_seconds": 34.2,
|
|
172
|
+
"terminated_by": null, # or "budget_exhausted" or "loop_detected"
|
|
173
|
+
"events": [...] # Full timeline of every costed action
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
This report can be piped to your observability stack, your billing system, or simply logged.
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Architecture
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
┌─────────────────────────────────────────────────────────┐
|
|
185
|
+
│ Your Agent Code │
|
|
186
|
+
│ │
|
|
187
|
+
│ budget = AgentBudget("$5.00") │
|
|
188
|
+
│ with budget.session() as s: │
|
|
189
|
+
│ s.wrap(llm.chat(...)) # Auto-costed │
|
|
190
|
+
│ s.track(api_call(), cost=0.01) # Manual cost │
|
|
191
|
+
│ │
|
|
192
|
+
└─────────────┬───────────────────────────┬───────────────┘
|
|
193
|
+
│ │
|
|
194
|
+
▼ ▼
|
|
195
|
+
┌─────────────────────┐ ┌─────────────────────────────┐
|
|
196
|
+
│ LLM Cost Engine │ │ Tool Cost Registry │
|
|
197
|
+
│ │ │ │
|
|
198
|
+
│ • Pricing table │ │ • Per-call fixed costs │
|
|
199
|
+
│ (model → $/token) │ │ • Estimation functions │
|
|
200
|
+
│ • Auto-detect model │ │ • Custom cost annotations │
|
|
201
|
+
│ • Cache-aware │ │ │
|
|
202
|
+
│ pricing │ │ │
|
|
203
|
+
└─────────┬───────────┘ └──────────────┬──────────────┘
|
|
204
|
+
│ │
|
|
205
|
+
▼ ▼
|
|
206
|
+
┌─────────────────────────────────────────────────────────┐
|
|
207
|
+
│ Budget Ledger │
|
|
208
|
+
│ │
|
|
209
|
+
│ Running total │ Remaining balance │ Event timeline │
|
|
210
|
+
│ │
|
|
211
|
+
│ ┌──────────────────────────────────────────────────┐ │
|
|
212
|
+
│ │ Circuit Breaker │ │
|
|
213
|
+
│ │ │ │
|
|
214
|
+
│ │ • Soft limit warning (configurable threshold) │ │
|
|
215
|
+
│ │ • Hard limit enforcement (budget exhausted) │ │
|
|
216
|
+
│ │ • Loop detection (repeated similar calls) │ │
|
|
217
|
+
│ │ • Callback hooks for custom handling │ │
|
|
218
|
+
│ └──────────────────────────────────────────────────┘ │
|
|
219
|
+
│ │
|
|
220
|
+
│ Events → Webhook / Logger / Billing system │
|
|
221
|
+
└─────────────────────────────────────────────────────────┘
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### What it is NOT
|
|
225
|
+
- **Not an LLM proxy.** It doesn't sit between you and OpenAI. It wraps your existing client calls.
|
|
226
|
+
- **Not an observability platform.** It produces cost data, but it doesn't store dashboards. Pipe the output to LangSmith, Datadog, or wherever.
|
|
227
|
+
- **Not a billing system.** It enforces budgets, but it doesn't invoice customers. Pipe cost reports to Stripe/Orb/Lago for that.
|
|
228
|
+
- **Not infrastructure.** No Redis, no servers, no cloud account. It's a library that runs in your process.
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Integration Points
|
|
233
|
+
|
|
234
|
+
### Framework Support
|
|
235
|
+
```python
|
|
236
|
+
# LangChain / LangGraph
|
|
237
|
+
from agentbudget.integrations import LangChainBudgetCallback
|
|
238
|
+
agent.run(callbacks=[LangChainBudgetCallback(budget="$5.00")])
|
|
239
|
+
|
|
240
|
+
# CrewAI
|
|
241
|
+
from agentbudget.integrations import CrewAIBudgetMiddleware
|
|
242
|
+
crew = Crew(agents=[...], middleware=[CrewAIBudgetMiddleware(budget="$3.00")])
|
|
243
|
+
|
|
244
|
+
# Raw OpenAI / Anthropic SDK
|
|
245
|
+
from agentbudget import AgentBudget
|
|
246
|
+
budget = AgentBudget("$5.00")
|
|
247
|
+
with budget.session() as s:
|
|
248
|
+
response = s.wrap(client.chat.completions.create(...))
|
|
249
|
+
|
|
250
|
+
# MCP Tool Calls
|
|
251
|
+
@session.track_tool(cost=0.02)
|
|
252
|
+
def my_mcp_tool(params):
|
|
253
|
+
return mcp_client.call_tool("search", params)
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Event Hooks
|
|
257
|
+
```python
|
|
258
|
+
budget = AgentBudget(
|
|
259
|
+
max_spend="$5.00",
|
|
260
|
+
on_soft_limit=lambda report: logger.warning(f"90% budget used: {report}"),
|
|
261
|
+
on_hard_limit=lambda report: alert_ops_team(report),
|
|
262
|
+
on_loop_detected=lambda report: logger.error(f"Loop detected: {report}"),
|
|
263
|
+
webhook_url="https://your-app.com/api/budget-events", # Optional
|
|
264
|
+
)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Why Open Source?
|
|
270
|
+
|
|
271
|
+
1. **Trust.** A budget enforcement SDK that you can't audit is worthless. You need to see exactly how costs are calculated and when circuit breakers trip.
|
|
272
|
+
|
|
273
|
+
2. **Adoption.** The agent ecosystem is fragmented — LangChain, CrewAI, AutoGen, custom frameworks, raw SDK calls. An open-source library gets adopted across all of them. A proprietary service gets adopted by none.
|
|
274
|
+
|
|
275
|
+
3. **Community pricing data.** Model pricing changes constantly. An open-source project with community contributions keeps the pricing table accurate across hundreds of models and providers.
|
|
276
|
+
|
|
277
|
+
4. **Composability.** AgentBudget should be a building block, not a platform. It produces structured cost data that feeds into whatever observability, billing, or alerting stack you already use.
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## Summary
|
|
283
|
+
|
|
284
|
+
AgentBudget exists because AI agents have made software cost unpredictable for the first time. Traditional software has deterministic resource usage — you know how many database queries a request makes. Agent software doesn't. An agent might make 3 LLM calls or 300, use cheap models or expensive ones, invoke 1 tool or 50. This unpredictability is a feature of agents, not a bug — but it requires a new primitive for cost control.
|
|
285
|
+
|
|
286
|
+
That primitive is a per-session budget with real-time enforcement. AgentBudget is that primitive — nothing more, nothing less.
|
|
287
|
+
|
|
288
|
+
**Ship your agents with confidence. Set a budget. Move on.**
|