Skip to main content

YAML spec + validation SDK for production agent reliability — cost control, tool-use security, and audit trails.

Project description

Agent Contracts

PyPI CI

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

  1. Spec + SDK, not protocol or platform — the OpenAPI model
  2. YAML primary — JSON Schema validation, CEL-like inline expressions
  3. Graduated tiers — Tier 0 is 4 fields, not 40
  4. Effects: authorized vs. declared — intersection for delegation, union for audit
  5. Enforcement at SDK layer — never in prompts (prompt injection can't bypass)
  6. MCP extension, not forkx-agent-contract on 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aicontracts-0.1.1.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aicontracts-0.1.1-py3-none-any.whl (45.3 kB view details)

Uploaded Python 3

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

Hashes for aicontracts-0.1.1.tar.gz
Algorithm Hash digest
SHA256 df3f93861f99713239fd72d5bc9b5ee8d257a74b485b8313b08c03a86e0c40e9
MD5 7f2c0d4c58f2fc75ad8bda5d9ab080e4
BLAKE2b-256 152dd34dfc077f9723fcc67c5d3cb9ada595f694704fc2798665ff894cbb3308

See more details on using hashes here.

Provenance

The following attestation bundles were made for aicontracts-0.1.1.tar.gz:

Publisher: publish.yml on pyyush/agentcontracts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for aicontracts-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d034658133c3cbd9efc6819f77158e168d85f7e52dfa8543d8e2e6f24cf7c5d4
MD5 a3fc76a97b3d32ee306fc34eae5d02b4
BLAKE2b-256 66428f35d621716378bb8b822cc9cbb98a66ba838baa1b4cb3d46f9d7ebee031

See more details on using hashes here.

Provenance

The following attestation bundles were made for aicontracts-0.1.1-py3-none-any.whl:

Publisher: publish.yml on pyyush/agentcontracts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page