Python SDK

Integrate Alephant Analytics into your Python applications
View as Markdown

The official Alephant Analytics Python SDK provides convenient access to the Alephant Analytics API from Python scripts or data science notebooks. It supports both synchronous and asynchronous execution, enabling high-performance queries for dashboards, AI cost optimization, and alerting engines.

Installation

Install the SDK via pip:

$pip install alephantai-analytics-api

Initialization

You can initialize either a synchronous or asynchronous client depending on your architecture.

Synchronous Client

1import os
2from alephantai_analytics import AlephantAnalytics, AlephantAnalyticsEnvironment
3
4client = AlephantAnalytics(
5 # Set the target environment
6 environment=AlephantAnalyticsEnvironment.DEFAULT,
7 # Pass your API token
8 token=f"Bearer {os.getenv('ALEPHANT_API_TOKEN')}",
9 # Pass the Workspace ID mapping
10 workspace_id=os.getenv('ALEPHANT_WORKSPACE_ID')
11)

Asynchronous Client

Ideal for FastAPI, async web frameworks, or highly concurrent applications:

1import os
2from alephantai_analytics import AsyncAlephantAnalytics, AlephantAnalyticsEnvironment
3
4async_client = AsyncAlephantAnalytics(
5 environment=AlephantAnalyticsEnvironment.DEFAULT,
6 token=f"Bearer {os.getenv('ALEPHANT_API_TOKEN')}",
7 workspace_id=os.getenv('ALEPHANT_WORKSPACE_ID')
8)

Example Usage

Live 24-Hour Panel Overview

Retrieve a rolling live-24h dashboard style panel of top models and keys.

1def fetch_live_summary():
2 try:
3 response = client.saas.get_saas_live_24h(limit=5)
4 print(f"Top Model: {response.data.top_model}")
5 print(f"Total Requests: {response.data.total_requests}")
6 except Exception as e:
7 print(f"Failed to fetch live summary: {e}")

Retrieving Department Overrides and Analytics

Fetch cost breakdowns for a specific department.

1async def get_department_cost_breakdown(department_id: str):
2 response = await async_client.departments.cost_breakdown(
3 department_id=department_id,
4 preset="30d", # Compare last 30 days
5 limit=50
6 )
7
8 print(f"Current Period Cost: {response.data.summary.cost_sum}")
9 for row in response.data.rows:
10 print(f"Agent: {row.agent_name} | Spend: ${row.spend}")

Error Handling

Any request that results in a 4xx or 5xx status code will raise an exception corresponding to the type of error (ApiError, RateLimitError, etc.). Catch these natively in Python to perform fallback logic or exponential backoffs.