Skip to main content

Python SDK: Spaces & Clusters

Organize memories into spaces and clusters with the Python SDK.

Overview

Spaces and clusters help you organize memories into logical groups. Spaces provide isolated knowledge bases, while clusters group related memories within a space.

Use spaces to separate different projects, users, or contexts. Use clusters to organize memories by topic or category within a space.

Spaces have human-readable slugs (e.g., project-alpha) that you can use instead of IDs. Slugs are auto-generated from names or can be customized.

Spaces

Spaces provide isolated knowledge bases. Use them to separate different projects, users, or contexts.

Create Space

create_space.py
space = client.spaces.create(
    name="Project Alpha",
    slug="alpha",  # Optional - auto-generated if not provided
    description="Knowledge base for Project Alpha"
)

print(f"Space ID: {space.id}")
print(f"Slug: {space.slug}")  # alpha

List Spaces

list_spaces.py
# List all spaces (returns SpaceList paginated wrapper)
result = client.spaces.list()
for space in result.data:
    print(f"{space.name} ({space.slug})")

Get Space

get_space.py
# Get by slug (recommended - human-friendly)
space = client.spaces.get("alpha")

# Or by slug method
space = client.spaces.get_by_slug("alpha")

Update Space

update_space.py
client.spaces.update(
    "alpha",
    name="Project Alpha v2",
    slug="alpha-v2",  # Old slug will redirect to this
    description="Updated knowledge base"
)

Delete Space

delete_space.py
# Delete space
client.spaces.delete("space_old")

Deleting a space will also delete all memories within that space. This action cannot be undone.

Working with Space Memories

space_memories.py
# Create memory in a specific space
memory = client.memories.create(
    content="Project Alpha documentation",
    space_id="space_alpha",
    tags=["docs"]
)

# Search within a space
results = client.memories.list(
    q="documentation",
    space_id="space_alpha",
    limit=10
)

# Iterate through all memories in a space
for memory in client.memories.iter(space_id="space_alpha"):
    print(memory.content)

Clusters

Clusters group related memories together. Use them to organize knowledge into logical collections.

Create Cluster

create_cluster.py
cluster = client.clusters.create(
    name="Python Best Practices",
    description="Collection of Python patterns and tips",
    color="#3776AB",                      # Python blue
    metadata={
        "category": "programming",
        "priority": "high"
    }
)

Manage Cluster Memories

manage_cluster.py
# Add memory to cluster
client.clusters.add_memory(
    cluster_id="cluster_abc",
    memory_id="mem_new",
    confidence=0.95                       # How confident the match is
)

# Remove memory from cluster
client.clusters.remove_memory("cluster_abc", "mem_old")

# Expand cluster with similar memories
expansion = client.clusters.expand(
    cluster_id="cluster_abc",
    limit=10,
    threshold=0.7                         # Minimum similarity
)

print(f"Found {len(expansion.memories)} similar memories")

List Clusters

list_clusters.py
# Paginated list
result = client.clusters.list(limit=20)
for cluster in result.data:
    print(cluster.name)

# Iterate through all clusters
for cluster in client.clusters.iter(page_size=50):
    print(f"{cluster.name}: {cluster.memory_count} memories")

Get Cluster

get_cluster.py
# Get cluster with details
cluster = client.clusters.get("cluster_abc")

print(f"Name: {cluster.name}")
print(f"Description: {cluster.description}")
print(f"Memory count: {cluster.memory_count}")
print(f"Color: {cluster.color}")

Update Cluster

update_cluster.py
# Update cluster
client.clusters.update(
    "cluster_abc",
    name="Python Best Practices 2024",
    description="Updated collection",
    color="#4B8BBE"
)

Delete Cluster

delete_cluster.py
# Delete cluster (memories are not deleted)
client.clusters.delete("cluster_old")

Deleting a cluster does not delete the memories within it. The memories will remain in the system but will no longer be associated with the cluster.

Bulk Cluster Operations

Bulk Create

bulk_create_clusters.py
from trix import ClusterCreate

