Skip to main content

CortexHub Python SDK - Policy-as-Code for AI Agents

Project description

CortexHub Python SDK

Runtime Governance for AI Agents - Policy enforcement, PII/secrets detection, complete audit trails with OpenTelemetry.

Installation

# Core SDK
pip install cortexhub

# With framework support (choose one or more)
pip install cortexhub[langgraph]      # LangGraph
pip install cortexhub[crewai]         # CrewAI
pip install cortexhub[openai-agents]  # OpenAI Agents SDK
pip install cortexhub[claude-agents]  # Claude Agent SDK

# All frameworks (for development)
pip install cortexhub[all]

Quick Start

from cortexhub import init, Framework

# Initialize CortexHub FIRST, before importing your framework
cortex = init(
    agent_id="customer_support_agent",
    framework=Framework.LANGGRAPH,  # or CREWAI, OPENAI_AGENTS, CLAUDE_AGENTS
)

# Now import and use your framework
from langgraph.prebuilt import create_react_agent

# Continue with your LangGraph setup...

Supported Frameworks

Framework Enum Value Install
LangGraph Framework.LANGGRAPH pip install cortexhub[langgraph]
CrewAI Framework.CREWAI pip install cortexhub[crewai]
OpenAI Agents Framework.OPENAI_AGENTS pip install cortexhub[openai-agents]
Claude Agents Framework.CLAUDE_AGENTS pip install cortexhub[claude-agents]

Configuration

# Required: API key
export CORTEXHUB_API_KEY=ch_live_...

Features

  • Policy Enforcement - Cloud configuration, local evaluation
  • PII Detection - 50+ entity types, configurable
  • Secrets Detection - 30+ secret types
  • Configurable Guardrails - Select specific PII/secret types to redact
  • Custom Patterns - Add company-specific regex patterns
  • OpenTelemetry - Industry-standard observability
  • Framework Adapters - Automatic interception for all major frameworks
  • Privacy Mode - Metadata-only by default, safe for production

Privacy Modes

# Production (default) - only metadata sent
cortex = init(agent_id="...", framework=..., privacy=True)
# Sends: tool names, arg schemas, PII types detected
# Never: raw values, prompts, responses

# Development - full data for testing policies  
cortex = init(agent_id="...", framework=..., privacy=False)
# Also sends: raw args, results, prompts (for policy testing)

Policy Enforcement

Policies are created in the CortexHub dashboard from detected risks. The SDK automatically fetches and enforces them:

from cortexhub.errors import PolicyViolationError, ApprovalRequiredError

# Policies are fetched automatically during init()
# If policies exist, enforcement mode is enabled

try:
    agent.run("Process a $10,000 refund")
except PolicyViolationError as e:
    print(f"Blocked by policy: {e.policy_name}")
    print(f"Reason: {e.reasoning}")
except ApprovalRequiredError as e:
    print(f"\n⏸️  APPROVAL REQUIRED")
    print(f"   Approval ID: {e.approval_id}")
    print(f"   Tool: {e.tool_name}")
    print(f"   Reason: {e.reason}")
    print(f"   Expires: {e.expires_at}")
    print(f"\n   Decision endpoint: {e.decision_endpoint}")
    print(f"   Configure a webhook to receive approval.decisioned event")

Guardrail Configuration

Guardrails detect PII and secrets in LLM prompts. Configure in the dashboard:

  1. Select types to redact: Choose specific PII types (email, phone, etc.)
  2. Add custom patterns: Regex for company-specific data (employee IDs, etc.)
  3. Choose action: Redact, block, or monitor only

The SDK applies your configuration automatically:

# With guardrail policy active:
# Input prompt: "Contact john@email.com about employee EMP-123456"
# After redaction: "Contact [REDACTED-EMAIL_ADDRESS] about employee [REDACTED-CUSTOM_EMPLOYEE_ID]"
# Only configured types are redacted

Examples

Examples live in the separate examples repository

git clone git@github.com:cortexhub/examples.git
cd examples/langgraph  # or crewai, openai-agents, claude-agents

Important: Initialization Order

Always initialize CortexHub FIRST, before importing your framework:

# ✅ CORRECT
from cortexhub import init, Framework
cortex = init(agent_id="my_agent", framework=Framework.LANGGRAPH)

from langgraph.prebuilt import create_react_agent  # Import AFTER init

# ❌ WRONG
from langgraph.prebuilt import create_react_agent  # Framework imported first
from cortexhub import init, Framework
cortex = init(...)  # Too late!

This ensures:

  1. CortexHub sets up OpenTelemetry before frameworks that also use it
  2. Framework decorators/classes are properly wrapped

Architecture

Agent Decides → [CortexHub] → Agent Executes
                    │
              ┌─────┴─────┐
              │           │
         Policy      Guardrails
         Engine      (PII/Secrets)
              │           │
              └─────┬─────┘
                    │
              OpenTelemetry
               (to backend)

Development

cd python

# Install with all frameworks
uv sync --all-extras

# Run tests
uv run pytest

# Lint
uv run ruff check .

Links

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

cortexhub-0.1.2.tar.gz (55.7 kB view details)

Uploaded Source

Built Distribution

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

cortexhub-0.1.2-py3-none-any.whl (72.1 kB view details)

Uploaded Python 3

File details

Details for the file cortexhub-0.1.2.tar.gz.

File metadata

  • Download URL: cortexhub-0.1.2.tar.gz
  • Upload date:
  • Size: 55.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for cortexhub-0.1.2.tar.gz
Algorithm Hash digest
SHA256 27ed5b37516b38f695f31a350725c2e9af09da2f2f8d9ad5d935480af6f9cc54
MD5 d1ae5caec4637847b8527177248d0d9f
BLAKE2b-256 92754c08d69362a2a35cfe2a8f12c95e19b6e5ea9da46ea5adf4a46693f4b35b

See more details on using hashes here.

File details

Details for the file cortexhub-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: cortexhub-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 72.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for cortexhub-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6adfafceba88ba98c8c5baa7c115928653a980b2ac0e601f80a5f8bc10ab5268
MD5 9399d4bf0e131dd6767e525d6d1dbdb6
BLAKE2b-256 8608e0dbb6374762e96a45146692d0d51df969d6cf9922f488548a785315ffef

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