A formal framework for governing autonomous AI agents through explicit resource constraints and temporal boundaries
Project description
Agent Contracts
A formal framework for governing autonomous AI agents through explicit resource constraints and temporal boundaries.
Overview
Agent Contracts transforms autonomous AI agents from unbounded explorers into bounded optimizers by introducing formal contracts that specify:
- ๐ฏ Resource Budgets - Tokens, API calls, compute time, and costs
- โฑ๏ธ Temporal Constraints - Deadlines, duration limits, and lifecycle boundaries
- ๐ Success Criteria - Measurable conditions for contract fulfillment
- ๐ Lifecycle Management - Clear states from activation to termination
The Problem
Current agentic AI systems face critical challenges:
- Unbounded Resource Consumption - Agents can consume unpredictable amounts of tokens, API calls, and compute time
- Unclear Lifecycles - No explicit termination criteria, leading to resource leaks
- Difficult Governance - Hard to audit, ensure compliance, and attribute costs
- Coordination Complexity - Multi-agent systems lack formal resource allocation mechanisms
The Solution
Agent Contracts provide a mathematical framework that enables:
- Predictable Costs - Explicit resource budgets prevent runaway consumption
- Formal Verification - Contract states and constraints are machine-verifiable
- Time-Resource Tradeoffs - Strategic optimization between speed and economy
- Multi-Agent Coordination - Hierarchical contracts and resource markets
Quick Examples
Basic LLM Integration
from agent_contracts import Contract, ContractedLLM, ResourceConstraints, ContractMode
# Define a contract with resource budgets
contract = Contract(
id="research-task",
name="Research Assistant",
mode=ContractMode.BALANCED, # Optimize for quality-cost-time balance
resources=ResourceConstraints(
tokens=10000,
api_calls=50,
cost_usd=1.0
)
)
# Execute LLM calls within contract constraints
with ContractedLLM(contract) as llm:
response = llm.completion(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize recent AI papers"}]
)
# Contract automatically enforces:
# โ
Token budget limits
# โ
API call tracking
# โ
Cost monitoring
# โ
Violations trigger warnings or stops
Per-Tool Resource Limits
Fine-grained control over individual tool usage:
from agent_contracts import Contract, ResourceConstraints
contract = Contract(
id="research-agent",
name="Research Agent",
resources=ResourceConstraints(
tokens=10000,
tool_invocations=20, # Total limit across all tools
per_tool_limits={
"web_search": 5, # Max 5 web searches
"code_exec": 3, # Max 3 code executions
# Other tools limited only by aggregate
}
)
)
Pre-Execution Hooks (Custom Policy)
Add custom governance logic that runs before every constraint check:
from agent_contracts import (
Contract, ContractedLLM, CheckContext, HookResult,
EnforcementAction, ResourceConstraints,
)
# Define a hook that blocks off-topic requests
def topic_guard(ctx: CheckContext) -> HookResult:
messages = ctx.metadata.get("messages", [])
if any("off-topic" in str(m) for m in messages):
return HookResult(
allow=False,
reason="Request outside allowed domain",
action=EnforcementAction.HARD_STOP,
)
return HookResult() # allow by default
contract = Contract(
id="guarded-agent",
resources=ResourceConstraints(tokens=10000, cost_usd=1.0)
)
with ContractedLLM(contract) as llm:
llm.enforcer.add_pre_check_hook(topic_guard)
# Hooks fire automatically on every LLM call
# Works with all integrations: LiteLLM, LangGraph, Google ADK, Claude SDK
LangGraph Multi-Agent Workflows โญ
For complex workflows with cycles and multi-agent coordination:
from langgraph.graph import StateGraph, END
from agent_contracts import Contract, ResourceConstraints
from agent_contracts.integrations.langgraph import ContractedGraph
# Build complex graph with validation cycle
workflow = StateGraph(AgentState)
workflow.add_node("research", research_agent)
workflow.add_node("validate", validate_agent)
workflow.add_conditional_edges(
"validate",
should_retry,
{True: "research", False: END} # Can loop!
)
app = workflow.compile()
# Wrap with contract to prevent runaway loops
contract = Contract(
id="research-workflow",
resources=ResourceConstraints(
tokens=50000,
api_calls=25, # Limit iterations!
cost_usd=2.0
)
)
contracted_workflow = ContractedGraph(contract=contract, graph=app)
result = contracted_workflow.invoke({"query": "Research topic"})
# Budget enforced across ALL nodes and cycles:
# โ
Prevents infinite loops
# โ
Multi-agent budget sharing
# โ
Real-time violation detection
# โ
Cumulative tracking across entire graph
Google ADK Multi-Agent Systems
For Google ADK-based agents and multi-agent hierarchies:
from google.adk.agents import LlmAgent
from agent_contracts import Contract, ResourceConstraints
from agent_contracts.integrations.google_adk import ContractedAdkAgent
# Create multi-agent hierarchy
researcher = LlmAgent(
name="researcher",
model="gemini-2.0-flash",
instruction="You research topics thoroughly."
)
summarizer = LlmAgent(
name="summarizer",
model="gemini-2.0-flash",
instruction="You create concise summaries."
)
coordinator = LlmAgent(
name="coordinator",
model="gemini-2.0-flash",
instruction="You coordinate research and summarization.",
sub_agents=[researcher, summarizer]
)
# Single budget for ENTIRE multi-agent system
contract = Contract(
id="research-system",
resources=ResourceConstraints(
tokens=50000, # For ALL agents combined
api_calls=25,
cost_usd=2.0
)
)
contracted_system = ContractedAdkAgent(contract=contract, agent=coordinator)
result = contracted_system.run(
user_id="user1",
session_id="session1",
message="Research and summarize quantum computing"
)
# Budget enforced across ALL agents in hierarchy:
# โ
Detailed token tracking (prompt/response/thinking/cached)
# โ
Multi-turn conversation protection
# โ
Multi-agent coordination governance
# โ
Tool execution monitoring
Contract Modes
Choose the mode that fits your requirements:
# URGENT mode: Minimize time, accept higher costs
contract = Contract(
mode=ContractMode.URGENT,
resources=ResourceConstraints(tokens=10000)
)
# โ 50% faster execution, 20% more tokens
# BALANCED mode: Optimize quality-cost-time tradeoff
contract = Contract(
mode=ContractMode.BALANCED,
resources=ResourceConstraints(tokens=10000)
)
# โ Standard execution with quality focus
# ECONOMICAL mode: Minimize costs, accept longer runtime
contract = Contract(
mode=ContractMode.ECONOMICAL,
resources=ResourceConstraints(tokens=10000)
)
# โ 60% fewer tokens, 50% longer execution
Documentation
Key Resources
- Whitepaper - Complete theoretical framework with mathematical foundations
- Pre-Execution Hooks - Custom governance hooks and behavioral monitor design
- Examples - Coming soon: Practical implementation examples
Quick Start by Role
- Researchers: Read the Formal Framework and Future Directions
- Engineers: Check Implementation Architecture and Use Cases
- Product Managers: Start with the Introduction and Use Cases
Key Concepts
Contract Definition
An Agent Contract C = (I, O, S, R, T, ฮฆ, ฮจ) includes:
- I: Input specification
- O: Output specification
- S: Skills (tools, capabilities)
- R: Resource constraints
- T: Temporal constraints
- ฮฆ: Success criteria
- ฮจ: Termination conditions
Time-Resource Tradeoff
Agents can optimize along multiple dimensions:
| Mode | Time | Resources | Quality |
|---|---|---|---|
| Urgent | Low โก | High ๐ฐ | 85% |
| Balanced | Medium โฑ๏ธ | Medium ๐ต | 95% |
| Economical | High ๐ข | Low ๐ธ | 90% |
Contract States
DRAFTED โ ACTIVE โ {FULFILLED, VIOLATED, EXPIRED, TERMINATED}
Agent Skills (agentskills.io Standard)
Agent Contracts supports the agentskills.io open standard for defining reusable agent behaviors:
from agent_contracts import SkillSpec, Capabilities, Contract
# Define a rich skill with full instructions
code_review = SkillSpec(
name="code-reviewer",
description="Review code for best practices, security issues, and test coverage.",
instructions="""
## Instructions
1. Read the target files
2. Check for common issues:
- Error handling
- Security vulnerabilities
- Test coverage
3. Provide detailed feedback
""",
allowed_tools=["Read", "Grep", "Glob"],
version="1.0.0",
)
# Use in capabilities (mix strings and SkillSpec)
contract = Contract(
id="review-task",
name="Code Review",
capabilities=Capabilities(
skills=[code_review, "simple-skill"], # Both types work
tools=["web_search"],
),
)
# Access skills programmatically
skill = contract.capabilities.get_skill("code-reviewer")
print(skill.instructions)
Features:
- โ Compatible with Microsoft, OpenAI, Cursor, and other adopters
- โ
SKILL.md import/export (
to_skill_md(),from_skill_md()) - โ Progressive disclosure (metadata vs full instructions)
- โ Backward compatible (string skills still work)
Repository Status
๐ Ready for Release (November 2025)
Current Version: 0.1.0 Status: Production-ready, validated, documented
Phase 1: Core Framework โ Complete
- โ Contract data structures (C = I, O, S, R, T, ฮฆ, ฮจ)
- โ Resource monitoring and enforcement
- โ Token counting and cost tracking
- โ LiteLLM integration wrapper
- โ 145 tests, 96% coverage
- โ Live demo with Gemini 2.0 Flash
Phase 2A: Strategic Optimization โ Complete
- โ Contract modes (URGENT, BALANCED, ECONOMICAL)
- โ Budget-aware prompt generation
- โ Strategic planning utilities
- โ Quality-cost-time Pareto benchmark
- โ 209 core tests passing
Phase 2B: Governance & Benchmarks โ Complete
- โ Multi-step research benchmark (research agent with quality evaluation)
- โ Budget violation policy testing (100% enforcement validation)
- โ Cost governance validation (organizational policy compliance)
- โ Variance reduction analysis (N=20 validation, temperature=0 effect discovered)
- โ Quality metrics framework (3-phase validation study, CV=5.2%)
- โ LangChain 1.0+ integration (governance & compliance)
- โ Pre-commit hooks and code quality infrastructure
LangGraph Integration โ Complete (Premium Feature)
- โ ContractedGraph for complex multi-agent workflows
- โ Cumulative budget tracking across ALL nodes and cycles
- โ Loop/retry protection (prevents runaway costs)
- โ Multi-agent budget sharing
- โ 27 comprehensive tests, 85% coverage
- โ Real-world demos (validation cycles, parallel agents)
Google ADK Integration โ Complete
- โ ContractedAdkAgent for Google ADK agents
- โ Detailed token tracking (prompt, response, thinking, cached)
- โ Multi-turn conversation protection
- โ Multi-agent hierarchy governance
- โ Tool execution monitoring
- โ 11 comprehensive tests, 90% coverage
- โ Real-world demos (multi-turn, multi-agent)
Claude Agent SDK Integration โ Complete
- โ ContractedClaudeAgent with hook-based enforcement
- โ Exact token tracking from AssistantMessage.usage
- โ Per-tool limits and temporal enforcement via PreToolUse hooks
- โ Audit trail via PostToolUse hooks
- โ Full SDK passthrough (tools, MCP, subagents, skills, permissions)
- โ
Dual API: async
aexecute()and syncexecute() - โ 33 comprehensive tests
Pre-Execution Hooks โ Complete
- โ User-defined pre/post-check hooks on ContractEnforcer
- โ
CheckContext,HookResult,CheckHooktypes for custom policy governance - โ Integration metadata pass-through (all 5 integrations)
- โ Hook actions: WARN, THROTTLE (informational) and SOFT_STOP, HARD_STOP (blocking)
- โ Post-check hooks are observational (cannot block)
- โ Backward compatible โ existing code works unchanged
Evaluation Pipelines โ Complete
- โ Research Pipeline: Multi-agent report generation (25 topics)
- โ Code Review Pipeline: CoderโReviewer loop (175 LiveCodeBench problems)
- โ CONTRACTED vs UNCONTRACTED comparison framework
- โ Conservation law enforcement in multi-agent delegation
- โ Iteration limits prevent runaway agent loops
Total: 646+ tests, 81%+ coverage
Use Cases
Agent Contracts are designed for:
- Production AI Systems - Cost control and SLA compliance
- Complex Multi-Agent Workflows โญ - LangGraph loops, retries, validation cycles
- Enterprise Deployments - Governance, audit trails, and compliance
- Claude Agent SDK - Govern Claude agents with per-tool limits and audit trails
- Google ADK Applications - Multi-turn conversations and multi-agent hierarchies
- LangChain Applications - Simple chains with budget enforcement
- Research - Studying optimal agent behavior under constraints
Where Agent Contracts Shines
LangChain (simple chains):
- 3-10 LLM calls per execution
- Budget risk: LOW to MODERATE
- Value: Governance, compliance, multi-call protection
LangGraph (complex workflows) โญ:
- 30+ LLM calls per execution (cycles, retries, parallel agents)
- Budget risk: VERY HIGH (can spiral to $10+ without limits!)
- Value: Loop protection, multi-agent coordination, cumulative tracking
- This is the killer feature for production deployments
Claude Agent SDK (agentic coding & file/web/terminal):
- 10-100+ tool calls per session (Read, Edit, Bash, WebSearch, subagents)
- Budget risk: HIGH (open-ended agents with many tools can spiral)
- Value: Per-tool limits, temporal enforcement, audit trail, hook-based governance
- Ideal for: Claude-powered agents, coding assistants, research agents
Google ADK (multi-turn & multi-agent):
- 10-50+ LLM calls per conversation (turns, agent coordination, tool use)
- Budget risk: HIGH (multi-agent hierarchies can explode costs)
- Value: Multi-turn protection, hierarchical governance, detailed token tracking
- Ideal for: Google Cloud deployments, Gemini-based agents, conversational AI
Project Structure
agent-contracts/
โโโ src/agent_contracts/ # Core package
โ โโโ core/
โ โ โโโ contract.py # Contract data structures
โ โ โโโ monitor.py # Resource monitoring
โ โ โโโ enforcement.py # Constraint enforcement
โ โ โโโ tokens.py # Token counting
โ โ โโโ planning.py # Strategic planning
โ โ โโโ prompts.py # Budget-aware prompts
โ โโโ integrations/
โ โโโ litellm_wrapper.py # LiteLLM integration
โ โโโ langchain.py # LangChain integration
โ โโโ langgraph.py # LangGraph integration โญ
โ โโโ google_adk.py # Google ADK integration
โ โโโ claude_agent_sdk.py # Claude Agent SDK integration
โโโ tests/ # 247+ tests, 94%+ coverage
โ โโโ core/ # Core module tests (209 tests)
โ โโโ integrations/ # Integration tests (38 tests)
โโโ benchmarks/ # Live demonstrations & benchmarks
โ โโโ demo_phase1.py # Phase 1 interactive demo
โ โโโ strategic/ # Strategic optimization benchmarks
โ โโโ research_agent/ # Multi-step research benchmark
โ โโโ governance/ # Policy & governance tests
โ โโโ langchain/ # LangChain demos
โ โโโ langgraph/ # LangGraph demos (multi-agent)
โ โโโ google_adk/ # Google ADK demos (multi-turn, multi-agent)
โโโ evaluation/ # Experimental evaluations
โ โโโ research_pipeline/ # Multi-agent research experiment
โ โโโ code_review_pipeline/ # CoderโReviewer experiment
โโโ docs/
โ โโโ whitepaper.md # Complete theoretical framework
โ โโโ testing-strategy.md # Testing & validation plan
โโโ pyproject.toml # Package configuration
โโโ README.md # This file
Installation
# Install from PyPI
pip install ai-agent-contracts
# Or with uv
uv add ai-agent-contracts
The package is importable as agent_contracts:
from agent_contracts import Contract, ResourceConstraints
For development (from source):
git clone https://github.com/flyersworder/agent-contracts.git
cd agent-contracts
uv sync --dev
Requirements: Python โฅ 3.12
Optional dependencies:
litellm- For LLM integration (automatically installed)langchain- For LangChain integration (uv sync --extra langchain)langgraph- For LangGraph integration โญ (uv sync --extra langgraph)google-adk- For Google ADK integration (uv sync --extra google-adk)claude-agent-sdk- For Claude Agent SDK integration (uv sync --extra claude-agent-sdk)matplotlib- For visualization benchmarks (pip install matplotlib)
Development
Setup
This project uses uv for dependency management. To set up the development environment:
# Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone the repository
git clone https://github.com/flyersworder/agent-contracts.git
cd agent-contracts
# Install dependencies (including dev dependencies)
uv sync --dev
# Install pre-commit hooks
uv run pre-commit install
Code Quality
This project uses several tools to maintain code quality:
- Ruff: Fast Python linter and formatter (replaces black, isort, flake8)
- mypy: Static type checker
- pre-commit: Git hooks for automated checks
Pre-commit hooks will automatically run on every commit. To manually run all checks:
# Run all pre-commit hooks
uv run pre-commit run --all-files
# Run specific tools
uv run ruff check . # Linting
uv run ruff format . # Formatting
uv run mypy . # Type checking
Running Tests
# Run tests (when available)
uv run pytest
# Run with coverage
uv run pytest --cov=agent_contracts --cov-report=html
Project Structure
docs/- Documentation (whitepaper, testing strategy)src/- Source code (planned)tests/- Test suite (planned)pyproject.toml- Project configuration and dependenciesuv.lock- Locked dependencies for reproducibility
Contributing
This is an evolving framework. We welcome contributions in:
- Reference implementations (Python, TypeScript)
- Integration with existing frameworks (LangChain, AutoGPT, etc.)
- Practical examples and tutorials
- Empirical studies and benchmarks
License
This project is licensed under CC BY 4.0.
Authors
Qing Ye (with assistance from Claude, Anthropic)
Citation
If you use this framework in your research, please cite:
@techreport{ye2025agentcontracts,
title={Agent Contracts: A Resource-Bounded Optimization Framework for Autonomous AI Systems},
author={Ye, Qing},
year={2025},
month={October}
}
Learn More
- ๐ Read the Whitepaper
- ๐ฏ Browse Documentation
- ๐ฌ Open an Issue for questions or discussions
Version: 0.3.0 | Last Updated: March 28, 2026 | Status: Production Ready โญ
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
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 ai_agent_contracts-0.3.1.tar.gz.
File metadata
- Download URL: ai_agent_contracts-0.3.1.tar.gz
- Upload date:
- Size: 2.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4efb15fcfd84f425921b0184fba15416bb333536d29d9dfa204e2022e1293ac
|
|
| MD5 |
422da39862e950433e0ce176847250e0
|
|
| BLAKE2b-256 |
cac8fd4b74b482730c4a74f42bcdc7827cfd2ce6456978ec53846a34853bed44
|
Provenance
The following attestation bundles were made for ai_agent_contracts-0.3.1.tar.gz:
Publisher:
ci.yml on flyersworder/agent-contracts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ai_agent_contracts-0.3.1.tar.gz -
Subject digest:
e4efb15fcfd84f425921b0184fba15416bb333536d29d9dfa204e2022e1293ac - Sigstore transparency entry: 1373664788
- Sigstore integration time:
-
Permalink:
flyersworder/agent-contracts@b5880794442b39903e212e8dc9bf7e1970e34b5a -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/flyersworder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@b5880794442b39903e212e8dc9bf7e1970e34b5a -
Trigger Event:
release
-
Statement type:
File details
Details for the file ai_agent_contracts-0.3.1-py3-none-any.whl.
File metadata
- Download URL: ai_agent_contracts-0.3.1-py3-none-any.whl
- Upload date:
- Size: 99.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4aca672989fcbda1a56b1424e1d7008dd0b885f348b44f7a4ef34a94db6515bd
|
|
| MD5 |
3c80086846b8f11ac71fe449507e762c
|
|
| BLAKE2b-256 |
41494a9ce7a56f5ef01745a90451644f4bc4068d17013fe3572c7bb14613fe35
|
Provenance
The following attestation bundles were made for ai_agent_contracts-0.3.1-py3-none-any.whl:
Publisher:
ci.yml on flyersworder/agent-contracts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ai_agent_contracts-0.3.1-py3-none-any.whl -
Subject digest:
4aca672989fcbda1a56b1424e1d7008dd0b885f348b44f7a4ef34a94db6515bd - Sigstore transparency entry: 1373664869
- Sigstore integration time:
-
Permalink:
flyersworder/agent-contracts@b5880794442b39903e212e8dc9bf7e1970e34b5a -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/flyersworder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@b5880794442b39903e212e8dc9bf7e1970e34b5a -
Trigger Event:
release
-
Statement type: