Skip to main content

Meta-cognition layer for AI agents - prevents infinite loops, learns from failures, provides self-awareness

Project description

GuardianLayer

PyPI Tests Docs License: MIT Python 3.8+

GuardianLayer is a meta-cognition layer for AI agents that prevents infinite loops, learns from failures, and provides self-awareness capabilities. It acts as a safety shield between your AI agent and external tools, ensuring robust and reliable agent behavior.

๐ŸŒŸ Key Features

๐Ÿ›ก๏ธ 5-Layer Protection System

  • Layer 0: Smart Circuit Breaker - Three-state pattern (CLOSED/OPEN/HALF_OPEN) with automatic recovery and error classification
  • Layer 1: Loop Detection - Hash-based O(1) detection of infinite loops and repetitive behavior
  • Layer 2: Schema Validation - MCP-compatible tool call validation with custom hooks
  • Layer 3: Experience Learning - Multi-tiered storage (Session/Process/Global) that learns from past successes and failures
  • Layer 4: Advice Injection - Context-aware prompt injections to guide AI behavior

๐Ÿš€ Performance

  • O(1) loop detection using SHA-256 hashing instead of string comparison
  • Smart caching for validation results and advice generation
  • Async/await support for high-performance applications
  • Pluggable backends - Bring your own database (PostgreSQL, Redis, etc.)

๐Ÿ“Š Observability

  • Health scoring (0-100) for each tool
  • Detailed metrics for ROI visibility (tokens saved, loops prevented, etc.)
  • Error classification (system vs user vs business errors)
  • Tool reliability tracking based on historical success rates

๐Ÿ“ฆ Installation

Install from PyPI:

pip install GuardianLayer

Or install from source for development:

git clone https://github.com/Mk3Ang8l/GuardianLayer.git
cd GuardianLayer
pip install -e '.[dev]'

๐Ÿš€ Quick Start

Basic Usage

from GuardianLayer import GuardianLayer, AdviceStyle

# Initialize the guardian
guardian = GuardianLayer(
    db_path="experience.db",
    max_repeats=2,
    advice_style=AdviceStyle.EXPERT
)

# Register MCP tools
mcp_tools = [{
    "name": "web_search",
    "description": "Search the web",
    "inputSchema": {
        "type": "object",
        "properties": {
            "query": {"type": "string"}
        },
        "required": ["query"]
    }
}]
guardian.ingest_tools(mcp_tools)

# Check a tool call before executing
tool_call = {"tool": "web_search", "arguments": {"query": "Python tutorials"}}
result = guardian.check(tool_call)

if result["allowed"]:
    # Execute the tool
    response = execute_tool(tool_call)
    # Report success/failure for learning
    guardian.report_result(tool_call, success=True)
else:
    print(f"Blocked: {result['reason']}")
    print(f"Suggestion: {result['suggestion']}")

Async Usage

import asyncio
from GuardianLayer import GuardianLayer

async def main():
    guardian = GuardianLayer()
    
    # Async check
    result = await guardian.check_async(tool_call)
    
    if result["allowed"]:
        response = await execute_tool_async(tool_call)
        await guardian.report_result_async(tool_call, success=True)
    
    await guardian.close_async()

asyncio.run(main())

Prompt Injection for Self-Awareness

# Get awareness context to inject into your LLM prompt
awareness = guardian.get_awareness_context()

prompt = f"""
You are an AI assistant.

{awareness}

User: {user_message}
"""

๐Ÿ—๏ธ Architecture

GuardianLayer uses a layered protection model:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚      AI Agent (LLM)                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚ Proposed Tool Call
              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  GuardianLayer                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚ L0: Circuit Breaker         โ”‚    โ”‚ โ† Health monitoring
โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค    โ”‚
โ”‚  โ”‚ L1: Loop Detection          โ”‚    โ”‚ โ† Hash-based O(1)
โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค    โ”‚
โ”‚  โ”‚ L2: Schema Validation       โ”‚    โ”‚ โ† MCP compatible
โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค    โ”‚
โ”‚  โ”‚ L3: Experience Learning     โ”‚    โ”‚ โ† SQLite/PostgreSQL
โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค    โ”‚
โ”‚  โ”‚ L4: Advice Generation       โ”‚    โ”‚ โ† Prompt injection
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚ โœ… Allowed / โŒ Blocked
              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚      External Tools/APIs            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š Documentation

