langchain-agent-budget 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,9 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ .venv/
5
+ dist/
6
+ build/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-11-18)
4
+
5
+ First release.
6
+
7
+ - `SpendBudgetMiddleware` for `create_agent` and deepagents: reserves each model call's
8
+ worst-case cost (approximate or exact input tokens plus `max_tokens`, priced by llm-prices
9
+ at the dearest input-side rate) from an agent-budget `Budget` before the call, and books the
10
+ real `usage_metadata` cost after it. `exit_behavior` `"end"` or `"error"`.
11
+ - Budgets shared across runs, threads and asyncio tasks; with no `budget` argument the
12
+ middleware uses `current_budget()`, so `with fleet.child(...): agent.invoke(...)` gives a run
13
+ its own slice.
14
+ - Subagents: while a `task` tool call runs the budget stays current; `subagent_usd` /
15
+ `subagent_fraction` give each subagent run its own slice, refunded when it ends.
16
+ - Tested with deepagents 0.7: two parallel subagents against room for one call make one call.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matthew Busel
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,71 @@
1
+ Metadata-Version: 2.5
2
+ Name: langchain-agent-budget
3
+ Version: 0.1.0
4
+ Summary: Hard dollar budgets for LangChain agents: reserve each model call's worst-case cost before the call, settle the real cost after, shared safely by parallel runs and deepagents subagents.
5
+ Project-URL: Homepage, https://github.com/Mattbusel/langchain-agent-budget
6
+ Project-URL: Documentation, https://github.com/Mattbusel/langchain-agent-budget/blob/main/docs/index.md
7
+ Project-URL: Changelog, https://github.com/Mattbusel/langchain-agent-budget/blob/main/CHANGELOG.md
8
+ Project-URL: Issues, https://github.com/Mattbusel/langchain-agent-budget/issues
9
+ Project-URL: Budgets, https://github.com/Mattbusel/agent-budget
10
+ Project-URL: Prices, https://github.com/Mattbusel/llm-prices
11
+ Author: Matthew Busel
12
+ License-Expression: MIT
13
+ License-File: LICENSE
14
+ Keywords: agents,budget,cost,deepagents,langchain,langgraph,llm,middleware
15
+ Classifier: Development Status :: 4 - Beta
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Classifier: Topic :: Software Development :: Libraries
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.10
24
+ Requires-Dist: langchain-core<2,>=1.0
25
+ Requires-Dist: langchain-llm-prices<0.2,>=0.1.0
26
+ Requires-Dist: langchain<2,>=1.0
27
+ Requires-Dist: llm-agent-budget<0.2,>=0.1.3
28
+ Requires-Dist: llm-prices<2,>=1.7
29
+ Description-Content-Type: text/markdown
30
+
31
+ # langchain-agent-budget
32
+
33
+ A dollar budget a LangChain agent cannot go past. `SpendBudgetMiddleware` reserves each model
34
+ call's worst-case cost before the call and books the real cost after it. A call that does not
35
+ fit is never made, and parallel runs or deepagents subagents sharing one budget cannot spend
36
+ more than it holds together.
37
+
38
+ ```bash
39
+ pip install -U langchain-agent-budget
40
+ ```
41
+
42
+ ```python
43
+ from agent_budget import Budget
44
+ from langchain.agents import create_agent
45
+ from langchain_anthropic import ChatAnthropic
46
+ from langchain_agent_budget import SpendBudgetMiddleware
47
+
48
+ budget = Budget(2.00, name="support-bot")
49
+ agent = create_agent(
50
+ ChatAnthropic(model="claude-sonnet-4-6", max_tokens=2048),
51
+ tools=[search],
52
+ middleware=[SpendBudgetMiddleware(budget)],
53
+ )
54
+ ```
55
+
56
+ - **Worst case first**: input tokens (approximate, or your own counter) plus the call's
57
+ `max_tokens`, priced by [llm-prices](https://github.com/Mattbusel/llm-prices) at the dearest
58
+ input-side rate. The real cost from `usage_metadata` replaces the hold when the reply comes back.
59
+ - **Shared budgets**: one `Budget` across runs, threads and asyncio tasks; per-user or per-job
60
+ slices with `with fleet.child("user-42", usd=0.50): agent.invoke(...)`.
61
+ - **deepagents**: give subagents `SpendBudgetMiddleware()` and they spend from the
62
+ orchestrator's budget; `subagent_usd=` gives each subagent run its own slice.
63
+
64
+ `ModelCallLimitMiddleware` counts calls; a check after each call lets the call that crosses the
65
+ cap through. In `examples/offline_demo.py` (no API key needed) a $0.02 cap checked after each
66
+ call ends at $0.02268; the same agent with `SpendBudgetMiddleware` stops at $0.01512.
67
+
68
+ Docs: [docs/index.md](docs/index.md). Built on [agent-budget](https://github.com/Mattbusel/agent-budget)
69
+ (budgets and child scopes) and [langchain-llm-prices](https://github.com/Mattbusel/langchain-llm-prices).
70
+
71
+ MIT license. Maintained by Matthew Busel.
@@ -0,0 +1,41 @@
1
+ # langchain-agent-budget
2
+
3
+ A dollar budget a LangChain agent cannot go past. `SpendBudgetMiddleware` reserves each model
4
+ call's worst-case cost before the call and books the real cost after it. A call that does not
5
+ fit is never made, and parallel runs or deepagents subagents sharing one budget cannot spend
6
+ more than it holds together.
7
+
8
+ ```bash
9
+ pip install -U langchain-agent-budget
10
+ ```
11
+
12
+ ```python
13
+ from agent_budget import Budget
14
+ from langchain.agents import create_agent
15
+ from langchain_anthropic import ChatAnthropic
16
+ from langchain_agent_budget import SpendBudgetMiddleware
17
+
18
+ budget = Budget(2.00, name="support-bot")
19
+ agent = create_agent(
20
+ ChatAnthropic(model="claude-sonnet-4-6", max_tokens=2048),
21
+ tools=[search],
22
+ middleware=[SpendBudgetMiddleware(budget)],
23
+ )
24
+ ```
25
+
26
+ - **Worst case first**: input tokens (approximate, or your own counter) plus the call's
27
+ `max_tokens`, priced by [llm-prices](https://github.com/Mattbusel/llm-prices) at the dearest
28
+ input-side rate. The real cost from `usage_metadata` replaces the hold when the reply comes back.
29
+ - **Shared budgets**: one `Budget` across runs, threads and asyncio tasks; per-user or per-job
30
+ slices with `with fleet.child("user-42", usd=0.50): agent.invoke(...)`.
31
+ - **deepagents**: give subagents `SpendBudgetMiddleware()` and they spend from the
32
+ orchestrator's budget; `subagent_usd=` gives each subagent run its own slice.
33
+
34
+ `ModelCallLimitMiddleware` counts calls; a check after each call lets the call that crosses the
35
+ cap through. In `examples/offline_demo.py` (no API key needed) a $0.02 cap checked after each
36
+ call ends at $0.02268; the same agent with `SpendBudgetMiddleware` stops at $0.01512.
37
+
38
+ Docs: [docs/index.md](docs/index.md). Built on [agent-budget](https://github.com/Mattbusel/agent-budget)
39
+ (budgets and child scopes) and [langchain-llm-prices](https://github.com/Mattbusel/langchain-llm-prices).
40
+
41
+ MIT license. Maintained by Matthew Busel.
@@ -0,0 +1,157 @@
1
+ # Hard dollar budgets for LangChain agents
2
+
3
+ `langchain-agent-budget` gives a `create_agent` or deepagents agent a dollar budget it cannot
4
+ go past. Before each model call, `SpendBudgetMiddleware` reserves the most that call can cost.
5
+ If the reservation does not fit, the call is not made. When the reply comes back, the hold is
6
+ swapped for the real cost and the unused part goes back.
7
+
8
+ A check that runs after each call (or before the next one, from what was spent so far) lets
9
+ the call that crosses the line through, so a run ends over its cap by up to one call. Parallel
10
+ subagents that each read the same "remaining" figure can overshoot by one call each. Reserving
11
+ first closes both gaps: the check and the reservation are one step under the budget's lock.
12
+
13
+ Budgets and child scopes come from [`agent-budget`](https://github.com/Mattbusel/agent-budget);
14
+ prices come from [`llm-prices`](https://github.com/Mattbusel/llm-prices) through
15
+ [`langchain-llm-prices`](https://github.com/Mattbusel/langchain-llm-prices).
16
+
17
+ ## Overview
18
+
19
+ | Component | Class | Package | Version |
20
+ |---|---|---|---|
21
+ | middleware | `SpendBudgetMiddleware` | `langchain-agent-budget` | 0.1.0 |
22
+
23
+ ## Setup
24
+
25
+ ```bash
26
+ pip install -U langchain-agent-budget
27
+ ```
28
+
29
+ No credentials and no network: the price table ships inside `llm-prices`.
30
+
31
+ ## Usage
32
+
33
+ ```python
34
+ from agent_budget import Budget
35
+ from langchain.agents import create_agent
36
+ from langchain_anthropic import ChatAnthropic
37
+ from langchain_agent_budget import SpendBudgetMiddleware
38
+
39
+ budget = Budget(2.00, name="support-bot") # $2 for every run of this agent
40
+
41
+ agent = create_agent(
42
+ ChatAnthropic(model="claude-sonnet-4-6", max_tokens=2048),
43
+ tools=[search],
44
+ middleware=[SpendBudgetMiddleware(budget)],
45
+ )
46
+ agent.invoke({"messages": [{"role": "user", "content": "..."}]})
47
+ print(budget.spent_usd, budget.remaining_usd)
48
+ ```
49
+
50
+ `SpendBudgetMiddleware(2.00)` creates the `Budget` for you.
51
+
52
+ When the next call does not fit:
53
+
54
+ - `exit_behavior="end"` (default): no call is made; the run ends with an `AIMessage` such as
55
+ `Spend budget reached: the next model call can cost up to $0.0310 and budget 'support-bot'
56
+ has $0.0120 left.` It carries `additional_kwargs={"lc_source": "spend_budget"}`.
57
+ - `exit_behavior="error"`: raises agent-budget's `BudgetExhausted` (or `BudgetKilled` /
58
+ `ScopeClosed`, all subclasses of `BudgetError`).
59
+
60
+ ### What is reserved
61
+
62
+ - **Output**: the call's `max_tokens` (from the request's model settings or the chat model),
63
+ else `max_output_tokens` given to the middleware, else the model profile's maximum output.
64
+ A lower `max_tokens` means smaller holds. If nothing caps the output, the middleware warns
65
+ once and the hold covers the input only.
66
+ - **Input**: `count_tokens_approximately` over the system message, messages and tools, or your
67
+ own `token_counter` (for example `model.get_num_tokens_from_messages`). It is priced at the
68
+ dearest input-side rate the model lists (a 1-hour cache write on Anthropic), because prompt
69
+ caching can bill input that way.
70
+ - **After the call**: the reply's `usage_metadata` is priced (cache reads and writes at their
71
+ own rates) and booked in place of the hold. A reply with no usage books the hold.
72
+
73
+ With an exact token counter and `max_tokens` set, no call can take the budget past its limit.
74
+ With the approximate counter, the input side is an estimate, so a run can only exceed the
75
+ limit by the error in that estimate.
76
+
77
+ ### Per run, per user, per job
78
+
79
+ The budget is a live object, so it can be shared however you like:
80
+
81
+ ```python
82
+ from agent_budget import Budget
83
+
84
+ fleet = Budget(50.00, name="all-users")
85
+ agent = create_agent(model, tools=tools, middleware=[SpendBudgetMiddleware()]) # no budget here
86
+
87
+ with fleet.child(f"user-{user_id}", usd=0.50): # this run gets $0.50 of the fleet budget
88
+ agent.invoke(inputs)
89
+ ```
90
+
91
+ With no budget argument, the middleware uses the budget that is current when the call is
92
+ made. The child scope's unused part goes back to the fleet when the block ends.
93
+
94
+ ### Parallel runs and threads
95
+
96
+ Many runs (threads, asyncio tasks, server workers in one process) can share one `Budget`.
97
+ They are admitted one reservation at a time, so together they cannot spend more than it holds.
98
+
99
+ ### deepagents subagents
100
+
101
+ ```python
102
+ from agent_budget import Budget
103
+ from deepagents import create_deep_agent
104
+ from langchain_agent_budget import SpendBudgetMiddleware
105
+
106
+ budget = Budget(5.00, name="orchestrator")
107
+ researcher = {
108
+ "name": "researcher",
109
+ "description": "Researches one question.",
110
+ "system_prompt": "...",
111
+ "middleware": [SpendBudgetMiddleware()], # spends from the orchestrator's budget
112
+ }
113
+ agent = create_deep_agent(
114
+ model="anthropic:claude-sonnet-4-6",
115
+ subagents=[researcher],
116
+ middleware=[SpendBudgetMiddleware(budget, subagent_usd=1.00)],
117
+ )
118
+ ```
119
+
120
+ While a `task` tool call runs, the orchestrator's budget is current, so the subagent's
121
+ `SpendBudgetMiddleware()` reserves from it. Two subagents started in parallel cannot both take
122
+ the last few cents. With `subagent_usd` (or `subagent_fraction`), each subagent run gets its
123
+ own slice, reserved before it starts and refunded when it ends, so one runaway subagent cannot
124
+ use up the others' share. `subagent_tools` names the tools that start subagents (`("task",)`
125
+ by default).
126
+
127
+ ## Arguments
128
+
129
+ | Argument | Default | Meaning |
130
+ |---|---|---|
131
+ | `budget` | `None` | A `Budget` (or child scope), a dollar amount, or `None` for the current budget |
132
+ | `exit_behavior` | `"end"` | `"end"` or `"error"` |
133
+ | `max_output_tokens` | `None` | Output tokens to assume when the model sets no `max_tokens` |
134
+ | `token_counter` | `None` | Input token counter; `count_tokens_approximately` when `None` |
135
+ | `subagent_tools` | `("task",)` | Tools that run a subagent |
136
+ | `subagent_usd` | `None` | A fixed slice per subagent run |
137
+ | `subagent_fraction` | `None` | A slice of this fraction of what is left, per subagent run |
138
+ | `provider` | `None` | Provider to price with when the model does not report one |
139
+ | `unpriced` | `"warn"` | Model with no price: `"warn"` once and let it through uncounted, `"ignore"`, or `"error"` (raise `UnpricedModelError`) |
140
+
141
+ ## Compared with the built-in limits
142
+
143
+ `ModelCallLimitMiddleware` and `ToolCallLimitMiddleware` count calls. `CostCapMiddleware`
144
+ (langchain-llm-prices) counts dollars per run and per thread in agent state, persisted by the
145
+ checkpointer, and checks before each call from what was already spent.
146
+ `SpendBudgetMiddleware` holds each call's worst case first, and its budget can span runs,
147
+ threads and subagents.
148
+
149
+ `examples/offline_demo.py` runs both on the same scripted agent (five tool rounds, $0.00756
150
+ per call, $0.02 cap): the after-the-fact cap makes 3 calls and spends $0.02268; the
151
+ reservation makes 2 calls and spends $0.01512.
152
+
153
+ ## Maintenance
154
+
155
+ Maintained by Matthew Busel. MIT license. agent-budget and llm-prices are maintained by the
156
+ same author; llm-prices rows cite each provider's pricing page and the date it was checked.
157
+ Issues: https://github.com/Mattbusel/langchain-agent-budget/issues
@@ -0,0 +1,72 @@
1
+ """Runs with no API key: a post-call cap overshoots, a reservation does not.
2
+
3
+ A scripted model stands in for claude-sonnet-4-5. Every call costs $0.00756 at list price
4
+ (20 input and 500 output tokens) and the agent wants five tool rounds. The cap is $0.02.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import Any
10
+
11
+ from agent_budget import Budget
12
+ from langchain.agents import create_agent
13
+ from langchain_core.language_models import BaseChatModel
14
+ from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
15
+ from langchain_core.outputs import ChatGeneration, ChatResult
16
+ from langchain_core.tools import tool
17
+ from langchain_llm_prices import CostCapMiddleware
18
+
19
+ from langchain_agent_budget import SpendBudgetMiddleware
20
+
21
+
22
+ class Scripted(BaseChatModel):
23
+ model_name: str = "claude-sonnet-4-5"
24
+ max_tokens: int = 500
25
+ calls: int = 0
26
+
27
+ @property
28
+ def _llm_type(self) -> str:
29
+ return "scripted"
30
+
31
+ def _get_ls_params(self, stop: list[str] | None = None, **kwargs: Any) -> Any:
32
+ params = super()._get_ls_params(stop=stop, **kwargs)
33
+ params["ls_provider"] = "anthropic"
34
+ return params
35
+
36
+ def _generate(self, messages: list[BaseMessage], *args: Any, **kwargs: Any) -> ChatResult:
37
+ self.calls += 1
38
+ rounds = sum(1 for m in messages if m.type == "tool")
39
+ calls = [{"name": "lookup", "args": {"q": "x"}, "id": f"c{rounds}"}] if rounds < 5 else []
40
+ message = AIMessage(
41
+ content="working" if calls else "answer",
42
+ tool_calls=calls,
43
+ usage_metadata={"input_tokens": 20, "output_tokens": 500, "total_tokens": 520},
44
+ response_metadata={"model_name": "claude-sonnet-4-5", "model_provider": "anthropic"},
45
+ )
46
+ return ChatResult(generations=[ChatGeneration(message=message)])
47
+
48
+ def bind_tools(self, tools: Any, **kwargs: Any) -> Any:
49
+ return self.bind(**kwargs)
50
+
51
+
52
+ @tool
53
+ def lookup(q: str) -> str:
54
+ """Look something up."""
55
+ return "result"
56
+
57
+
58
+ CAP = 0.02
59
+
60
+ model = Scripted()
61
+ create_agent(model, tools=[lookup], middleware=[CostCapMiddleware(max_usd_per_run=CAP)]).invoke(
62
+ {"messages": [HumanMessage("go")]}
63
+ )
64
+ print(f"check after each call: {model.calls} calls, ${model.calls * 0.00756:.5f} of ${CAP}")
65
+
66
+ model = Scripted()
67
+ budget = Budget(CAP)
68
+ result = create_agent(model, tools=[lookup], middleware=[SpendBudgetMiddleware(budget)]).invoke(
69
+ {"messages": [HumanMessage("go")]}
70
+ )
71
+ print(f"reserve before each call: {model.calls} calls, ${budget.spent_usd:.5f} of ${CAP}")
72
+ print(result["messages"][-1].content)
@@ -0,0 +1,35 @@
1
+ """Hard dollar budgets for LangChain agents, reserved before each model call.
2
+
3
+ - `SpendBudgetMiddleware`: reserves each model call's worst-case cost from an agent-budget
4
+ `Budget` before the call and books the real cost after it, so a run never goes past its
5
+ budget, and parallel subagents sharing one budget cannot overspend it together.
6
+ - `CallEstimate`: what the middleware worked out before a call.
7
+
8
+ Prices come from llm-prices (through langchain-llm-prices); budgets and child scopes come from
9
+ agent-budget.
10
+ """
11
+
12
+ from agent_budget import (
13
+ Budget,
14
+ BudgetError,
15
+ BudgetExhausted,
16
+ BudgetKilled,
17
+ ScopeClosed,
18
+ current_budget,
19
+ )
20
+
21
+ from langchain_agent_budget.middleware import CallEstimate, SpendBudgetMiddleware
22
+
23
+ __version__ = "0.1.0"
24
+
25
+ __all__ = [
26
+ "Budget",
27
+ "BudgetError",
28
+ "BudgetExhausted",
29
+ "BudgetKilled",
30
+ "CallEstimate",
31
+ "ScopeClosed",
32
+ "SpendBudgetMiddleware",
33
+ "__version__",
34
+ "current_budget",
35
+ ]