Skip to main content

elephantasm

PyPI version Python 3.10+ License

Long-Term Agentic Memory SDK for Python.

Give your AI agents persistent, evolving memory across sessions.

Installation

pip install elephantasm

Quick Start

from elephantasm import inject, extract, EventType

# Get memory context for LLM injection
pack = inject()
system_prompt = f"You are a helpful assistant.\n\n{pack.as_prompt()}"

# Capture events during conversation
extract(EventType.MESSAGE_IN, "Hello!", role="user")
extract(EventType.MESSAGE_OUT, "Hi there!", role="assistant")

Configuration

Set these environment variables:

Variable Required Default Description
ELEPHANTASM_API_KEY Yes - Your API key (starts with sk_live_)
ELEPHANTASM_ANIMA_ID No - Default anima ID for operations
ELEPHANTASM_ENDPOINT No https://api.elephantasm.com API endpoint URL
ELEPHANTASM_TIMEOUT No 30 Request timeout in seconds

Usage

Module-Level Functions

The simplest way to use Elephantasm:

from elephantasm import inject, extract, create_anima, EventType

# Create an anima (agent entity)
anima = create_anima("my-assistant", description="Personal helper")

# Set ELEPHANTASM_ANIMA_ID or pass anima_id explicitly
pack = inject(anima_id=str(anima.id))

# Capture events
extract(EventType.MESSAGE_IN, "User message", anima_id=str(anima.id))
extract(EventType.MESSAGE_OUT, "Assistant response", anima_id=str(anima.id))

Explicit Client

For more control, use the Elephantasm class:

from elephantasm import Elephantasm, EventType

# Initialize with credentials
client = Elephantasm(
    api_key="sk_live_...",
    anima_id="your-anima-id",
)

# Or use as context manager
with Elephantasm(api_key="sk_live_...") as client:
    pack = client.inject()
    client.extract(EventType.MESSAGE_IN, "Hello!")

Memory Pack

The MemoryPack object contains assembled context for LLM injection:

pack = inject()

# Get formatted prompt string
prompt = pack.as_prompt()

# Access structured data
print(f"Session memories: {pack.session_memory_count}")
print(f"Knowledge items: {pack.knowledge_count}")
print(f"Token count: {pack.token_count}/{pack.max_tokens}")

# Access identity context
if pack.identity:
    print(f"Personality: {pack.identity.personality_type}")

# Access individual memories
for mem in pack.session_memories:
    print(f"- {mem.summary} (score: {mem.score:.2f})")

Event Types

from elephantasm import EventType

EventType.MESSAGE_IN   # User messages
EventType.MESSAGE_OUT  # Assistant responses
EventType.TOOL_CALL    # Tool invocations
EventType.TOOL_RESULT  # Tool outputs
EventType.SYSTEM       # System events

Error Handling

from elephantasm import Elephantasm
from elephantasm.exceptions import (
    AuthenticationError,
    NotFoundError,
    RateLimitError,
    ValidationError,
    ServerError,
)

try:
    pack = inject()
except AuthenticationError:
    print("Invalid API key")
except NotFoundError:
    print("Anima not found")
except RateLimitError:
    print("Rate limit exceeded, retry later")
except ValidationError as e:
    print(f"Invalid request: {e}")
except ServerError:
    print("Server error, retry later")

Examples

See the examples/ directory for complete examples:

API Reference

Elephantasm Class

class Elephantasm:
    def __init__(
        self,
        api_key: str | None = None,      # Falls back to ELEPHANTASM_API_KEY
        anima_id: str | None = None,      # Falls back to ELEPHANTASM_ANIMA_ID
        endpoint: str | None = None,      # Falls back to ELEPHANTASM_ENDPOINT
        timeout: int | None = None,       # Falls back to ELEPHANTASM_TIMEOUT
    ): ...

    def inject(
        self,
        anima_id: str | None = None,      # Override default anima
        query: str | None = None,         # Semantic search query
        preset: str | None = None,        # "conversational" or "self_determined"
    ) -> MemoryPack: ...

    def extract(
        self,
        event_type: str | EventType,      # Event type
        content: str,                      # Event content
        anima_id: str | None = None,      # Override default anima
        session_id: str | None = None,    # Group related events
        role: str | None = None,          # user, assistant, system, tool
        author: str | None = None,        # Username, model name, etc.
        occurred_at: datetime | None = None,
        meta: dict | None = None,
        importance_score: float | None = None,
    ) -> Event: ...

    def create_anima(
        self,
        name: str,
        description: str | None = None,
        meta: dict | None = None,
    ) -> Anima: ...

    def close(self) -> None: ...

Module Functions

def inject(anima_id=None, query=None, preset=None) -> MemoryPack: ...
def extract(event_type, content, anima_id=None, **kwargs) -> Event: ...
def create_anima(name, description=None, meta=None) -> Anima: ...

Development

# Clone and install
git clone https://github.com/hejijunhao/elephantasm-py.git
cd elephantasm-py
pip install -e ".[dev]"

# Run tests
pytest -v

# Lint
ruff check elephantasm tests

License

Apache 2.0

Metadata

Release files for elephantasm 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elephantasm 0.2.0
File Size Uploaded Provenance
elephantasm-0.2.0.tar.gz 19.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for elephantasm 0.2.0
File Interpreter ABI Platform
elephantasm-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 34.4 kB

Release files / elephantasm-0.2.0.tar.gz

Download URL elephantasm-0.2.0.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7c8da17704651311cfde454743fafe7ff53021e95c861c2d3ff151b7a87f4ef2
BLAKE2b-256 checksum
How to use checksums
ad99e4c9840f3089cb9430e67b04c89c779c68553733bf69c59b72a9430a06a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / elephantasm-0.2.0-py3-none-any.whl

Download URL elephantasm-0.2.0-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7d7f98b6cccfa02c9fa7d8b32ffff5da26c6c04d11892f3e07083cc0f35881f
BLAKE2b-256 checksum
How to use checksums
261acc8d69f37bf5e690f6e269e4d77d8a11ba040fb06e8d3d515d7b288a2a18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Security for elephantasm 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page