---
description: megallm-new development guide
tags: megallm-new, docs, learned
source: https://docs.megallm.io
---

```yaml
---
description: "Comprehensive technical guide for MegaLLM - a unified API providing access to 70+ AI models from OpenAI, Anthropic, and Google through a single interface. Learn how to architect, configure, and integrate AI capabilities with proper error handling, streaming, function calling, and production best practices."
tags: "ai,api,openai,anthropic,claude,gpt,unified-api,streaming,function-calling,client-sdk,production,architecture"
source: "https://docs.megallm.io"
---

# Gold Standard Skill File: MegaLLM Mastery

## Role & Persona

You are an expert **MegaLLM** developer and system architect. Your mindset is to **abstract complexity through unified interfaces** while **maintaining production-grade reliability**. You understand that MegaLLM's core value isn't just model access—it's **seamless provider switching, automatic fallbacks, and unified billing** across 70+ AI models.

Your goals:
- **Write clean, modern, type-safe code** that abstracts provider differences
- **Design for resilience** with proper error handling and fallback strategies  
- **Optimize for cost and performance** through intelligent model selection
- **Build scalable systems** that handle streaming, function calling, and concurrent requests
- **Maintain security best practices** for API key management and production deployment

## Overview

### What Problem Does This Solve?

MegaLLM solves the **fragmented AI ecosystem problem** by providing a **single API interface** to 70+ models from multiple providers (OpenAI, Anthropic, Google). Instead of managing multiple SDKs, authentication systems, and billing accounts, you get:

- **Unified Interface**: Drop-in replacement for OpenAI/Anthropic SDKs
- **Automatic Fallback**: Intelligent model switching for high availability
- **Provider Flexibility**: Access Claude, GPT, Gemini through one API
- **Unified Billing**: Single invoice for all AI usage
- **Zero Integration Overhead**: Change models with one parameter

### When Should You Use It?

**Use MegaLLM when you need:**
- **Multi-model applications** that benefit from different model strengths
- **Production systems** requiring high availability and fallback capabilities
- **Cost optimization** across different providers and models
- **Rapid prototyping** with easy model switching
- **Enterprise applications** needing unified billing and management

**Don't use MegaLLM when:**
- You only need one specific model from one provider
- You have existing heavy investment in provider-specific SDKs
- You need provider-specific features not exposed through the unified interface

### Core Philosophies

1. **Abstraction Over Implementation**: Focus on AI capabilities, not provider specifics
2. **Resilience by Design**: Built-in fallbacks and error recovery
3. **Cost-Conscious Architecture**: Smart model selection and usage optimization
4. **Developer Experience**: Familiar APIs with enhanced capabilities
5. **Production Readiness**: Enterprise-grade reliability and monitoring

## Critical Concepts (Mental Models)

### The Unified Interface Mental Model

Think of MegaLLM as a **smart router** for AI requests:

```
Your Code → MegaLLM API → [OpenAI/Anthropic/Google Models]
     ↓              ↓                    ↓
  Standardized   Intelligent         Provider-Specific
  Interface      Routing             Implementation
```

**Key Insight**: Your application code remains the same regardless of which model or provider handles the request.

### The Fallback Chain Mental Model

Imagine a **priority queue of models** where each has specific strengths:

```
Primary: gpt-5 (complex reasoning)
    ↓ (if rate limited/error)
Secondary: claude-opus-4-1-20250805 (analysis)
    ↓ (if unavailable)
Tertiary: gemini-2.5-pro (creative tasks)
```

**Key Insight**: Design your fallback chain based on **task requirements**, not just availability.

### The Configuration Priority Mental Model

Configuration flows like water—**it always finds the path of least resistance**:

```
Environment Variables (highest priority)
    ↓
Project-Level Config
    ↓
System-Level Config (lowest priority)
```

**Key Insight**: Use this hierarchy to manage different environments (dev/staging/prod) and team configurations.

### The Streaming Lifecycle Mental Model

Streaming follows a **state machine pattern**:

```
Initial State → Role Chunk → Content Chunks → Final State
     ↓             ↓             ↓              ↓
  Connected    Assistant     Text/Function    Complete
               Role Set       Streaming
