YAML spec + validation SDK for production agent reliability — cost control, tool-use security, and audit trails.
Project description
Agent Contracts
YAML spec + validation SDK for production agent reliability.
Cost control, tool-use security, and audit trails in under 30 minutes of integration. Works with any framework. Enforces at the runtime layer, not via prompts.
pip install aicontracts
The Problem
Production agents fail at 41-87% rates. 97% of enterprises with agents in production haven't figured out how to scale them. The four pain points:
| Problem | Without Contracts | With Contracts |
|---|---|---|
| Cost runaway | No ceiling on token spend | Budget circuit breaker per invocation |
| Unauthorized tool use | Ambient authority, prompt-bypassable | Default-deny allowlist at SDK layer |
| No audit trail | No record of authorized vs. actual | OTel-compatible violation events |
| Silent regressions | Prompt changes break things invisibly | Versioned contracts with SLO monitoring |
5-Minute Quick Start
1. Write a Contract (or generate one)
# AGENT_CONTRACT.yaml
agent_contract: "0.1.0"
identity:
name: my-agent
version: "1.0.0"
contract:
postconditions:
- name: produces_output
check: "output is not None"
enforcement: sync_block
severity: critical
effects:
authorized:
tools: [search, database.read]
network: ["https://api.example.com/*"]
resources:
budgets:
max_cost_usd: 0.50
max_tokens: 10000
max_tool_calls: 20
Or generate from observed behavior:
aicontracts init --from-trace traces.jsonl -o AGENT_CONTRACT.yaml
2. Enforce at Runtime
from agent_contracts import load_contract, ContractEnforcer
contract = load_contract("AGENT_CONTRACT.yaml")
with ContractEnforcer(contract) as enforcer:
# Each tool call is checked against the allowlist and budget
enforcer.check_tool_call("search") # OK - in allowlist
enforcer.check_tool_call("delete_all") # BLOCKED - not authorized
enforcer.add_cost(0.05) # Tracked against max_cost_usd
enforcer.add_tokens(500) # Tracked against max_tokens
# Postconditions evaluated after execution
enforcer.evaluate_postconditions(result)
3. Framework Integration (3 lines)
LangChain:
from agent_contracts.adapters.langchain import ContractCallbackHandler
handler = ContractCallbackHandler.from_file("AGENT_CONTRACT.yaml")
agent.invoke({"input": query}, config={"callbacks": [handler]})
CrewAI:
from agent_contracts.adapters.crewai import ContractGuard
guard = ContractGuard.from_file("AGENT_CONTRACT.yaml")
result = guard.execute(crew, inputs={"query": query})
Pydantic AI:
from agent_contracts.adapters.pydantic_ai import ContractMiddleware
middleware = ContractMiddleware.from_file("AGENT_CONTRACT.yaml")
result = await middleware.run(agent, prompt)
OpenAI Agents SDK:
from agent_contracts.adapters.openai_agents import ContractRunHooks
hooks = ContractRunHooks.from_file("AGENT_CONTRACT.yaml")
result = await Runner.run(agent, "prompt", run_hooks=[hooks])
Claude Agent SDK:
from agent_contracts.adapters.claude_agent import ContractHooks
hooks = ContractHooks.from_file("AGENT_CONTRACT.yaml")
# Pass hooks.pre_tool_use to ClaudeAgentOptions
Three Tiers
Start simple, add guarantees as production demands.
| Tier | Fields | Value |
|---|---|---|
| 0: Standalone | identity + 1 postcondition (4 fields) | Self-documentation, local validation |
| 1: Enforceable | + schemas, effects, budgets | Cost control, tool gating, I/O validation |
| 2: Composable | + failure model, delegation, observability, SLOs | Multi-agent composition, audit trails, canary gates |
CLI
# Validate a contract
aicontracts validate AGENT_CONTRACT.yaml
# Check composition compatibility
aicontracts check-compat producer.yaml consumer.yaml
# Generate from execution traces
aicontracts init --from-trace traces.jsonl
# Run eval suite against postconditions
aicontracts test AGENT_CONTRACT.yaml --eval-suite tests/
Key Design Decisions
- Spec + SDK, not protocol or platform — the OpenAPI model
- YAML primary — JSON Schema validation, CEL-like inline expressions
- Graduated tiers — Tier 0 is 4 fields, not 40
- Effects: authorized vs. declared — intersection for delegation, union for audit
- Enforcement at SDK layer — never in prompts (prompt injection can't bypass)
- MCP extension, not fork —
x-agent-contracton tool definitions
Positioning
MCP governs how agents connect. Agent Skills govern what agents advertise. A2A governs how agents find each other. Agent Contracts govern what agents must do, must not do, and what happens when they fail.
Project Structure
schemas/ JSON Schema for AGENT_CONTRACT.yaml
spec/SPECIFICATION.md Human-readable spec narrative
mcp/x-agent-contract.md MCP extension proposal
src/agent_contracts/ Python SDK
loader.py Contract loading + validation
enforcer.py Runtime enforcement middleware
effects.py Default-deny effect gating
budgets.py Budget tracking + circuit breaker
postconditions.py Postcondition evaluation
violations.py OTel-compatible violation events
composition.py Contract Differential checker
cli.py CLI tool
adapters/ Framework adapters
examples/ Reference contracts (Tier 0, 1, 2)
License
Apache-2.0
Author
Piyush Vyas
Project details
Release history Release notifications | RSS feed
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 aicontracts-0.1.1.tar.gz.
File metadata
- Download URL: aicontracts-0.1.1.tar.gz
- Upload date:
- Size: 36.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df3f93861f99713239fd72d5bc9b5ee8d257a74b485b8313b08c03a86e0c40e9
|
|
| MD5 |
7f2c0d4c58f2fc75ad8bda5d9ab080e4
|
|
| BLAKE2b-256 |
152dd34dfc077f9723fcc67c5d3cb9ada595f694704fc2798665ff894cbb3308
|
Provenance
The following attestation bundles were made for aicontracts-0.1.1.tar.gz:
Publisher:
publish.yml on pyyush/agentcontracts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aicontracts-0.1.1.tar.gz -
Subject digest:
df3f93861f99713239fd72d5bc9b5ee8d257a74b485b8313b08c03a86e0c40e9 - Sigstore transparency entry: 1186409566
- Sigstore integration time:
-
Permalink:
pyyush/agentcontracts@3a9bcf42aaaeef381ab6850870188c2fc7724d9c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pyyush
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3a9bcf42aaaeef381ab6850870188c2fc7724d9c -
Trigger Event:
release
-
Statement type:
File details
Details for the file aicontracts-0.1.1-py3-none-any.whl.
File metadata
- Download URL: aicontracts-0.1.1-py3-none-any.whl
- Upload date:
- Size: 45.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d034658133c3cbd9efc6819f77158e168d85f7e52dfa8543d8e2e6f24cf7c5d4
|
|
| MD5 |
a3fc76a97b3d32ee306fc34eae5d02b4
|
|
| BLAKE2b-256 |
66428f35d621716378bb8b822cc9cbb98a66ba838baa1b4cb3d46f9d7ebee031
|
Provenance
The following attestation bundles were made for aicontracts-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on pyyush/agentcontracts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aicontracts-0.1.1-py3-none-any.whl -
Subject digest:
d034658133c3cbd9efc6819f77158e168d85f7e52dfa8543d8e2e6f24cf7c5d4 - Sigstore transparency entry: 1186409569
- Sigstore integration time:
-
Permalink:
pyyush/agentcontracts@3a9bcf42aaaeef381ab6850870188c2fc7724d9c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pyyush
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3a9bcf42aaaeef381ab6850870188c2fc7724d9c -
Trigger Event:
release
-
Statement type: