Skip to main content

Official Python SDK for the Claiv Memory API (V2.1 - Production-Grade Memory)

Project description

claiv-memory

Official Python SDK for the Claiv Memory API.

Installation

pip install claiv-memory

Quick Start

from claiv import ClaivClient

client = ClaivClient(api_key="your-api-key")

# Store a memory
result = client.ingest({
    "user_id": "user-123",
    "type": "message",
    "content": "User prefers dark mode and uses VS Code",
})
print(result["event_id"])

# Recall relevant memory
result = client.recall({
    "user_id": "user-123",
    "task": "Help the user configure their editor",
    "token_budget": 2000,
})
for block in result["memory_blocks"]:
    print(f"[{block['type']}] {block['content']}")

# Forget memory for a user
result = client.forget({"user_id": "user-123"})
print(f"Deleted: {result['deleted_counts']}")

Async Support

from claiv import AsyncClaivClient

async with AsyncClaivClient(api_key="your-api-key") as client:
    result = await client.ingest({
        "user_id": "user-123",
        "type": "message",
        "content": "User prefers dark mode",
    })

API Reference

ClaivClient(*, api_key, base_url, timeout, max_retries, http_client)

Parameter Type Default Description
api_key str required API key (sent as Bearer token)
base_url str https://api.claiv.io API base URL
timeout float 30.0 Request timeout in seconds
max_retries int 2 Retries on 429/5xx (0 to disable)
http_client httpx.Client None Custom httpx client

AsyncClaivClient accepts the same parameters (with httpx.AsyncClient).

Core Methods

client.ingest(request) -> IngestResponse

result = client.ingest({
    "user_id": "user-123",         # required
    "type": "message",             # required: "message" | "tool_call" | "app_event"
    "content": "The actual text",  # required
    "thread_id": "thread-456",     # optional
    "metadata": {"source": "chat"},# optional
    "event_time": "2025-01-01T00:00:00Z",  # optional: ISO 8601
    "idempotency_key": "unique-1", # optional: prevents duplicates
})
# result: {"event_id": str, "deduped": bool}

client.recall(request) -> RecallResponse

result = client.recall({
    "user_id": "user-123",                  # required
    "task": "Help configure their editor",  # required
    "token_budget": 2000,                   # required: 200–8000
    "thread_id": "thread-456",              # optional
    "scope": {"project": "claiv"},          # optional
})
# result: {
#   "system_context": str,
#   "memory_blocks": [{"type", "content", "source_ids", "score"}, ...],
#   "citations": [str, ...],
#   "token_estimate": int,
# }

Memory block types: open_loop, fact, claim, episode, chunk.

client.forget(request) -> ForgetResponse

result = client.forget({
    "user_id": "user-123",                     # required
    "thread_id": "thread-456",                 # optional
    "from_time": "2025-01-01T00:00:00Z",       # optional
    "to_time": "2025-06-01T00:00:00Z",         # optional
})
# result: {"receipt_id": str, "deleted_counts": {...}}

Usage Methods

summary = client.get_usage_summary("30d")    # "7d" | "30d" | "month" | "today"
breakdown = client.get_usage_breakdown("today")
limits = client.get_usage_limits()

Health Check

result = client.health_check()  # no auth required
# {"ok": True}

Error Handling

All errors inherit from ClaivError.

from claiv import ClaivApiError, ClaivTimeoutError, ClaivNetworkError

try:
    client.ingest({...})
except ClaivApiError as e:
    print(e.status)      # HTTP status code
    print(e.code)        # "invalid_request" | "unauthorized" | "quota_exceeded" | ...
    print(e.request_id)  # server request ID for support
    print(e.details)     # validation errors, quota info, etc.
except ClaivTimeoutError:
    pass  # request timed out
except ClaivNetworkError:
    pass  # DNS failure, connection refused, etc.

Retries

The SDK automatically retries on 429 (rate limited) and 5xx (server error) responses with exponential backoff and jitter. Client errors (400, 401, 403, 404) are never retried.

# Default: 2 retries (3 total attempts)
client = ClaivClient(api_key="key")

# Disable retries
client = ClaivClient(api_key="key", max_retries=0)

# More retries for critical paths
client = ClaivClient(api_key="key", max_retries=5)

Context Manager

Both clients support context managers for automatic cleanup:

with ClaivClient(api_key="key") as client:
    client.ingest({...})

async with AsyncClaivClient(api_key="key") as client:
    await client.ingest({...})

Type Hints

All request/response types are exported as TypedDicts:

from claiv import (
    IngestRequest, IngestResponse,
    RecallRequest, RecallResponse, ContextPack, MemoryBlock,
    ForgetRequest, ForgetResponse, DeletedCounts,
    UsageSummaryResponse, UsageBreakdownResponse, UsageLimitsResponse,
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claiv_memory-0.2.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

claiv_memory-0.2.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file claiv_memory-0.2.1.tar.gz.

File metadata

  • Download URL: claiv_memory-0.2.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for claiv_memory-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cf37efa266a1ea86d755d4c9c2f06920be38de0eccdccc81fc03e0edf451871b
MD5 5a17f6bab72895d428cfa0ded71d9b1d
BLAKE2b-256 1e7957124ca3f4b918d8cf5654e1d230578f946d9cd4cc6907e8190112a4a84f

See more details on using hashes here.

File details

Details for the file claiv_memory-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: claiv_memory-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for claiv_memory-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e81de60bec79585a61d316126708b00b281312d17bd9f0ca3c3900b2fa0a4ec9
MD5 89494a88e3584ef42794f53f3a986fd1
BLAKE2b-256 fb5c02a145edd137218b02ffd00c3a9429804272fac2994699c36e38e10f6dba

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