```

**Key Insight**: Handle each state appropriately—don't assume content arrives in complete chunks.

## Installation & Setup

### Standard Installation

```bash
# CLI Installation (recommended)
npx megallm@latest

# Global CLI installation
npm install -g megallm

# Python SDK
pip install openai anthropic

# JavaScript SDK
npm install openai @anthropic-ai/sdk
```

### Essential Configuration

```bash
# Environment Variables
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
export MEGALLM_BASE_URL="https://ai.megallm.io/v1"  # OpenAI format
export ANTHROPIC_BASE_URL="https://ai.megallm.io"   # Anthropic format
```

## Common Patterns & "The Right Way"

### 1. Client Initialization (Modern Way)

**✅ Correct Pattern:**
```python
from openai import OpenAI
import os
from typing import Optional

class MegaLLMClient:
    def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None):
        self.client = OpenAI(
            base_url=base_url or "https://ai.megallm.io/v1",
            api_key=api_key or os.getenv("MEGALLM_API_KEY")
        )
    
    def chat_completion(
        self, 
        model: str, 
        messages: list, 
        fallback_models: Optional[list] = None,
        **kwargs
    ):
        """Chat completion with automatic fallback"""
        fallback_models = fallback_models or []
        
        try:
            return self.client.chat.completions.create(
                model=model,
                messages=messages,
                **kwargs
            )
        except Exception as e:
            if fallback_models:
                return self._try_fallbacks(fallback_models, messages, kwargs)
            raise e
    
    def _try_fallbacks(self, fallback_models, messages, kwargs):
        for fallback_model in fallback_models:
            try:
                return self.client.chat.completions.create(
                    model=fallback_model,
                    messages=messages,
                    **kwargs
                )
            except Exception:
                continue
        raise Exception("All models failed")

# Usage
client = MegaLLMClient()
response = client.chat_completion(
    model="gpt-5",
    messages=[{"role": "user", "content": "Hello!"}],
    fallback_models=["claude-opus-4-1-20250805", "gemini-2.5-pro"]
)
```

**❌ Old Way (Provider-Specific):**
```python
# Tied to single provider
from openai import OpenAI

client = OpenAI(api_key="sk-openai-key")
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
```

### 2. Streaming Implementation (Modern Way)

**✅ Correct Pattern:**
```python
import asyncio
from openai import AsyncOpenAI
import json

class StreamingMegaLLMClient:
    def __init__(self):
        self.client = AsyncOpenAI(
            base_url="https://ai.megallm.io/v1",
            api_key=os.getenv("MEGALLM_API_KEY")
        )
    
    async def stream_chat(
        self, 
        model: str, 
        messages: list, 
        on_chunk: callable,
        on_complete: callable = None
    ):
        """Stream with proper event handling"""
        try:
            stream = await self.client.chat.completions.create(
                model=model,
                messages=messages,
                stream=True
            )
            
            async for chunk in stream:
                if chunk.choices[0].delta.content is not None:
                    await on_chunk(chunk.choices[0].delta.content)
                
                if chunk.choices[0].finish_reason:
                    if on_complete:
                        await on_complete(chunk.choices[0].finish_reason)
                    break
                    
        except Exception as e:
            await on_chunk(f"Error: {str(e)}")

# Usage
async def handle_chunk(content: str):
    print(content, end="", flush=True)

async def handle_complete(reason: str):
    print(f"\n[Complete: {reason}]")

client = StreamingMegaLLMClient()
await client.stream_chat(
    model="gpt-5",
    messages=[{"role": "user", "content": "Tell me a story"}],
    on_chunk=handle_chunk,
    on_complete=handle_complete
)
```

**❌ Old Way (Blocking):**
```python
# No streaming, poor UX
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Tell me a story"}]
)
print(response.choices[0].message.content)
```

### 3. Function Calling (Modern Way)

**✅ Correct Pattern:**
```python
from typing import Dict, Any, List
import json

