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.4.tar.gz (82.3 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.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: guardianlayer-2.0.4.tar.gz
  • Upload date:
  • Size: 82.3 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.4.tar.gz
Algorithm Hash digest
SHA256 4b3d33933a9027e24795c42192a4b45261d7fd1362cfad8a039c45e1014f9d90
MD5 212cb7cdd10d32a21e0d9a4ebdf2fc4b
BLAKE2b-256 5c1b184686919ade912ccdcba03806ebb076a564e5c8abbdec81924908aaf587

See more details on using hashes here.

File details

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

File metadata

  • Download URL: guardianlayer-2.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6e2027df50c8377d546b9d31d476d5b0b2ff8703160d453151f8c166f933504a
MD5 2aaf5668ceccfbd6c0170b7a4502a3a7
BLAKE2b-256 ac4a7990d5af2db0c9bac7b165f4ae2880074a80f1fb336cdac32ba01c65d31b

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