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 anchoraiFor framework integrations:
pip install anchorai[langchain] # LangChain
pip install anchorai[crewai] # CrewAI
pip install anchorai[mem0] # Mem0
pip install anchorai[all] # All integrationsQuick 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) # TrueSDK Structure
The SDK has 5 namespaces:
| Namespace | Purpose | Key Methods |
|---|---|---|
anchor.agents | Agent registry and lifecycle | create, get, list, update, delete, suspend, activate |
anchor.config | Agent configuration with versioning | get, update, versions, get_version, rollback |
anchor.data | Governed key-value data storage | write, write_batch, read, read_full, delete, delete_prefix, list, search |
anchor.checkpoints | State snapshots and rollback | create, list, get, restore, delete |
anchor.audit | Hash-chained audit trail | query, get, verify, export |
Framework Integrations
LangChain
from anchor.integrations.langchain import AnchorMemory
memory = AnchorMemory(anchor=anchor, agent_id=agent.id)
# Use with LangChain chains/agentsCrewAI
from anchor.integrations.crewai import AnchorCrewMemory
memory = AnchorCrewMemory(anchor=anchor)
# Use with CrewAI crewsMem0
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 policiesError 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.