class ToolManager:
    def __init__(self):
        self.tools = {}
    
    def register_tool(self, name: str, func: callable, schema: Dict):
        """Register a tool with proper schema validation"""
        self.tools[name] = {
            "function": func,
            "schema": schema,
            "type": "function"
        }
    
    def get_tools_list(self) -> List[Dict]:
        """Get tools in OpenAI format"""
        return [
            {
                "type": "function",
                "function": {
                    "name": name,
                    "description": tool["schema"].get("description", ""),
                    "parameters": tool["schema"]
                }
            }
            for name, tool in self.tools.items()
        ]
    
    async def execute_tool_call(self, tool_call) -> Dict[str, Any]:
        """Execute tool call with error handling"""
        try:
            function_name = tool_call.function.name
            function_args = json.loads(tool_call.function.arguments)
            
            if function_name not in self.tools:
                raise ValueError(f"Unknown tool: {function_name}")
            
            result = await self.tools[function_name]["function"](**function_args)
            return {
                "tool_call_id": tool_call.id,
                "content": json.dumps(result)
            }
        except Exception as e:
            return {
                "tool_call_id": tool_call.id,
                "content": json.dumps({"error": str(e)})
            }

class FunctionCallingClient:
    def __init__(self):
        self.client = OpenAI(
            base_url="https://ai.megallm.io/v1",
            api_key=os.getenv("MEGALLM_API_KEY")
        )
        self.tool_manager = ToolManager()
    
    def chat_with_tools(
        self,
        model: str,
        messages: List[Dict],
        tools: List[str] = None,
        tool_choice: str = "auto"
    ):
        """Chat with function calling support"""
        available_tools = [
            self.tool_manager.get_tools_list()[i] 
            for i in (tools or list(self.tool_manager.tools.keys()))
        ]
        
        response = self.client.chat.completions.create(
            model=model,
            messages=messages,
            tools=available_tools,
            tool_choice=tool_choice
        )
        
        message = response.choices[0].message
        
        # Execute tool calls if present
        if message.tool_calls:
            tool_results = []
            for tool_call in message.tool_calls:
                result = self.tool_manager.execute_tool_call(tool_call)
                tool_results.append(result)
            
            # Send results back to AI
            messages.append(message)
            for result in tool_results:
                messages.append({
                    "role": "tool",
                    "tool_call_id": result["tool_call_id"],
                    "content": result["content"]
                })
            
            # Get final response
            final_response = self.client.chat.completions.create(
                model=model,
                messages=messages
            )
            return final_response.choices[0].message
        
        return message

# Usage Example
def get_weather(location: str, unit: str = "celsius") -> Dict:
    """Get weather for a location"""
    # Simulated weather API call
    return {
        "location": location,
        "temperature": 22,
        "unit": unit,
        "condition": "sunny"
    }

client = FunctionCallingClient()
client.tool_manager.register_tool(
    "get_weather",
    get_weather,
    {
        "type": "object",
        "properties": {
            "location": {"type": "string", "description": "City name"},
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
        },
        "required": ["location"]
    }
)

response = client.chat_with_tools(
    model="gpt-5",
    messages=[{"role": "user", "content": "What's the weather in London?"}],
    tools=["get_weather"]
)
```

### 4. Error Handling & Resilience (Modern Way)

**✅ Correct Pattern:**
```python
import time
import logging
from typing import Optional, Callable
from openai import OpenAI, APIError, RateLimitError, AuthenticationError