๐Ÿ”ง Advanced Features

Custom Storage Providers

from GuardianLayer import GuardianLayer
from GuardianLayer.interfaces import StorageProvider

class PostgreSQLStorageProvider(StorageProvider):
    # Implement your custom storage backend
    pass

guardian = GuardianLayer(storage_provider=PostgreSQLStorageProvider())

Custom Advice Resolvers

from GuardianLayer import AdviceContext

def my_custom_advice(context: AdviceContext) -> str:
    if context.failure_count > 5:
        return "๐Ÿšจ CRITICAL: This tool is failing repeatedly!"
    return ""

guardian.set_custom_advice_resolver(my_custom_advice)

Health Monitoring

# Get health metrics for all tools
health = guardian.health_monitor.get_all_health()

for tool_name, status in health.items():
    print(f"{tool_name}: {status['score']}/100 - {status['state']}")

# Manually reset a tool's health
guardian.reset_tool("problematic_tool")

Metrics and ROI

# Get comprehensive metrics
metrics = guardian.get_metrics()

print(f"Loops prevented: {metrics['loops_prevented']}")
print(f"Circuit breaks: {metrics['circuit_breaks']}")
print(f"Estimated tokens saved: {metrics['tokens_saved']}")
print(f"Estimated latency saved: {metrics['latency_saved_ms']}ms")

๐Ÿงช Testing

Run the test suite:

pytest tests/ -v --cov=src

Run the demo examples:

python examples/demo.py
python examples/demo_v2.py
python examples/demo_cache_perf.py

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup

# Clone the repository
git clone https://github.com/Mk3Ang8l/GuardianLayer.git
cd GuardianLayer

# Install development dependencies
pip install -e '.[dev]'

# Run tests
pytest -v

# Run linters
ruff check src/
mypy src/

๐Ÿ“„ License

GuardianLayer is licensed under the MIT License. See LICENSE for details.

๐Ÿ™ Acknowledgments

  • Inspired by the need for safer AI agent systems
  • Built for the MCP (Model Context Protocol) ecosystem
  • Designed to work with any AI framework (LangChain, AutoGen, CrewAI, etc.)

๐Ÿ’– Support the Project

If you find GuardianLayer useful, consider supporting its development:

Your support helps maintain and improve GuardianLayer!

๐Ÿ“ž Support


Made with โค๏ธ for safer AI agents

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

guardianlayer-2.0.5.tar.gz (83.9 kB view details)

Uploaded Source

Built Distribution

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

guardianlayer-2.0.5-py3-none-any.whl (38.9 kB view details)

Uploaded Python 3

File details

Details for the file guardianlayer-2.0.5.tar.gz.

File metadata

  • Download URL: guardianlayer-2.0.5.tar.gz
  • Upload date:
  • Size: 83.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for guardianlayer-2.0.5.tar.gz
Algorithm Hash digest
SHA256 88656ea9b35ab719591f9dd3eb88b766ce119e1249497b7f80ccbde311f5e102
MD5 077fd6b817c25c758228a96ecb6cf789
BLAKE2b-256 5f4f5833585ae4526ddd89ba58ac59bf3635ec2898f972cb8931a179cba34d50

See more details on using hashes here.

File details

Details for the file guardianlayer-2.0.5-py3-none-any.whl.

File metadata

  • Download URL: guardianlayer-2.0.5-py3-none-any.whl
  • Upload date:
  • Size: 38.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for guardianlayer-2.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cbe21804ffb9ab9db033276f94de729cab800f01f108092f33f2162d5ea42057
MD5 6db4abae8ad68a4b13d0be71eac20501
BLAKE2b-256 b7e35d8fa5b83bfb8504aee0553c7b8266d946c65a5f9c4a521ae0896fc01618

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