# Bulk create
clusters = client.clusters.bulk_create([
    ClusterCreate(name="Frontend", color="#61DAFB"),
    ClusterCreate(name="Backend", color="#68A063"),
    ClusterCreate(name="DevOps", color="#2496ED")
])

print(f"Created {len(clusters)} clusters")

Bulk Update

bulk_update_clusters.py
# Bulk update
client.clusters.bulk_update([
    {"id": "cluster_1", "description": "Updated"},
    {"id": "cluster_2", "color": "#FF0000"}
])

Bulk Delete

bulk_delete_clusters.py
# Bulk delete
client.clusters.bulk_delete(["cluster_old1", "cluster_old2"])

Webhooks

Webhooks notify your application when events occur in Trix.

Create Webhook

create_webhook.py
from trix import WebhookEvent

webhook = client.webhooks.create(
    name="Memory Updates",
    url="https://api.yourapp.com/trix-webhook",
    events=[
        WebhookEvent.MEMORY_CREATED,
        WebhookEvent.MEMORY_UPDATED,
        WebhookEvent.MEMORY_DELETED,
        WebhookEvent.RELATIONSHIP_CREATED,
        WebhookEvent.CLUSTER_CREATED
    ],
    headers={
        "X-Custom-Header": "value",
        "Authorization": "Bearer your_secret"
    },
    space_id="space_prod"                 # Optional: filter by space
)

Available Events

  • MEMORY_CREATED, MEMORY_UPDATED, MEMORY_DELETED
  • RELATIONSHIP_CREATED
  • CLUSTER_CREATED

Test and Monitor

test_webhook.py
# Test webhook delivery
result = client.webhooks.test(
    "whk_abc",
    event_type=WebhookEvent.MEMORY_CREATED
)
print(f"Test successful: {result.success}")

# Get delivery history
deliveries = client.webhooks.get_deliveries(
    "whk_abc",
    limit=10,
    status="failed"
)

for delivery in deliveries.data:
    print(f"{delivery.event_type}: {delivery.status_code}")
    if not delivery.success:
        print(f"  Error: {delivery.error}")

# Retry failed delivery
client.webhooks.retry_delivery("whk_abc", "del_xyz")

Manage Webhooks

manage_webhooks.py
# List webhooks
webhooks = client.webhooks.list()
for webhook in webhooks:
    print(f"{webhook.name}: {webhook.url}")

# Update webhook
client.webhooks.update(
    "whk_abc",
    events=[WebhookEvent.MEMORY_CREATED],
    active=False
)

# Delete webhook
client.webhooks.delete("whk_abc")

Complete Example

Here's an example showing how to organize a knowledge base:

organization_example.py
from trix import Trix, ClusterCreate

with Trix.from_env() as client:
    # Create a space for the project
    space = client.spaces.create(
        name="Documentation Project",
        description="Technical documentation knowledge base"
    )

    # Create clusters for organization
    clusters = client.clusters.bulk_create([
        ClusterCreate(name="API Reference", color="#4CAF50"),
        ClusterCreate(name="Tutorials", color="#2196F3"),
        ClusterCreate(name="Best Practices", color="#FF9800")
    ])

    # Create memories in the space
    api_memory = client.memories.create(
        content="The /memories endpoint supports CRUD operations",
        space_id=space.id,
        tags=["api", "rest"]
    )

    tutorial_memory = client.memories.create(
        content="Getting started with the Python SDK",
        space_id=space.id,
        tags=["tutorial", "python"]
    )

    # Add memories to clusters
    client.clusters.add_memory(
        cluster_id=clusters[0].id,  # API Reference
        memory_id=api_memory.id
    )

    client.clusters.add_memory(
        cluster_id=clusters[1].id,  # Tutorials
        memory_id=tutorial_memory.id
    )

    # Search within the space
    results = client.memories.list(
        q="Python SDK",
        space_id=space.id,
        limit=10
    )

    print(f"Found {len(results.data)} memories")

    # List all clusters
    for cluster in client.clusters.iter():
        print(f"{cluster.name}: {cluster.memory_count} memories")