Requirements
- Python 3.9+
- At least one supported LLM SDK installed (see Integrations)
Install actguard
Set a USD limit
Stop spending as soon as a user’s request crosses $0.05:Set a token limit
Set both limits
Either limit triggers the error, whichever is hit first:Async usage
BudgetGuard is also an async context manager:
Streaming
Streaming responses are fully supported. actguard wraps the iterator transparently and captures the usage chunk emitted at the end of the stream:
Note: For streaming chat completions, actguard automatically injects
stream_options={"include_usage": true} into the request so the OpenAI SDK
returns a usage chunk. This is harmless to your code.
Configure actguard (optional)
actguard.configure() wires in the ActGuard gateway so tool-guard checks can also be reported for global enforcement across processes. Decorators work with no configuration.
Three ways to provide config:
- JSON file path: pass a file containing
agent_id,gateway_url, andapi_key. - Base64 JSON string: pass a base64-encoded version of the same JSON.
ACTGUARD_CONFIGenv var: set the variable and callconfigure()with no args.
RunContext for runtime-state decorators
max_attempts and idempotent rely on run-scoped state, so they require an active RunContext:
Rate-limit a tool
Add a per-user rate limit to any tool function with a single decorator:scope="user_id" means each distinct user_id gets its own counter. Omit scope for one global counter.
Circuit-break a tool
Add a dependency-health breaker so repeated infra failures short-circuit quickly:Time-bound a tool
Usetimeout to bound wall-clock runtime for sync or async tools:
Deduplicate with idempotency keys
Useidempotent to enforce at-most-once execution per (tool, idempotency_key) in a run:
Chain-of-custody: session + prove + enforce + in-memory store
prove and enforce use a chain-of-custody session, so they require actguard.session(...):
RunContext for max_attempts/idempotent, and session for prove/enforce.
Prove then enforce in one flow
Useprove on read tools to mint verified facts, then enforce on write tools:
enforce raises GuardError with code MISSING_FACT.
In-memory store behavior (local by default)
- Runtime facts and guard state are stored in-memory in the current process.
- State is ephemeral (not durable across process restarts).
- For cross-process/global enforcement visibility, configure the ActGuard gateway.
Combine guards with @actguard.tool
Use the unified decorator when you want one declaration:Which guard should I use?
- Use
rate_limitto cap request volume per window. - Use
circuit_breakerto stop hammering unhealthy dependencies. - Use
max_attemptsto cap retries/loops per run. - Use
timeoutto bound wall-clock latency. - Use
idempotentto deduplicate side-effectful tools. - Use
prove+enforceto require read-before-write chain-of-custody.
What’s next
- Core Concepts - understand how limits and isolation work
- Tool Guards - tool decorator behavior and framework integrations
- Integrations - provider-specific requirements
- API Reference - full parameter and exception reference