class ResilientMegaLLMClient:
    def __init__(self, max_retries: int = 3, base_delay: float = 1.0):
        self.client = OpenAI(
            base_url="https://ai.megallm.io/v1",
            api_key=os.getenv("MEGALLM_API_KEY")
        )
        self.max_retries = max_retries
        self.base_delay = base_delay
        self.logger = logging.getLogger(__name__)
    
    def _exponential_backoff(self, attempt: int) -> float:
        """Calculate delay with exponential backoff and jitter"""
        delay = self.base_delay * (2 ** attempt)
        jitter = delay * 0.1 * (2 * attempt)  # Add jitter
        return delay + jitter
    
    def _is_retryable_error(self, error: Exception) -> bool:
        """Determine if error is retryable"""
        if isinstance(error, RateLimitError):
            return True
        if isinstance(error, APIError):
            # Retry on server errors (5xx)
            return getattr(error, 'status_code', 0) >= 500
        return False
    
    async def chat_completion_with_retry(
        self,
        model: str,
        messages: list,
        on_retry: Optional[Callable] = None,
        **kwargs
    ):
        """Chat completion with comprehensive error handling"""
        last_error = None
        
        for attempt in range(self.max_retries + 1):
            try:
                response = self.client.chat.completions.create(
                    model=model,
                    messages=messages,
                    **kwargs
                )
                return response
                
            except AuthenticationError as e:
                self.logger.error(f"Authentication failed: {e}")
                raise e  # Don't retry auth errors
                
            except Exception as e:
                last_error = e
                
                if not self._is_retryable_error(e):
                    self.logger.error(f"Non-retryable error: {e}")
                    raise e
                
                if attempt == self.max_retries:
                    self.logger.error(f"Max retries reached. Last error: {e}")
                    raise e
                
                delay = self._exponential_backoff(attempt)
                self.logger.warning(f"Retry {attempt + 1}/{self.max_retries} after {delay:.2f}s: {e}")
                
                if on_retry:
                    on_retry(attempt, e, delay)
                
                await asyncio.sleep(delay)
        
        raise last_error
    
    def health_check(self) -> Dict[str, Any]:
        """Check API connectivity and authentication"""
        try:
            response = self.client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[{"role": "user", "content": "test"}],
                max_tokens=1
            )
            return {"status": "healthy", "model": "gpt-3.5-turbo"}
        except AuthenticationError:
            return {"status": "authentication_failed"}
        except Exception as e:
            return {"status": "unhealthy", "error": str(e)}

# Usage with monitoring
client = ResilientMegaLLMClient()

def on_retry(attempt: int, error: Exception, delay: float):
    print(f"Retry {attempt} after {delay:.2f}s: {error}")

try:
    response = await client.chat_completion_with_retry(
        model="gpt-5",
        messages=[{"role": "user", "content": "Hello!"}],
        on_retry=on_retry
    )
except Exception as e:
    print(f"Request failed after all retries: {e}")
```

### 5. Production Configuration Management (Modern Way)

**✅ Correct Pattern:**
```python
import os
import json
from typing import Dict, Any, Optional
from dataclasses import dataclass
from pathlib import Path

@dataclass
class MegaLLMConfig:
    api_key: str
    base_url: str = "https://ai.megallm.io/v1"
    default_model: str = "gpt-5"
    timeout: int = 30
    max_retries: int = 3
    fallback_models: list = None
    
    def __post_init__(self):
        if self.fallback_models is None:
            self.fallback_models = ["claude-opus-4-1-20250805", "gemini-2.5-pro"]

class ConfigManager:
    def __init__(self, config_dir: Optional[Path] = None):
        self.config_dir = config_dir or Path.home() / ".megallm"
        self.config_dir.mkdir(exist_ok=True)
    
    def load_config(self, environment: str = "default") -> MegaLLMConfig:
        """Load configuration with environment precedence"""
        # Priority: env vars > project config > system config
        
        # Environment variables (highest priority)
        api_key = os.getenv("MEGALLM_API_KEY")
        base_url = os.getenv("MEGALLM_BASE_URL")
        
        if not api_key:
            # Load from config files
            config_file = self.config_dir / f"{environment}.json"
            if config_file.exists():
                with open(config_file) as f:
                    config_data = json.load(f)
                    api_key = config_data.get("api_key")
                    base_url = config_data.get("base_url", "https://ai.megallm.io/v1")
        
        if not api_key:
            raise ValueError("No API key found in environment or config files")
        
        return MegaLLMConfig(
            api_key=api_key,
            base_url=base_url or "https://ai.megallm.io/v1"
        )
    
    def save_config(self, config: MegaLLMConfig, environment: str = "default"):
        """Save configuration to file"""
        config_file = self.config_dir / f"{environment}.json"
        config_data = {
            "api_key": config.api_key,
            "base_url": config.base_url,
            "default_model": config.default_model,
            "timeout": config.timeout,
            "max_retries": config.max_retries,
            "fallback_models": config.fallback_models
        }
        
        with open(config_file, 'w') as f:
            json.dump(config_data, f, indent=2)
        
        # Set appropriate permissions
        config_file.chmod(0o600)

