Error handling
All SDK methods raise AICPError on non-2xx responses or network failures.
AICPError
from aicp import AICPClient
from aicp.errors import AICPError
client = AICPClient(api_key="aicp-...", base_url="https://your-gateway")
try:
response = client.chat.complete(
model="auto",
messages=[{"role": "user", "content": "Hello!"}],
)
except AICPError as e:
print(e.status) # HTTP status code (0 for network errors)
print(str(e)) # human-readable message
print(e.error_type) # error type string
print(e.code) # machine-readable code or None
Common error types
401error_type: unauthorizedInvalid or missing API key
402error_type: plan_upgrade_requiredFeature requires a Cloud plan or above
403error_type: forbiddenInsufficient role for this operation
404error_type: not_foundResource does not exist
422error_type: validation_errorBad request body
429error_type: rate_limit_exceededToo many requests
502error_type: provider_errorUpstream provider returned an error
0error_type: network_errorRequest never reached the server
Retry on network errors
import time
from aicp.errors import AICPError
def with_retry(fn, retries=3):
for attempt in range(retries):
try:
return fn()
except AICPError as e:
if e.error_type == "network_error" and attempt < retries - 1:
time.sleep(2 ** attempt * 0.5)
continue
raise
response = with_retry(lambda: client.chat.complete(
model="auto",
messages=[{"role": "user", "content": "Hello!"}],
))
Async retry
import asyncio
from aicp.errors import AICPError
async def with_retry_async(fn, retries=3):
for attempt in range(retries):
try:
return await fn()
except AICPError as e:
if e.error_type == "network_error" and attempt < retries - 1:
await asyncio.sleep(2 ** attempt * 0.5)
continue
raise