Python SDK

The Anchor Python SDK provides a full-featured client for the Anchor API with support for framework integrations, type hints, and comprehensive error handling.

Installation

pip install anchorai

For framework integrations:

pip install anchorai[langchain]  # LangChain
pip install anchorai[crewai]     # CrewAI
pip install anchorai[mem0]       # Mem0
pip install anchorai[all]        # All integrations

Quick Start

from anchor import Anchor

anchor = Anchor(api_key="your-api-key")

# Create an agent
agent = anchor.agents.create("support-bot", metadata={"environment": "production"})

# Configure policies
anchor.config.update(agent.id, {
    "policies": {
        "block_pii": True,
        "block_secrets": True,
        "retention_days": 90
    }
})

# Store data (policy-checked, audit-logged)
result = anchor.data.write(agent.id, "user:123:preference", "dark_mode")
print(result.allowed)  # True

# PII is blocked automatically
result = anchor.data.write(agent.id, "user:123:ssn", "123-45-6789")
print(result.allowed)     # False
print(result.blocked_by)  # "policy:block_pii"

# Verify audit chain integrity
verification = anchor.audit.verify(agent.id)
print(verification.valid)  # True

SDK Structure

The SDK has 5 namespaces:

NamespacePurposeKey Methods
anchor.agentsAgent registry and lifecyclecreate, get, list, update, delete, suspend, activate
anchor.configAgent configuration with versioningget, update, versions, get_version, rollback
anchor.dataGoverned key-value data storagewrite, write_batch, read, read_full, delete, delete_prefix, list, search
anchor.checkpointsState snapshots and rollbackcreate, list, get, restore, delete
anchor.auditHash-chained audit trailquery, get, verify, export

Framework Integrations

LangChain

from anchor.integrations.langchain import AnchorMemory

memory = AnchorMemory(anchor=anchor, agent_id=agent.id)
# Use with LangChain chains/agents

CrewAI

from anchor.integrations.crewai import AnchorCrewMemory

memory = AnchorCrewMemory(anchor=anchor)
# Use with CrewAI crews

Mem0

Anchor wraps Mem0 to add policy enforcement, audit trails, and checkpoints. Mem0 handles semantic memory and retrieval; Anchor adds governance.

from anchor.integrations.mem0 import AnchorMem0
from mem0 import Memory

# Wrap Mem0 with Anchor governance
wrapped = AnchorMem0(anchor=anchor, agent_id=agent.id, mem0_client=Memory())

# Mem0 handles "what does the agent remember?"
# Anchor handles "what is the agent allowed to remember?"
result = wrapped.add("User prefers dark mode", user_id="user_123")
print(result.allowed)  # True or False based on policies

Error Handling

from anchor import (
    AnchorError,
    AuthenticationError,
    NotFoundError,
    ValidationError,
    PolicyViolationError,
    RateLimitError
)

try:
    result = anchor.data.write(agent.id, "key", "value")
except PolicyViolationError as e:
    print(f"Blocked: {e.message}")
except AuthenticationError:
    print("Invalid API key")
except NotFoundError:
    print("Agent not found")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after}s")

Requirements

  • Python 3.8+
  • requests >= 2.28.0

Documentation

For complete API reference, see the API documentation. For guides and examples, see the Guides section.