Production Infrastructure for AI Agents - Cost controls, observability, and framework-agnostic deployment
Project description
๐ก๏ธ AgentSentry
Production Infrastructure for AI Agents
Prevent runaway costs, gain observability, and deploy AI agents with confidence.
Quick Start โข Documentation โข Contributing โข Examples
๐ฏ The Problem
AI agents are transforming how we build software, but deploying them to production introduces critical operational challenges that traditional MLOps tools don't address:
๐ธ Runaway Costs
- Agents can enter infinite loops, burning through API credits without oversight
- A single bug can cost hundreds or thousands of dollars in a matter of hours
- No standardized way to set budget limits or loop thresholds per session
๐ Lack of Observability
- Black box execution: you don't know what your agent is doing until it's too late
- Token usage tracking is manual and error-prone
- No distributed tracing for multi-agent or multi-step workflows
๐ง Framework Lock-In
- Each agent framework (CrewAI, AutoGen, LangChain) has its own patterns
- Migrating between frameworks requires rewriting infrastructure code
- No common interface for deployment, monitoring, or cost controls
๐จ Production Instability
- Agents fail silently or unpredictably
- No circuit breakers or safety mechanisms
- Limited audit trails for debugging and compliance
โจ The Solution
AgentSentry is an open-source production infrastructure layer that provides:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your AI Agent โ
โ (CrewAI, AutoGen, LangChain, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AgentSentry Layer โ
โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ Token โ โ Observabilityโ โ Framework โ โ
โ โ Guardrails โ โ & Tracing โ โ Adapters โ โ
โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ LLM APIs โ
โ (OpenAI, Anthropic)โ
โโโโโโโโโโโโโโโโโโโโโโโ
๐ก๏ธ Core Features
| Feature | Description |
|---|---|
| Budget Guardrails | Circuit breaker pattern that kills execution when USD budget or loop thresholds are exceeded |
| Token Tracking | Automatic tracking of token usage, costs, and API calls with per-model pricing |
| Framework Adapters | Unified interface for CrewAI, AutoGen, LangChain, and custom frameworks |
| OpenTelemetry Integration | Distributed tracing and metrics for agent execution flows |
| Database Audit Logs | SQLAlchemy-based persistence for session logs, breaches, and metrics |
| CLI Tools | agentsentry stack deploy, agentsentry monitor sessions, and more |
๐ Quick Start
Installation
# Install core package
pip install agentsentry
# Install with framework support
pip install agentsentry[crewai] # For CrewAI
pip install agentsentry[langchain] # For LangChain
pip install agentsentry[autogen] # For AutoGen
3 Commands to Production
1๏ธโฃ Initialize Your Project
agentsentry init --framework crewai
This generates:
agentsentry.yaml- Configuration file.env.template- Environment variables templateexample_agent.py- Sample integration code
2๏ธโฃ Configure Your Limits
Edit agentsentry.yaml:
max_budget_usd: 5.0 # Kill agent if cost exceeds $5
max_loops: 20 # Kill agent after 20 iterations
enable_db_logging: true
database_url: "postgresql://localhost/agentsentry"
3๏ธโฃ Run Your Agent with Protection
from agentsentry import AgentRunner, Config
from agentsentry.adapters.crewai import CrewAIAdapter
# Load configuration
config = Config.from_file('agentsentry.yaml')
# Initialize runner
runner = AgentRunner(config=config)
runner.register_adapter('crewai', CrewAIAdapter)
# Run your agent with cost controls
result = runner.run('crewai', {
'name': 'research-agent',
'task': 'Research AI trends',
'model': 'gpt-4'
})
print(f"Cost: ${result['metrics']['total_cost_usd']:.4f}")
print(f"Tokens: {result['metrics']['total_tokens']}")
That's it! Your agent now has:
- โ Budget enforcement
- โ Token tracking
- โ Loop detection
- โ Execution logs
๐ Architecture
AgentSentry follows a modular, adapter-based architecture designed for extensibility:
agentsentry/
โโโ core/ # Core orchestration engine
โ โโโ runner.py # Main AgentRunner class
โ โโโ config.py # Configuration management
โ โโโ base_adapter.py # Abstract adapter interface
โโโ adapters/ # Framework-specific adapters
โ โโโ crewai.py # CrewAI integration
โ โโโ autogen.py # AutoGen integration
โ โโโ langchain.py # LangChain integration
โโโ middleware/ # Interception & control layer
โ โโโ token_guardrail.py # Budget & loop enforcement
โโโ observability/ # Telemetry & tracking
โ โโโ tracking.py # Execution tracker
โ โโโ telemetry.py # OpenTelemetry provider
โโโ persistence/ # Database layer
โ โโโ database.py # SQLAlchemy logger
โ โโโ models.py # ORM models
โโโ cli/ # Command-line interface
โโโ commands/
โโโ stack.py # Stack deployment
โโโ monitor.py # Monitoring tools
โโโ init.py # Project initialization
๐ How Adapters Work
Each framework adapter implements the BaseAdapter interface:
class BaseAdapter(ABC):
@abstractmethod
def initialize(self) -> None:
"""Initialize framework dependencies"""
@abstractmethod
def execute(self, agent_config: Dict) -> Dict:
"""Execute agent with AgentSentry controls"""
@abstractmethod
def shutdown(self) -> None:
"""Clean resource cleanup"""
This allows AgentSentry to support any agent framework through a plugin system.
๐ฐ Token Guardrail Deep Dive
The TokenGuardrail middleware implements the Circuit Breaker pattern for LLM API calls:
How It Works
- Intercepts API Calls: Hooks into framework API calls to track tokens
- Calculates Costs: Uses per-model pricing to compute USD cost
- Checks Thresholds: Compares against
max_budget_usdandmax_loops - Kills Process: Raises
BudgetExceededErrorwhen limits are exceeded - Logs Events: Persists metrics to database for audit trails
Example: Guardrail in Action
from agentsentry.middleware import TokenGuardrail, BudgetExceededError
guardrail = TokenGuardrail(config={
'max_budget_usd': 2.0,
'max_loops': 10
})
try:
with guardrail.track_execution(session_id='session-123'):
# Your agent code here
for i in range(100): # Will hit loop limit
response = llm.call(prompt)
# Track the call
guardrail.intercept_api_call(
session_id='session-123',
model='gpt-4',
prompt_tokens=100,
completion_tokens=50,
is_loop_iteration=True
)
except BudgetExceededError as e:
print(f"๐จ Circuit breaker triggered: {e.threshold_type}")
print(f"Total cost: ${e.total_cost}")
print(f"Loops executed: {e.loops_executed}")
Supported Pricing Models:
- OpenAI: GPT-4, GPT-4 Turbo, GPT-3.5 Turbo
- Anthropic: Claude 3 Opus, Sonnet, Haiku
- Custom models (configurable in
agentsentry.yaml)
๐ Observability & Monitoring
AgentSentry integrates with OpenTelemetry for distributed tracing and metrics:
CLI Monitoring
# View active sessions
agentsentry monitor sessions
# Real-time cost analytics
agentsentry monitor costs --period 24h
# Follow a specific session
agentsentry monitor sessions --session-id abc123 --follow
Metrics Collected
| Metric | Description |
|---|---|
total_tokens |
Cumulative token usage (prompt + completion) |
total_cost_usd |
Total cost in USD |
loops_executed |
Number of loop iterations |
api_calls |
Total API calls made |
duration_seconds |
Execution time |
Database Schema
AgentSentry uses SQLAlchemy with two core tables:
session_logs: Records for each agent execution
session_id,status,total_tokens,total_cost_usd,loops_executed
threshold_breaches: Audit trail for budget/loop violations
session_id,threshold_type,error_message,breached_at
๐ ๏ธ How to Contribute
AgentSentry is in private beta but actively seeking core contributors for the open-source substrate. We're particularly interested in:
๐ฏ Priority Areas
-
New Framework Adapters: Help us support more agent frameworks
- OpenAI Assistants API
- Semantic Kernel
- LlamaIndex Agents
- Custom agent frameworks
-
Observability Enhancements: Expand telemetry capabilities
- Prometheus metrics exporter
- Jaeger tracing integration
- Custom dashboard templates
-
Cost Optimization: Advanced budget management
- Multi-tenant cost allocation
- Budget forecasting
- Cost anomaly detection
-
Production Hardening: Enterprise features
- Kubernetes operator
- High-availability deployments
- Multi-region support
๐ค Contributing Guide
Step 1: Set Up Development Environment
# Clone the repository
git clone https://github.com/nitindme/agentsentry.git
cd agentsentry
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
Step 2: Build a New Adapter
The easiest way to contribute is by adding support for a new framework:
# agentsentry/adapters/your_framework.py
from agentsentry.core.base_adapter import BaseAdapter
from agentsentry.middleware.token_guardrail import TokenGuardrail
class YourFrameworkAdapter(BaseAdapter):
def __init__(self, config):
super().__init__(framework_name="your_framework", config=config)
self.guardrail = TokenGuardrail(config=self.config)
def initialize(self):
# Initialize your framework
pass
def execute(self, agent_config):
with self.guardrail.track_execution(agent_config['session_id']):
# Execute agent
# Track API calls using self.guardrail.intercept_api_call()
return {"status": "success"}
def shutdown(self):
# Cleanup
pass
Step 3: Add Tests and Documentation
# Add tests
tests/adapters/test_your_framework.py
# Add example
examples/your_framework_example.py
# Update docs
docs/adapters/your_framework.md
Step 4: Submit Pull Request
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-adapter) - Commit your changes (
git commit -am 'Add YourFramework adapter') - Push to the branch (
git push origin feature/your-adapter) - Open a Pull Request with a clear description
๐ Code Standards
- Style: We use
blackfor formatting andrufffor linting - Types: Type hints are required for public APIs
- Tests: Maintain >80% code coverage
- Docs: Docstrings for all public classes and methods
๐ Examples
See the examples/ directory for complete working examples:
crewai_example.py: Integrating with CrewAI agentsguardrail_example.py: Using the guardrail middleware directlycustom_adapter_example.py: Building a custom adapter
๐บ๏ธ Roadmap
Q1 2026
- โ Core infrastructure (runner, guardrails, adapters)
- โ CrewAI, AutoGen, LangChain adapters
- โ OpenTelemetry integration
- โ CLI tools
- ๐ Private beta launch
Q2 2026
- ๐ฒ Public open-source release
- ๐ฒ Kubernetes operator
- ๐ฒ Web dashboard for monitoring
- ๐ฒ Multi-tenant cost allocation
Q3 2026
- ๐ฒ Enterprise features (RBAC, SSO)
- ๐ฒ Advanced cost forecasting
- ๐ฒ Mainframe modernization tools (agent-based COBOL translation)
๐ FAQ
Q: Is AgentSentry a framework for building agents?
A: No. AgentSentry is infrastructure that wraps around your existing agent frameworks to add production features like cost controls and observability.
Q: Does it work with my existing agents?
A: Yes! AgentSentry is designed to integrate with minimal code changes. If we don't support your framework yet, you can build a custom adapter.
Q: How much overhead does it add?
A: Minimal. Token tracking adds <10ms per API call. The guardrail middleware is designed for production workloads.
Q: Can I use it without a database?
A: Yes. Set enable_db_logging: false in your config. Metrics will still be tracked in memory.
Q: Is it production-ready?
A: We're currently in private beta. The core infrastructure is stable, but we recommend thorough testing before production deployment.
๐ License
AgentSentry is released under the MIT License. See LICENSE for details.
๐ Community & Support
- GitHub Issues: Report bugs or request features
- Discussions: Ask questions and share use cases
- Discord: Join our community server (link coming soon)
- Email: contact@agentsentry.dev
โญ If AgentSentry solves a problem for you, please star the repo to help others discover it!
Made with โค๏ธ by the AgentSentry community
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 agentsentry-0.1.0.tar.gz.
File metadata
- Download URL: agentsentry-0.1.0.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d201372859ffd9bd695778acdbd92be7e42ff8ca1e77e0e3bc8013e36e41ee67
|
|
| MD5 |
7019667f444616a6fafafe13f73a20ab
|
|
| BLAKE2b-256 |
982c2303377b98af25cff38cfed5c3120f7f866764564578a795c5b37985d4f5
|
File details
Details for the file agentsentry-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agentsentry-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d563b8464003438a3b135f2df0a2bb3022e516e32950fc48b9c7fd3edd6ec628
|
|
| MD5 |
69b0c7f223f3da77b3cb52f249e0c4bf
|
|
| BLAKE2b-256 |
f22e84935394abd9089748f32988ffd3d2a0deb36bf9bb6032ad9feb1ee4223b
|