guardloop 0.2.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.
- guardloop-0.2.0/.github/workflows/publish-pypi.yml +57 -0
- guardloop-0.2.0/.gitignore +14 -0
- guardloop-0.2.0/LICENSE +21 -0
- guardloop-0.2.0/PKG-INFO +188 -0
- guardloop-0.2.0/README.md +148 -0
- guardloop-0.2.0/docs/design.md +48 -0
- guardloop-0.2.0/docs/pypi-publishing.md +66 -0
- guardloop-0.2.0/docs/roadmap.md +27 -0
- guardloop-0.2.0/examples/live_anthropic_basic.py +47 -0
- guardloop-0.2.0/examples/live_openai_basic.py +42 -0
- guardloop-0.2.0/examples/runaway_cost_prevention.py +73 -0
- guardloop-0.2.0/examples/tool_circuit_breaker.py +58 -0
- guardloop-0.2.0/pyproject.toml +98 -0
- guardloop-0.2.0/src/guardloop/__init__.py +48 -0
- guardloop-0.2.0/src/guardloop/budget.py +180 -0
- guardloop-0.2.0/src/guardloop/circuit_breaker.py +243 -0
- guardloop-0.2.0/src/guardloop/context.py +68 -0
- guardloop-0.2.0/src/guardloop/exceptions.py +92 -0
- guardloop-0.2.0/src/guardloop/models.py +94 -0
- guardloop-0.2.0/src/guardloop/pricing.py +116 -0
- guardloop-0.2.0/src/guardloop/providers/__init__.py +6 -0
- guardloop-0.2.0/src/guardloop/providers/anthropic.py +150 -0
- guardloop-0.2.0/src/guardloop/providers/openai.py +138 -0
- guardloop-0.2.0/src/guardloop/py.typed +0 -0
- guardloop-0.2.0/src/guardloop/runtime.py +190 -0
- guardloop-0.2.0/src/guardloop/telemetry/__init__.py +5 -0
- guardloop-0.2.0/src/guardloop/telemetry/conventions.py +98 -0
- guardloop-0.2.0/src/guardloop/telemetry/tracer.py +86 -0
- guardloop-0.2.0/src/guardloop/tokenization.py +49 -0
- guardloop-0.2.0/src/guardloop/tools.py +171 -0
- guardloop-0.2.0/tests/__init__.py +1 -0
- guardloop-0.2.0/tests/fakes.py +86 -0
- guardloop-0.2.0/tests/test_budget.py +76 -0
- guardloop-0.2.0/tests/test_circuit_breaker.py +285 -0
- guardloop-0.2.0/tests/test_providers.py +74 -0
- guardloop-0.2.0/tests/test_runtime.py +90 -0
- guardloop-0.2.0/tests/test_telemetry.py +106 -0
- guardloop-0.2.0/uv.lock +1218 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
name: Build distributions
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- name: Check out repository
|
|
18
|
+
uses: actions/checkout@v6
|
|
19
|
+
|
|
20
|
+
- name: Set up Python
|
|
21
|
+
uses: actions/setup-python@v6
|
|
22
|
+
with:
|
|
23
|
+
python-version: "3.11"
|
|
24
|
+
|
|
25
|
+
- name: Set up uv
|
|
26
|
+
uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
|
|
27
|
+
|
|
28
|
+
- name: Build package
|
|
29
|
+
run: uv build --sdist --wheel
|
|
30
|
+
|
|
31
|
+
- name: Upload distributions
|
|
32
|
+
uses: actions/upload-artifact@v7
|
|
33
|
+
with:
|
|
34
|
+
name: python-distributions
|
|
35
|
+
path: dist/*
|
|
36
|
+
if-no-files-found: error
|
|
37
|
+
|
|
38
|
+
publish:
|
|
39
|
+
name: Publish distributions to PyPI
|
|
40
|
+
runs-on: ubuntu-latest
|
|
41
|
+
needs: build
|
|
42
|
+
environment:
|
|
43
|
+
name: pypi
|
|
44
|
+
url: https://pypi.org/project/guardloop/
|
|
45
|
+
permissions:
|
|
46
|
+
id-token: write
|
|
47
|
+
contents: read
|
|
48
|
+
|
|
49
|
+
steps:
|
|
50
|
+
- name: Download distributions
|
|
51
|
+
uses: actions/download-artifact@v7
|
|
52
|
+
with:
|
|
53
|
+
name: python-distributions
|
|
54
|
+
path: dist/
|
|
55
|
+
|
|
56
|
+
- name: Publish package distributions to PyPI
|
|
57
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
guardloop-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 awesome-pro
|
|
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.
|
guardloop-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: guardloop
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A production runtime guardrail for AI agents: budget caps, timeouts, tool limits, and OpenTelemetry traces.
|
|
5
|
+
Project-URL: Homepage, https://github.com/awesome-pro/guardloop
|
|
6
|
+
Project-URL: Documentation, https://github.com/awesome-pro/guardloop#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/awesome-pro/guardloop
|
|
8
|
+
Project-URL: Issues, https://github.com/awesome-pro/guardloop/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/awesome-pro/guardloop/releases
|
|
10
|
+
Author-email: awesome-pro <147910430+awesome-pro@users.noreply.github.com>
|
|
11
|
+
Maintainer-email: awesome-pro <147910430+awesome-pro@users.noreply.github.com>
|
|
12
|
+
License-Expression: MIT
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Keywords: agentic-ai,ai-agents,ai-safety,anthropic,circuit-breaker,llm,mlops,openai,opentelemetry,runtime-guardrails
|
|
15
|
+
Classifier: Development Status :: 3 - Alpha
|
|
16
|
+
Classifier: Framework :: AsyncIO
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Intended Audience :: Science/Research
|
|
19
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
20
|
+
Classifier: Operating System :: OS Independent
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
22
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
27
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Typing :: Typed
|
|
30
|
+
Requires-Python: >=3.11
|
|
31
|
+
Requires-Dist: anthropic<1,>=0.97.0
|
|
32
|
+
Requires-Dist: openai<3,>=2.33.0
|
|
33
|
+
Requires-Dist: opentelemetry-api<2,>=1.41.1
|
|
34
|
+
Requires-Dist: pydantic<3,>=2.13.3
|
|
35
|
+
Requires-Dist: tiktoken<1,>=0.12.0
|
|
36
|
+
Provides-Extra: otel
|
|
37
|
+
Requires-Dist: opentelemetry-exporter-otlp<2,>=1.41.1; extra == 'otel'
|
|
38
|
+
Requires-Dist: opentelemetry-sdk<2,>=1.41.1; extra == 'otel'
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
|
|
41
|
+
# GuardLoop
|
|
42
|
+
|
|
43
|
+
GuardLoop is a production runtime guardrail for AI agents. It wraps model
|
|
44
|
+
clients and tools with hard budget caps, timeout control, tool-call limits, and
|
|
45
|
+
per-tool circuit breakers, with OpenTelemetry traces for every protected call.
|
|
46
|
+
Runaway agent loops can be stopped before they burn through money, and flaky
|
|
47
|
+
tools can be cut off before an agent retries them into a bigger incident.
|
|
48
|
+
|
|
49
|
+
The v0.2 focus is intentionally sharp: **runtime guardrails for async Python
|
|
50
|
+
agents** using direct OpenAI and Anthropic wrappers plus protected tool calls.
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from guardloop import (
|
|
54
|
+
GuardLoop,
|
|
55
|
+
BudgetConfig,
|
|
56
|
+
CircuitBreakerConfig,
|
|
57
|
+
CircuitBreakerPolicy,
|
|
58
|
+
RunContext,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
runtime = GuardLoop(
|
|
62
|
+
budget=BudgetConfig(
|
|
63
|
+
cost_limit_usd="0.10",
|
|
64
|
+
token_limit=10_000,
|
|
65
|
+
time_limit_seconds=60,
|
|
66
|
+
tool_call_limit=20,
|
|
67
|
+
),
|
|
68
|
+
circuit_breakers=CircuitBreakerConfig(
|
|
69
|
+
default=CircuitBreakerPolicy(
|
|
70
|
+
failure_threshold=3,
|
|
71
|
+
recovery_timeout_seconds=30,
|
|
72
|
+
)
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
async def agent(ctx: RunContext, prompt: str) -> str:
|
|
78
|
+
response = await ctx.openai.responses.create(
|
|
79
|
+
model="gpt-5.2",
|
|
80
|
+
input=prompt,
|
|
81
|
+
max_output_tokens=300,
|
|
82
|
+
)
|
|
83
|
+
return str(response.output_text)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
result = await runtime.run(agent, "research agent runtime safety")
|
|
87
|
+
print(result.model_dump_json(indent=2))
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Why This Exists
|
|
91
|
+
|
|
92
|
+
Agents are loops around probabilistic systems. When they go wrong, they can call
|
|
93
|
+
the same model or tool repeatedly, spend unexpected money, and fail without a
|
|
94
|
+
clear trace. GuardLoop puts an explicit execution layer around that loop:
|
|
95
|
+
|
|
96
|
+
```mermaid
|
|
97
|
+
flowchart LR
|
|
98
|
+
U["User code"] --> R["GuardLoop"]
|
|
99
|
+
R --> B["BudgetController"]
|
|
100
|
+
R --> CB["CircuitBreakerRegistry"]
|
|
101
|
+
R --> T["OpenTelemetry spans"]
|
|
102
|
+
R --> C["RunContext"]
|
|
103
|
+
C --> O["Wrapped OpenAI client"]
|
|
104
|
+
C --> A["Wrapped Anthropic client"]
|
|
105
|
+
C --> W["Wrapped tools"]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Install
|
|
109
|
+
|
|
110
|
+
After the first PyPI release is published:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pip install guardloop
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
For local development:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
uv sync
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Optional OpenTelemetry exporters are available through the `otel` extra:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
pip install "guardloop[otel]"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
For local development with the extra:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
uv sync --extra otel
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Try the No-Key Demo
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
uv run python examples/runaway_cost_prevention.py
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The demo uses a fake OpenAI-compatible client and intentionally loops forever.
|
|
141
|
+
GuardLoop stops it when the next model request would exceed the cost cap.
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
uv run python examples/tool_circuit_breaker.py
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
This demo uses a failing fake tool. GuardLoop allows the first failures,
|
|
148
|
+
opens the circuit breaker, then rejects the next call without invoking the tool.
|
|
149
|
+
|
|
150
|
+
## Live Provider Smoke Tests
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
export OPENAI_API_KEY="..."
|
|
154
|
+
export ANTHROPIC_API_KEY="..."
|
|
155
|
+
|
|
156
|
+
uv run python examples/live_openai_basic.py
|
|
157
|
+
uv run python examples/live_anthropic_basic.py
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Both live examples can be customized with `OPENAI_MODEL` or `ANTHROPIC_MODEL`.
|
|
161
|
+
|
|
162
|
+
## Quality Gates
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
uv run pytest
|
|
166
|
+
uv run pytest --cov=guardloop
|
|
167
|
+
uv run ruff check .
|
|
168
|
+
uv run ruff format --check .
|
|
169
|
+
uv run pyright
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## v0.2 Scope
|
|
173
|
+
|
|
174
|
+
- Async Python runtime with `src/` package layout.
|
|
175
|
+
- Hard caps for cost, tokens, time, and tool calls.
|
|
176
|
+
- Per-tool circuit breakers with closed, open, and half-open states.
|
|
177
|
+
- Global default breaker policy plus per-tool overrides.
|
|
178
|
+
- Direct wrappers for `AsyncOpenAI.responses.create`.
|
|
179
|
+
- Direct wrappers for `AsyncAnthropic.messages.create`.
|
|
180
|
+
- OpenTelemetry spans for agent runs, LLM calls, and tools.
|
|
181
|
+
- Fake-client tests and demos that do not require API keys.
|
|
182
|
+
|
|
183
|
+
## Roadmap
|
|
184
|
+
|
|
185
|
+
- v0.2: per-tool circuit breakers.
|
|
186
|
+
- v0.3: verifier/self-healing retry loop.
|
|
187
|
+
- v0.4: LangGraph and OpenAI Agents SDK adapters.
|
|
188
|
+
- v0.5: Jaeger/Phoenix trace screenshots, blog post, and GitHub release.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# GuardLoop
|
|
2
|
+
|
|
3
|
+
GuardLoop is a production runtime guardrail for AI agents. It wraps model
|
|
4
|
+
clients and tools with hard budget caps, timeout control, tool-call limits, and
|
|
5
|
+
per-tool circuit breakers, with OpenTelemetry traces for every protected call.
|
|
6
|
+
Runaway agent loops can be stopped before they burn through money, and flaky
|
|
7
|
+
tools can be cut off before an agent retries them into a bigger incident.
|
|
8
|
+
|
|
9
|
+
The v0.2 focus is intentionally sharp: **runtime guardrails for async Python
|
|
10
|
+
agents** using direct OpenAI and Anthropic wrappers plus protected tool calls.
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from guardloop import (
|
|
14
|
+
GuardLoop,
|
|
15
|
+
BudgetConfig,
|
|
16
|
+
CircuitBreakerConfig,
|
|
17
|
+
CircuitBreakerPolicy,
|
|
18
|
+
RunContext,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
runtime = GuardLoop(
|
|
22
|
+
budget=BudgetConfig(
|
|
23
|
+
cost_limit_usd="0.10",
|
|
24
|
+
token_limit=10_000,
|
|
25
|
+
time_limit_seconds=60,
|
|
26
|
+
tool_call_limit=20,
|
|
27
|
+
),
|
|
28
|
+
circuit_breakers=CircuitBreakerConfig(
|
|
29
|
+
default=CircuitBreakerPolicy(
|
|
30
|
+
failure_threshold=3,
|
|
31
|
+
recovery_timeout_seconds=30,
|
|
32
|
+
)
|
|
33
|
+
),
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def agent(ctx: RunContext, prompt: str) -> str:
|
|
38
|
+
response = await ctx.openai.responses.create(
|
|
39
|
+
model="gpt-5.2",
|
|
40
|
+
input=prompt,
|
|
41
|
+
max_output_tokens=300,
|
|
42
|
+
)
|
|
43
|
+
return str(response.output_text)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
result = await runtime.run(agent, "research agent runtime safety")
|
|
47
|
+
print(result.model_dump_json(indent=2))
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Why This Exists
|
|
51
|
+
|
|
52
|
+
Agents are loops around probabilistic systems. When they go wrong, they can call
|
|
53
|
+
the same model or tool repeatedly, spend unexpected money, and fail without a
|
|
54
|
+
clear trace. GuardLoop puts an explicit execution layer around that loop:
|
|
55
|
+
|
|
56
|
+
```mermaid
|
|
57
|
+
flowchart LR
|
|
58
|
+
U["User code"] --> R["GuardLoop"]
|
|
59
|
+
R --> B["BudgetController"]
|
|
60
|
+
R --> CB["CircuitBreakerRegistry"]
|
|
61
|
+
R --> T["OpenTelemetry spans"]
|
|
62
|
+
R --> C["RunContext"]
|
|
63
|
+
C --> O["Wrapped OpenAI client"]
|
|
64
|
+
C --> A["Wrapped Anthropic client"]
|
|
65
|
+
C --> W["Wrapped tools"]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Install
|
|
69
|
+
|
|
70
|
+
After the first PyPI release is published:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install guardloop
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
For local development:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
uv sync
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Optional OpenTelemetry exporters are available through the `otel` extra:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pip install "guardloop[otel]"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
For local development with the extra:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
uv sync --extra otel
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Try the No-Key Demo
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
uv run python examples/runaway_cost_prevention.py
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The demo uses a fake OpenAI-compatible client and intentionally loops forever.
|
|
101
|
+
GuardLoop stops it when the next model request would exceed the cost cap.
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
uv run python examples/tool_circuit_breaker.py
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
This demo uses a failing fake tool. GuardLoop allows the first failures,
|
|
108
|
+
opens the circuit breaker, then rejects the next call without invoking the tool.
|
|
109
|
+
|
|
110
|
+
## Live Provider Smoke Tests
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
export OPENAI_API_KEY="..."
|
|
114
|
+
export ANTHROPIC_API_KEY="..."
|
|
115
|
+
|
|
116
|
+
uv run python examples/live_openai_basic.py
|
|
117
|
+
uv run python examples/live_anthropic_basic.py
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Both live examples can be customized with `OPENAI_MODEL` or `ANTHROPIC_MODEL`.
|
|
121
|
+
|
|
122
|
+
## Quality Gates
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
uv run pytest
|
|
126
|
+
uv run pytest --cov=guardloop
|
|
127
|
+
uv run ruff check .
|
|
128
|
+
uv run ruff format --check .
|
|
129
|
+
uv run pyright
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## v0.2 Scope
|
|
133
|
+
|
|
134
|
+
- Async Python runtime with `src/` package layout.
|
|
135
|
+
- Hard caps for cost, tokens, time, and tool calls.
|
|
136
|
+
- Per-tool circuit breakers with closed, open, and half-open states.
|
|
137
|
+
- Global default breaker policy plus per-tool overrides.
|
|
138
|
+
- Direct wrappers for `AsyncOpenAI.responses.create`.
|
|
139
|
+
- Direct wrappers for `AsyncAnthropic.messages.create`.
|
|
140
|
+
- OpenTelemetry spans for agent runs, LLM calls, and tools.
|
|
141
|
+
- Fake-client tests and demos that do not require API keys.
|
|
142
|
+
|
|
143
|
+
## Roadmap
|
|
144
|
+
|
|
145
|
+
- v0.2: per-tool circuit breakers.
|
|
146
|
+
- v0.3: verifier/self-healing retry loop.
|
|
147
|
+
- v0.4: LangGraph and OpenAI Agents SDK adapters.
|
|
148
|
+
- v0.5: Jaeger/Phoenix trace screenshots, blog post, and GitHub release.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# GuardLoop v0.2 Design
|
|
2
|
+
|
|
3
|
+
GuardLoop is a wrapper, not an agent framework. A user passes an async agent
|
|
4
|
+
callable to `runtime.run()`. The runtime creates a `RunContext` containing
|
|
5
|
+
wrapped provider clients and tool helpers. The agent still owns its loop; the
|
|
6
|
+
runtime owns enforcement.
|
|
7
|
+
|
|
8
|
+
## Enforcement Points
|
|
9
|
+
|
|
10
|
+
- Before each LLM request, the runtime estimates input tokens, reserves the
|
|
11
|
+
declared maximum output tokens, and checks cost/token caps.
|
|
12
|
+
- After each LLM response, the runtime records actual usage from provider
|
|
13
|
+
metadata.
|
|
14
|
+
- Before each tool call, the runtime checks the tool's circuit breaker, then
|
|
15
|
+
checks and increments the tool-call count.
|
|
16
|
+
- The full run is bounded by `asyncio.timeout()` and monotonic clock checks.
|
|
17
|
+
|
|
18
|
+
## Circuit Breakers
|
|
19
|
+
|
|
20
|
+
Circuit breaker state lives on the `GuardLoop` instance, so it persists
|
|
21
|
+
across multiple `runtime.run()` calls without becoming process-global. Each
|
|
22
|
+
tool name gets an independent state machine:
|
|
23
|
+
|
|
24
|
+
```mermaid
|
|
25
|
+
stateDiagram-v2
|
|
26
|
+
[*] --> closed
|
|
27
|
+
closed --> open: failure threshold reached
|
|
28
|
+
open --> half_open: cooldown elapsed
|
|
29
|
+
half_open --> closed: trial successes reached
|
|
30
|
+
half_open --> open: trial failure
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
When a breaker is open, the tool call is rejected before the tool-call budget is
|
|
34
|
+
incremented and before user code is invoked. Normal tool exceptions count as
|
|
35
|
+
failures. Controlled runtime stops, cancellations, timeouts, and open-breaker
|
|
36
|
+
rejections do not count as tool failures.
|
|
37
|
+
|
|
38
|
+
## Pricing
|
|
39
|
+
|
|
40
|
+
Built-in prices are defaults, not truth forever. Callers can pass
|
|
41
|
+
`ModelPricing` entries to override or add models as providers update pricing.
|
|
42
|
+
|
|
43
|
+
## Telemetry
|
|
44
|
+
|
|
45
|
+
Provider wrappers emit OpenTelemetry spans through a small conventions module.
|
|
46
|
+
This keeps GenAI semantic convention names isolated while the standard evolves.
|
|
47
|
+
Tool spans also include circuit breaker state, failure count, and whether a
|
|
48
|
+
call was blocked.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Publishing GuardLoop to PyPI
|
|
2
|
+
|
|
3
|
+
GuardLoop is configured for PyPI Trusted Publishing from GitHub Actions.
|
|
4
|
+
This avoids long-lived PyPI API tokens and gives GitHub a visible `pypi`
|
|
5
|
+
deployment in the repository sidebar after a successful publish.
|
|
6
|
+
|
|
7
|
+
## One-Time PyPI Setup
|
|
8
|
+
|
|
9
|
+
Create or log in to your PyPI account at https://pypi.org. The package name is
|
|
10
|
+
`guardloop`.
|
|
11
|
+
|
|
12
|
+
Because the project does not exist on PyPI yet, add a pending trusted publisher:
|
|
13
|
+
|
|
14
|
+
- Go to your PyPI account sidebar and open **Publishing**.
|
|
15
|
+
- Add a new GitHub pending publisher.
|
|
16
|
+
- Use owner `awesome-pro`.
|
|
17
|
+
- Use repository `guardloop`.
|
|
18
|
+
- Use workflow filename `publish-pypi.yml`.
|
|
19
|
+
- Leave environment blank so PyPI shows `Environment name: (Any)`.
|
|
20
|
+
- Use project name `guardloop`.
|
|
21
|
+
|
|
22
|
+
Pending publishers do not reserve the package name until the first successful
|
|
23
|
+
publish, so run the first publish soon after creating it.
|
|
24
|
+
|
|
25
|
+
The GitHub workflow still uses a GitHub environment named `pypi` so the
|
|
26
|
+
repository sidebar shows a clean `pypi` deployment after publishing. A PyPI
|
|
27
|
+
publisher configured with `Environment name: (Any)` accepts that workflow.
|
|
28
|
+
|
|
29
|
+
## First Publish
|
|
30
|
+
|
|
31
|
+
After the pending publisher exists on PyPI, run the GitHub workflow manually:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
gh workflow run publish-pypi.yml --repo awesome-pro/guardloop --ref main
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Then watch it:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
gh run list --repo awesome-pro/guardloop --workflow publish-pypi.yml --limit 1
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
When the run succeeds, PyPI should show:
|
|
44
|
+
|
|
45
|
+
```text
|
|
46
|
+
https://pypi.org/project/guardloop/
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The package can then be installed with:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install guardloop
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Future Releases
|
|
56
|
+
|
|
57
|
+
For future versions:
|
|
58
|
+
|
|
59
|
+
1. Bump `version` in `pyproject.toml`.
|
|
60
|
+
2. Build and test locally.
|
|
61
|
+
3. Commit and tag the release.
|
|
62
|
+
4. Create a GitHub release.
|
|
63
|
+
|
|
64
|
+
The `Publish to PyPI` workflow also runs automatically when a GitHub release is
|
|
65
|
+
published, so normal releases will publish to PyPI without a manual workflow
|
|
66
|
+
dispatch.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# GuardLoop Roadmap
|
|
2
|
+
|
|
3
|
+
## v0.1: Runtime Guardrails
|
|
4
|
+
|
|
5
|
+
The first release proves the most interview-friendly production failure mode:
|
|
6
|
+
a runaway agent loop is stopped before the next LLM call would exceed the
|
|
7
|
+
configured budget.
|
|
8
|
+
|
|
9
|
+
## v0.2: Circuit Breakers
|
|
10
|
+
|
|
11
|
+
Implemented per-tool state machines with closed, open, and half-open states.
|
|
12
|
+
Tool calls that repeatedly fail are rejected immediately during the open period.
|
|
13
|
+
|
|
14
|
+
## v0.3: Verifier Retry Loop
|
|
15
|
+
|
|
16
|
+
Next, add deterministic and LLM-based verifiers that can reject final outputs
|
|
17
|
+
and send bounded feedback into a retry attempt.
|
|
18
|
+
|
|
19
|
+
## v0.4: Framework Adapters
|
|
20
|
+
|
|
21
|
+
Add adapters for LangGraph and OpenAI Agents SDK without changing the core
|
|
22
|
+
runtime model.
|
|
23
|
+
|
|
24
|
+
## v0.5: Portfolio Polish
|
|
25
|
+
|
|
26
|
+
Add Jaeger/Phoenix trace screenshots, a demo video, a blog post, and release
|
|
27
|
+
packaging for GitHub and PyPI.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Optional live Anthropic smoke test."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
from guardloop import BudgetConfig, GuardLoop, RunContext
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _content_text(message: object) -> str:
|
|
12
|
+
blocks = getattr(message, "content", [])
|
|
13
|
+
texts: list[str] = []
|
|
14
|
+
for block in blocks:
|
|
15
|
+
text = getattr(block, "text", None)
|
|
16
|
+
if isinstance(text, str):
|
|
17
|
+
texts.append(text)
|
|
18
|
+
return "\n".join(texts)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def agent(ctx: RunContext, prompt: str) -> str:
|
|
22
|
+
message = await ctx.anthropic.messages.create(
|
|
23
|
+
model=os.getenv("ANTHROPIC_MODEL", "claude-sonnet-4-20250514"),
|
|
24
|
+
max_tokens=120,
|
|
25
|
+
messages=[{"role": "user", "content": prompt}],
|
|
26
|
+
)
|
|
27
|
+
return _content_text(message)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
async def main() -> None:
|
|
31
|
+
if not os.getenv("ANTHROPIC_API_KEY"):
|
|
32
|
+
raise SystemExit("Set ANTHROPIC_API_KEY before running this live example.")
|
|
33
|
+
|
|
34
|
+
runtime = GuardLoop(
|
|
35
|
+
budget=BudgetConfig(
|
|
36
|
+
cost_limit_usd="0.05",
|
|
37
|
+
token_limit=5_000,
|
|
38
|
+
time_limit_seconds=60,
|
|
39
|
+
tool_call_limit=5,
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
result = await runtime.run(agent, "Explain agent runtime budget caps in two sentences.")
|
|
43
|
+
print(result.model_dump_json(indent=2))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Optional live OpenAI smoke test."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import os
|
|
7
|
+
from typing import Protocol, cast
|
|
8
|
+
|
|
9
|
+
from guardloop import BudgetConfig, GuardLoop, RunContext
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class HasOutputText(Protocol):
|
|
13
|
+
output_text: str
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async def agent(ctx: RunContext, prompt: str) -> str:
|
|
17
|
+
response = await ctx.openai.responses.create(
|
|
18
|
+
model=os.getenv("OPENAI_MODEL", "gpt-5.2"),
|
|
19
|
+
input=prompt,
|
|
20
|
+
max_output_tokens=120,
|
|
21
|
+
)
|
|
22
|
+
return cast(HasOutputText, response).output_text
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def main() -> None:
|
|
26
|
+
if not os.getenv("OPENAI_API_KEY"):
|
|
27
|
+
raise SystemExit("Set OPENAI_API_KEY before running this live example.")
|
|
28
|
+
|
|
29
|
+
runtime = GuardLoop(
|
|
30
|
+
budget=BudgetConfig(
|
|
31
|
+
cost_limit_usd="0.05",
|
|
32
|
+
token_limit=5_000,
|
|
33
|
+
time_limit_seconds=60,
|
|
34
|
+
tool_call_limit=5,
|
|
35
|
+
)
|
|
36
|
+
)
|
|
37
|
+
result = await runtime.run(agent, "Explain agent runtime budget caps in two sentences.")
|
|
38
|
+
print(result.model_dump_json(indent=2))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
asyncio.run(main())
|