BudgetGuard
with) and async (async with) usage.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
user_id | str | — | Identifier for the budget owner. Included in BudgetExceededError messages and on the exception object. |
token_limit | int | None | None | Maximum cumulative tokens (input + output combined). Raises BudgetExceededError when tokens_used >= token_limit. |
usd_limit | float | None | None | Maximum cumulative USD cost. Raises BudgetExceededError when usd_used >= usd_limit. |
None, usage is tracked but no error is ever raised.
Properties
| Property | Type | Description |
|---|---|---|
tokens_used | int | Total tokens consumed so far (input + output across all calls). |
usd_used | float | Total USD cost so far. |
user_id | str | The user_id passed to the constructor. |
token_limit | int | None | The token_limit passed to the constructor. |
usd_limit | float | None | The usd_limit passed to the constructor. |
BudgetExceededError
BudgetGuard when a token or USD limit is exceeded.
Attributes
| Attribute | Type | Description |
|---|---|---|
user_id | str | The user ID from the active BudgetGuard. |
tokens_used | int | Total tokens consumed at the moment the limit was hit. |
usd_used | float | Total USD cost at the moment the limit was hit. |
token_limit | int | None | The token limit that was set (may be None if no token limit). |
usd_limit | float | None | The USD limit that was set (may be None if no USD limit). |
limit_type | Literal["token", "usd"] | Which limit triggered the error. |
Tool Runtime
RunContext
max_attempts, idempotent). Supports sync and async usage.
Constructor params
| Parameter | Type | Default | Description |
|---|---|---|---|
run_id | str | None | auto-generated UUID | Optional explicit run identifier used by runtime exceptions |
Methods
| Method | Signature | Description |
|---|---|---|
| enter | __enter__() -> RunContext | Activate run state |
| exit | __exit__(exc_type, exc_val, exc_tb) -> None | Restore previous run state |
| async enter | __aenter__() -> RunContext | Async enter |
| async exit | __aexit__(exc_type, exc_val, exc_tb) -> None | Async exit |
| attempts | get_attempt_count(tool_id: str) -> int | Return current attempt count for a tool id in this context |
Tool Guards
configure()
@rate_limit
| Parameter | Type | Default | Description |
|---|---|---|---|
max_calls | int | 10 | Maximum calls allowed within period |
period | float | 60.0 | Sliding-window length in seconds |
scope | str | None | None | Argument name used as counter key; None means one global counter |
FailureKind
@circuit_breaker.
Members:
TRANSPORTTIMEOUTOVERLOADEDTHROTTLEDAUTHINVALIDNOT_FOUNDCONFLICTUNKNOWN
Preset constants
FailureKind values.
@circuit_breaker
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | required | Dependency name |
max_fails | int | 3 | Counted failures before OPEN |
reset_timeout | float | 60.0 | Seconds before calls are allowed again |
fail_on | set[FailureKind] | FAIL_ON_DEFAULT | Kinds that increment/open |
ignore_on | set[FailureKind] | IGNORE_ON_DEFAULT | Kinds that do not affect breaker state |
@max_attempts
RunContext.
| Parameter | Type | Default | Description |
|---|---|---|---|
calls | int | required | Max allowed attempts per run |
MissingRuntimeContextError if no RunContext is active.
@timeout
| Parameter | Type | Default | Description |
|---|---|---|---|
seconds | float | required | Timeout threshold in seconds |
executor | Executor | None | None | Optional executor for sync tool execution |
ToolTimeoutError when exceeded.
shutdown()
@timeout when no custom executor is passed.
@idempotent
(tool_id, idempotency_key) in an active RunContext.
| Parameter | Type | Default | Description |
|---|---|---|---|
ttl_s | float | 3600 | TTL for idempotency entries |
on_duplicate | "return" | "raise" | "return" | Return cached result or raise |
safe_exceptions | tuple | () | Exceptions that clear state and allow retry |
idempotency_key parameter.
Chain-of-custody APIs
session()
@prove and @enforce.
Supports sync and async usage.
| Parameter | Type | Default | Description |
|---|---|---|---|
id | str | required | Session identifier (request/run correlation id) |
scope | dict[str, str] | None | None | Optional scope dimensions (for example {"user_id": "u42"}) |
GuardSession
Context manager returned bysession(...).
| Method | Signature | Description |
|---|---|---|
| enter | __enter__() -> GuardSession | Activate session state |
| exit | __exit__(...) -> None | Restore previous session state |
| async enter | __aenter__() -> GuardSession | Async enter |
| async exit | __aexit__(...) -> None | Async exit |
@prove
| Parameter | Type | Default | Description |
|---|---|---|---|
kind | str | required | Fact kind/category |
extract | str | Callable | required | Field/attribute name or callable extractor |
ttl | float | 300 | Fact TTL in seconds |
max_items | int | 200 | Maximum values to mint per invocation |
on_too_many | "block" | "truncate" | "block" | Block with GuardError or truncate extracted values |
actguard.session(...). Without it, raises GuardError(code="NO_SESSION").
@enforce
| Parameter | Type | Default | Description |
|---|---|---|---|
rules | list[Rule] | required | Rule objects evaluated in order |
actguard.session(...). Without it, raises GuardError(code="NO_SESSION").
RequireFact
Threshold
BlockRegex
- Fact verification state is in-memory, process-local, and ephemeral.
- Facts are isolated by session id and scope hash.
- State is not durable across restarts and is not shared across processes.
@tool
| Kwarg | Type | Description |
|---|---|---|
rate_limit | dict | None | max_calls, period, scope |
circuit_breaker | dict | None | name, max_fails, reset_timeout, fail_on, ignore_on |
max_attempts | dict | None | calls |
timeout | float | None | Timeout in seconds |
timeout_executor | Executor | None | Custom executor for sync timeout execution |
idempotent | dict | None | ttl_s, on_duplicate, safe_exceptions |
policy | — | Reserved stub |
idempotent -> max_attempts -> circuit_breaker -> rate_limit -> timeout -> fn
Exceptions
ToolGuardError
GuardError
@prove and @enforce.
| Attribute | Type |
|---|---|
code | str |
message | str |
details | dict |
fix_hint | str | None |
code values: NO_SESSION, MISSING_FACT, TOO_MANY_RESULTS,
THRESHOLD_EXCEEDED, PATTERN_BLOCKED.
ToolExecutionError
RateLimitExceeded
| Attribute | Type |
|---|---|
func_name | str |
scope_value | str | None |
max_calls | int |
period | float |
retry_after | float |
CircuitOpenError
| Attribute | Type |
|---|---|
dependency_name | str |
reset_at | float |
retry_after | float |
MissingRuntimeContextError
RunContext.
MaxAttemptsExceeded
| Attribute | Type |
|---|---|
run_id | str |
tool_name | str |
limit | int |
used | int |
ToolTimeoutError
| Attribute | Type |
|---|---|
tool_name | str |
timeout_s | float |
run_id | str | None |
InvalidIdempotentToolError
idempotency_key.
MissingIdempotencyKeyError
idempotency_key is empty or missing.
IdempotencyInProgress
DuplicateIdempotencyKey
on_duplicate="raise".
