Error Handling

Handle SDK errors gracefully with typed exception classes.

Error Classes

Both SDKs provide typed error classes for common failure scenarios:

from elephantasm import inject
from elephantasm.exceptions import (
  AuthenticationError,  # 401 - Invalid API key
  NotFoundError,        # 404 - Anima not found
  RateLimitError,       # 429 - Too many requests
  ValidationError,      # 422 - Invalid parameters
  ServerError,          # 5xx - Server error
)

try:
  pack = inject()
except AuthenticationError:
  print("Check your ELEPHANTASM_API_KEY")
except NotFoundError:
  print("Anima not found - check ELEPHANTASM_ANIMA_ID")
except RateLimitError:
  print("Rate limited - retry later")

Error Reference

NameTypeRequiredDescription
AuthenticationError401optionalInvalid or missing API key. Check ELEPHANTASM_API_KEY.
NotFoundError404optionalAnima not found. Check ELEPHANTASM_ANIMA_ID or the anima_id parameter.
ValidationError422optionalInvalid parameters (wrong types, missing required fields).
RateLimitError429optionalToo many requests. Back off and retry with exponential delay.
ServerError5xxoptionalServer-side error. Retry after a brief delay.

Null Pack Handling

inject() returns null (TypeScript) or None (Python) when no memory packs exist — for example, for a new anima with no events yet. Always check before accessing pack methods:

pack = inject()
if pack:
  prompt = pack.as_prompt()
else:
  prompt = ""  # No memories yet

A null pack is normal for new animas. Your agent should work without memory context and build it over time.

Retry Strategy

For production applications, wrap SDK calls with retry logic:

import time
from elephantasm import inject
from elephantasm.exceptions import RateLimitError, ServerError

def inject_with_retry(max_retries=3):
  for attempt in range(max_retries):
      try:
          return inject()
      except RateLimitError:
          time.sleep(2 ** attempt)  # Exponential backoff
      except ServerError:
          time.sleep(1)
  return None