Skip to main content

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.

Metadata

Release files for agentcontract 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentcontract 0.2.0
File Size Uploaded
agentcontract-0.2.0.tar.gz 22.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agentcontract 0.2.0
File Interpreter ABI Platform
agentcontract-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 45.7 kB

Release files / agentcontract-0.2.0.tar.gz

Download URL agentcontract-0.2.0.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
12c461e602597a1176d62470bf170f1911ca2b2af9a716515381bd03b181a27f
BLAKE2b-256 checksum
How to use checksums
ff0883bbf681975b6f6416903d14d6fdee60d84e5dbad13af865eb0c8464cd3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / agentcontract-0.2.0-py3-none-any.whl

Download URL agentcontract-0.2.0-py3-none-any.whl
Size 23.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f828a19196db808ff56bb42d10512db675ae184855e935dce446b1f28b917e97
BLAKE2b-256 checksum
How to use checksums
933a75f4e5bf3dfae910a5e2a1e70cd8776d9695fe09d3b43355a5a83cece503
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page