While using the OpenAI SDK is possible, we recommend using the Maitai SDK for the best experience and full access to all features.

Configure OpenAI SDK

You can use Maitai with your existing OpenAI SDK implementation by changing the base URL to point to Maitai’s API endpoint, and adding the required headers.

import openai

# Initialize the client with Maitai's base URL
client = openai.OpenAI(
    base_url="https://api.trymaitai.ai",
    api_key="YOUR_MAITAI_API_KEY"  # Get this from portal.trymaitai.ai
)

messages = [
    {"role": "system", "content": "You are a helpful ordering assistant..."},
    {"role": "user", "content": "Generate a response to the customer..."},
]

response = client.chat.completions.create(
    model="gpt-4o",  # Use any supported model
    messages=messages,
    extra_headers={  # Required Maitai headers
        "X-Maitai-Application": "YOUR_APPLICATION",
        "X-Maitai-Intent": "CONVERSATION",
        "X-Maitai-Session-Id": "YOUR_SESSION_ID"
    }
)

Limitations

Downsides of the Base URL Approach

While using the base URL approach is simpler for existing OpenAI integrations, you’ll miss out on key Maitai features:

  • No Automatic Fallbacks: If your selected provider is unavailable, requests will fail instead of automatically falling back to another provider. Native SDK users can configure fallback behavior for maximum reliability.

  • No Configuration Management: You’ll need to hardcode model selection in your code instead of using Maitai’s centralized configuration management to dynamically select models based on rules.

For production applications, we recommend using the native Maitai SDK.

Authentication with Base URL Approach

You can provide provider keys through headers:

import openai

client = openai.OpenAI(
    base_url="https://api.trymaitai.ai",
    api_key="YOUR_MAITAI_API_KEY"
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
    extra_headers={
        # Required Maitai headers
        "X-Maitai-Application": "YOUR_APPLICATION",
        "X-Maitai-Intent": "CONVERSATION",
        "X-Maitai-Session-Id": "YOUR_SESSION_ID",
        
        # Provider authentication headers (optional)
        "X-Maitai-Auth-OpenAI": "YOUR_OPENAI_API_KEY",
        "X-Maitai-Auth-Anthropic": "YOUR_ANTHROPIC_API_KEY",
        "X-Maitai-Auth-Groq": "YOUR_GROQ_API_KEY",
        "X-Maitai-Auth-Gemini": "YOUR_GEMINI_API_KEY",
        "X-Maitai-Auth-Azure": "YOUR_AZURE_API_KEY",
        "X-Maitai-Auth-Sambanova": "YOUR_SAMBANOVA_API_KEY"
    }
)