AI Agent Cost Governance โ budget enforcement, per-tool cost tracking, multi-tenant workspaces
Project description
๐ก agent-gov
AI Agent Cost Governance Platform
A reverse proxy that tracks, budgets, and controls what your AI agents spend. Like a credit card with limits โ but for your agents.
What Problem Does This Solve?
AI agents call expensive tools (LLMs, browsers, APIs, email services). Without controls:
- A recursive agent burns โน5,000 in one night on GPT-4
- A buggy loop sends 10,000 emails before you notice
- You discover the bill when your credit card statement arrives
agent-gov sits between your agents and their tools. Every call goes through us. We check budgets, track costs, and auto-pause overspending agents.
Quick Start
1. Install & Run
git clone <repo-url>
cd agent-gov
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn pydantic httpx
python app.py
Server starts at http://localhost:8000.
2. Register an Agent
curl -X POST http://localhost:8000/agents/register \
-H "Content-Type: application/json" \
-d '{"name": "My Bot", "daily_budget": 500}'
Response:
{
"api_key": "ag-abc123...",
"name": "My Bot",
"daily_budget": 500,
"message": "Save this API key โ it won't be shown again!"
}
3. Route Agent Calls Through the Proxy
Instead of calling tools directly, your agent calls our proxy:
# BEFORE (no governance):
response = call_openai(prompt)
# AFTER (with agent-gov):
gov_response = requests.post("http://localhost:8000/proxy/call", json={
"agent_key": "ag-abc123...",
"tool_name": "openai-gpt4",
"estimated_cost": 12.50 # โน12.50 for this call
})
if gov_response.status_code == 429:
print("Budget exceeded! Agent auto-paused.")
else:
response = call_openai(prompt) # Proceed with actual call
4. Watch the Dashboard
Open http://localhost:8000/dashboard โ live view of all agents, their spend, and budget status.
5. (Optional) Pre-Register Common Tools
# Register 24 common AI tools with realistic costs (โน)
python seed_tools.py
# Or bash version:
bash seed-tools.sh
# Preview before registering:
python seed_tools.py --list
# Register only tools matching a keyword:
python seed_tools.py --filter deepseek
API Reference
| Method | Endpoint | Description |
|---|---|---|---|
| GET | / | Health check + stats (includes tool count) |
| POST | /agents/register | Create agent, get API key |
| POST | /proxy/call | Proxy a tool call (budget checking + real cost lookup) |
|| POST | /agents/{key}/resume | Resume a paused agent (resets budget) |
|| POST | /agents/{key}/reset | Reset daily budget counters (no unpause) |
|| POST | /workspaces | Create a workspace (returns API key) |
|| GET | /workspaces | List all workspaces |
|| POST | /tools/register | Register a tool with its known cost per call (scoped to workspace) |
| GET | /tools | List all registered tools |
| GET | /analytics/tools | Per-tool spend statistics |
| GET | /dashboard | Live HTML dashboard (agents + per-tool breakdown) |
| GET | /docs | Interactive OpenAPI docs (Swagger) |
POST /agents/register
{
"name": "string (1-100 chars, required)",
"daily_budget": "float (positive, required)"
}
POST /proxy/call
{
"agent_key": "string (required)",
"tool_name": "string (required)",
"estimated_cost": "float (default: 0)"
}
Responses:
200โ Call approved401โ Invalid API key429โ Budget exceeded / agent paused
POST /agents/{api_key}/resume
Resume a paused agent. Resets daily spend to 0.
Architecture
Agent (your code)
โ
โ POST /proxy/call { agent_key, tool_name, estimated_cost }
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ agent-gov โ
โ โ
โ 1. Auth: Is key valid? โ
โ 2. Budget: Will this exceed? โ
โ 3. Log: Track cost + tool โ
โ โ
โ If approved โ return 200 โ
โ If denied โ return 429 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โ Agent calls actual tool
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Actual Tool (OpenAI, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Budget Enforcement Rules
| Condition | Status Code | Behavior |
|---|---|---|
| Valid key, under budget | 200 | Approved |
| Invalid API key | 401 | Rejected |
| Call would exceed budget | 429 | Rejected + auto-pause |
| Agent is paused | 429 | Rejected |
Auto-paused agents must be manually resumed via /agents/{key}/resume.
Technology Stack
| Component | Technology | Why | |
|---|---|---|---|
| API Framework | FastAPI | Fast, async, auto-docs | |
| Validation | Pydantic | Type-safe input validation | |
| Server | Uvicorn | Production ASGI server | |
| Storage | SQLite via aiosqlite | Persistent, zero setup | |
| Templates | Jinja2 | Server-rendered dashboard | |
| Tool Registry | SQLite + UPSERT | Tools have known costs, agents can't lie | |
| Workspaces | SQLite + migrations | Multi-tenancy with workspace isolation | |
| Testing | pytest + httpx | Fast, isolated tests |
Running Tests
cd agent-gov
source venv/bin/activate
pip install pytest httpx
python -m pytest test_app.py -v
45 tests covering:
- Agent registration + validation + workspace scoping
- Proxy approval flow
- Budget enforcement + auto-pause + auto-reset
- Resume functionality
- Reset endpoint (manual, paused agent, not found)
- Invalid key rejection
- Dashboard rendering + per-tool breakdown + reset info + workspace filter
- Health checks with tool count
- Edge cases (zero cost, exact budget, large/small values)
- Tool registry (register, update, list, validation, workspace isolation)
- Real cost lookup (registered tool, fallback to estimate, budget enforcement)
- Per-tool analytics + workspace filter
- Daily budget auto-reset (midnight boundary, no spurious reset)
- Workspace CRUD (create, list, default exists)
- Agent/tool workspace isolation
- Backward compatibility (default workspace)
Roadmap
| Phase | What | Status | |---|---|---|---| | v0.1 | Core proxy + in-memory storage | โ Done | | v0.2 | SQLite persistence | โ Done | | v0.3 | Tool registry + real cost tracking | โ Done | | v0.4 | Daily budget auto-reset | โ Done | | v0.5 | Multi-tenancy (workspaces) | โ Done | | v1.0 | Open-source release | ๐ Next |
License
MIT
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 agent_gov_saas-0.5.0.tar.gz.
File metadata
- Download URL: agent_gov_saas-0.5.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b801cdb4daf1f16f4a52834cff7dfd840b051d777f399cee8bf09bd3e18e7e23
|
|
| MD5 |
21447490c717568abbd2bda66fac1caf
|
|
| BLAKE2b-256 |
7e26efa1af1d610e05a78d3084dca06a87fc578f569772834c287879484cbd84
|
File details
Details for the file agent_gov_saas-0.5.0-py3-none-any.whl.
File metadata
- Download URL: agent_gov_saas-0.5.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb807adc35d41559eec389a1d112f0169ac15039c6a39801fba670e9150aea33
|
|
| MD5 |
c5ce447f0ec10996fcc1137cab9ffe72
|
|
| BLAKE2b-256 |
823611f3da242f0746cc517816ee37a3763231ef3d208c90980cce3a33fc8063
|