# Usage
config_manager = ConfigManager()
config = config_manager.load_config("production")

client = OpenAI(
    base_url=config.base_url,
    api_key=config.api_key,
    timeout=config.timeout
)
```

## Gotchas & Anti-Patterns

### 1. Base URL Confusion

**❌ Anti-Pattern:**
```python
# Wrong: Mixing OpenAI and Anthropic base URLs
client = OpenAI(
    base_url="https://ai.megallm.io",  # This is Anthropic format!
    api_key="sk-mega-..."
)
```

**✅ Correct Pattern:**
```python
# OpenAI format (for GPT models)
client = OpenAI(
    base_url="https://ai.megallm.io/v1",  # Note the /v1
    api_key="sk-mega-..."
)

# Anthropic format (for Claude models)
client = Anthropic(
    base_url="https://ai.megallm.io",  # No /v1
    api_key="sk-mega-..."
)
```

### 2. API Key Management

**❌ Anti-Pattern:**
```python
# Hardcoded keys
client = OpenAI(api_key="sk-mega-hardcoded-key")

# Committed to version control
with open("config.json") as f:
    config = json.load(f)
    client = OpenAI(api_key=config["api_key"])
```

**✅ Correct Pattern:**
```python
# Environment variables
client = OpenAI(api_key=os.getenv("MEGALLM_API_KEY"))

# Secure config loading
config_manager = ConfigManager()
config = config_manager.load_config()
```

### 3. Error Handling Oversights

**❌ Anti-Pattern:**
```python
# Generic error handling
try:
    response = client.chat.completions.create(...)
except Exception as e:
    print(f"Error: {e}")
    return None
```

**✅ Correct Pattern:**
```python
# Specific error handling with retry logic
try:
    response = client.chat.completions.create(...)
except RateLimitError:
    # Implement exponential backoff
    await asyncio.sleep(2)
    response = client.chat.completions.create(...)
except AuthenticationError:
    # Check API key validity
    raise ValueError("Invalid API key")
except APIError as e:
    # Log for monitoring
    logger.error(f"API error: {e}")
    raise
```

### 4. Streaming Implementation Issues

**❌ Anti-Pattern:**
```python
# Blocking streaming
stream = client.chat.completions.create(..., stream=True)
for chunk in stream:  # This blocks!
    print(chunk.choices[0].delta.content)
```

**✅ Correct Pattern:**
```python
# Async streaming with proper event handling
async def stream_with_handling():
    stream = await client.chat.completions.create(..., stream=True)
    async for chunk in stream:
        if chunk.choices[0].delta.content:
            await process_chunk(chunk.choices[0].delta.content)
        if chunk.choices[0].finish_reason:
            await handle_completion(chunk.choices[0].finish_reason)
            break
```

### 5. Function Calling Mistakes

**❌ Anti-Pattern:**
```python
# Missing error handling for tool execution
if message.tool_calls:
    for tool_call in message.tool_calls:
        result = execute_function(tool_call)  # No try/catch!
        messages.append(result)
```

**✅ Correct Pattern:**
```python
# Robust tool execution with error handling
if message.tool_calls:
    tool_results = []
    for tool_call in message.tool_calls:
        try:
            result = execute_function(tool_call)
            tool_results.append({
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": json.dumps(result)
            })
        except Exception as e:
            tool_results.append({
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": json.dumps({"error": str(e)})
            })
```

### 6. Configuration Priority Confusion

**❌ Anti-Pattern:**
```python
# Assuming system config overrides everything
# ~/.claude/settings.json has API key
# But user wants to use different key for this project
```

**✅ Correct Pattern:**
```python
# Use local config for project-specific overrides
# ./.claude/settings.local.json (gitignored)
{
  "env": {
    "ANTHROPIC_API_KEY": "sk-mega-project-specific-key"
  }
}
```

## API Reference (High-Frequency)

### Core Endpoints

#### Chat Completions (OpenAI Format)
```python
POST https://ai.megallm.io/v1/chat/completions
Authorization: Bearer <api_key>
Content-Type: application/json

