agentic-gate (Python)
Deterministic schema validation gates and circuit breakers for LLM function calling — a Pydantic-based port of the agentic-gate npm package. Same engine, same guarantees, Python API.
See the main repo README for the full problem statement and architecture diagram. This document covers the Python-specific API.
Install
pip install agentic-gate
The only dependency is pydantic>=2.0.0.
Quick start
import asyncio
from typing import Literal
from pydantic import BaseModel
from agentic_gate import AgenticGate
class RestartEc2Args(BaseModel):
instance_id: str
region: Literal["us-east-1", "us-west-2", "ap-south-1"]
async def restart_ec2(args: RestartEc2Args):
# Your real downstream call (boto3, an HTTP client, etc.)
return {"status": "success", "instance_id": args.instance_id, "region": args.region}
async def main():
gate = AgenticGate()
gate.register_tool("restart_ec2_instance", RestartEc2Args, execute=restart_ec2)
# Feed it raw, untrusted arguments straight from the LLM's tool-call payload
result = await gate.intercept_and_execute(
"restart_ec2_instance",
{"instance_id": "i-0123456789abcdef0", "region": "eu-central-1"}, # not in the enum
)
if not result.success:
print(result.error)
# "[Validation Gate Failed]: region: Input should be 'us-east-1', 'us-west-2' or 'ap-south-1'"
# Feed this string back into the LLM's message history so it can self-correct.
asyncio.run(main())
Circuit breaker + telemetry
gate = AgenticGate(
max_consecutive_failures=3, # 0 disables the breaker
on_gate_failure=lambda e: metrics.increment(f"gate.failure.{e.reason}", tags={"tool": e.tool_name}),
on_gate_success=lambda e: metrics.increment("gate.success", tags={"tool": e.tool_name}),
)
# After 3 consecutive failures for "restart_ec2_instance", the gate short-circuits:
# GateResult(success=False, error="[Circuit Breaker OPEN]: Tool 'restart_ec2_instance' has failed 3 consecutive times...")
gate.reset_circuit("restart_ec2_instance") # once the underlying issue is fixed
Async external-state validation
Pydantic only checks the shape of the arguments — it can't tell you whether
i-0123456789abcdef0 is an EC2 instance that actually exists. For that, pass
a validate callback: it runs after the schema passes and before execute,
and raising rejects the call exactly like a schema failure (same circuit
breaker, same telemetry, reason "async-validation"):
import boto3
ec2 = boto3.client("ec2")
async def validate_instance_exists(args: RestartEc2Args):
response = ec2.describe_instances(InstanceIds=[args.instance_id])
if not response["Reservations"]:
raise ValueError(f"Instance '{args.instance_id}' does not exist in {args.region}")
gate.register_tool(
"restart_ec2_instance",
RestartEc2Args,
execute=restart_ec2,
validate=validate_instance_exists,
)
API
AgenticGate(max_consecutive_failures=3, on_gate_success=None, on_gate_failure=None)
max_consecutive_failures— trips the circuit breaker after this many consecutive failures for a given tool.0disables it.on_gate_success(event: GateSuccessEvent)/on_gate_failure(event: GateFailureEvent)— telemetry hooks called on everyintercept_and_execute.
gate.register_tool(name, schema, execute, validate=None)
schema— a PydanticBaseModelsubclass.execute(args: schema) -> Awaitable[Any]— runs only if validation (andvalidate, if given) succeeds.validate(args: schema) -> Awaitable[None]— optional; raise to reject.
await gate.intercept_and_execute(tool_name, raw_arguments) -> GateResult
GateResult has success: bool, data: Any | None, error: str | None.
gate.reset_circuit(tool_name)
Manually clears a tool's failure count, e.g. after fixing the underlying issue.
Development
cd python
python -m venv .venv
./.venv/bin/pip install -e ".[dev]" # Windows: .venv\Scripts\pip
pytest
License
MIT — see LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agentic_gate-1.0.0.tar.gz.
File metadata
- Download URL: agentic_gate-1.0.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4acb6f2b746fec4c5c904ab08e32db395af4859d9e5b3fd4702b78d59b8e8494
|
|
| MD5 |
0c7078cc3cf64a88b5e03c08f84a655d
|
|
| BLAKE2b-256 |
621fb0c42061a355017f318a3d29f8cd0ebed6f245213957f193dedae29133ff
|
Provenance
The following attestation bundles were made for agentic_gate-1.0.0.tar.gz:
Publisher:
publish-python.yml on Akhilesh-Varute/agentic-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agentic_gate-1.0.0.tar.gz -
Subject digest:
4acb6f2b746fec4c5c904ab08e32db395af4859d9e5b3fd4702b78d59b8e8494 - Sigstore transparency entry: 2529244964
- Sigstore integration time:
-
Permalink:
Akhilesh-Varute/agentic-gate@35c8967f343004b8a5174399e08cd037f5ed5a2a -
Branch / Tag:
refs/tags/python-v1.0.0 - Owner: https://github.com/Akhilesh-Varute
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@35c8967f343004b8a5174399e08cd037f5ed5a2a -
Trigger Event:
release
-
Statement type:
File details
Details for the file agentic_gate-1.0.0-py3-none-any.whl.
File metadata
- Download URL: agentic_gate-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
428da85db58f17aabd4848190f47f42718619d778b2eac7b4bb69ee80c6cf5b6
|
|
| MD5 |
e765aa8d6382238b6a7252c616c310a9
|
|
| BLAKE2b-256 |
a957658c957089c76e08e6b889a3dba0926373eeee27d519d6218aa68b190c25
|
Provenance
The following attestation bundles were made for agentic_gate-1.0.0-py3-none-any.whl:
Publisher:
publish-python.yml on Akhilesh-Varute/agentic-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agentic_gate-1.0.0-py3-none-any.whl -
Subject digest:
428da85db58f17aabd4848190f47f42718619d778b2eac7b4bb69ee80c6cf5b6 - Sigstore transparency entry: 2529245221
- Sigstore integration time:
-
Permalink:
Akhilesh-Varute/agentic-gate@35c8967f343004b8a5174399e08cd037f5ed5a2a -
Branch / Tag:
refs/tags/python-v1.0.0 - Owner: https://github.com/Akhilesh-Varute
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@35c8967f343004b8a5174399e08cd037f5ed5a2a -
Trigger Event:
release
-
Statement type: