agent-stoploss 1.0.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.
- agent_stoploss-1.0.0/.gitignore +12 -0
- agent_stoploss-1.0.0/CHANGELOG.md +26 -0
- agent_stoploss-1.0.0/LICENSE +201 -0
- agent_stoploss-1.0.0/PKG-INFO +255 -0
- agent_stoploss-1.0.0/README.md +221 -0
- agent_stoploss-1.0.0/docs/design-notes.md +96 -0
- agent_stoploss-1.0.0/examples/agent_sdk.py +58 -0
- agent_stoploss-1.0.0/examples/anthropic_loop.py +83 -0
- agent_stoploss-1.0.0/examples/offline_demo.py +93 -0
- agent_stoploss-1.0.0/pyproject.toml +63 -0
- agent_stoploss-1.0.0/src/agent_stoploss/__init__.py +13 -0
- agent_stoploss-1.0.0/src/agent_stoploss/claude_agent.py +304 -0
- agent_stoploss-1.0.0/src/agent_stoploss/cli.py +138 -0
- agent_stoploss-1.0.0/src/agent_stoploss/config.py +344 -0
- agent_stoploss-1.0.0/src/agent_stoploss/dashboard.py +221 -0
- agent_stoploss-1.0.0/src/agent_stoploss/engine.py +182 -0
- agent_stoploss-1.0.0/src/agent_stoploss/errors.py +84 -0
- agent_stoploss-1.0.0/src/agent_stoploss/fingerprint.py +122 -0
- agent_stoploss-1.0.0/src/agent_stoploss/guard.py +173 -0
- agent_stoploss-1.0.0/src/agent_stoploss/journal.py +135 -0
- agent_stoploss-1.0.0/src/agent_stoploss/pricing.py +119 -0
- agent_stoploss-1.0.0/src/agent_stoploss/py.typed +0 -0
- agent_stoploss-1.0.0/src/agent_stoploss/replay.py +223 -0
- agent_stoploss-1.0.0/src/agent_stoploss/run.py +347 -0
- agent_stoploss-1.0.0/src/agent_stoploss/static/dashboard.html +924 -0
- agent_stoploss-1.0.0/src/agent_stoploss/wrap.py +108 -0
- agent_stoploss-1.0.0/stoploss.example.yaml +39 -0
- agent_stoploss-1.0.0/tests/__init__.py +0 -0
- agent_stoploss-1.0.0/tests/conftest.py +34 -0
- agent_stoploss-1.0.0/tests/parity/__init__.py +0 -0
- agent_stoploss-1.0.0/tests/parity/research_journal.py +178 -0
- agent_stoploss-1.0.0/tests/parity/test_research_parity.py +80 -0
- agent_stoploss-1.0.0/tests/test_claude_agent.py +364 -0
- agent_stoploss-1.0.0/tests/test_config.py +65 -0
- agent_stoploss-1.0.0/tests/test_dashboard.py +102 -0
- agent_stoploss-1.0.0/tests/test_engine.py +131 -0
- agent_stoploss-1.0.0/tests/test_errors.py +62 -0
- agent_stoploss-1.0.0/tests/test_fingerprint.py +60 -0
- agent_stoploss-1.0.0/tests/test_replay_cli.py +111 -0
- agent_stoploss-1.0.0/tests/test_run.py +277 -0
- agent_stoploss-1.0.0/tests/test_wrap.py +90 -0
- agent_stoploss-1.0.0/tests/ui/README.md +8 -0
- agent_stoploss-1.0.0/tests/ui/make_research_logs.py +27 -0
- agent_stoploss-1.0.0/tests/ui/package.json +5 -0
- agent_stoploss-1.0.0/tests/ui/ui_test.js +110 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 — 2026-11-26
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `StopLoss` with shadow and enforce modes; one guard serves any number of
|
|
8
|
+
concurrent runs (threads or asyncio tasks).
|
|
9
|
+
- Mechanisms: loop detector on call and result fingerprints, error classifier
|
|
10
|
+
(terminal and transient HTTP statuses, exception names), circuit breaker per
|
|
11
|
+
tool with a sliding window, cooldown and probe, run budget in tokens or USD
|
|
12
|
+
(off by default), strict pricing.
|
|
13
|
+
- Notices to the agent one step before a loop or failing-tool stop; `Stopped`
|
|
14
|
+
with the reason and a run summary; `on_stop` and `on_verdict` callbacks.
|
|
15
|
+
- Integrations: `@guard.tool` for sync and async tools; `guard.wrap()` for the
|
|
16
|
+
Anthropic and OpenAI clients, sync and async; the Claude Agent SDK through
|
|
17
|
+
`agent_stoploss.claude_agent` (`guarded_query`, `StopLossHooks`), checked
|
|
18
|
+
live against claude-agent-sdk 0.2.189.
|
|
19
|
+
- JSONL journal, schema 1, fingerprints and shapes instead of raw text unless
|
|
20
|
+
`log.raw_text` is set.
|
|
21
|
+
- `agent-stoploss replay`: what a config would have stopped, saved and broken
|
|
22
|
+
on recorded runs, with a suggested run budget.
|
|
23
|
+
- `agent-stoploss dashboard`: a local page to tune thresholds against recorded
|
|
24
|
+
runs and export `stoploss.yaml`.
|
|
25
|
+
- `agent-stoploss check-config`.
|
|
26
|
+
- Built-in prices for the Claude models as of 2026-10-06.
|
|
@@ -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,255 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: agent-stoploss
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Stop AI agents from burning tokens on broken tools.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Aerantrix/agent-stoploss
|
|
6
|
+
Project-URL: Issues, https://github.com/Aerantrix/agent-stoploss/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/Aerantrix/agent-stoploss/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Nataliya Agafonova
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai-agents,circuit-breaker,finops,llm-agents,loop-detection,retry,token-cost
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: pyyaml>=6
|
|
26
|
+
Provides-Extra: claude-agent-sdk
|
|
27
|
+
Requires-Dist: claude-agent-sdk>=0.2.189; extra == 'claude-agent-sdk'
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest-cov>=5; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
32
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# agent-stoploss
|
|
36
|
+
|
|
37
|
+
**Stop AI agents from burning tokens on broken tools.**
|
|
38
|
+
|
|
39
|
+
Based on the [Agent Fuel study](https://github.com/Aerantrix/Agent-Fuel): 660
|
|
40
|
+
recorded agent runs under injected tool failures, with the controller, the run
|
|
41
|
+
records and the analysis that this library's defaults come from.
|
|
42
|
+
|
|
43
|
+
An agent calls an API, the key has expired, and the agent tries again. And
|
|
44
|
+
again, sixty times, each attempt resending the whole history of failures. A
|
|
45
|
+
retry loop like this turns a task worth a few hundred tokens into a runaway
|
|
46
|
+
bill, and a step limit or a monthly budget only notices once the money is gone.
|
|
47
|
+
|
|
48
|
+
agent-stoploss watches each agent run and ends it when the agent is spending
|
|
49
|
+
without making progress:
|
|
50
|
+
|
|
51
|
+
- the same tool call again and again;
|
|
52
|
+
- the same answer from a tool again and again;
|
|
53
|
+
- an error that retrying cannot fix (401, 403, 404, ...);
|
|
54
|
+
- a tool that keeps failing.
|
|
55
|
+
|
|
56
|
+
It starts in **shadow mode**: it records what it would have done and changes
|
|
57
|
+
nothing. `agent-stoploss replay` then shows, on your own recorded runs, what a
|
|
58
|
+
configuration would have saved and which good runs it would have broken. Switch
|
|
59
|
+
to **enforce** when the numbers look right.
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
pip install agent-stoploss
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Python 3.10+. One dependency (PyYAML). Apache-2.0.
|
|
66
|
+
|
|
67
|
+
## Why another spend guard?
|
|
68
|
+
|
|
69
|
+
Most guards count: steps, tokens, dollars. agent-stoploss also reads what the
|
|
70
|
+
tools answer, so it can tell an agent that is stuck from one that is simply
|
|
71
|
+
working through a long task.
|
|
72
|
+
|
|
73
|
+
| It stops | It leaves alone |
|
|
74
|
+
|---|---|
|
|
75
|
+
| a call answered with 401: retrying cannot help, so the run ends at the first one | forty different domains looked up one by one |
|
|
76
|
+
| the same call made three times, even with case or spacing changed | paging through results, `page=1..N` |
|
|
77
|
+
| the same result returned three times, timestamps and request ids ignored | a tool that fails now and then, fewer than 4 of its last 5 calls |
|
|
78
|
+
| a tool that failed in 4 of its last 5 calls | polling a job you marked as `loop_detector: off` |
|
|
79
|
+
|
|
80
|
+
A run budget is there too, off by default: a fixed ceiling also cuts honest long
|
|
81
|
+
tasks short, so replay suggests a value from your own runs instead.
|
|
82
|
+
|
|
83
|
+
## Quick start
|
|
84
|
+
|
|
85
|
+
### Claude Agent SDK
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
pip install "agent-stoploss[claude-agent-sdk]"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from claude_agent_sdk import ClaudeAgentOptions
|
|
93
|
+
from agent_stoploss import StopLoss, Stopped
|
|
94
|
+
from agent_stoploss.claude_agent import guarded_query
|
|
95
|
+
|
|
96
|
+
guard = StopLoss.from_file("stoploss.yaml") # or StopLoss(mode="shadow")
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
async for message in guarded_query(guard, prompt="...", options=ClaudeAgentOptions(...),
|
|
100
|
+
agent="weekly-report"):
|
|
101
|
+
... # the same messages query() yields
|
|
102
|
+
except Stopped as stop:
|
|
103
|
+
print(stop.reason, stop.summary)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`guarded_query` adds hooks before and after every tool call and reads token
|
|
107
|
+
usage from the messages. In enforce mode a stop denies the pending tool call
|
|
108
|
+
and ends the agent's turn, then `Stopped` is raised. A stop decided on a tool's
|
|
109
|
+
answer, such as a 401, takes effect at the agent's next tool call: Claude Code
|
|
110
|
+
lets the model reply once more first. Token counts are corrected from the
|
|
111
|
+
SDK's final totals, so the journal matches what the SDK reports. With `ClaudeSDKClient`,
|
|
112
|
+
use `StopLossHooks` directly; see [`agent_stoploss/claude_agent.py`](src/agent_stoploss/claude_agent.py).
|
|
113
|
+
|
|
114
|
+
### Your own agent loop
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import anthropic
|
|
118
|
+
from agent_stoploss import StopLoss, Stopped
|
|
119
|
+
|
|
120
|
+
guard = StopLoss.from_file("stoploss.yaml")
|
|
121
|
+
client = guard.wrap(anthropic.Anthropic()) # tokens and cost recorded for you
|
|
122
|
+
|
|
123
|
+
@guard.tool # calls, results and errors checked
|
|
124
|
+
def analytics(domain: str, period: str = "30d") -> dict: ...
|
|
125
|
+
|
|
126
|
+
with guard.run(agent="weekly-report") as run:
|
|
127
|
+
try:
|
|
128
|
+
while True:
|
|
129
|
+
response = client.messages.create(model=..., max_tokens=..., tools=tools, messages=messages)
|
|
130
|
+
if response.stop_reason == "end_turn":
|
|
131
|
+
break
|
|
132
|
+
... # run the tools, collect tool_result blocks
|
|
133
|
+
results += [{"type": "text", "text": t} for t in run.notice_texts()]
|
|
134
|
+
messages.append({"role": "user", "content": results})
|
|
135
|
+
run.outcome("ok")
|
|
136
|
+
except Stopped as stop:
|
|
137
|
+
run.outcome("failed")
|
|
138
|
+
report_partial_results(stop.reason)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`wrap()` works with the Anthropic and OpenAI clients, sync and async. Without
|
|
142
|
+
them, record each model call yourself with `run.record_usage(response.usage, model=...)`
|
|
143
|
+
and each tool call with `run.before_tool(...)` / `run.after_tool(...)`.
|
|
144
|
+
|
|
145
|
+
Full examples: [`examples/agent_sdk.py`](examples/agent_sdk.py),
|
|
146
|
+
[`examples/anthropic_loop.py`](examples/anthropic_loop.py), and
|
|
147
|
+
[`examples/offline_demo.py`](examples/offline_demo.py), which runs the whole
|
|
148
|
+
workflow on a scripted agent with no API key.
|
|
149
|
+
|
|
150
|
+
## Shadow, replay, enforce
|
|
151
|
+
|
|
152
|
+
1. **Shadow.** Deploy with `mode: shadow`. Every run is recorded to
|
|
153
|
+
`./stoploss_logs/stoploss-YYYY-MM-DD.jsonl`; the agent never notices.
|
|
154
|
+
2. **Replay.** Point the replay at the logs:
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
$ agent-stoploss replay ./stoploss_logs --config stoploss.yaml
|
|
158
|
+
Runs 660 (660 labelled by the developer, the rest by heuristic)
|
|
159
|
+
Recorded spend 12,888,348 tokens
|
|
160
|
+
Would have stopped 251
|
|
161
|
+
same call repeated 129
|
|
162
|
+
error retrying cannot fix 73
|
|
163
|
+
same result repeated 46
|
|
164
|
+
tool keeps failing 3
|
|
165
|
+
Would have saved 2,312,156 tokens
|
|
166
|
+
of the excess in runs not ok: 99%
|
|
167
|
+
|
|
168
|
+
Runs labelled ok that would have been stopped: 6
|
|
169
|
+
...
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The two lists that matter are the good runs it would have stopped and the
|
|
173
|
+
failed runs it would have missed. Label your runs with `run.outcome("ok")`
|
|
174
|
+
or `run.outcome("failed")`; without a label the replay guesses and says so.
|
|
175
|
+
3. **Tune.** Exempt a tool, raise a threshold, replay again. Or use the dashboard.
|
|
176
|
+
4. **Enforce.** Set `mode: enforce`. Before a loop or a failing tool ends the
|
|
177
|
+
run, the agent gets a short notice it can act on; an error that retrying
|
|
178
|
+
cannot fix ends the run at once. On a stop your code gets `Stopped`.
|
|
179
|
+
|
|
180
|
+
## Dashboard
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
agent-stoploss dashboard ./stoploss_logs --config stoploss.yaml
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
A local page for tuning thresholds against your recorded runs: what would be
|
|
187
|
+
stopped and why, what it would save, which good runs it would break, and each
|
|
188
|
+
run step by step with the point where it would have ended. Every change is
|
|
189
|
+
replayed at once. It exports `stoploss.yaml` with only what differs from the
|
|
190
|
+
defaults.
|
|
191
|
+
|
|
192
|
+
It listens on 127.0.0.1 only, loads nothing from the internet, writes nothing
|
|
193
|
+
to disk and never controls a running agent.
|
|
194
|
+
|
|
195
|
+
## Configuration
|
|
196
|
+
|
|
197
|
+
Every key is optional. [`stoploss.example.yaml`](stoploss.example.yaml) lists
|
|
198
|
+
them all with their defaults:
|
|
199
|
+
|
|
200
|
+
```yaml
|
|
201
|
+
mode: shadow
|
|
202
|
+
loop_detector: {threshold: 3}
|
|
203
|
+
circuit_breaker: {window: 5, max_failures: 4, cooldown_seconds: 60}
|
|
204
|
+
error_classifier:
|
|
205
|
+
terminal_statuses: [400, 401, 403, 404, 405, 410, 422]
|
|
206
|
+
transient_statuses: [408, 425, 429, 500, 502, 503, 504]
|
|
207
|
+
run_budget: {max_tokens: null, max_cost_usd: null}
|
|
208
|
+
tools:
|
|
209
|
+
job_status: {loop_detector: off} # polls the same status on purpose
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`agent-stoploss check-config stoploss.yaml` rejects unknown keys and bad values
|
|
213
|
+
instead of quietly falling back to a default. Why the defaults are what they
|
|
214
|
+
are: [`docs/design-notes.md`](docs/design-notes.md).
|
|
215
|
+
|
|
216
|
+
## What it records
|
|
217
|
+
|
|
218
|
+
One JSON line per event: run start and end, each step, model call (tokens,
|
|
219
|
+
cost), tool call and result, verdict, notice. Arguments and results are kept as
|
|
220
|
+
fingerprints and shapes, not text, unless you set `log.raw_text: true`. The
|
|
221
|
+
journal stays on your disk; agent-stoploss sends nothing anywhere.
|
|
222
|
+
|
|
223
|
+
Built-in prices cover the Claude models (as of 2026-10-06). Other models need
|
|
224
|
+
a `prices:` entry, or their cost is counted as $0 with a warning;
|
|
225
|
+
`strict_pricing: true` stops such runs instead.
|
|
226
|
+
|
|
227
|
+
## What it does not catch
|
|
228
|
+
|
|
229
|
+
- **An agent that changes its request every time** and gets a different error
|
|
230
|
+
each time. Each call looks new; only the circuit breaker and the budget see it.
|
|
231
|
+
- **Slow burns that make progress**: a long task that is merely expensive is a
|
|
232
|
+
budget question, not a loop.
|
|
233
|
+
- **Streaming responses** inside a run: their tokens cannot be counted yet, so
|
|
234
|
+
agent-stoploss raises `StreamingNotSupported` rather than count them as zero.
|
|
235
|
+
- **Behaviour after a warning** is not in the replay. It assumes the run went on
|
|
236
|
+
as recorded; in enforce mode an agent that heeds the notice may stop earlier.
|
|
237
|
+
- **Large models and real APIs** have not been measured yet. The numbers below
|
|
238
|
+
come from small models and a stub service.
|
|
239
|
+
|
|
240
|
+
## Based on the Agent Fuel study
|
|
241
|
+
|
|
242
|
+
The mechanisms and defaults come from the
|
|
243
|
+
[Agent Fuel study](https://github.com/Aerantrix/Agent-Fuel) (CSIT305, University
|
|
244
|
+
of Wollongong in Dubai, 2026): 660 runs of two small models, Qwen 2.5 7B and
|
|
245
|
+
Claude Haiku 5.5, against a stub analytics service in nine scenarios (six
|
|
246
|
+
failures of the service, three healthy tasks), under thirteen system prompts,
|
|
247
|
+
nine of them the default prompts of agent frameworks. Replayed with the default configuration, agent-stoploss stops 245
|
|
248
|
+
of the 444 failing runs and saves 99% of the tokens they spent beyond the
|
|
249
|
+
minimum needed to see the failure. It would have stopped 6 of the 216 healthy
|
|
250
|
+
runs, all with the local 7B model; 8 failing runs that spent above that minimum
|
|
251
|
+
got through. The test suite reproduces these numbers from the study's records.
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
Apache-2.0. See [LICENSE](LICENSE).
|