Skip to main content

Production Infrastructure for AI Agents - Cost controls, observability, and framework-agnostic deployment

Project description

๐Ÿ›ก๏ธ AgentSentry

Production Infrastructure for AI Agents

License: MIT Python 3.9+ Code style: black

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 template
  • example_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

  1. Intercepts API Calls: Hooks into framework API calls to track tokens
  2. Calculates Costs: Uses per-model pricing to compute USD cost
  3. Checks Thresholds: Compares against max_budget_usd and max_loops
  4. Kills Process: Raises BudgetExceededError when limits are exceeded
  5. 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

  1. New Framework Adapters: Help us support more agent frameworks

    • OpenAI Assistants API
    • Semantic Kernel
    • LlamaIndex Agents
    • Custom agent frameworks
  2. Observability Enhancements: Expand telemetry capabilities

    • Prometheus metrics exporter
    • Jaeger tracing integration
    • Custom dashboard templates
  3. Cost Optimization: Advanced budget management

    • Multi-tenant cost allocation
    • Budget forecasting
    • Cost anomaly detection
  4. 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 black for formatting and ruff for 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 agents
  • guardrail_example.py: Using the guardrail middleware directly
  • custom_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

agentsentry-0.1.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

agentsentry-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

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

Hashes for agentsentry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d201372859ffd9bd695778acdbd92be7e42ff8ca1e77e0e3bc8013e36e41ee67
MD5 7019667f444616a6fafafe13f73a20ab
BLAKE2b-256 982c2303377b98af25cff38cfed5c3120f7f866764564578a795c5b37985d4f5

See more details on using hashes here.

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

Hashes for agentsentry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d563b8464003438a3b135f2df0a2bb3022e516e32950fc48b9c7fd3edd6ec628
MD5 69b0c7f223f3da77b3cb52f249e0c4bf
BLAKE2b-256 f22e84935394abd9089748f32988ffd3d2a0deb36bf9bb6032ad9feb1ee4223b

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