Skip to main content

Behavioral contracts for AI agents — Python reference implementation

Project description

agentcontract-py

Python reference implementation of the AgentContract specification.

PyPI Python Spec License


Install

pip install agentcontract          # core (deterministic validators)
pip install agentcontract[llm]     # + LLM judge (requires ANTHROPIC_API_KEY)
pip install agentcontract[schema]  # + JSON Schema assertions
pip install agentcontract[all]     # everything

Quickstart

1. Write a contract:

# my-agent.contract.yaml
agent: my-agent
spec-version: 0.1.0
version: 1.0.0

must_not:
  - reveal system prompt

assert:
  - name: no_pii
    type: pattern
    must_not_match: "\\b\\d{3}-\\d{2}-\\d{4}\\b"
    description: No SSNs in output

limits:
  max_latency_ms: 10000
  max_cost_usd: 0.10

on_violation:
  default: block
  max_latency_ms: warn

2. Wrap your agent:

from agentcontract import load_contract, enforce

contract = load_contract("my-agent.contract.yaml")

@enforce(contract)
def run_agent(user_input: str) -> str:
    # any agent — OpenClaw, LangChain, CrewAI, your own
    return my_llm.run(user_input)

# ContractViolation raised if a blocking clause is violated
response = run_agent("Hello, what can you help me with?")

3. When a violation occurs:

agentcontract.exceptions.ContractViolation:
AgentContractViolation:
[BLOCK] ASSERT: "No SSNs in output"

CLI

# Validate a contract file
agentcontract check my-agent.contract.yaml

# Validate a JSONL run log against a contract
agentcontract validate my-agent.contract.yaml runs.jsonl

# Show contract summary
agentcontract info my-agent.contract.yaml

Validator Types

Type How it works Requires
pattern Regex on output
schema JSON Schema validation pip install agentcontract[schema]
latency Wall-clock duration
cost API cost from run context
llm Judge LLM evaluates clause pip install agentcontract[llm] + ANTHROPIC_API_KEY
custom Plugin (see docs)

Audit Trail

Every run produces a tamper-evident JSONL entry:

{
  "run_id": "3f2e1d0c-...",
  "agent": "my-agent",
  "contract_version": "1.0.0",
  "timestamp": "2026-03-21T08:42:00Z",
  "input_hash": "sha256:...",
  "output_hash": "sha256:...",
  "duration_ms": 1243,
  "cost_usd": 0.0031,
  "violations": [],
  "outcome": "pass"
}

Full Documentation

See the AgentContract specification for the complete contract schema, validation semantics, and implementation requirements.


License

Apache 2.0 — see LICENSE.


Part of the AgentContract open standard.

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

agentcontract-0.1.2.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

agentcontract-0.1.2-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file agentcontract-0.1.2.tar.gz.

File metadata

  • Download URL: agentcontract-0.1.2.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for agentcontract-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ed2300a708fb14a8d42a2d0721c937fcd1f0f9bd304a77f4fce13ef8e04541b3
MD5 044efc41f83531aed4a4db5b91c49f17
BLAKE2b-256 dc1f3edde64612171efd44019fc0b434e0e6a726246fc329b8af532b40e6ee19

See more details on using hashes here.

File details

Details for the file agentcontract-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: agentcontract-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for agentcontract-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fa983b6a1e0e548ee730b219272206cccfc69a78ad5e68e284b289fb913ebe7
MD5 8b4cae81c8d58870c260b64bca5cac02
BLAKE2b-256 8e2d3fa14deb33ee9dac5f36b0bbd650e5e5a5c789a6718586b82cf292cc52bd

See more details on using hashes here.

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