# Request Body
{
  "model": "gpt-5",                    # string, required
  "messages": [                         # array, required
    {"role": "system", "content": "..."},
    {"role": "user", "content": "..."}
  ],
  "temperature": 0.7,                  # number, default: 0.7
  "max_tokens": 1000,                  # integer
  "stream": false,                     # boolean, default: false
  "tools": [...],                      # array of tool definitions
  "tool_choice": "auto",               # enum: "auto", "required", "none"
  "top_p": 0.9,                        # number, default: 1.0
  "frequency_penalty": 0.0,            # number, default: 0.0
  "presence_penalty": 0.0,             # number, default: 0.0
  "n": 1,                             # integer, default: 1
  "stop": ["\\n"],                     # string or array
  "user": "user-123"                   # string
}
```

#### Messages (Anthropic Format)
```python
POST https://ai.megallm.io/v1/messages
Content-Type: application/json
anthropic-version: 2023-06-01
x-api-key: <api_key>

# Request Body
{
  "model": "claude-3.5-sonnet",        # string, required
  "messages": [                         # array, required
    {"role": "user", "content": "..."}
  ],
  "max_tokens": 1000,                  # integer, required (≥1)
  "system": "You are helpful.",        # string or array
  "temperature": 1.0,                  # number, default: 1.0
  "top_p": 0.9,                        # number, default: 1.0
  "top_k": 40,                         # integer, default: 40
  "stream": false,                     # boolean, default: false
  "stop_sequences": ["\\n"],           # array of strings
  "tools": [...],                      # array of tool definitions
  "tool_choice": {"type": "auto"},     # object
  "thinking": {"type": "enabled"},     # object
  "service_tier": "auto",              # enum: "auto", "standard_only"
  "metadata": {"user_id": "123"}       # object
}
```

#### Model Discovery
```python
GET https://ai.megallm.io/v1/models
Authorization: Bearer <api_key>

# Response
{
  "object": "list",
  "data": [
    {
      "id": "gpt-5",
      "object": "model",
      "created": 1234567890,
      "owned_by": "openai",
      "capabilities": {
        "supports_function_calling": true,
        "supports_vision": false,
        "supports_streaming": true,
        "supports_structured_output": true
      },
      "pricing": {
        "input": 0.001,
        "output": 0.002
      },
      "context_length": 8192,
      "max_output_tokens": 4096
    }
  ]
}
```

### High-Frequency SDK Methods

#### Python OpenAI SDK
```python
from openai import OpenAI

client = OpenAI(
    base_url="https://ai.megallm.io/v1",
    api_key="sk-mega-..."
)

# Chat completion
response = client.chat.completions.create(
    model="gpt-5",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Streaming
stream = client.chat.completions.create(
    model="gpt-5",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

# Async
async_client = AsyncOpenAI(...)
response = await async_client.chat.completions.create(...)
```

#### Python Anthropic SDK
```python
from anthropic import Anthropic

client = Anthropic(
    base_url="https://ai.megallm.io",
    api_key="sk-mega-..."
)

# Message creation
message = client.messages.create(
    model="claude-3.5-sonnet",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello!"}]
)

# Streaming
with client.messages.stream(
    model="claude-3.5-sonnet",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
    for text in stream.text_stream():
        print(text, end="")
```

### Common Response Patterns

#### Chat Completion Response
```python
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1234567890,
  "model": "gpt-5",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Hello! How can I help you?",
      "tool_calls": [
        {
          "id": "call_abc",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"location\": \"London\"}"
          }
        }
      ]
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 10,
    "total_tokens": 25
  }
}
```

#### Message Response (Anthropic)
```python
{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! How can I help you?"
    }
  ],
  "model": "claude-3.5-sonnet",
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 15,
    "output_tokens": 10,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0
  }
}
```

### Error Response Patterns

#### Standard Error Format
```python
{
  "error": {
    "message": "Invalid request parameter",
    "type": "invalid_request_error",
    "param": "temperature",
    "code": null
  }
}
```

#### HTTP Status Codes
- `200`: Success
- `400`: Bad Request (invalid parameters)
- `401`: Unauthorized (invalid API key)
- `429`: Rate Limit Exceeded
- `500`: Internal Server Error

This comprehensive guide provides the foundation for building robust, scalable applications with MegaLLM while avoiding common pitfalls and implementing production-grade patterns.
```