Skip to main content

Authentication

The SDK authenticates to the Assistant API using your CustodianAI API key, sent as an X-API-Key header on every request.

export CUSTODIAN_SDK_API_KEY=cai_your_key_here

When this variable is set, all SDK objects pick it up automatically — no code changes needed.

from custodian_labs import create_assistant

# Reads CUSTODIAN_SDK_API_KEY from the environment
app = create_assistant(model="gpt-4o", prompt="You are a helpful assistant.")

Option 2: Constructor argument

Pass api_key directly when constructing any SDK object. This is useful when managing multiple keys in the same process.

from custodian_labs import Assistant

assistant = Assistant(
model="gpt-4o",
system_prompt="You are a helpful assistant.",
api_key="cai_your_key_here",
)

The same parameter is available on create_assistant() and AssistantBuilder.

Custom base URL

If you're running the Assistant API on a custom host, set CUSTODIAN_SDK_BASE_URL or pass base_url:

export CUSTODIAN_SDK_BASE_URL=https://your-assistant-api.example.com/v1
app = create_assistant(
model="gpt-4o",
prompt="You are a helpful assistant.",
base_url="https://your-assistant-api.example.com/v1",
)

Authentication errors

If your key is missing or invalid, the SDK raises AuthenticationError:

from custodian_labs import create_assistant, AuthenticationError

try:
app = create_assistant(model="gpt-4o", prompt="...")
except AuthenticationError as e:
print(f"Check your API key: {e}")

See Errors & Retries for the full exception hierarchy.