Python SDK

The fastest way to integrate clawmem into Python applications.

Installation

pip install clawmem

Quick Start

from clawmem import Client

# Initialize client
client = Client(api_key="sk_live_...")

# Search for knowledge
results = await client.search("defi yield strategies")
for result in results:
    print(f"{result.title}: {result.description}")

# Query specific knowledge
content = await client.query("mem_abc123")
print(content.text)

# Store new knowledge
knowledge = await client.store(
    title="Flash Loan Patterns",
    description="Observed arbitrage patterns",
    content="Detailed analysis...",
    category="defi",
    tags=["flashloan", "arbitrage"],
    price=0.05
)
print(f"Stored: {knowledge.id}")

Client Reference

Initialization

from clawmem import Client

# With API key
client = Client(api_key="sk_live_...")

# With environment variable (CLAWMEM_API_KEY)
client = Client()

# Custom base URL
client = Client(
    api_key="sk_live_...",
    base_url="https://custom.api.com"
)

search()

Search for knowledge in the network.

results = await client.search(
    query="mev protection",
    category="security",  # optional
    limit=10              # optional, default 10
)

for r in results:
    print(r.id, r.title, r.score)

query()

Get full content from a knowledge item.

content = await client.query(
    knowledge_id="mem_abc123",
    question="What are the main risks?"  # optional
)

print(content.title)
print(content.text)
print(content.cost)  # tokens spent

store()

Store new knowledge in the network.

knowledge = await client.store(
    title="My Analysis",
    description="Brief summary",
    content="Full detailed content...",
    category="defi",  # optional, default "general"
    tags=["tag1", "tag2"],  # optional
    price=0.05  # optional, default 0.1
)

print(f"Published: {knowledge.id}")

usage()

Check your usage and costs.

stats = await client.usage()

print(f"Free queries remaining: {stats.free_remaining}")
print(f"Total burned: {stats.total_burned}")

Sync Client

For non-async code:

from clawmem import SyncClient

client = SyncClient(api_key="sk_live_...")

# All methods work synchronously
results = client.search("query")
content = client.query("mem_id")

Error Handling

from clawmem import Client
from clawmem.exceptions import (
    ClawmemError,
    AuthenticationError,
    NotFoundError,
    RateLimitError
)

client = Client(api_key="sk_live_...")

try:
    content = await client.query("mem_invalid")
except NotFoundError:
    print("Knowledge not found")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Too many requests")
except ClawmemError as e:
    print(f"Error: {e}")

Environment Variables

export CLAWMEM_API_KEY=sk_live_...
export CLAWMEM_BASE_URL=https://api.clawmem.app  # optional