guardloop 0.2.0__py3-none-any.whl
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/__init__.py +48 -0
- guardloop/budget.py +180 -0
- guardloop/circuit_breaker.py +243 -0
- guardloop/context.py +68 -0
- guardloop/exceptions.py +92 -0
- guardloop/models.py +94 -0
- guardloop/pricing.py +116 -0
- guardloop/providers/__init__.py +6 -0
- guardloop/providers/anthropic.py +150 -0
- guardloop/providers/openai.py +138 -0
- guardloop/py.typed +0 -0
- guardloop/runtime.py +190 -0
- guardloop/telemetry/__init__.py +5 -0
- guardloop/telemetry/conventions.py +98 -0
- guardloop/telemetry/tracer.py +86 -0
- guardloop/tokenization.py +49 -0
- guardloop/tools.py +171 -0
- guardloop-0.2.0.dist-info/METADATA +188 -0
- guardloop-0.2.0.dist-info/RECORD +21 -0
- guardloop-0.2.0.dist-info/WHEEL +4 -0
- guardloop-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""OpenTelemetry attribute names behind one compatibility layer.
|
|
2
|
+
|
|
3
|
+
The GenAI semantic conventions are still evolving, so provider wrappers should
|
|
4
|
+
use these constants instead of scattering raw strings through the codebase.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from decimal import Decimal
|
|
10
|
+
|
|
11
|
+
AttributeValue = str | bool | int | float
|
|
12
|
+
Attributes = dict[str, AttributeValue]
|
|
13
|
+
|
|
14
|
+
GEN_AI_SYSTEM = "gen_ai.system"
|
|
15
|
+
GEN_AI_OPERATION_NAME = "gen_ai.operation.name"
|
|
16
|
+
GEN_AI_REQUEST_MODEL = "gen_ai.request.model"
|
|
17
|
+
GEN_AI_RESPONSE_MODEL = "gen_ai.response.model"
|
|
18
|
+
GEN_AI_USAGE_INPUT_TOKENS = "gen_ai.usage.input_tokens"
|
|
19
|
+
GEN_AI_USAGE_OUTPUT_TOKENS = "gen_ai.usage.output_tokens"
|
|
20
|
+
|
|
21
|
+
GUARDLOOP_COST_USD = "guardloop.budget.cost_usd"
|
|
22
|
+
GUARDLOOP_ESTIMATED_COST_USD = "guardloop.budget.estimated_cost_usd"
|
|
23
|
+
GUARDLOOP_TOOL_NAME = "guardloop.tool.name"
|
|
24
|
+
GUARDLOOP_TOOL_CALLS_USED = "guardloop.tool.calls_used"
|
|
25
|
+
GUARDLOOP_CIRCUIT_BREAKER_STATE = "guardloop.circuit_breaker.state"
|
|
26
|
+
GUARDLOOP_CIRCUIT_BREAKER_FAILURE_COUNT = "guardloop.circuit_breaker.failure_count"
|
|
27
|
+
GUARDLOOP_CIRCUIT_BREAKER_BLOCKED = "guardloop.circuit_breaker.blocked"
|
|
28
|
+
GUARDLOOP_CIRCUIT_BREAKER_REMAINING_OPEN_SECONDS = (
|
|
29
|
+
"guardloop.circuit_breaker.remaining_open_seconds"
|
|
30
|
+
)
|
|
31
|
+
GUARDLOOP_TERMINATED_REASON = "guardloop.terminated_reason"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def decimal_attr(value: Decimal) -> float:
|
|
35
|
+
return float(value)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def run_attributes() -> Attributes:
|
|
39
|
+
return {"guardloop.operation": "agent.run"}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def llm_request_attributes(
|
|
43
|
+
*,
|
|
44
|
+
provider: str,
|
|
45
|
+
model: str,
|
|
46
|
+
estimated_input_tokens: int,
|
|
47
|
+
reserved_output_tokens: int,
|
|
48
|
+
estimated_cost_usd: Decimal,
|
|
49
|
+
) -> Attributes:
|
|
50
|
+
return {
|
|
51
|
+
GEN_AI_SYSTEM: provider,
|
|
52
|
+
GEN_AI_OPERATION_NAME: "chat",
|
|
53
|
+
GEN_AI_REQUEST_MODEL: model,
|
|
54
|
+
"guardloop.estimated_input_tokens": estimated_input_tokens,
|
|
55
|
+
"guardloop.reserved_output_tokens": reserved_output_tokens,
|
|
56
|
+
GUARDLOOP_ESTIMATED_COST_USD: decimal_attr(estimated_cost_usd),
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def llm_response_attributes(
|
|
61
|
+
*,
|
|
62
|
+
model: str,
|
|
63
|
+
input_tokens: int,
|
|
64
|
+
output_tokens: int,
|
|
65
|
+
cost_usd: Decimal,
|
|
66
|
+
) -> Attributes:
|
|
67
|
+
return {
|
|
68
|
+
GEN_AI_RESPONSE_MODEL: model,
|
|
69
|
+
GEN_AI_USAGE_INPUT_TOKENS: input_tokens,
|
|
70
|
+
GEN_AI_USAGE_OUTPUT_TOKENS: output_tokens,
|
|
71
|
+
GUARDLOOP_COST_USD: decimal_attr(cost_usd),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def tool_attributes(
|
|
76
|
+
*,
|
|
77
|
+
tool_name: str,
|
|
78
|
+
calls_used: int,
|
|
79
|
+
breaker_state: str | None = None,
|
|
80
|
+
breaker_failure_count: int | None = None,
|
|
81
|
+
breaker_blocked: bool | None = None,
|
|
82
|
+
breaker_remaining_open_seconds: float | None = None,
|
|
83
|
+
) -> Attributes:
|
|
84
|
+
attributes: Attributes = {
|
|
85
|
+
GUARDLOOP_TOOL_NAME: tool_name,
|
|
86
|
+
GUARDLOOP_TOOL_CALLS_USED: calls_used,
|
|
87
|
+
}
|
|
88
|
+
if breaker_state is not None:
|
|
89
|
+
attributes[GUARDLOOP_CIRCUIT_BREAKER_STATE] = breaker_state
|
|
90
|
+
if breaker_failure_count is not None:
|
|
91
|
+
attributes[GUARDLOOP_CIRCUIT_BREAKER_FAILURE_COUNT] = breaker_failure_count
|
|
92
|
+
if breaker_blocked is not None:
|
|
93
|
+
attributes[GUARDLOOP_CIRCUIT_BREAKER_BLOCKED] = breaker_blocked
|
|
94
|
+
if breaker_remaining_open_seconds is not None:
|
|
95
|
+
attributes[GUARDLOOP_CIRCUIT_BREAKER_REMAINING_OPEN_SECONDS] = (
|
|
96
|
+
breaker_remaining_open_seconds
|
|
97
|
+
)
|
|
98
|
+
return attributes
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""OpenTelemetry span creation and optional exporter setup."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Generator
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
|
|
8
|
+
from opentelemetry import trace
|
|
9
|
+
from opentelemetry.trace import Span, Status, StatusCode, Tracer
|
|
10
|
+
|
|
11
|
+
from guardloop.models import TelemetryConfig
|
|
12
|
+
from guardloop.telemetry.conventions import Attributes
|
|
13
|
+
|
|
14
|
+
_otel_configured = False
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def configure_otel_export(config: TelemetryConfig) -> None:
|
|
18
|
+
"""Configure an SDK exporter only when the user asks for one."""
|
|
19
|
+
|
|
20
|
+
global _otel_configured
|
|
21
|
+
if _otel_configured or (not config.otlp_endpoint and not config.console_exporter):
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
|
|
26
|
+
from opentelemetry.sdk.resources import Resource
|
|
27
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
28
|
+
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
|
|
29
|
+
except ImportError as exc:
|
|
30
|
+
raise RuntimeError(
|
|
31
|
+
"OpenTelemetry export requires the 'otel' extra: pip install guardloop[otel]"
|
|
32
|
+
) from exc
|
|
33
|
+
|
|
34
|
+
resource = Resource.create({"service.name": config.service_name})
|
|
35
|
+
provider = TracerProvider(resource=resource)
|
|
36
|
+
|
|
37
|
+
if config.otlp_endpoint:
|
|
38
|
+
provider.add_span_processor(
|
|
39
|
+
BatchSpanProcessor(OTLPSpanExporter(endpoint=config.otlp_endpoint))
|
|
40
|
+
)
|
|
41
|
+
if config.console_exporter:
|
|
42
|
+
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
|
|
43
|
+
|
|
44
|
+
trace.set_tracer_provider(provider)
|
|
45
|
+
_otel_configured = True
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Telemetry:
|
|
49
|
+
"""Thin wrapper around OpenTelemetry spans."""
|
|
50
|
+
|
|
51
|
+
def __init__(self, config: TelemetryConfig, *, tracer: Tracer | None = None) -> None:
|
|
52
|
+
self.config = config
|
|
53
|
+
if config.enabled:
|
|
54
|
+
configure_otel_export(config)
|
|
55
|
+
self.tracer = tracer or trace.get_tracer("guardloop")
|
|
56
|
+
|
|
57
|
+
@contextmanager
|
|
58
|
+
def start_span(self, name: str, attributes: Attributes | None = None) -> Generator[Span]:
|
|
59
|
+
if not self.config.enabled:
|
|
60
|
+
yield trace.get_current_span()
|
|
61
|
+
return
|
|
62
|
+
with self.tracer.start_as_current_span(name, attributes=attributes) as span:
|
|
63
|
+
yield span
|
|
64
|
+
|
|
65
|
+
def set_attributes(self, span: Span, attributes: Attributes) -> None:
|
|
66
|
+
if not self.config.enabled:
|
|
67
|
+
return
|
|
68
|
+
for key, value in attributes.items():
|
|
69
|
+
span.set_attribute(key, value)
|
|
70
|
+
|
|
71
|
+
def record_exception(self, span: Span, exc: BaseException) -> None:
|
|
72
|
+
if not self.config.enabled:
|
|
73
|
+
return
|
|
74
|
+
span.record_exception(exc)
|
|
75
|
+
span.set_status(Status(StatusCode.ERROR, str(exc)))
|
|
76
|
+
|
|
77
|
+
def mark_ok(self, span: Span) -> None:
|
|
78
|
+
if self.config.enabled:
|
|
79
|
+
span.set_status(Status(StatusCode.OK))
|
|
80
|
+
|
|
81
|
+
@staticmethod
|
|
82
|
+
def trace_id(span: Span) -> str | None:
|
|
83
|
+
context = span.get_span_context()
|
|
84
|
+
if not context.is_valid:
|
|
85
|
+
return None
|
|
86
|
+
return f"{context.trace_id:032x}"
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Token estimation helpers used before provider calls."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import math
|
|
7
|
+
from collections.abc import Mapping, Sequence
|
|
8
|
+
from typing import cast
|
|
9
|
+
|
|
10
|
+
import tiktoken
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def payload_to_text(payload: object) -> str:
|
|
14
|
+
"""Flatten common LLM input payloads into text for token estimation."""
|
|
15
|
+
|
|
16
|
+
if payload is None:
|
|
17
|
+
return ""
|
|
18
|
+
if isinstance(payload, str):
|
|
19
|
+
return payload
|
|
20
|
+
if isinstance(payload, Mapping):
|
|
21
|
+
values = cast(Mapping[object, object], payload).values()
|
|
22
|
+
return " ".join(payload_to_text(value) for value in values)
|
|
23
|
+
if isinstance(payload, Sequence) and not isinstance(payload, bytes | bytearray | str):
|
|
24
|
+
items = cast(Sequence[object], payload)
|
|
25
|
+
return " ".join(payload_to_text(item) for item in items)
|
|
26
|
+
try:
|
|
27
|
+
return json.dumps(payload, default=str, sort_keys=True)
|
|
28
|
+
except TypeError:
|
|
29
|
+
return str(payload)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def estimate_openai_tokens(model: str, payload: object) -> int:
|
|
33
|
+
text = payload_to_text(payload)
|
|
34
|
+
if not text:
|
|
35
|
+
return 0
|
|
36
|
+
try:
|
|
37
|
+
encoding = tiktoken.encoding_for_model(model)
|
|
38
|
+
except KeyError:
|
|
39
|
+
encoding = tiktoken.get_encoding("o200k_base")
|
|
40
|
+
return len(encoding.encode(text))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def estimate_anthropic_tokens(payload: object) -> int:
|
|
44
|
+
"""Conservative local estimate when Anthropic token counting is not used."""
|
|
45
|
+
|
|
46
|
+
text = payload_to_text(payload)
|
|
47
|
+
if not text:
|
|
48
|
+
return 0
|
|
49
|
+
return max(1, math.ceil(len(text) / 4))
|
guardloop/tools.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""Runtime-aware tool wrappers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import inspect
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from opentelemetry.trace import Span
|
|
11
|
+
|
|
12
|
+
from guardloop.budget import BudgetController
|
|
13
|
+
from guardloop.circuit_breaker import CircuitBreakerDecision, CircuitBreakerRegistry
|
|
14
|
+
from guardloop.exceptions import CircuitBreakerOpen, GuardLoopError
|
|
15
|
+
from guardloop.telemetry.conventions import tool_attributes
|
|
16
|
+
from guardloop.telemetry.tracer import Telemetry
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ToolRunner:
|
|
20
|
+
"""Wrap arbitrary sync or async Python callables with runtime checks."""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
budget: BudgetController,
|
|
25
|
+
telemetry: Telemetry,
|
|
26
|
+
circuit_breakers: CircuitBreakerRegistry,
|
|
27
|
+
) -> None:
|
|
28
|
+
self._budget = budget
|
|
29
|
+
self._telemetry = telemetry
|
|
30
|
+
self._circuit_breakers = circuit_breakers
|
|
31
|
+
|
|
32
|
+
def wrap(self, name: str, func: Callable[..., Any]) -> Callable[..., Any]:
|
|
33
|
+
async def wrapped(*args: Any, **kwargs: Any) -> Any:
|
|
34
|
+
return await self.call(name, func, *args, **kwargs)
|
|
35
|
+
|
|
36
|
+
return wrapped
|
|
37
|
+
|
|
38
|
+
async def call(self, name: str, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
|
39
|
+
with self._telemetry.start_span(
|
|
40
|
+
f"tool_call {name}",
|
|
41
|
+
tool_attributes(tool_name=name, calls_used=self._budget.tool_calls),
|
|
42
|
+
) as span:
|
|
43
|
+
try:
|
|
44
|
+
decision = self._circuit_breakers.before_call(name)
|
|
45
|
+
_apply_breaker_decision(
|
|
46
|
+
span,
|
|
47
|
+
self._telemetry,
|
|
48
|
+
decision,
|
|
49
|
+
calls_used=self._budget.tool_calls,
|
|
50
|
+
blocked=False,
|
|
51
|
+
)
|
|
52
|
+
except CircuitBreakerOpen as exc:
|
|
53
|
+
_apply_breaker_open(
|
|
54
|
+
span,
|
|
55
|
+
self._telemetry,
|
|
56
|
+
name,
|
|
57
|
+
exc,
|
|
58
|
+
calls_used=self._budget.tool_calls,
|
|
59
|
+
)
|
|
60
|
+
self._telemetry.record_exception(span, exc)
|
|
61
|
+
raise
|
|
62
|
+
|
|
63
|
+
self._budget.record_tool_call_started(name)
|
|
64
|
+
self._telemetry.set_attributes(
|
|
65
|
+
span,
|
|
66
|
+
tool_attributes(tool_name=name, calls_used=self._budget.tool_calls),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
try:
|
|
70
|
+
result = func(*args, **kwargs)
|
|
71
|
+
if inspect.isawaitable(result):
|
|
72
|
+
result = await result
|
|
73
|
+
decision = self._circuit_breakers.record_success(name)
|
|
74
|
+
_apply_breaker_decision(
|
|
75
|
+
span,
|
|
76
|
+
self._telemetry,
|
|
77
|
+
decision,
|
|
78
|
+
calls_used=self._budget.tool_calls,
|
|
79
|
+
blocked=False,
|
|
80
|
+
)
|
|
81
|
+
self._telemetry.mark_ok(span)
|
|
82
|
+
return result
|
|
83
|
+
except GuardLoopError as exc:
|
|
84
|
+
self._telemetry.record_exception(span, exc)
|
|
85
|
+
raise
|
|
86
|
+
except (asyncio.CancelledError, TimeoutError) as exc:
|
|
87
|
+
self._telemetry.record_exception(span, exc)
|
|
88
|
+
raise
|
|
89
|
+
except Exception as exc:
|
|
90
|
+
decision = self._circuit_breakers.record_failure(name)
|
|
91
|
+
_apply_breaker_decision(
|
|
92
|
+
span,
|
|
93
|
+
self._telemetry,
|
|
94
|
+
decision,
|
|
95
|
+
calls_used=self._budget.tool_calls,
|
|
96
|
+
blocked=False,
|
|
97
|
+
)
|
|
98
|
+
self._telemetry.record_exception(span, exc)
|
|
99
|
+
raise
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _apply_breaker_decision(
|
|
103
|
+
span: Span,
|
|
104
|
+
telemetry: Telemetry,
|
|
105
|
+
decision: CircuitBreakerDecision | None,
|
|
106
|
+
*,
|
|
107
|
+
calls_used: int,
|
|
108
|
+
blocked: bool,
|
|
109
|
+
) -> None:
|
|
110
|
+
if decision is None:
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
snapshot = decision.snapshot
|
|
114
|
+
telemetry.set_attributes(
|
|
115
|
+
span,
|
|
116
|
+
tool_attributes(
|
|
117
|
+
tool_name=snapshot.tool_name,
|
|
118
|
+
calls_used=calls_used,
|
|
119
|
+
breaker_state=snapshot.state.value,
|
|
120
|
+
breaker_failure_count=snapshot.failure_count,
|
|
121
|
+
breaker_blocked=blocked,
|
|
122
|
+
breaker_remaining_open_seconds=snapshot.remaining_open_seconds,
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
if not telemetry.config.enabled:
|
|
126
|
+
return
|
|
127
|
+
for event_name in decision.events:
|
|
128
|
+
span.add_event(
|
|
129
|
+
event_name,
|
|
130
|
+
tool_attributes(
|
|
131
|
+
tool_name=snapshot.tool_name,
|
|
132
|
+
calls_used=calls_used,
|
|
133
|
+
breaker_state=snapshot.state.value,
|
|
134
|
+
breaker_failure_count=snapshot.failure_count,
|
|
135
|
+
breaker_blocked=blocked,
|
|
136
|
+
breaker_remaining_open_seconds=snapshot.remaining_open_seconds,
|
|
137
|
+
),
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _apply_breaker_open(
|
|
142
|
+
span: Span,
|
|
143
|
+
telemetry: Telemetry,
|
|
144
|
+
tool_name: str,
|
|
145
|
+
exc: CircuitBreakerOpen,
|
|
146
|
+
*,
|
|
147
|
+
calls_used: int,
|
|
148
|
+
) -> None:
|
|
149
|
+
attributes = tool_attributes(
|
|
150
|
+
tool_name=tool_name,
|
|
151
|
+
calls_used=calls_used,
|
|
152
|
+
breaker_state=str(exc.details.get("state", "open")),
|
|
153
|
+
breaker_failure_count=_int_detail(exc.details.get("failure_count")),
|
|
154
|
+
breaker_blocked=True,
|
|
155
|
+
breaker_remaining_open_seconds=_float_detail(exc.details.get("remaining_open_seconds")),
|
|
156
|
+
)
|
|
157
|
+
telemetry.set_attributes(span, attributes)
|
|
158
|
+
if telemetry.config.enabled:
|
|
159
|
+
span.add_event("guardloop.circuit_breaker.blocked", attributes)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _int_detail(value: object) -> int | None:
|
|
163
|
+
if isinstance(value, int):
|
|
164
|
+
return value
|
|
165
|
+
return None
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _float_detail(value: object) -> float | None:
|
|
169
|
+
if isinstance(value, int | float):
|
|
170
|
+
return float(value)
|
|
171
|
+
return None
|
|
@@ -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,21 @@
|
|
|
1
|
+
guardloop/__init__.py,sha256=z_rm7p_BCSR-S7VUwydPKbsvKFO8UXy_Tfllyjn0LmA,1144
|
|
2
|
+
guardloop/budget.py,sha256=FblwBnSc4chzEQLyVlO5Rwx1CEPS7eQDNuDvaPCvtDc,6313
|
|
3
|
+
guardloop/circuit_breaker.py,sha256=JUPV-rAhl9R4VDuQm2DCGtPibeb1rmtM6RhGLV4ufwo,8511
|
|
4
|
+
guardloop/context.py,sha256=oCFBbjoL04_Vb47tnZYdIks7r4reQpycL3HvxazBzNw,2274
|
|
5
|
+
guardloop/exceptions.py,sha256=mzSUpfCsL-ZB8_jm8eq18E_mjnfEwpteBOog6_jl17k,2500
|
|
6
|
+
guardloop/models.py,sha256=uOKSPRcvs5SqK09jqrbXQrBdQJWxgG12TOylYtz4xAg,3103
|
|
7
|
+
guardloop/pricing.py,sha256=b6c-809SK1CZer-qfVXDKgsJTAQ09yBE2O_u0PlI7uQ,4452
|
|
8
|
+
guardloop/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
guardloop/runtime.py,sha256=3VAfJ6Y6AxT_IswWkbavfujy1OXBpaaioA7L1Nw2ToA,6991
|
|
10
|
+
guardloop/tokenization.py,sha256=XrZSPTctqLU_EzV2CrkhdufG-_Q_DHj-eYkDk494ZRQ,1492
|
|
11
|
+
guardloop/tools.py,sha256=_V0utz3mRyaeFV8ebEy-Ml1iwvAugDd0LB02LsR-JCM,5608
|
|
12
|
+
guardloop/providers/__init__.py,sha256=sVVUMpTgimGDYFse6z9p--DtuXSngWEyvp4v3KfCX9k,227
|
|
13
|
+
guardloop/providers/anthropic.py,sha256=o2UxZ3QS48gkTj9x2YwxKWH73ie49WP20oUn5Z6ZilI,5142
|
|
14
|
+
guardloop/providers/openai.py,sha256=5lucBznrljtENeRihbyi26CI3Yk-Uvn1fcWEq8dDkkU,4889
|
|
15
|
+
guardloop/telemetry/__init__.py,sha256=toNqNsQqM5PDyBL6wRNEftpaR2erOax-PD9lXZnI6vU,114
|
|
16
|
+
guardloop/telemetry/conventions.py,sha256=3RIQbgAMz-eefsgtzW9ad-7n4Q3jsGzz1t5NQOA6AqY,3232
|
|
17
|
+
guardloop/telemetry/tracer.py,sha256=UHStG6IzlxxcFZuqG6oOaeGZRB0H3OhtJSXDENlVInI,3011
|
|
18
|
+
guardloop-0.2.0.dist-info/METADATA,sha256=Z8zFkvXCkVO4RTgsY4-ITi1X3z0Nryb83shRmYi2zCA,5806
|
|
19
|
+
guardloop-0.2.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
20
|
+
guardloop-0.2.0.dist-info/licenses/LICENSE,sha256=Ep4HSTCouInWuzqdZd-rio_XKIZSrtgGYw5mbgFCGsU,1068
|
|
21
|
+
guardloop-0.2.0.dist-info/RECORD,,
|
|
@@ -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.
|