# Create chat completion
Source: https://docs.megallm.io/api-reference/chat/create-chat-completion
/cn/api-reference/openapi.json post /chat/completions
Generate conversational AI responses using the OpenAI-compatible chat completions API
# Create message
Source: https://docs.megallm.io/api-reference/messages/create-message
/cn/api-reference/openapi.json post /messages
Create conversational messages using the Anthropic-compatible Messages API
# List models
Source: https://docs.megallm.io/api-reference/models/list-models
/cn/api-reference/openapi.json get /models
List all available AI models with their capabilities and pricing
# Claude Code Configuration
Source: https://docs.megallm.io/en/agents/claude
Configure Claude Code to use MegaLLM
Claude Code uses JSON configuration files for settings and API key approvals. Configuration can be at the system level (global) or project level (local).
## Configuration Files
### System-Level Configuration
**Settings File**: `~/.claude/settings.json`
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
}
}
```
**API Key Approvals**: `~/.claude.json`
```json theme={null}
{
"customApiKeyResponses": {
"approved": ["last-20-chars-of-key"],
"rejected": []
}
}
```
The API key approval file stores the last 20 characters of your API key to remember your approval decision when Claude Code prompts you.
### Project-Level Configuration
**Settings File**: `./.claude/settings.json`
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
}
}
```
**Local Settings (Gitignored)**: `./.claude/settings.local.json`
```json theme={null}
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-your-personal-key"
}
}
```
Use `settings.local.json` to keep your personal API key out of version control while sharing base configuration with your team.
## Environment Variables
The CLI sets these environment variables in your shell configuration:
```bash theme={null}
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY="sk-mega-your-api-key-here"
```
These are added to:
* `~/.bashrc` (bash)
* `~/.zshrc` (zsh)
* `~/.config/fish/config.fish` (fish)
* PowerShell profile (Windows)
### Verify Environment Variables
```bash theme={null}
echo $ANTHROPIC_BASE_URL
# Output: https://ai.megallm.io
echo $ANTHROPIC_API_KEY
# Output: sk-mega-your-api-key-here
```
## Configuration Priority
Claude Code loads configuration in this order (highest to lowest priority):
`ANTHROPIC_BASE_URL` and `ANTHROPIC_API_KEY` from your shell
`./.claude/settings.local.json` in current directory
`./.claude/settings.json` in current directory
`~/.claude/settings.json` in home directory
## Statusline Configuration (Optional)
Claude Code supports an enhanced statusline for better terminal UI:
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
},
"statusline": {
"enabled": true,
"components": {
"directory": true,
"gitBranch": true,
"model": true,
"contextUsage": true,
"cost": true,
"sessionTimer": true,
"tokenAnalytics": true
}
}
}
```
The CLI will prompt you to set this up during configuration.
## Manual Setup
If you prefer not to use the CLI:
### System-Level Manual Setup
```bash theme={null}
# 1. Create directory
mkdir -p ~/.claude
# 2. Create settings file
cat > ~/.claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "your-api-key"
}
}
EOF
# 3. Create API key approval file
cat > ~/.claude.json << 'EOF'
{
"customApiKeyResponses": {
"approved": ["last-20-chars-of-your-key"],
"rejected": []
}
}
EOF
# 4. Add environment variables to shell config
echo 'export ANTHROPIC_BASE_URL="https://ai.megallm.io"' >> ~/.bashrc
echo 'export ANTHROPIC_API_KEY="your-api-key"' >> ~/.bashrc
# 5. Reload shell
source ~/.bashrc
```
### Project-Level Manual Setup
```bash theme={null}
# 1. Navigate to your project
cd ~/projects/my-project
# 2. Create directory
mkdir -p .claude
# 3. Create settings file (without API key for version control)
cat > .claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
}
}
EOF
# 4. Create local settings file (with API key, not committed)
cat > .claude/settings.local.json << 'EOF'
{
"env": {
"ANTHROPIC_API_KEY": "your-api-key"
}
}
EOF
# 5. Add to .gitignore
echo ".claude/settings.local.json" >> .gitignore
echo ".claude.json" >> .gitignore
# 6. Commit shared config
git add .claude/settings.json .gitignore
git commit -m "Add MegaLLM configuration for Claude Code"
```
## Team Configuration
For team projects, separate shared config from personal API keys:
**Shared Configuration** (`.claude/settings.json` - committed to git):
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
},
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096
}
```
**Personal Configuration** (`.claude/settings.local.json` - not committed):
```json theme={null}
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-personal-key-here"
}
}
```
**Setup Instructions for Team** (`.claude/README.md`):
````markdown theme={null}
# MegaLLM Claude Code Setup
## Prerequisites
1. Get your MegaLLM API key from https://megallm.io/dashboard
2. Install Claude Code: `npm install -g @anthropic-ai/claude-code`
## Setup
1. Create `.claude/settings.local.json`:
```json
{
"env": {
"ANTHROPIC_API_KEY": "your-key-here"
}
}
````
2. Or set environment variable:
```bash theme={null}
export ANTHROPIC_API_KEY="your-key-here"
```
````
## Configuration Options
### Available Settings
```json
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-...",
"ANTHROPIC_MODEL": "gpt-5"
},
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096,
"streaming": true,
"contextWindow": 8192,
"autoSave": true,
"fileWatcher": true,
"gitIntegration": true
}
````
### Model Selection
Change the default model:
```json theme={null}
{
"env": {
"ANTHROPIC_MODEL": "claude-opus-4-1-20250805"
}
}
```
Or specify in environment variable:
```bash theme={null}
export ANTHROPIC_MODEL="gpt-5"
```
See [Models Catalog](/home/models) for available models.
## Verification
### Check Configuration Files
```bash theme={null}
# View settings
cat ~/.claude/settings.json | jq .
# View API key approval
cat ~/.claude.json | jq .
# Check project config
cat .claude/settings.json | jq .
cat .claude/settings.local.json | jq .
```
### Test API Connection
```bash theme={null}
# Test API with your credentials
curl -H "Authorization: Bearer $ANTHROPIC_API_KEY" \
-H "Content-Type: application/json" \
$ANTHROPIC_BASE_URL/v1/models
# Should return list of available models
```
### Test Claude Code
```bash theme={null}
# Run Claude Code
claude-code
# Or test with a simple prompt
echo "What is 2+2?" | claude-code
```
## Troubleshooting
**Check file locations:**
```bash theme={null}
ls -la ~/.claude/
ls -la .claude/
```
**Verify JSON syntax:**
```bash theme={null}
jq . ~/.claude/settings.json
# Should show formatted JSON, or error if invalid
```
**Check permissions:**
```bash theme={null}
ls -la ~/.claude/settings.json
# Should be readable: -rw-r--r--
```
**Verify environment variable:**
```bash theme={null}
echo $ANTHROPIC_API_KEY
```
If empty, reload shell:
```bash theme={null}
source ~/.bashrc # or ~/.zshrc
```
**Check API key in config:**
```bash theme={null}
jq .env.ANTHROPIC_API_KEY ~/.claude/settings.json
```
**Verify API key format:**
* Must start with `sk-mega-`
* At least 20 characters long
* No extra spaces or quotes
**Check environment variable:**
```bash theme={null}
echo $ANTHROPIC_BASE_URL
# Should be: https://ai.megallm.io
```
**Verify in config:**
```bash theme={null}
jq .env.ANTHROPIC_BASE_URL ~/.claude/settings.json
```
**Common mistake - trailing slash:**
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io" // Correct
// "ANTHROPIC_BASE_URL": "https://ai.megallm.io/" // Wrong
}
}
```
**Check you're in the right directory:**
```bash theme={null}
pwd
ls -la .claude/
```
**Verify config priority:**
```bash theme={null}
# Project config should override system
cat .claude/settings.json
cat ~/.claude/settings.json
```
**Check for settings.local.json:**
```bash theme={null}
cat .claude/settings.local.json
# This has highest priority
```
## Advanced Configuration
### Multiple Profiles
Use different configurations for different use cases:
```bash theme={null}
# Development profile
cat > ~/.claude/settings.dev.json << 'EOF'
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-dev-key"
},
"model": "gpt-4o-mini",
"temperature": 0.9
}
EOF
# Production profile
cat > ~/.claude/settings.prod.json << 'EOF'
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-prod-key"
},
"model": "gpt-5",
"temperature": 0.5
}
EOF
# Switch profiles
cp ~/.claude/settings.dev.json ~/.claude/settings.json
```
### Environment-Specific Configuration
```bash theme={null}
# Set different configs based on environment
if [ "$NODE_ENV" = "production" ]; then
export ANTHROPIC_API_KEY="$PROD_API_KEY"
else
export ANTHROPIC_API_KEY="$DEV_API_KEY"
fi
```
## Best Practices
Use `.gitignore` for `settings.local.json` to keep API keys private
Use project-level config for team projects with shared settings
Prefer environment variables in CI/CD environments
Keep Claude Code updated for latest features and fixes
## Next Steps
Configure Codex/Windsurf
Configure OpenCode
See practical examples
Configure other CLI and GUI agents
Common issues and solutions
# Cline Configuration
Source: https://docs.megallm.io/en/agents/cline
Configure Cline VSCode extension to use MegaLLM
Cline is a powerful VSCode extension that combines AI assistance with terminal integration, autonomous task execution, and advanced context awareness. Configure it to use MegaLLM for access to multiple AI models.
## Quick Configuration
### Configure for Claude Models
* Press `Ctrl+Shift+P` / `Cmd+Shift+P`
* Type: `Cline: Open Settings`
* Or: `Settings → Extensions → Cline`
* **API Provider**: Select `Anthropic`
* **Base URL**: `https://ai.megallm.io`
* **API Key**: `sk-mega-your-api-key-here`
* **Default Model**: `claude-sonnet-4`
* **Context Window**: `200000` tokens
* **Temperature**: `0.5`
* **Auto Context Detection**
* **File Watcher**
* **Terminal Integration**
* **Git Integration**
**Configuration in settings.json:**
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "sk-mega-your-api-key-here",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4",
"cline.contextWindow": 200000,
"cline.features": {
"autoContext": true,
"fileWatcher": true,
"terminalIntegration": true,
"gitIntegration": true
}
}
```
**Note:** For Anthropic format, baseURL should be `https://ai.megallm.io` (without `/v1`)
### Configure for GPT Models
* Press `Ctrl+Shift+P` / `Cmd+Shift+P`
* Type: `Cline: Open Settings`
* **API Provider**: Select `OpenAI`
* **Base URL**: `https://ai.megallm.io/v1`
* **API Key**: `sk-mega-your-api-key-here`
* **Default Model**: `gpt-5`
* **Context Window**: `128000` tokens
* **Temperature**: `0.7`
* **Auto Context Detection**
* **File Watcher**
* **Terminal Integration**
**Configuration in settings.json:**
```json theme={null}
{
"cline.apiProvider": "openai",
"cline.openai": {
"apiKey": "sk-mega-your-api-key-here",
"baseURL": "https://ai.megallm.io/v1"
},
"cline.defaultModel": "gpt-5",
"cline.contextWindow": 128000,
"cline.features": {
"autoContext": true,
"fileWatcher": true,
"terminalIntegration": true
}
}
```
**Note:** For OpenAI format, baseURL should include `/v1`: `https://ai.megallm.io/v1`
### Secure Configuration
**Step 1**: Set environment variables
```bash theme={null}
# Add to ~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
# For Claude models
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
# For GPT models
export OPENAI_BASE_URL="https://ai.megallm.io/v1"
```
**Step 2**: Reload shell
```bash theme={null}
source ~/.bashrc # or ~/.zshrc
```
**Step 3**: Reference in settings.json
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "${env:ANTHROPIC_BASE_URL}"
},
"cline.defaultModel": "claude-sonnet-4"
}
```
API key stays secure and out of version control
## Scenario Examples
### Scenario 1: First-Time Installation
Complete setup from scratch:
1. Open VSCode
2. Go to Extensions: `Ctrl+Shift+X` / `Cmd+Shift+X`
3. Search: `Cline`
4. Click **Install**
5. Reload VSCode if prompted
1. Visit [MegaLLM Dashboard](https://megallm.io/dashboard)
2. Navigate to **API Keys** section
3. Click **Create New Key**
4. Copy the key (starts with `sk-mega-`)
Open Command Palette and run: `Cline: Open Settings`
Choose your preferred format:
**For Claude Models:**
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "sk-mega-your-actual-key",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4"
}
```
**For GPT Models:**
```json theme={null}
{
"cline.apiProvider": "openai",
"cline.openai": {
"apiKey": "sk-mega-your-actual-key",
"baseURL": "https://ai.megallm.io/v1"
},
"cline.defaultModel": "gpt-5"
}
```
1. Click Cline icon in left sidebar
2. Or: `Ctrl+Shift+P` → `Cline: Open`
3. Should see connection status: ✓ Connected
In Cline chat, type:
```
What model are you using? Write a hello world function in Python.
```
**Expected Response:**
```
I'm using claude-sonnet-4 via MegaLLM.
def hello_world():
print("Hello, World!")
if __name__ == "__main__":
hello_world()
```
***
### Scenario 2: Autonomous Task Execution
Use Cline for complex, multi-step tasks:
**Scenario:** "Refactor the authentication module to use JWT tokens"
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-opus-4-1-20250805",
"cline.autonomousMode": {
"enabled": true,
"requireApproval": "perFile",
"maxIterations": 10
},
"cline.features": {
"terminalIntegration": true,
"gitIntegration": true,
"fileWatcher": true
}
}
```
Open Cline panel and provide detailed instructions:
```
Task: Refactor authentication to use JWT
Requirements:
1. Install jsonwebtoken package
2. Update login endpoint to generate JWT
3. Create middleware to verify JWT
4. Update protected routes
5. Write tests for new auth flow
6. Update documentation
Current auth is in: src/auth/
```
Cline will:
1. Analyze current auth code
2. Run: `npm install jsonwebtoken`
3. Create new JWT utilities
4. Update login controller
5. Create middleware
6. Update routes
7. Generate tests
8. Update docs
**You approve each file change before it's written**
```bash theme={null}
# Cline can execute git commands
git add .
git commit -m "Refactor: Implement JWT authentication"
```
**Result:** Complete authentication refactor with minimal manual work!
***
### Scenario 3: Project-Specific Cline Configuration
Different projects need different Cline setups:
**Python Data Analysis Project:**
`.vscode/settings.json`:
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-opus-4-1-20250805",
"cline.context": {
"language": "python",
"frameworks": ["pandas", "numpy", "matplotlib"],
"codeStyle": "pep8"
},
"cline.customPrompts": {
"analyze": "Analyze this data processing code for efficiency",
"visualize": "Suggest matplotlib visualization",
"optimize": "Optimize pandas operations for large datasets"
}
}
```
**React/TypeScript Web App:**
`.vscode/settings.json`:
```json theme={null}
{
"cline.apiProvider": "openai",
"cline.openai": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
},
"cline.defaultModel": "gpt-5",
"cline.context": {
"language": "typescript",
"frameworks": ["react", "next.js", "tailwind"],
"codeStyle": "airbnb"
},
"cline.customPrompts": {
"component": "Create React component with TypeScript",
"api": "Generate API route with error handling",
"test": "Write React Testing Library tests"
}
}
```
**Rust Systems Project:**
`.vscode/settings.json`:
```json theme={null}
{
"cline.apiProvider": "openai",
"cline.openai": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
},
"cline.defaultModel": "gemini-2.5-pro",
"cline.context": {
"language": "rust",
"codeStyle": "rustfmt"
},
"cline.customPrompts": {
"review": "Review for memory safety and performance",
"unsafe": "Analyze unsafe block for correctness",
"optimize": "Suggest performance optimizations"
}
}
```
***
### Scenario 4: Terminal Integration Workflow
Use Cline's terminal powers for DevOps tasks:
**Configuration:**
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4",
"cline.features": {
"terminalIntegration": true,
"gitIntegration": true,
"fileWatcher": true
},
"cline.terminal": {
"autoExecute": false,
"requireApproval": true,
"allowedCommands": ["npm", "git", "docker", "kubectl"]
}
}
```
**Example Workflow:**
```plaintext theme={null}
You: "Setup Docker development environment for this Node.js app"
Cline: "I'll help set up Docker. I'll need to:
1. Create Dockerfile
2. Create docker-compose.yml
3. Add .dockerignore
4. Test the build
Shall I proceed?"
You: "Yes"
Cline executes:
1. Creates Dockerfile with Node.js 18
2. Creates docker-compose.yml with app + postgres
3. Adds .dockerignore
4. Runs: docker-compose build
5. Runs: docker-compose up -d
6. Runs: docker-compose ps (to verify)
Result: Docker environment running
```
***
### Scenario 5: Migration from GitHub Copilot Chat
Switch from Copilot Chat to Cline with MegaLLM:
**Why Migrate:**
* Access to Claude (better reasoning) AND GPT
* Terminal integration (Copilot Chat doesn't have)
* Autonomous multi-step tasks
* Better pricing (pay-per-use vs seat-based)
* More context awareness
**Migration Steps:**
Note your favorite Copilot Chat features:
* Explaining code
* Generating functions
* Writing tests
* Debugging help
```json theme={null}
{
"github.copilot.enable": false
}
```
VSCode Extensions → Search "Cline" → Install
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4"
}
```
* `/explain` - Explain selected code
* `/test` - Generate tests
* `/fix` - Debug and fix issues
* `/refactor` - Refactor code
* `/task` - Execute multi-step task
**Advantages over Copilot Chat:**
* Can execute terminal commands
* Better at multi-file changes
* Claude Opus for complex reasoning
* Can run tests and verify changes
* Git integration built-in
**Feature Comparison:**
| Feature | Copilot Chat | Cline + MegaLLM |
| ---------------------- | -------------------------------------------- | ---------------------------------------- |
| **Chat Interface** | | |
| **Code Explanation** | | Better with Claude |
| **Code Generation** | | |
| **Terminal Execution** | | |
| **Multi-File Edit** | Limited | Extensive |
| **Model Choice** | GPT-4 only | GPT, Claude, Gemini |
| **Autonomous Tasks** | | |
| **Git Integration** | Basic | Advanced |
| **Pricing** | \$10-20/month | Pay-per-use |
***
### Scenario 6: Multi-Model Strategy
Switch models based on task complexity:
**Configuration:**
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4",
"cline.modelProfiles": {
"fast": {
"provider": "openai",
"model": "gpt-4o-mini",
"description": "Quick tasks, simple completions"
},
"balanced": {
"provider": "anthropic",
"model": "claude-sonnet-4",
"description": "Most tasks, good balance"
},
"powerful": {
"provider": "anthropic",
"model": "claude-opus-4-1-20250805",
"description": "Complex reasoning, refactoring"
},
"creative": {
"provider": "openai",
"model": "gpt-5",
"description": "Documentation, naming"
}
}
}
```
**Usage Pattern:**
```plaintext theme={null}
Morning: Quick bug fixes
→ Use "fast" profile (gpt-4o-mini)
→ Fast responses for simple issues
Midday: Feature development
→ Use "balanced" profile (claude-sonnet-4)
→ Good quality, reasonable speed
Afternoon: Complex refactoring
→ Use "powerful" profile (claude-opus-4)
→ Best reasoning for architecture
Evening: Documentation
→ Use "creative" profile (gpt-5)
→ Engaging docs and comments
```
**Switch profiles:**
1. In Cline panel, click model name
2. Select profile from dropdown
3. Or use command: `/model powerful`
***
### Scenario 7: CI/CD Integration
Use Cline in automated workflows:
**GitHub Actions Example:**
`.github/workflows/cline-review.yml`:
```yaml theme={null}
name: Cline AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
ai-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install Cline CLI
run: npm install -g @cline/cli
- name: Configure Cline
env:
MEGALLM_API_KEY: ${{ secrets.MEGALLM_API_KEY }}
run: |
mkdir -p ~/.vscode
cat > ~/.vscode/settings.json << EOF
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "$MEGALLM_API_KEY",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4"
}
EOF
- name: Run AI Review
run: |
cline review \
--files "$(git diff --name-only origin/main...HEAD)" \
--output review.md \
--model claude-sonnet-4
- name: Run Security Check
run: |
cline security-scan \
--files "$(git diff --name-only origin/main...HEAD)" \
--output security.md
- name: Post Review Comments
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const review = fs.readFileSync('review.md', 'utf8');
const security = fs.readFileSync('security.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## Cline AI Review\n\n${review}\n\n## Security Analysis\n\n${security}`
});
```
## Configuration Options
### Complete Reference
```json theme={null}
{
// API Provider Configuration
"cline.apiProvider": "anthropic", // or "openai"
// Anthropic (Claude) Configuration
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io" // No /v1 for Anthropic
},
// OpenAI (GPT) Configuration
"cline.openai": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1" // Include /v1 for OpenAI
},
// Model Settings
"cline.defaultModel": "claude-sonnet-4",
"cline.contextWindow": 200000,
"cline.temperature": 0.5,
"cline.maxTokens": 4096,
// Features
"cline.features": {
"autoContext": true,
"fileWatcher": true,
"terminalIntegration": true,
"gitIntegration": true,
"multiFileEdit": true
},
// Autonomous Mode
"cline.autonomousMode": {
"enabled": true,
"requireApproval": "perFile", // "perFile", "perAction", "never"
"maxIterations": 10
},
// Terminal Settings
"cline.terminal": {
"autoExecute": false,
"requireApproval": true,
"allowedCommands": ["npm", "git", "docker"],
"blockedCommands": ["rm -rf", "sudo"]
},
// Context Settings
"cline.context": {
"language": "typescript",
"frameworks": ["react", "next.js"],
"codeStyle": "airbnb",
"maxContextFiles": 20,
"includeGitInfo": true
},
// Custom Prompts
"cline.customPrompts": {
"explain": "Explain this code in detail",
"test": "Generate comprehensive tests",
"review": "Review for bugs and improvements",
"refactor": "Refactor for better maintainability",
"doc": "Generate detailed documentation"
},
// UI Settings
"cline.ui": {
"theme": "auto",
"position": "sidebar", // "sidebar", "panel", "editor"
"showStatusBar": true,
"showInlineHints": true
}
}
```
### Model Selection Guide
| Task | Recommended Model | API Provider | Why |
| -------------------- | -------------------------- | ------------ | ------------------ |
| **Code Review** | `claude-sonnet-4` | Anthropic | Excellent analysis |
| **Refactoring** | `claude-opus-4-1-20250805` | Anthropic | Best reasoning |
| **Quick Tasks** | `gpt-4o-mini` | OpenAI | Fastest |
| **Web Development** | `gpt-5` | OpenAI | Great for JS/React |
| **Documentation** | `gpt-5` | OpenAI | Clear writing |
| **Systems Code** | `gemini-2.5-pro` | OpenAI | Precise logic |
| **Autonomous Tasks** | `claude-opus-4-1-20250805` | Anthropic | Complex reasoning |
## Verification
### Test 1: Basic Chat
1. Open Cline panel in VSCode
2. Start new session
3. Type: `What model are you using?`
4. Should respond with model name and MegaLLM confirmation
### Test 2: Code Explanation
1. Select a function in your code
2. In Cline: `/explain`
3. Should provide detailed explanation
### Test 3: Terminal Integration
1. In Cline: `List all files in current directory`
2. Cline should suggest: `ls -la`
3. Approve execution
4. Should see file listing
### Test 4: Multi-File Task
1. Request: `Add error handling to all API routes`
2. Cline analyzes multiple files
3. Shows proposed changes for each
4. Approve and apply
### Test 5: Status Check
Run command: `/status`
Should show:
```
✓ Connected to MegaLLM
✓ Model: claude-sonnet-4
✓ Context: 200k tokens available
✓ Terminal: Enabled
✓ Git: Enabled
```
## Troubleshooting
**Symptoms:**
* Messages sent but no response
* "Model initialization failed" error
* Connection timeout
**Solutions:**
1. **Check API provider configuration:**
```json theme={null}
// For Claude models
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"baseURL": "https://ai.megallm.io" // No /v1
}
}
// For GPT models
{
"cline.apiProvider": "openai",
"cline.openai": {
"baseURL": "https://ai.megallm.io/v1" // Include /v1
}
}
```
2. **Verify model is available:**
```bash theme={null}
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
https://ai.megallm.io/v1/models | jq '.data[].id'
```
3. **Check model name is correct:**
* `claude-sonnet-4`
* `claude-sonnet-3` (old version)
* `gpt-5`
* `gpt-5.0` (wrong format)
4. **Reset Cline:**
* Command Palette: `Cline: Reset Configuration`
* Restart VSCode
* Reconfigure from scratch
**Symptoms:**
* Cline suggests commands but doesn't execute
* "Terminal integration disabled" message
**Solutions:**
1. **Enable terminal integration:**
```json theme={null}
{
"cline.features": {
"terminalIntegration": true
}
}
```
2. **Check terminal permissions:**
* VSCode Settings → search "terminal allow"
* Enable: `Terminal > Integrated: Allow Workspace Configuration`
3. **Verify allowed commands:**
```json theme={null}
{
"cline.terminal": {
"autoExecute": false,
"requireApproval": true,
"allowedCommands": ["npm", "git", "docker"]
}
}
```
4. **Test manually:**
* Open VSCode terminal
* Try running commands directly
* Ensure terminal works before using Cline
**Symptoms:**
* "Context too large" error
* Cline can't include all files
**Solutions:**
1. **Reduce context window:**
```json theme={null}
{
"cline.contextWindow": 100000 // Reduce from 200000
}
```
2. **Limit files included:**
```json theme={null}
{
"cline.context": {
"maxContextFiles": 10 // Reduce from 20
}
}
```
3. **Be more specific:**
Instead of: "Review the entire project"
Say: "Review only the auth module"
4. **Use smaller model:**
```json theme={null}
{
"cline.defaultModel": "gpt-4o-mini" // Smaller context
}
```
**Symptoms:**
* Cline keeps iterating without progress
* Task never completes
* Same errors repeating
**Solutions:**
1. **Limit max iterations:**
```json theme={null}
{
"cline.autonomousMode": {
"maxIterations": 5 // Reduce from 10
}
}
```
2. **Require more approvals:**
```json theme={null}
{
"cline.autonomousMode": {
"requireApproval": "perAction" // Instead of "perFile"
}
}
```
3. **Stop and restart:**
* Click "Stop" button in Cline panel
* Review what Cline attempted
* Provide more specific instructions
4. **Break down task:**
Instead of: "Refactor entire authentication system"
Try: "First, just update the login endpoint to use JWT"
**Symptoms:**
* "Invalid API key" error
* 401 Unauthorized
* Connection refused
**Solutions:**
1. **Verify key in environment:**
```bash theme={null}
echo $MEGALLM_API_KEY
# Should output: sk-mega-...
```
2. **Reload VSCode:**
* Close all VSCode windows
* Reload shell: `source ~/.bashrc`
* Reopen VSCode
3. **Set key directly (testing):**
```json theme={null}
{
"cline.anthropic": {
"apiKey": "sk-mega-your-actual-key" // Temporarily for testing
}
}
```
4. **Test key manually:**
```bash theme={null}
curl -X POST https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}'
```
5. **Check key is active:**
* Login to [Dashboard](https://megallm.io/dashboard)
* Verify key not revoked/expired
**Symptoms:**
* Multiple AI suggestions appearing
* Keyboard shortcuts not working
* Performance issues
**Solutions:**
1. **Disable conflicting extensions:**
```json theme={null}
{
"github.copilot.enable": false,
"tabnine.disable": true,
"aws.codeWhisperer.enabled": false,
"kilocode.enable": false
}
```
2. **Check for keybinding conflicts:**
* File → Preferences → Keyboard Shortcuts
* Search for Cline commands
* Resolve any conflicts
3. **Try one extension at a time:**
* Disable all AI extensions
* Enable only Cline
* Test functionality
* Re-enable others one by one
## Best Practices
Keep API keys in env vars: `${env:MEGALLM_API_KEY}`
Different models per project in `.vscode/settings.json`
Always require approval for terminal commands
Test autonomous mode on small tasks first
Watch context usage to avoid hitting limits
Let Cline handle git operations for you
## Advanced Tips
### Custom Slash Commands
Create shortcuts for common tasks:
```json theme={null}
{
"cline.customCommands": {
"/deploy": "Build the app, run tests, and deploy to staging",
"/review-pr": "Review all changed files in this PR for bugs and improvements",
"/setup-test": "Set up test file structure and write initial tests",
"/doc-api": "Generate API documentation from code comments"
}
}
```
Usage: Type `/deploy` in Cline chat
### Context Awareness
Improve Cline's understanding:
```json theme={null}
{
"cline.context": {
"projectType": "microservices",
"architecture": "event-driven",
"databases": ["postgresql", "redis"],
"testingFramework": "jest",
"cicd": "github-actions"
}
}
```
### Git Integration
Let Cline manage git workflows:
```json theme={null}
{
"cline.git": {
"autoCommit": false,
"commitMessageStyle": "conventional",
"autoCreateBranches": true,
"branchPrefix": "cline/"
}
}
```
## Next Steps
Configure Kilocode for code completion
Configure RooCode standalone app
Browse all available models
View all CLI and GUI agents
Explore the API docs
Join for support
# Codex/Windsurf Configuration
Source: https://docs.megallm.io/en/agents/codex
Configure Codex and Windsurf to use MegaLLM
Codex and Windsurf use TOML configuration format and only support system-level (global) configuration. The CLI automatically detects which variant you have installed.
Windsurf is a variant of Codex with enhanced features. The configuration is identical for both.
## Configuration File
**Location**: `~/.codex/config.toml`
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[tools]
web_search = true
file_browser = true
```
**Codex/Windsurf only supports system-level configuration.** Project-level configuration is not available.
## Environment Variable
The configuration references an environment variable for the API key:
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
This is added to your shell configuration file:
* `~/.bashrc` (bash)
* `~/.zshrc` (zsh)
* `~/.config/fish/config.fish` (fish)
* PowerShell profile (Windows)
### Verify Environment Variable
```bash theme={null}
echo $MEGALLM_API_KEY
# Output: sk-mega-your-api-key-here
```
## Manual Setup
If you prefer not to use the CLI:
```bash theme={null}
# 1. Create directory
mkdir -p ~/.codex
# 2. Create config file
cat > ~/.codex/config.toml << 'EOF'
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[tools]
web_search = true
file_browser = true
EOF
# 3. Add environment variable to shell config
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
# 4. Reload shell
source ~/.bashrc
```
## Configuration Options
### Model Selection
Change the default model in the configuration:
```toml theme={null}
model = "claude-opus-4-1-20250805" # or any supported model
```
**Available models:**
* `gpt-5` - Latest GPT model
* `gpt-4` - GPT-4
* `gpt-4o` - GPT-4 Optimized
* `claude-opus-4-1-20250805` - Claude Opus
* `claude-sonnet-4` - Claude Sonnet
* `gemini-2.5-pro` - Gemini Pro
* See [Models Catalog](/home/models) for full list
### Tool Settings
Enable or disable integrated tools:
```toml theme={null}
[tools]
web_search = true # Enable web search capability
file_browser = true # Enable file browser
terminal = true # Enable terminal access
code_execution = true # Enable code execution
```
### Advanced Configuration
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
temperature = 0.7
max_tokens = 4096
top_p = 0.9
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[tools]
web_search = true
file_browser = true
terminal = true
[ui]
theme = "dark"
font_size = 14
show_line_numbers = true
```
## Multiple API Keys
If you need different API keys for different purposes:
### Using Environment Variables
```bash theme={null}
# Development key
export MEGALLM_API_KEY="sk-mega-dev-key"
# Production key
export MEGALLM_API_KEY_PROD="sk-mega-prod-key"
```
### Switching Configurations
```bash theme={null}
# Create backup of current config
cp ~/.codex/config.toml ~/.codex/config.toml.backup
# Development config
cat > ~/.codex/config.toml.dev << 'EOF'
model_provider = "megallm"
model = "gpt-4o-mini"
[model_providers.megallm]
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY_DEV"
EOF
# Production config
cat > ~/.codex/config.toml.prod << 'EOF'
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY_PROD"
EOF
# Switch to dev
cp ~/.codex/config.toml.dev ~/.codex/config.toml
# Switch to prod
cp ~/.codex/config.toml.prod ~/.codex/config.toml
```
## Windsurf-Specific Features
Windsurf includes additional configuration options:
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[windsurf]
cascade_mode = true # Enable Cascade AI feature
multi_file_edit = true # Allow editing multiple files
context_awareness = "enhanced" # enhanced, standard, minimal
[tools]
web_search = true
file_browser = true
terminal = true
supercomplete = true # Windsurf autocomplete feature
```
## Verification
### Check Configuration File
```bash theme={null}
# View configuration
cat ~/.codex/config.toml
# Validate TOML syntax (if toml-cli installed)
toml-check ~/.codex/config.toml
# Check file permissions
ls -la ~/.codex/config.toml
```
### Test API Connection
```bash theme={null}
# Test API with your credentials
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
# Should return list of available models
```
### Test Codex/Windsurf
```bash theme={null}
# Run Codex/Windsurf
codex # or 'windsurf'
# Check version
codex --version # or 'windsurf --version'
```
## Troubleshooting
**Check if directory exists:**
```bash theme={null}
ls -la ~/.codex/
```
**Create if missing:**
```bash theme={null}
mkdir -p ~/.codex
# Then create config.toml
```
**Verify file path:**
```bash theme={null}
# Should be exactly:
~/.codex/config.toml
# Not:
~/.config/codex/config.toml # Wrong location
```
**Check environment variable is set:**
```bash theme={null}
echo $MEGALLM_API_KEY
```
If empty:
```bash theme={null}
# Add to shell config
echo 'export MEGALLM_API_KEY="your-key"' >> ~/.bashrc
source ~/.bashrc
```
**Verify key format:**
* Must start with `sk-mega-`
* At least 20 characters
* No extra spaces or quotes
**Test the key:**
```bash theme={null}
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
https://ai.megallm.io/v1/models
```
**Check config file:**
```bash theme={null}
cat ~/.codex/config.toml | grep model_provider
# Should show: model_provider = "megallm"
```
**Verify base\_url:**
```bash theme={null}
cat ~/.codex/config.toml | grep base_url
# Should show: base_url = "https://ai.megallm.io/v1"
```
**Ensure no typos:**
```toml theme={null}
model_provider = "megallm" # Correct
# model_provider = "megalm" # Wrong (typo)
# model_provider = "openai" # Wrong (different provider)
```
**Validate syntax:**
```bash theme={null}
# If you have toml-cli installed
toml-check ~/.codex/config.toml
# Or use Python
python3 -c "import tomli; tomli.load(open('~/.codex/config.toml', 'rb'))"
```
**Common TOML mistakes:**
```toml theme={null}
# Wrong - missing quotes
model_provider = megallm
# Correct
model_provider = "megallm"
# Wrong - incorrect section syntax
model_providers.megallm
base_url = "..."
# Correct
[model_providers.megallm]
base_url = "..."
```
**Restart Codex/Windsurf:**
```bash theme={null}
# Close all instances
pkill codex # or 'pkill windsurf'
# Start fresh
codex # or 'windsurf'
```
**Check for multiple config files:**
```bash theme={null}
find ~ -name "config.toml" -path "*/.codex/*"
# Should only show one file
```
**Verify permissions:**
```bash theme={null}
chmod 644 ~/.codex/config.toml
```
## Why System-Level Only?
Codex and Windsurf don't support project-level configuration because:
1. **Single Instance** - Codex/Windsurf runs as a single instance across all projects
2. **Global Settings** - Tool preferences apply system-wide
3. **Simplified Management** - One configuration to manage
**Workaround for Project-Specific Keys:**
Use environment variables in your project:
```bash theme={null}
# In project directory
cat > .env << 'EOF'
MEGALLM_API_KEY=project-specific-key
EOF
# Load before running Codex
source .env && codex
```
Or create shell aliases:
```bash theme={null}
# In ~/.bashrc or ~/.zshrc
alias codex-project-a='MEGALLM_API_KEY="key-for-project-a" codex'
alias codex-project-b='MEGALLM_API_KEY="key-for-project-b" codex'
```
## Best Practices
Keep backup of `config.toml` before making changes
Store API keys in environment variables, not in config file
You can commit `config.toml` if env\_key is used (no hardcoded keys)
Keep Codex/Windsurf updated for latest features
## Comparison: Codex vs Windsurf
| Feature | Codex | Windsurf |
| ------------------ | --------------------- | --------------------- |
| Base Configuration | | |
| MegaLLM Support | | |
| Config Location | `~/.codex/` | `~/.codex/` |
| Cascade AI | | |
| Supercomplete | | |
| Multi-file Edit | Basic | Enhanced |
Both Codex and Windsurf use the same configuration file location and format.
## Next Steps
Configure Claude Code
Configure OpenCode
See practical examples
Configure other CLI and GUI agents
Browse available models
# Kilocode Configuration
Source: https://docs.megallm.io/en/agents/kilocode
Configure Kilocode VSCode extension to use MegaLLM
Kilocode is a powerful VSCode extension that provides AI-powered code completion, inline chat, and code actions. Configure it to use MegaLLM for access to multiple AI models.
## Quick Configuration
### Step-by-Step Setup
* Press `Ctrl+Shift+P` (Windows/Linux) or `Cmd+Shift+P` (Mac)
* Type: `Preferences: Open Settings (UI)`
* Search for: `Kilocode`
* **API Provider**: Select `Custom`
* **Provider Name**: `MegaLLM`
* **Base URL**: `https://ai.megallm.io/v1`
* **API Key**: `sk-mega-your-api-key-here`
* **Default Model**: `gpt-5` (or any [supported model](/home/models))
* **Temperature**: `0.3` (lower = more deterministic)
* **Max Tokens**: `500` (for completions)
* **Enable AutoComplete**
* **Enable Inline Chat**
* **Enable Code Actions**
* **Enable Suggestions**
### Configuration File
**Location**: `.vscode/settings.json` or `~/.config/Code/User/settings.json`
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-api-key-here"
},
"kilocode.defaultModel": "gpt-5",
"kilocode.enableAutoComplete": true,
"kilocode.enableInlineChat": true,
"kilocode.enableCodeActions": true,
"kilocode.modelSettings": {
"temperature": 0.3,
"maxTokens": 500,
"topP": 0.9
}
}
```
You can use environment variables: `"apiKey": "${env:MEGALLM_API_KEY}"`
### Secure Configuration
**Step 1**: Set environment variables
```bash theme={null}
# Add to ~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
export MEGALLM_BASE_URL="https://ai.megallm.io/v1"
```
**Step 2**: Reload shell
```bash theme={null}
source ~/.bashrc # or ~/.zshrc
```
**Step 3**: Reference in settings.json
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "${env:MEGALLM_BASE_URL}",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gpt-5"
}
```
API key stays secure and out of version control
## Scenario Examples
### Scenario 1: First-Time Installation
Complete setup from scratch:
1. Open VSCode
2. Go to Extensions: `Ctrl+Shift+X` / `Cmd+Shift+X`
3. Search: `Kilocode`
4. Click **Install**
5. Reload VSCode window
1. Visit [MegaLLM Dashboard](https://megallm.io/dashboard)
2. Navigate to **API Keys** section
3. Click **Create New Key**
4. Copy the key (starts with `sk-mega-`)
5. Store it securely
Open settings (`Ctrl+,` / `Cmd+,`) and add:
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-actual-key-here"
},
"kilocode.defaultModel": "gpt-5"
}
```
1. Create a new file: `test.js`
2. Type a comment: `// function to calculate fibonacci`
3. Press `Tab` to trigger completion
4. Should see AI-generated code
**Expected Result:**
```javascript theme={null}
// function to calculate fibonacci
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
```
***
### Scenario 2: Team Project Configuration
Set up Kilocode for entire development team:
**Project Structure:**
```
my-project/
├── .vscode/
│ ├── settings.json # Shared config (committed)
│ └── settings.local.json # Personal keys (gitignored)
├── .gitignore
└── README.md
```
**Step 1: Create Shared Config**
`.vscode/settings.json` (committed to git):
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gpt-5",
"kilocode.modelSettings": {
"temperature": 0.7,
"maxTokens": 1000
},
"kilocode.enableAutoComplete": true,
"kilocode.enableInlineChat": true
}
```
**Step 2: Setup .gitignore**
```bash theme={null}
# .gitignore
.vscode/settings.local.json
.env
```
**Step 3: Create Setup Instructions**
`README.md`:
````markdown theme={null}
## Kilocode Setup
### Prerequisites
1. VSCode with Kilocode extension installed
2. MegaLLM API key ([Get one](https://megallm.io/dashboard))
### Configuration
1. **Set environment variable:**
```bash
export MEGALLM_API_KEY="your-key-here"
````
Add to your shell config (\~/.bashrc or \~/.zshrc) to persist.
2. **Or create local settings** (not committed):
`.vscode/settings.local.json`:
```json theme={null}
{
"kilocode.customProvider": {
"apiKey": "your-key-here"
}
}
```
3. **Reload VSCode** and start coding!
### Verification
Type `// hello world function` and press Tab.
Should see AI-generated code.
````
**Step 4: Team Members Clone and Setup**
```bash
# Team member workflow
git clone https://github.com/company/my-project.git
cd my-project
# Add personal API key
export MEGALLM_API_KEY="their-personal-key"
# Or create .vscode/settings.local.json with their key
# Open in VSCode
code .
````
***
### Scenario 3: Project-Specific Model Selection
Use different models for different projects:
**Python Data Science Project:**
`.vscode/settings.json`:
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "claude-opus-4-1-20250805",
"kilocode.customPrompts": {
"analyze": "Analyze this data processing code for efficiency",
"doc": "Generate numpy-style docstring for this function"
}
}
```
**JavaScript/React Project:**
`.vscode/settings.json`:
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gpt-5",
"kilocode.customPrompts": {
"component": "Generate a React functional component",
"test": "Write Jest tests for this component"
}
}
```
**Systems Programming (Rust/Go):**
`.vscode/settings.json`:
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gemini-2.5-pro",
"kilocode.modelSettings": {
"temperature": 0.2 // Lower for more precise systems code
}
}
```
***
### Scenario 4: Multi-Model Workflow
Switch models dynamically based on task:
**Configuration:**
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gpt-5",
"kilocode.modelProfiles": {
"fast": {
"model": "gpt-4o-mini",
"temperature": 0.5,
"maxTokens": 300
},
"quality": {
"model": "claude-opus-4-1-20250805",
"temperature": 0.3,
"maxTokens": 1000
},
"creative": {
"model": "gpt-5",
"temperature": 0.9,
"maxTokens": 800
}
}
}
```
**Usage:**
* **Morning (rapid prototyping)**: Use `fast` profile with GPT-4o-mini
* **Afternoon (quality code)**: Use `quality` profile with Claude Opus
* **Documentation**: Use `creative` profile with higher temperature
**Switch profiles via Command Palette:**
1. `Ctrl+Shift+P` / `Cmd+Shift+P`
2. `Kilocode: Switch Model Profile`
3. Select: `fast`, `quality`, or `creative`
***
### Scenario 5: Migration from GitHub Copilot
Switching from Copilot to Kilocode with MegaLLM:
**Current Setup (Copilot):**
```json theme={null}
{
"github.copilot.enable": true
}
```
**New Setup (Kilocode + MegaLLM):**
```json theme={null}
{
"github.copilot.enable": false
}
```
VSCode Extensions → Search "Kilocode" → Install
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gpt-5"
}
```
**Benefits over Copilot:**
* Access to multiple models (GPT, Claude, Gemini)
* Better pricing and no seat limits
* Inline chat for explanations
* Custom model selection per project
* Code actions beyond completion
## Configuration Options
### Complete Reference
```json theme={null}
{
// Provider Configuration
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
// Model Settings
"kilocode.defaultModel": "gpt-5",
"kilocode.modelSettings": {
"temperature": 0.3, // 0.0-2.0: Lower = more focused
"maxTokens": 500, // Max completion length
"topP": 0.9, // Nucleus sampling
"frequencyPenalty": 0.0, // Reduce repetition
"presencePenalty": 0.0 // Encourage diversity
},
// Feature Toggles
"kilocode.enableAutoComplete": true,
"kilocode.enableInlineChat": true,
"kilocode.enableCodeActions": true,
"kilocode.enableSuggestions": true,
"kilocode.enableHover": true,
// Behavior
"kilocode.autoTrigger": true,
"kilocode.debounceDelay": 300, // ms before triggering
"kilocode.maxSuggestions": 3,
"kilocode.showInlineHints": true,
// Custom Prompts
"kilocode.customPrompts": {
"doc": "Generate comprehensive documentation",
"test": "Write unit tests with high coverage",
"refactor": "Refactor for better maintainability",
"optimize": "Optimize for performance",
"secure": "Review for security vulnerabilities"
},
// Keyboard Shortcuts
"kilocode.shortcuts": {
"acceptSuggestion": "Tab",
"nextSuggestion": "Alt+]",
"prevSuggestion": "Alt+[",
"dismissSuggestion": "Esc",
"triggerInlineChat": "Ctrl+K"
},
// UI Preferences
"kilocode.ui": {
"showStatusBar": true,
"showInlineButtons": true,
"theme": "auto", // auto, light, dark
"position": "right" // left, right
}
}
```
### Model Selection Guide
| Task | Recommended Model | Reason |
| ------------------- | -------------------------- | ---------------------- |
| **Code Completion** | `gpt-4o-mini` | Fast, cost-effective |
| **Complex Logic** | `claude-opus-4-1-20250805` | Superior reasoning |
| **Web Development** | `gpt-5` | Excellent JS/TS/React |
| **Data Science** | `claude-sonnet-4` | Strong analysis |
| **Documentation** | `gpt-5` | Clear explanations |
| **Algorithms** | `gemini-2.5-pro` | Mathematical precision |
See [full model catalog](/home/models) for all options.
## Verification
### Test 1: Basic Completion
```javascript theme={null}
// Type this comment and press Tab:
// function to check if string is palindrome
// Expected output:
function isPalindrome(str) {
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleaned === cleaned.split('').reverse().join('');
}
```
### Test 2: Inline Chat
1. Select a function
2. Press `Ctrl+K` (or `Cmd+K` on Mac)
3. Type: `Explain this function`
4. Should see explanation in chat panel
### Test 3: Code Actions
1. Right-click on code
2. Should see "Kilocode Actions" in context menu
3. Options: Explain, Improve, Generate Tests, etc.
### Test 4: Status Bar
Check bottom-right of VSCode:
* Should show: `Kilocode: Connected`
* Model name: `gpt-5` (or your selected model)
* Click to see connection details
## Troubleshooting
**Symptoms:**
* No suggestions when typing
* Status bar shows "Disconnected"
**Solutions:**
1. **Check API key:**
```bash theme={null}
echo $MEGALLM_API_KEY
# Should output: sk-mega-...
```
2. **Verify configuration:**
```json theme={null}
{
"kilocode.apiProvider": "custom", // Must be "custom"
"kilocode.customProvider": {
"baseURL": "https://ai.megallm.io/v1" // No trailing slash
}
}
```
3. **Reload VSCode:**
* `Ctrl+Shift+P` / `Cmd+Shift+P`
* Run: `Developer: Reload Window`
4. **Check extension is enabled:**
* Extensions panel
* Search: Kilocode
* Should show "Enabled"
5. **Test API manually:**
```bash theme={null}
curl -X POST https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"test"}]}'
```
**Symptoms:**
* Completions are incorrect
* Suggestions don't match code style
* Irrelevant responses
**Solutions:**
1. **Adjust temperature:**
```json theme={null}
{
"kilocode.modelSettings": {
"temperature": 0.2 // Lower = more focused (0.0-1.0)
}
}
```
2. **Try different model:**
```json theme={null}
{
"kilocode.defaultModel": "claude-sonnet-4" // Better for analysis
}
```
3. **Increase context:**
```json theme={null}
{
"kilocode.modelSettings": {
"maxTokens": 1000 // More detailed completions
}
}
```
4. **Add project-specific prompt:**
```json theme={null}
{
"kilocode.systemPrompt": "You are an expert in TypeScript and React. Follow Airbnb style guide."
}
```
**Symptoms:**
* Long wait for suggestions
* Timeout errors
**Solutions:**
1. **Use faster model:**
```json theme={null}
{
"kilocode.defaultModel": "gpt-4o-mini" // Fastest
}
```
2. **Reduce max tokens:**
```json theme={null}
{
"kilocode.modelSettings": {
"maxTokens": 300 // Faster completions
}
}
```
3. **Increase debounce delay:**
```json theme={null}
{
"kilocode.debounceDelay": 500 // Wait longer before triggering
}
```
4. **Check network:**
```bash theme={null}
ping ai.megallm.io
curl -w "@-" -o /dev/null -s https://ai.megallm.io/v1/models <<'EOF'
time_total: %{time_total}s
EOF
```
**Symptoms:**
* "Invalid API key" error
* 401 Unauthorized
**Solutions:**
1. **Verify key format:**
* Must start with `sk-mega-`
* At least 60 characters
* No spaces or quotes
2. **Check key is active:**
* Login to [Dashboard](https://megallm.io/dashboard)
* Go to API Keys
* Verify key is not revoked/expired
3. **Test key directly:**
```bash theme={null}
curl -H "Authorization: Bearer sk-mega-your-key" \
https://ai.megallm.io/v1/models
```
4. **Regenerate if needed:**
* Dashboard → API Keys → Create New
* Update configuration with new key
**Symptoms:**
* Kilocode completions conflict with other AI tools
* Multiple suggestions appearing
**Solutions:**
1. **Disable conflicting extensions:**
```json theme={null}
{
"github.copilot.enable": false,
"tabnine.disable": true,
"aws.codeWhisperer.enabled": false
}
```
2. **Adjust trigger keys:**
```json theme={null}
{
"kilocode.shortcuts": {
"acceptSuggestion": "Alt+Enter" // Different from Tab
}
}
```
3. **Set priority:**
```json theme={null}
{
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.quickSuggestions": {
"other": "on",
"comments": "off",
"strings": "on"
}
}
```
## Best Practices
Keep API keys in env vars, reference with `${env:MEGALLM_API_KEY}`
Configure different models in `.vscode/settings.json` per project
Use 0.2-0.4 for code generation, 0.7-0.9 for documentation
Check [Dashboard](https://megallm.io/dashboard) to optimize costs
## Advanced Tips
### Custom Keyboard Shortcuts
Add to `keybindings.json`:
```json theme={null}
[
{
"key": "ctrl+alt+k",
"command": "kilocode.triggerSuggestion",
"when": "editorTextFocus"
},
{
"key": "ctrl+alt+e",
"command": "kilocode.explainCode",
"when": "editorHasSelection"
},
{
"key": "ctrl+alt+t",
"command": "kilocode.generateTests",
"when": "editorTextFocus"
}
]
```
### Workspace-Specific Prompts
`.vscode/settings.json`:
```json theme={null}
{
"kilocode.customPrompts": {
"api": "Generate RESTful API endpoint following our conventions",
"schema": "Create Prisma schema model",
"hook": "Generate React custom hook",
"component": "Create styled-component with TypeScript"
}
}
```
### Context-Aware Completions
```json theme={null}
{
"kilocode.contextSettings": {
"includeOpenFiles": true,
"includeImports": true,
"includeTypes": true,
"maxContextFiles": 5
}
}
```
## Next Steps
Configure RooCode application
Configure Cline VSCode extension
Browse all available models
View all CLI and GUI agents
Explore the API docs
Join for support
# OpenCode Configuration
Source: https://docs.megallm.io/en/agents/opencode
Configure OpenCode to use MegaLLM
OpenCode uses JSON configuration format and supports both system-level (global) and project-level (local) configuration.
## Configuration Files
### System-Level Configuration
**Location**: `~/.config/opencode/opencode.json`
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
The CLI fetches available models from MegaLLM and adds them to the `provider.anthropic.models` section automatically.
### Project-Level Configuration
**Location**: `./opencode.json` (in project root)
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
Use project-level configuration to customize settings per project while keeping a global default. The `{env:MEGALLM_API_KEY}` syntax references the environment variable.
## Manual Setup
### System-Level Manual Setup
```bash theme={null}
# 1. Create directory
mkdir -p ~/.config/opencode
# 2. Create config file
cat > ~/.config/opencode/opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
EOF
# 3. Set environment variable
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
# 4. Verify
cat ~/.config/opencode/opencode.json | jq .
```
### Project-Level Manual Setup
```bash theme={null}
# 1. Navigate to project
cd ~/projects/my-project
# 2. Create config file
cat > opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
EOF
# 3. Set environment variable (if not already set)
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
# 4. Add to .gitignore
echo "opencode.json" >> .gitignore
```
## Configuration Priority
OpenCode loads configuration in this order (highest to lowest priority):
`MEGALLM_API_KEY` environment variable (referenced via `{env:MEGALLM_API_KEY}` in config)
`./opencode.json` in current directory
`~/.config/opencode/opencode.json` in home directory
## Configuration Options
### Provider Settings
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"models": {
"gpt-5": {
"id": "gpt-5",
"name": "GPT-5 (Via MegaLLM)"
},
"claude-sonnet-4": {
"id": "claude-sonnet-4",
"name": "Claude Sonnet 4 (Via MegaLLM)"
}
},
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
```
The CLI automatically fetches and populates the `models` object from the MegaLLM API. You can also manually add or override specific models.
### Tool Settings
```json theme={null}
{
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
},
"autoupdate": true
}
```
**Available models:**
* `gpt-5` - Latest GPT model
* `gpt-4` - GPT-4
* `claude-opus-4-1-20250805` - Claude Opus
* `claude-sonnet-4` - Claude Sonnet
* `gemini-2.5-pro` - Gemini Pro
* See [Models Catalog](/home/models) for full list
### Complete Example
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"models": {
"gpt-5": {
"id": "gpt-5",
"name": "GPT-5 (Via MegaLLM)"
},
"gpt-4": {
"id": "gpt-4",
"name": "GPT-4 (Via MegaLLM)"
},
"claude-sonnet-4": {
"id": "claude-sonnet-4",
"name": "Claude Sonnet 4 (Via MegaLLM)"
}
},
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
## Team Configuration
For team projects, use environment variables to keep API keys out of version control:
**Shared Configuration** (`opencode.json` - committed to git):
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
**Add to `.gitignore`**:
```
opencode.json
```
**Setup Instructions for Team** (`README.md`):
````markdown theme={null}
# OpenCode Setup
## Prerequisites
1. Get MegaLLM API key from https://megallm.io/dashboard
2. Install OpenCode: `npm install -g opencode-ai`
## Setup
Set the MEGALLM_API_KEY environment variable:
```bash
export MEGALLM_API_KEY="your-api-key-here"
````
Add to your shell config (\~/.bashrc or \~/.zshrc) to make it permanent.
````
## Environment Variables
The MegaLLM CLI sets the `MEGALLM_API_KEY` environment variable for OpenCode:
```bash
# Set by the CLI automatically
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
````
This environment variable is referenced in the OpenCode configuration using `{env:MEGALLM_API_KEY}`.
Add to your shell configuration:
```bash theme={null}
# ~/.bashrc or ~/.zshrc
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
### Verify Environment Variables
```bash theme={null}
echo $MEGALLM_API_KEY
# Output: sk-mega-your-api-key-here
```
## Verification
### Check Configuration Files
```bash theme={null}
# System config
cat ~/.config/opencode/opencode.json | jq .
# Project config
cat opencode.json | jq .
cat opencode.local.json | jq .
# Check file permissions
ls -la ~/.config/opencode/opencode.json
ls -la opencode.json
```
### Validate JSON Syntax
```bash theme={null}
# Validate JSON
jq . ~/.config/opencode/opencode.json
# Should show formatted JSON or error if invalid
```
### Test API Connection
```bash theme={null}
# Test with MEGALLM_API_KEY
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
# Should return list of available models
```
### Test OpenCode
```bash theme={null}
# Run OpenCode
opencode
# Check version
opencode --version
# Test with a file
echo "console.log('test')" > test.js
opencode test.js
```
## Troubleshooting
**Check file locations:**
```bash theme={null}
# System config
ls -la ~/.config/opencode/opencode.json
# Project config
ls -la opencode.json
```
**Validate JSON syntax:**
```bash theme={null}
jq . ~/.config/opencode/opencode.json
# Should show formatted JSON
```
**Check you're in the right directory:**
```bash theme={null}
pwd
# Should be your project directory for project-level config
```
**Check environment variable:**
```bash theme={null}
echo $MEGALLM_API_KEY
```
If empty, set it:
```bash theme={null}
export MEGALLM_API_KEY="your-api-key"
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
```
**Verify key format:**
* Must start with `sk-mega-`
* At least 20 characters
* No extra spaces or quotes
**Test the key:**
```bash theme={null}
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
```
**Check config:**
```bash theme={null}
jq '.provider.anthropic.options.baseURL' ~/.config/opencode/opencode.json
# Should show: "https://ai.megallm.io/v1"
```
**Common mistakes:**
```json theme={null}
{
"provider": {
"anthropic": {
"options": {
"baseURL": "https://ai.megallm.io/v1" // Correct
// "baseURL": "https://ai.megallm.io/v1/" // Wrong (trailing slash)
// "baseURL": "https://ai.megallm.io" // Wrong (missing /v1)
}
}
}
}
```
**Verify you have project config:**
```bash theme={null}
ls -la opencode.json
```
**Ensure proper JSON structure:**
Project config should use the same structure as system config with `provider.anthropic.options`.
**Common JSON mistakes:**
```json theme={null}
// Wrong - trailing comma
{
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
}
}
}
}
// Correct
{
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}"
}
}
}
}
// Wrong - single quotes
{
'provider': {
'anthropic': {
'options': {
'apiKey': '{env:MEGALLM_API_KEY}'
}
}
}
}
// Correct - double quotes
{
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}"
}
}
}
}
```
**Validate:**
```bash theme={null}
jq . ~/.config/opencode/opencode.json
# Will show error if invalid
```
## Best Practices
Keep API keys in `opencode.local.json` and add to `.gitignore`
Commit `opencode.json` without API keys for team consistency
Use project-level config for project-specific context and settings
Always validate JSON syntax after manual edits
## Advanced Usage
### Multiple Profiles
```bash theme={null}
# Create different profiles
cat > ~/.config/opencode/opencode.dev.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY_DEV}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
cat > ~/.config/opencode/opencode.prod.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY_PROD}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
# Set environment variables
export MEGALLM_API_KEY_DEV="sk-mega-dev-key"
export MEGALLM_API_KEY_PROD="sk-mega-prod-key"
# Switch profiles
alias opencode-dev='cp ~/.config/opencode/opencode.dev.json ~/.config/opencode/opencode.json && opencode'
alias opencode-prod='cp ~/.config/opencode/opencode.prod.json ~/.config/opencode/opencode.json && opencode'
```
### CI/CD Configuration
```yaml theme={null}
# GitHub Actions example
- name: Configure OpenCode
env:
MEGALLM_API_KEY: ${{ secrets.MEGALLM_API_KEY }}
run: |
mkdir -p ~/.config/opencode
cat > ~/.config/opencode/opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
```
## Next Steps
Configure Claude Code
Configure Codex/Windsurf
See practical examples
Configure other CLI and GUI agents
Browse available models
# AI Coding Agents Overview
Source: https://docs.megallm.io/en/agents/overview
Configure AI coding agents to use MegaLLM - CLI tools and GUI extensions
MegaLLM supports all major AI coding agents. This unified guide covers configuration for Claude Code, Codex/Windsurf, OpenCode, Kilocode, RooCode, and Cline.
## Available Agents
CLI + Editor integration with JSON config
CLI + Editor with TOML config
CLI + Editor with auto-model fetching
VSCode extension with inline chat (CLI coming soon)
Standalone app with visual interface
VSCode extension with autonomous tasks (CLI coming soon)
## Quick Comparison
| Agent | Interfaces | Config Format | Config Level | Best For |
| ------------------ | ----------------- | --------------- | ---------------- | -------------------------------------- |
| **Claude Code** | CLI + VSCode | JSON | System + Project | Terminal workflows, VSCode integration |
| **Codex/Windsurf** | CLI + Editor | TOML | System only | Advanced users, Cascade AI |
| **OpenCode** | CLI + Editor | JSON | System + Project | Multi-model switching, flexibility |
| **Kilocode** | VSCode (CLI soon) | VSCode settings | User + Workspace | Inline chat, code completion |
| **RooCode** | Standalone app | JSON | App-level | Visual UI, standalone workflow |
| **Cline** | VSCode (CLI soon) | VSCode settings | User + Workspace | Autonomous tasks, terminal ops |
## Agent Types
### CLI-First Agents (with Editor Support)
* CLI-first design
* VSCode extension available
* JSON configuration
* System & project-level
* Statusline support
* CLI-first design
* Editor integrations
* TOML configuration
* Cascade AI (Windsurf)
* Supercomplete features
* CLI-first design
* Editor plugins available
* JSON configuration
* Auto-fetch models
* Multi-provider support
**When to use CLI-first agents:**
* Terminal-based workflows
* CI/CD integration
* Server environments
* Scripting and automation
* Also work great in editors with extensions
### Editor-Only Agents (CLI Coming Soon)
* VSCode extension (primary)
* CLI under maintenance
* Inline chat interface
* Code completion
* File tree integration
* Standalone app
* Visual interface
* Multi-project support
* Code review features
* VSCode extension (primary)
* CLI under maintenance
* Autonomous task execution
* Terminal integration
* Git workflow support
**CLI Support for Kilocode & Cline:** CLI versions are currently under maintenance and will be available soon. Use the VSCode extensions in the meantime.
**When to use editor-focused agents:**
* Pure visual editing workflows
* Inline suggestions and completions
* Multi-file refactoring
* Code review workflows
* IDE-native experience
## Getting Started
Select a CLI agent for terminal workflows or a GUI agent for visual editing
Sign up at [MegaLLM Dashboard](https://megallm.io/dashboard) and get your API key starting with `sk-mega-`
Follow the specific configuration guide for your chosen agent (linked below)
Launch your agent and start using AI-powered coding assistance
## Configuration Guides
### CLI-First Agents (also work in editors)
**Works as:** CLI tool + VSCode extension
**Configuration files:**
* System: `~/.claude/settings.json`
* Project: `./.claude/settings.json`
* Local: `./.claude/settings.local.json`
**Quick setup:**
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
}
}
```
[Full Claude Code Configuration Guide →](/agents/claude)
**Works as:** CLI tool + Editor integrations (Windsurf is enhanced variant)
**Configuration file:**
* System: `~/.codex/config.toml` (only)
**Quick setup:**
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
[tools]
web_search = true
file_browser = true
```
[Full Codex/Windsurf Configuration Guide →](/agents/codex)
**Works as:** CLI tool + Editor plugins (VSCode, Vim, etc.)
**Configuration files:**
* System: `~/.config/opencode/opencode.json`
* Project: `./.opencode/opencode.json`
**Quick setup:**
```json theme={null}
{
"providers": [
{
"id": "megallm",
"name": "MegaLLM",
"type": "openai",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-api-key-here"
}
],
"defaultProvider": "megallm"
}
```
[Full OpenCode Configuration Guide →](/agents/opencode)
### Editor-Only Agents
**Configuration location:**
* User Settings (global): VSCode Settings UI or `settings.json`
* Workspace Settings (project): `.vscode/settings.json`
**Quick setup (settings.json):**
```json theme={null}
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
},
"kilocode.defaultModel": "gpt-5"
}
```
**Environment variable:**
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
[Full Kilocode Configuration Guide →](/agents/kilocode)
**Configuration location:**
* Windows: `%APPDATA%\RooCode\config.json`
* macOS: `~/Library/Application Support/RooCode/config.json`
* Linux: `~/.config/roocode/config.json`
**Quick setup:**
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-api-key-here",
"model": "gpt-5"
},
"features": {
"codeCompletion": true,
"chatInterface": true,
"codeReview": true,
"terminalIntegration": false
}
}
```
[Full RooCode Configuration Guide →](/agents/roocode)
**Configuration location:**
* User Settings (global): VSCode Settings UI or `settings.json`
* Workspace Settings (project): `.vscode/settings.json`
**Quick setup for GPT models (OpenAI format):**
```json theme={null}
{
"cline.apiProvider": "openai",
"cline.openai": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
},
"cline.defaultModel": "gpt-5"
}
```
**Quick setup for Claude models (Anthropic format):**
```json theme={null}
{
"cline.apiProvider": "anthropic",
"cline.anthropic": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io"
},
"cline.defaultModel": "claude-sonnet-4"
}
```
**Environment variable:**
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
[Full Cline Configuration Guide →](/agents/cline)
## Model Selection
All agents support the same models through MegaLLM:
### GPT Models
* `gpt-5` - Latest GPT model (recommended)
* `gpt-4` - GPT-4
* `gpt-4o` - GPT-4 Optimized
* `gpt-4o-mini` - Fast, cost-effective
### Claude Models
* `claude-opus-4-1-20250805` - Most capable
* `claude-sonnet-4` - Balanced (recommended)
* `claude-haiku-4` - Fast, efficient
### Gemini Models
* `gemini-2.5-pro` - Latest Gemini
* `gemini-2.0-flash` - Fast responses
[See Full Model Catalog →](/home/models)
## Environment Variables
Most agents support environment variables for API keys:
```bash theme={null}
# For CLI agents and some GUI agents
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
# For Claude Code specifically
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY="sk-mega-your-api-key-here"
```
Add to your shell configuration:
* Bash: `~/.bashrc`
* Zsh: `~/.zshrc`
* Fish: `~/.config/fish/config.fish`
## Common Configuration Patterns
### Pattern 1: System-Level for Personal Use
**Best for**: Personal projects, single user
```bash theme={null}
# System configuration
~/.claude/settings.json
# Environment variables
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY="sk-mega-your-key"
```
```bash theme={null}
# VSCode User Settings
# File > Preferences > Settings > Search "kilocode"
# Environment variable
export MEGALLM_API_KEY="sk-mega-your-key"
```
```bash theme={null}
# App configuration
~/.config/roocode/config.json
# Store API key in config or use env var
```
### Pattern 2: Project-Level for Teams
**Best for**: Team projects, shared configuration
```bash theme={null}
# Shared config (committed)
.claude/settings.json
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
}
}
# Personal key (not committed)
.claude/settings.local.json
{
"env": {
"ANTHROPIC_API_KEY": "your-personal-key"
}
}
# .gitignore
.claude/settings.local.json
```
```bash theme={null}
# Workspace settings (committed)
.vscode/settings.json
{
"kilocode.apiProvider": "custom",
"kilocode.customProvider": {
"name": "MegaLLM",
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}"
}
}
# Team members set their own env var
export MEGALLM_API_KEY="personal-key"
```
```bash theme={null}
# Project config (committed)
.opencode/opencode.json
{
"providers": [{
"baseURL": "https://ai.megallm.io/v1"
}]
}
# Personal key via env var
export MEGALLM_API_KEY="personal-key"
```
### Pattern 3: Multi-Model Configuration
**Best for**: Using different models for different tasks
```json theme={null}
{
"providers": [
{
"id": "megallm-gpt",
"type": "openai",
"baseURL": "https://ai.megallm.io/v1",
"models": ["gpt-5", "gpt-4o"]
},
{
"id": "megallm-claude",
"type": "anthropic",
"baseURL": "https://ai.megallm.io",
"models": ["claude-sonnet-4", "claude-opus-4"]
}
]
}
```
```json theme={null}
{
"providers": [
{
"name": "GPT for Chat",
"model": "gpt-5",
"useFor": ["chat", "completion"]
},
{
"name": "Claude for Review",
"model": "claude-sonnet-4",
"useFor": ["review", "analysis"]
}
]
}
```
## Troubleshooting
**Verify your API key:**
```bash theme={null}
# Check it starts with sk-mega-
echo $MEGALLM_API_KEY
# Test the API directly
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
https://ai.megallm.io/v1/models
```
**Common issues:**
* Missing `sk-mega-` prefix
* Extra spaces or quotes
* Wrong environment variable name
* Shell config not reloaded (run `source ~/.bashrc`)
**Check your base URL:**
For **OpenAI-compatible format** (GPT, Gemini):
```
https://ai.megallm.io/v1 (with /v1)
```
For **Anthropic format** (Claude):
```
https://ai.megallm.io (no /v1)
```
**Agent-specific endpoints:**
* Claude Code: `https://ai.megallm.io` (no /v1)
* Codex/Windsurf: `https://ai.megallm.io/v1`
* OpenCode: `https://ai.megallm.io/v1`
* Kilocode: `https://ai.megallm.io/v1`
* RooCode: `https://ai.megallm.io/v1`
* Cline: `https://ai.megallm.io` (Anthropic) or `https://ai.megallm.io/v1` (OpenAI)
**Verify model name spelling:**
Check [Models Catalog](/home/models) for exact model names:
```bash theme={null}
# Common mistakes:
"gpt5" # Missing dash
"gpt-5"
"claude-sonnet" # Missing version
"claude-sonnet-4"
"gemini-pro" # Wrong version
"gemini-2.5-pro"
```
**Check file locations:**
```bash theme={null}
# CLI agents
ls -la ~/.claude/settings.json # Claude Code
ls -la ~/.codex/config.toml # Codex
ls -la ~/.config/opencode/ # OpenCode
# Project configs
ls -la .claude/settings.json # Claude Code
ls -la .opencode/opencode.json # OpenCode
ls -la .vscode/settings.json # VSCode extensions
# Validate JSON/TOML syntax
jq . ~/.claude/settings.json # Test JSON
cat ~/.codex/config.toml | grep - # Test TOML
```
**Check permissions:**
```bash theme={null}
chmod 644 ~/.claude/settings.json
chmod 644 ~/.codex/config.toml
```
**Reload your shell config:**
```bash theme={null}
# Bash
source ~/.bashrc
# Zsh
source ~/.zshrc
# Fish
source ~/.config/fish/config.fish
# Verify
echo $MEGALLM_API_KEY
echo $ANTHROPIC_BASE_URL
```
**Check where it's defined:**
```bash theme={null}
# Search all shell configs
grep -r "MEGALLM_API_KEY" ~/.*rc ~/.config/
```
## Feature Comparison
### Interfaces & Usage
| Agent | CLI Available | Editor/GUI | Chat Interface | Multi-file Edit |
| -------------- | ----------------------------------- | ---------------------------------- | -------------------------------- | --------------------- |
| Claude Code | Primary | VSCode ext | CLI + GUI | |
| Codex/Windsurf | Primary | Editor | CLI + GUI | |
| OpenCode | Primary | Plugins | CLI + GUI | |
| Kilocode | Coming soon | VSCode only | GUI | |
| RooCode | | Standalone | GUI | |
| Cline | Coming soon | VSCode only | GUI | |
### Integrations
| Agent | Terminal Access | Git Integration | File Tree | Code Review |
| -------------- | ------------------------------------------------ | ------------------------------- | --------------------- | -------------------------------- |
| Claude Code | Native CLI | Native | | In editor |
| Codex/Windsurf | Native CLI | Native | | In editor |
| OpenCode | Native CLI | Native | | In editor |
| Kilocode | Via VSCode | VSCode | | |
| RooCode | Built-in | Built-in | | |
| Cline | VSCode term | VSCode | | |
### Configuration Flexibility
| Agent | System Config | Project Config | Env Vars | Team Sharing |
| -------------- | --------------------- | -------------------------------------------- | --------------------------------------------- | --------------------------------------------- |
| Claude Code | | | | |
| Codex/Windsurf | | | | Limited |
| OpenCode | | | | |
| Kilocode | | | | |
| RooCode | | Import | Limited | Export |
| Cline | | | | |
## Best Practices
Store API keys in environment variables, never commit them to version control
Use project-level config for shared settings, local files for personal keys
CLI for automation/CI/CD, GUI for interactive coding and visual workflows
Verify API connection with curl before configuring agents
Add sensitive files to .gitignore, use different keys for dev/prod
Keep agents updated for latest features, security patches, and bug fixes
## Next Steps
CLI with JSON config
CLI with TOML config
CLI with auto-fetch
VSCode extension
Standalone app
VSCode + autonomy
Browse all available models
Direct API integration
# RooCode Configuration
Source: https://docs.megallm.io/en/agents/roocode
Configure RooCode standalone application to use MegaLLM
RooCode is a powerful standalone AI coding assistant application with an interactive chat interface, code review capabilities, and multi-file editing. Configure it to use MegaLLM for access to multiple AI models.
## Quick Configuration
### Step-by-Step Setup
* Launch RooCode application
* Click the gear icon ⚙️ (bottom-left corner)
* Navigate to: `Settings → API Configuration`
* **Provider Type**: Select `OpenAI Compatible`
* This enables custom base URL configuration
* Allows MegaLLM integration
* **Base URL**: `https://ai.megallm.io/v1`
* **API Key**: `sk-mega-your-api-key-here`
* **Organization**: Leave empty (not required)
* Click **Save**
* **Default Model**: `gpt-5`
* **Fallback Model**: `gpt-4o` (optional)
* **Temperature**: `0.7` (balanced creativity)
* **Max Tokens**: `4096`
* **Code Completion**
* **Chat Interface**
* **Code Review**
* **Refactoring Assistant**
* **Multi-File Edit**
### Direct File Edit
**Configuration File Locations:**
```bash Windows theme={null}
%APPDATA%\RooCode\config.json
# Full path: C:\Users\YourName\AppData\Roaming\RooCode\config.json
```
```bash macOS theme={null}
~/Library/Application Support/RooCode/config.json
```
```bash Linux theme={null}
~/.config/RooCode/config.json
```
**Configuration Content:**
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-api-key-here",
"organization": "",
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096
},
"features": {
"codeCompletion": true,
"chatInterface": true,
"codeReview": true,
"refactoring": true,
"multiFileEdit": true
},
"ui": {
"theme": "dark",
"showInlineHints": true,
"autoSaveChats": true,
"fontSize": 14
}
}
```
After editing the config file, restart RooCode for changes to take effect.
### Import Pre-Made Config
**Step 1**: Create `roocode-megallm-config.json`
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "REPLACE_WITH_YOUR_KEY",
"model": "gpt-5"
},
"features": {
"codeCompletion": true,
"chatInterface": true,
"codeReview": true,
"refactoring": true
}
}
```
**Step 2**: Import in RooCode
1. Open RooCode
2. Go to: `File → Import Configuration`
3. Select your `roocode-megallm-config.json`
4. Replace `REPLACE_WITH_YOUR_KEY` with actual API key
5. Click **Apply**
6. Restart RooCode
Configuration imported successfully!
## Scenario Examples
### Scenario 1: First-Time Installation
Complete setup from downloading to first use:
1. Visit [RooCode.io](https://roocode.io) (example URL)
2. Download for your OS (Windows/macOS/Linux)
3. Install the application
4. Launch RooCode
1. Visit [MegaLLM Dashboard](https://megallm.io/dashboard)
2. Sign up or log in
3. Navigate to **API Keys** section
4. Click **Create New Key**
5. Copy the key (starts with `sk-mega-`)
In RooCode:
1. Click ⚙️ Settings icon
2. Go to **API Configuration**
3. Select **Provider**: `OpenAI Compatible`
4. Enter **Base URL**: `https://ai.megallm.io/v1`
5. Paste **API Key**: `sk-mega-your-key`
6. Click **Test Connection** (should show ✓ Success)
7. Click **Save**
1. In Settings → **Model Selection**
2. Select: `gpt-5` (recommended for general use)
3. Or browse available models
4. Set **Temperature**: `0.7` (balanced)
5. Save settings
1. Open a new chat in RooCode
2. Type: `Hello! What model are you using?`
3. Should respond with model information
4. Try: `Write a hello world function in Python`
**Expected Response:**
```python theme={null}
def hello_world():
print("Hello, World!")
if __name__ == "__main__":
hello_world()
```
***
### Scenario 2: Team Setup with Shared Configuration
Set up RooCode for an entire development team:
**Scenario:** Development team of 10 people need consistent RooCode configuration
**Step 1: Team Lead Creates Base Config**
`team-roocode-config.json`:
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "TEAM_MEMBER_REPLACES_THIS",
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096
},
"features": {
"codeCompletion": true,
"chatInterface": true,
"codeReview": true,
"refactoring": true,
"multiFileEdit": true
},
"projectSettings": {
"language": "typescript",
"framework": "react",
"codingStyle": "airbnb",
"testFramework": "jest"
},
"customPrompts": {
"review": "Review this code following our team's Airbnb style guide and best practices",
"test": "Generate Jest unit tests with at least 80% coverage",
"doc": "Generate JSDoc comments for this function",
"refactor": "Refactor this code for better maintainability and TypeScript strict mode"
},
"ui": {
"theme": "dark",
"showInlineHints": true,
"autoSaveChats": true,
"fontSize": 14
}
}
```
**Step 2: Create Setup Instructions**
`ROOCODE_SETUP.md`:
```markdown theme={null}
# RooCode Team Setup Guide
## Prerequisites
- RooCode application installed ([Download](https://roocode.io))
- MegaLLM API key ([Get yours](https://megallm.io/dashboard))
## Setup Steps
### 1. Get Your Personal API Key
1. Go to https://megallm.io/dashboard
2. Navigate to **API Keys**
3. Click **Create New Key**
4. Copy your key (starts with `sk-mega-`)
### 2. Import Team Configuration
1. Download `team-roocode-config.json` from this repo
2. Open RooCode
3. Go to: `File → Import Configuration`
4. Select `team-roocode-config.json`
### 3. Add Your Personal API Key
1. After import, go to: `Settings → API Configuration`
2. Replace `TEAM_MEMBER_REPLACES_THIS` with your actual key
3. Click **Test Connection** (should show ✓ Success)
4. Click **Save**
### 4. Restart RooCode
Close and reopen RooCode to apply all settings.
## Verification
1. Open a new chat
2. Type: `Generate a React component for a login form`
3. Should generate TypeScript code following Airbnb style
## Troubleshooting
- **Connection failed**: Check API key is correct
- **Wrong code style**: Verify configuration imported correctly
- **Model errors**: Ensure using `gpt-5` or other supported models
## Support
Contact team lead or check [MegaLLM Docs](https://docs.megallm.io)
```
**Step 3: Distribute to Team**
```bash theme={null}
# Team lead commits to repo
git add team-roocode-config.json ROOCODE_SETUP.md
git commit -m "Add RooCode team configuration"
git push
# Team members clone and setup
git pull
# Follow instructions in ROOCODE_SETUP.md
```
**Result:** Entire team uses consistent:
* Same AI model (gpt-5)
* Same temperature and settings
* Same custom prompts
* Same coding standards
* Personal API keys (not shared)
***
### Scenario 3: Project-Specific Configurations
Use different RooCode configurations for different project types:
**Python Data Science Project:**
`roocode-datascience.json`:
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}",
"model": "claude-opus-4-1-20250805",
"temperature": 0.5
},
"projectSettings": {
"language": "python",
"libraries": ["numpy", "pandas", "scikit-learn", "matplotlib"],
"codingStyle": "pep8"
},
"customPrompts": {
"analyze": "Analyze this data processing code for efficiency and correctness",
"optimize": "Optimize this pandas operation for large datasets",
"visualize": "Suggest matplotlib visualization for this data",
"doc": "Generate numpy-style docstring"
}
}
```
**React Web Application:**
`roocode-webapp.json`:
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}",
"model": "gpt-5",
"temperature": 0.7
},
"projectSettings": {
"language": "typescript",
"framework": "react",
"stateManagement": "zustand",
"styling": "tailwind"
},
"customPrompts": {
"component": "Generate a React functional component with TypeScript and Tailwind",
"test": "Write React Testing Library tests for this component",
"hook": "Create a custom React hook for this functionality",
"api": "Generate API integration with tanstack-query"
}
}
```
**Systems Programming (Rust):**
`roocode-systems.json`:
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "${env:MEGALLM_API_KEY}",
"model": "gemini-2.5-pro",
"temperature": 0.3
},
"projectSettings": {
"language": "rust",
"codingStyle": "rust-fmt"
},
"customPrompts": {
"review": "Review this Rust code for memory safety and performance",
"optimize": "Suggest optimizations for this performance-critical code",
"test": "Generate comprehensive Rust tests",
"unsafe": "Analyze this unsafe block for correctness"
}
}
```
**Switching Between Projects:**
```bash macOS/Linux theme={null}
# Create aliases in ~/.bashrc or ~/.zshrc
alias roocode-ds='cp ~/configs/roocode-datascience.json ~/.config/RooCode/config.json && roocode'
alias roocode-web='cp ~/configs/roocode-webapp.json ~/.config/RooCode/config.json && roocode'
alias roocode-sys='cp ~/configs/roocode-systems.json ~/.config/RooCode/config.json && roocode'
```
```powershell Windows theme={null}
# Create functions in PowerShell profile
function RooCode-DataScience {
Copy-Item "$HOME\configs\roocode-datascience.json" "$env:APPDATA\RooCode\config.json"
Start-Process "roocode"
}
function RooCode-Web {
Copy-Item "$HOME\configs\roocode-webapp.json" "$env:APPDATA\RooCode\config.json"
Start-Process "roocode"
}
```
***
### Scenario 4: Migration from Cursor/Other AI Tools
Switching from another AI coding tool to RooCode with MegaLLM:
**Current Setup: Using Cursor with OpenAI**
**Why Switch to RooCode + MegaLLM:**
* Access to multiple models (GPT, Claude, Gemini) with one key
* Standalone app (not tied to specific editor)
* Better pricing and no seat limits
* More customization options
**Migration Steps:**
From Cursor (if possible):
* Note your preferred models
* Save any custom prompts
* Document keyboard shortcuts you use
Download and install RooCode for your OS
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-megallm-key",
"model": "gpt-5"
}
}
```
Add your frequently used prompts:
```json theme={null}
{
"customPrompts": {
"explain": "Explain this code in detail",
"fix": "Find and fix bugs in this code",
"improve": "Suggest improvements for this code"
}
}
```
Compare side-by-side:
1. Open same code in both tools
2. Ask same questions
3. Compare code generation quality
4. Evaluate response speed
**Comparison Table:**
| Feature | Cursor | RooCode + MegaLLM |
| ---------------------- | --------------------- | ----------------------------- |
| **Editor Integration** | VS Code fork | Standalone + Any Editor |
| **Models Available** | GPT-4, GPT-3.5 | GPT, Claude, Gemini, and more |
| **API Key** | OpenAI only | One key for all models |
| **Pricing** | Per-seat subscription | Pay-per-use |
| **Customization** | Limited | Extensive |
| **Offline Mode** | No | No |
***
### Scenario 5: Multi-Model Workflow
Use different models for different types of tasks:
**Configuration with Model Profiles:**
```json theme={null}
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-api-key-here",
"model": "gpt-5"
},
"modelProfiles": {
"fast": {
"model": "gpt-4o-mini",
"temperature": 0.5,
"maxTokens": 1000,
"description": "Fast completions for simple tasks"
},
"balanced": {
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096,
"description": "Balanced quality and speed"
},
"quality": {
"model": "claude-opus-4-1-20250805",
"temperature": 0.3,
"maxTokens": 8192,
"description": "Highest quality for complex tasks"
},
"creative": {
"model": "gpt-5",
"temperature": 0.9,
"maxTokens": 4096,
"description": "Creative documentation and naming"
},
"analysis": {
"model": "claude-sonnet-4",
"temperature": 0.4,
"maxTokens": 6000,
"description": "Code review and analysis"
}
}
}
```
**Workflow Example:**
```plaintext theme={null}
Morning: Rapid Prototyping
→ Switch to "fast" profile (gpt-4o-mini)
→ Quick code generation
→ Fast iterations
Afternoon: Production Code
→ Switch to "quality" profile (claude-opus-4)
→ Generate production-ready code
→ Comprehensive error handling
Code Review Time
→ Switch to "analysis" profile (claude-sonnet-4)
→ Detailed code reviews
→ Security analysis
Documentation
→ Switch to "creative" profile (gpt-5, high temp)
→ Generate engaging docs
→ Creative naming suggestions
```
**Switching Profiles in RooCode:**
1. Click model dropdown (top bar)
2. Select profile: Fast / Balanced / Quality / Creative / Analysis
3. Profile applied to current session
***
### Scenario 6: CI/CD Integration
Use RooCode API for automated code reviews in CI/CD:
**GitHub Actions Example:**
`.github/workflows/roocode-review.yml`:
```yaml theme={null}
name: AI Code Review with RooCode
on:
pull_request:
types: [opened, synchronize]
jobs:
ai-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup RooCode CLI
run: |
curl -O https://roocode.io/cli/install.sh
bash install.sh
- name: Configure MegaLLM
env:
MEGALLM_API_KEY: ${{ secrets.MEGALLM_API_KEY }}
run: |
mkdir -p ~/.config/RooCode
cat > ~/.config/RooCode/config.json << EOF
{
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "$MEGALLM_API_KEY",
"model": "claude-sonnet-4"
}
}
EOF
- name: Run AI Code Review
run: |
roocode review \
--files "$(git diff --name-only origin/main...HEAD)" \
--output review.md
- name: Post Review Comment
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const review = fs.readFileSync('review.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## AI Code Review\n\n${review}`
});
```
## Configuration Options
### Complete Reference
```json theme={null}
{
// API Configuration
"provider": "openai-compatible",
"api": {
"baseURL": "https://ai.megallm.io/v1",
"apiKey": "sk-mega-your-api-key-here",
"organization": "",
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096,
"topP": 0.9,
"frequencyPenalty": 0.0,
"presencePenalty": 0.0,
"timeout": 30000
},
// Feature Toggles
"features": {
"codeCompletion": true,
"chatInterface": true,
"codeReview": true,
"refactoring": true,
"multiFileEdit": true,
"contextAwareness": true,
"autoSave": true
},
// Project Settings
"projectSettings": {
"language": "typescript",
"framework": "react",
"codingStyle": "airbnb",
"testFramework": "jest",
"linter": "eslint"
},
// Custom Prompts
"customPrompts": {
"review": "Review code for bugs and best practices",
"test": "Generate comprehensive unit tests",
"doc": "Generate detailed documentation",
"refactor": "Refactor for better maintainability",
"optimize": "Optimize for performance",
"secure": "Analyze for security vulnerabilities"
},
// UI Preferences
"ui": {
"theme": "dark",
"fontSize": 14,
"fontFamily": "Fira Code",
"showInlineHints": true,
"showLineNumbers": true,
"autoSaveChats": true,
"chatPosition": "right",
"enableAnimations": true
},
// Keyboard Shortcuts
"shortcuts": {
"newChat": "Cmd+N",
"sendMessage": "Cmd+Enter",
"clearChat": "Cmd+K",
"switchModel": "Cmd+M",
"openSettings": "Cmd+,"
},
// Advanced
"advanced": {
"logLevel": "info",
"cacheResponses": true,
"maxChatHistory": 100,
"contextWindowSize": 8000,
"streamResponses": true
}
}
```
### Model Selection Guide
| Task | Recommended Model | Why |
| --------------------- | -------------------------- | ------------------------ |
| **Quick Completions** | `gpt-4o-mini` | Fastest, cost-effective |
| **Code Review** | `claude-sonnet-4` | Excellent analysis |
| **Complex Logic** | `claude-opus-4-1-20250805` | Superior reasoning |
| **Web Development** | `gpt-5` | Best for JS/TS/React |
| **Documentation** | `gpt-5` | Clear, engaging writing |
| **Data Science** | `claude-sonnet-4` | Strong with pandas/numpy |
| **Systems Code** | `gemini-2.5-pro` | Precise, mathematical |
## Verification
### Test 1: Chat Functionality
1. Open RooCode
2. Click **New Chat**
3. Type: `Write a function to sort an array of objects by a property`
4. Should receive working code
**Expected Response:**
```javascript theme={null}
function sortByProperty(array, property) {
return array.sort((a, b) => {
if (a[property] < b[property]) return -1;
if (a[property] > b[property]) return 1;
return 0;
});
}
```
### Test 2: Code Review
1. Paste this code:
```python theme={null}
def divide(a, b):
return a / b
```
2. Click **Review Code** button
3. Should identify: Missing zero division check
### Test 3: Multi-File Edit
1. Open multi-file view
2. Request: "Add error handling to all API calls"
3. Should show changes across multiple files
4. Review and apply changes
### Test 4: Connection Status
Check status indicator (top-right):
* 🟢 Green = Connected to MegaLLM
* 🟡 Yellow = Connecting...
* 🔴 Red = Connection failed
Click status to see:
* Model: `gpt-5`
* Endpoint: `https://ai.megallm.io/v1`
* Token usage: Current session stats
## Troubleshooting
**Symptoms:**
* "Failed to connect to API"
* Requests timeout after 30s
* Chat messages don't send
**Solutions:**
1. **Verify base URL is correct:**
```
https://ai.megallm.io/v1 Correct
https://ai.megallm.io Missing /v1
https://api.openai.com/v1 Wrong endpoint
```
2. **Test connectivity:**
```bash theme={null}
# Test connection
curl -I https://ai.megallm.io/v1/models
# Should return: HTTP/2 200
```
3. **Check firewall:**
* Allow RooCode through firewall
* Check corporate proxy settings
* Verify no VPN blocking
4. **Increase timeout:**
```json theme={null}
{
"api": {
"timeout": 60000 // Increase to 60 seconds
}
}
```
5. **Test API key manually:**
```bash theme={null}
curl -X POST https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"test"}]}'
```
**Symptoms:**
* "Invalid API key" error
* 401 Unauthorized
* Authentication failed
**Solutions:**
1. **Verify key format:**
* Must start with `sk-mega-`
* Should be 60+ characters
* No extra spaces or line breaks
* No quotes in config file around the actual key
2. **Check key is active:**
* Login to [Dashboard](https://megallm.io/dashboard)
* Go to **API Keys**
* Verify key shows as "Active"
* Check it's not revoked or expired
3. **Re-enter the key:**
* Copy key from dashboard
* Delete old key from RooCode settings
* Paste new key
* Save and restart RooCode
4. **Test key directly:**
```bash theme={null}
curl -H "Authorization: Bearer sk-mega-your-actual-key" \
https://ai.megallm.io/v1/models
# Should return JSON list of models
```
**Symptoms:**
* Irrelevant code suggestions
* Wrong programming language
* Incomplete responses
**Solutions:**
1. **Provide better context:**
* Set project language in settings
* Add framework information
* Use custom prompts
2. **Adjust temperature:**
```json theme={null}
{
"api": {
"temperature": 0.3 // Lower = more focused (0.2-0.4 for code)
}
}
```
3. **Try different model:**
```json theme={null}
{
"api": {
"model": "claude-sonnet-4" // Better for analysis
}
}
```
4. **Increase max tokens:**
```json theme={null}
{
"api": {
"maxTokens": 8192 // Longer, more complete responses
}
}
```
5. **Use specific prompts:**
```json theme={null}
{
"customPrompts": {
"generate": "Generate production-ready TypeScript code following best practices"
}
}
```
**Symptoms:**
* Settings revert after restart
* Changes don't apply
* Using default configuration
**Solutions:**
1. **Check config file location:**
```bash Windows theme={null}
# Should be:
C:\Users\YourName\AppData\Roaming\RooCode\config.json
# Not:
C:\Program Files\RooCode\config.json # Wrong!
```
```bash macOS theme={null}
# Should be:
~/Library/Application Support/RooCode/config.json
# Not:
/Applications/RooCode.app/config.json # Wrong!
```
```bash Linux theme={null}
# Should be:
~/.config/RooCode/config.json
# Not:
/etc/roocode/config.json # Wrong!
```
2. **Validate JSON syntax:**
```bash theme={null}
# Check for errors
cat config.json | python3 -m json.tool
# Should show formatted JSON or error
```
3. **Check file permissions:**
```bash theme={null}
# Linux/macOS
chmod 644 ~/.config/RooCode/config.json
# Windows: Ensure user has read/write access
```
4. **Reset to defaults:**
* Backup current config
* Delete config.json
* Restart RooCode (creates new config)
* Reapply settings via UI
**Symptoms:**
* RooCode crashes on startup
* Freezes during long responses
* Unresponsive UI
**Solutions:**
1. **Check system resources:**
* Close other applications
* Ensure 4GB+ RAM available
* Check CPU usage
2. **Reduce max tokens:**
```json theme={null}
{
"api": {
"maxTokens": 2048 // Reduce from 8192
}
}
```
3. **Disable streaming:**
```json theme={null}
{
"advanced": {
"streamResponses": false
}
}
```
4. **Clear cache:**
```bash theme={null}
# Windows
rmdir /s "%APPDATA%\RooCode\cache"
# macOS/Linux
rm -rf ~/Library/Application\ Support/RooCode/cache
rm -rf ~/.config/RooCode/cache
```
5. **Reinstall RooCode:**
* Backup config.json
* Uninstall RooCode
* Download latest version
* Install and restore config
## Best Practices
Create separate configs for different project types
Set up profiles for different tasks: fast, quality, creative
Define reusable prompts for common tasks
Check [Dashboard](https://megallm.io/dashboard) regularly for token usage
Keep backups of working configurations
Use environment variables for API keys in shared configs
## Advanced Tips
### Environment Variables
RooCode supports environment variable substitution:
```json theme={null}
{
"api": {
"apiKey": "${env:MEGALLM_API_KEY}",
"baseURL": "${env:MEGALLM_BASE_URL}"
}
}
```
Set variables:
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-key"
export MEGALLM_BASE_URL="https://ai.megallm.io/v1"
```
### Context Management
Improve responses with better context:
```json theme={null}
{
"advanced": {
"contextWindowSize": 16000,
"includeProjectStructure": true,
"includeOpenFiles": true,
"maxRelevantFiles": 10
}
}
```
### Response Caching
Save API costs with intelligent caching:
```json theme={null}
{
"advanced": {
"cacheResponses": true,
"cacheExpiry": 3600, // seconds
"maxCacheSize": 100 // MB
}
}
```
## Next Steps
Configure Kilocode VSCode extension
Configure Cline VSCode extension
Browse all available models
View all CLI and GUI agents
Explore the API docs
Join for support
# Function Calling
Source: https://docs.megallm.io/en/api-reference/endpoint/function-calling
Enable AI models to interact with external tools, APIs, and functions for building intelligent agents and automation workflows.
## Overview
Function calling (also known as tool use) allows AI models to:
* **Call external APIs** - Fetch real-time data
* **Execute functions** - Perform calculations or operations
* **Access databases** - Query and retrieve information
* **Interact with systems** - Control external services
* **Chain operations** - Build complex multi-step workflows
**Parallel Function Calling**: MegaLLM supports executing multiple functions simultaneously for improved performance.
## How It Works
Specify available functions with JSON Schema descriptions
Model determines when and how to use tools based on context
Your application executes the requested functions
Send function results back to the AI
Model incorporates results into final response
## Tool Definition Format
### OpenAI Format
```json theme={null}
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}
}
```
### Anthropic Format
```json theme={null}
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
```
## Request Parameters
### OpenAI Format
| Parameter | Type | Description |
| ------------- | ------------- | --------------------------------------------------------------------- |
| `tools` | array | Array of tool definitions |
| `tool_choice` | string/object | Controls tool usage: `auto`, `required`, `none`, or specific function |
### Tool Choice Options
```python theme={null}
# Let AI decide (default)
tool_choice="auto"
# Force AI to call a function
tool_choice="required"
# Prevent function calling
tool_choice="none"
# Force specific function
tool_choice={
"type": "function",
"function": {"name": "get_weather"}
}
```
## Examples
```python theme={null}
from openai import OpenAI
import json
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Define your function
def get_weather(location: str, unit: str = "celsius"):
"""Get current weather (simulated)"""
return {
"location": location,
"temperature": 22,
"unit": unit,
"condition": "sunny"
}
# Define tools for AI
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather in a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
]
# Initial request
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "What's the weather in London?"}
],
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
# Check if AI wants to call a function
if message.tool_calls:
# Execute the function
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
if function_name == "get_weather":
result = get_weather(**function_args)
# Send result back to AI
follow_up = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "What's the weather in London?"},
message,
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
}
]
)
print(follow_up.choices[0].message.content)
```
```javascript theme={null}
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY,
});
// Define function
function getWeather(location, unit = 'celsius') {
return {
location,
temperature: 22,
unit,
condition: 'sunny'
};
}
// Define tools
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current weather in a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City and state, e.g. San Francisco, CA'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit']
}
},
required: ['location']
}
}
}
];
// Initial request
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: "What's the weather in London?" }
],
tools,
tool_choice: 'auto'
});
const message = response.choices[0].message;
if (message.tool_calls) {
const messages = [
{ role: 'user', content: "What's the weather in London?" },
message
];
for (const toolCall of message.tool_calls) {
const functionName = toolCall.function.name;
const functionArgs = JSON.parse(toolCall.function.arguments);
if (functionName === 'get_weather') {
const result = getWeather(functionArgs.location, functionArgs.unit);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result)
});
}
}
const followUp = await openai.chat.completions.create({
model: 'gpt-4',
messages
});
console.log(followUp.choices[0].message.content);
}
```
```typescript theme={null}
import OpenAI from 'openai';
interface WeatherParams {
location: string;
unit?: 'celsius' | 'fahrenheit';
}
interface WeatherResult {
location: string;
temperature: number;
unit: string;
condition: string;
}
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY!,
});
// Type-safe function
async function getWeather({ location, unit = 'celsius' }: WeatherParams): Promise {
return {
location,
temperature: 22,
unit,
condition: 'sunny'
};
}
// Function registry
const functionRegistry = {
get_weather: getWeather
} as const;
// Tool definitions
const tools: OpenAI.Chat.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current weather in a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City and state, e.g. San Francisco, CA'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit']
}
},
required: ['location']
}
}
}
];
async function handleToolCalls(message: OpenAI.Chat.ChatCompletionMessage) {
if (!message.tool_calls) return null;
const toolResults = await Promise.all(
message.tool_calls.map(async (toolCall) => {
const fn = functionRegistry[toolCall.function.name as keyof typeof functionRegistry];
if (!fn) throw new Error(`Unknown function: ${toolCall.function.name}`);
const args = JSON.parse(toolCall.function.arguments);
const result = await fn(args);
return {
role: 'tool' as const,
tool_call_id: toolCall.id,
content: JSON.stringify(result)
};
})
);
return toolResults;
}
// Usage
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: "What's the weather in London?" }],
tools,
tool_choice: 'auto'
});
const toolResults = await handleToolCalls(response.choices[0].message);
if (toolResults) {
const followUp = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: "What's the weather in London?" },
response.choices[0].message,
...toolResults
]
});
console.log(followUp.choices[0].message.content);
}
```
## Parallel Function Calling
Execute multiple functions simultaneously:
```python theme={null}
# AI can call multiple functions at once
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "Compare weather in London, Paris, and Tokyo"}
],
tools=tools
)
# Process multiple tool calls in parallel
if response.choices[0].message.tool_calls:
import asyncio
async def execute_tool(tool_call):
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
result = await async_function_registry[function_name](**function_args)
return {
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
}
# Execute all functions in parallel
tool_results = await asyncio.gather(
*[execute_tool(tc) for tc in response.choices[0].message.tool_calls]
)
```
## Advanced Patterns
### Function Chaining
Build complex workflows:
```python theme={null}
tools = [
{
"type": "function",
"function": {
"name": "search_products",
"description": "Search for products",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"}
},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
"name": "check_inventory",
"description": "Check product inventory",
"parameters": {
"type": "object",
"properties": {
"product_id": {"type": "string"}
},
"required": ["product_id"]
}
}
},
{
"type": "function",
"function": {
"name": "place_order",
"description": "Place an order",
"parameters": {
"type": "object",
"properties": {
"product_id": {"type": "string"},
"quantity": {"type": "integer"}
},
"required": ["product_id", "quantity"]
}
}
}
]
# AI will chain functions to complete complex tasks
# Example: "Find blue t-shirts and order 2 if in stock"
# 1. search_products(query="blue t-shirt")
# 2. check_inventory(product_id="...")
# 3. place_order(product_id="...", quantity=2)
```
### Error Handling
```python theme={null}
def safe_function_call(function_name, arguments):
try:
result = function_registry[function_name](**arguments)
return {"success": True, "result": result}
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__
}
# In tool response
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
result = safe_function_call(function_name, function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
```
### Streaming with Functions
```python theme={null}
stream = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
stream=True
)
function_call = None
for chunk in stream:
delta = chunk.choices[0].delta
if delta.tool_calls:
if function_call is None:
function_call = {"id": "", "name": "", "arguments": ""}
tool_call = delta.tool_calls[0]
if tool_call.id:
function_call["id"] = tool_call.id
if tool_call.function.name:
function_call["name"] = tool_call.function.name
if tool_call.function.arguments:
function_call["arguments"] += tool_call.function.arguments
if chunk.choices[0].finish_reason == "tool_calls":
# Execute function
result = execute_function(function_call)
```
## Real-World Use Cases
### Database Query Assistant
```python theme={null}
{
"type": "function",
"function": {
"name": "execute_sql",
"description": "Execute a SQL query",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"database": {
"type": "string",
"enum": ["users", "products", "orders"]
}
},
"required": ["query", "database"]
}
}
}
```
### API Integration Agent
```python theme={null}
{
"type": "function",
"function": {
"name": "call_api",
"description": "Make an HTTP API call",
"parameters": {
"type": "object",
"properties": {
"method": {
"type": "string",
"enum": ["GET", "POST", "PUT", "DELETE"]
},
"url": {"type": "string"},
"headers": {"type": "object"},
"body": {"type": "object"}
},
"required": ["method", "url"]
}
}
}
```
### File System Operations
```python theme={null}
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read file contents",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
}
}
}
```
## Best Practices
Write clear, detailed function descriptions. The AI uses these to decide when and how to call functions.
1. **Clear descriptions** - Be specific about what each function does
2. **Validate inputs** - Always validate function arguments before execution
3. **Handle errors gracefully** - Return error information to the AI
4. **Use type hints** - Leverage TypeScript/Python types for safety
5. **Rate limit** - Implement rate limiting for external API calls
6. **Security** - Validate and sanitize all function inputs
7. **Async execution** - Use async/await for better performance
## Response Format
### Tool Call Object (OpenAI)
```json theme={null}
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\": \"London\", \"unit\": \"celsius\"}"
}
}
```
### Tool Response Format
```json theme={null}
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"temperature\": 22, \"condition\": \"sunny\"}"
}
```
## Model Support
Function calling is supported by these models:
* **OpenAI**: gpt-4, gpt-4-turbo, gpt-3.5-turbo
* **Anthropic**: claude-3.5-sonnet, claude-opus-4, claude-sonnet-4
* **Others**: Check [Models page](/api-reference/get-models) for full list
## Related
* [Chat Completions API](/api-reference/post-chat-completions) - OpenAI-compatible function calling
* [Messages API](/api-reference/post-v1-messages) - Anthropic-compatible tool use
* [Streaming](/api-reference/endpoint/streaming) - Function calling with streaming
* [Models](/api-reference/get-models) - Model capabilities and features
# Streaming
Source: https://docs.megallm.io/en/api-reference/endpoint/streaming
Stream AI responses in real-time using Server-Sent Events (SSE) for improved user experience in interactive applications.
## Overview
Streaming allows you to receive response tokens as they're generated, rather than waiting for the complete response. This is perfect for:
* **Chatbots** - Display responses as they're typed
* **Live assistants** - Show progress in real-time
* **Long responses** - Start displaying content immediately
* **Better UX** - Reduce perceived latency
## How It Works
Set `stream: true` in your request
Get response tokens incrementally via SSE
Parse `data:` events containing JSON chunks
Watch for `[DONE]` signal to know when complete
## Endpoints
Streaming works with both API formats:
```
POST https://ai.megallm.io/v1/chat/completions
POST https://ai.megallm.io/v1/messages
```
Both endpoints support the `stream: true` parameter.
## Request Format
### OpenAI Format
```json theme={null}
{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Tell me a story"}
],
"stream": true
}
```
### Anthropic Format
```json theme={null}
{
"model": "claude-3.5-sonnet",
"max_tokens": 500,
"messages": [
{"role": "user", "content": "Tell me a story"}
],
"stream": true
}
```
## Response Format
### Event Stream Structure
Responses are sent as Server-Sent Events:
```
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1677858242,"model":"gpt-4","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1677858242,"model":"gpt-4","choices":[{"index":0,"delta":{"content":"Once"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1677858242,"model":"gpt-4","choices":[{"index":0,"delta":{"content":" upon"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1677858242,"model":"gpt-4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
```
### Stream Lifecycle
1. **Initial chunk** - Contains role:
```json theme={null}
{"choices": [{"delta": {"role": "assistant"}}]}
```
2. **Content chunks** - Incremental text:
```json theme={null}
{"choices": [{"delta": {"content": "Hello"}}]}
{"choices": [{"delta": {"content": " world"}}]}
```
3. **Final chunk** - Includes finish\_reason:
```json theme={null}
{"choices": [{"delta": {}, "finish_reason": "stop"}]}
```
4. **Stream end**:
```
data: [DONE]
```
## Implementation Examples
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Create streaming completion
stream = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "Tell me a story"}
],
stream=True
)
# Process the stream
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
```
### Async Python
```python theme={null}
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
async def stream_chat():
stream = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
async for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
asyncio.run(stream_chat())
```
```javascript theme={null}
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY,
});
async function streamChat() {
const stream = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
}
streamChat();
```
```javascript theme={null}
async function streamChatInBrowser() {
const response = await fetch('https://ai.megallm.io/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
try {
const json = JSON.parse(data);
const content = json.choices[0]?.delta?.content || '';
// Display content to user
document.getElementById('output').innerHTML += content;
} catch (e) {
console.error('Parse error:', e);
}
}
}
}
}
```
```jsx theme={null}
import { useState } from 'react';
function StreamingChat() {
const [messages, setMessages] = useState([]);
const [streaming, setStreaming] = useState(false);
const [currentResponse, setCurrentResponse] = useState('');
const sendMessage = async (content) => {
setStreaming(true);
setCurrentResponse('');
const response = await fetch('https://ai.megallm.io/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.REACT_APP_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4',
messages: [...messages, { role: 'user', content }],
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulated = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
setMessages(prev => [...prev,
{ role: 'user', content },
{ role: 'assistant', content: accumulated }
]);
setStreaming(false);
return;
}
try {
const json = JSON.parse(data);
const content = json.choices[0]?.delta?.content || '';
accumulated += content;
setCurrentResponse(accumulated);
} catch (e) {
// Handle parse errors
}
}
}
}
};
return (
{messages.map((msg, i) => (
{msg.content}
))}
{streaming && (
{currentResponse}
▊
)}
);
}
```
```bash theme={null}
curl -N https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Tell me a story"}],
"stream": true
}'
```
Process with jq:
```bash theme={null}
curl -N https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Tell me a joke"}],
"stream": true
}' | while read -r line; do
if [[ $line == data:* ]]; then
json="${line:6}"
if [[ $json != "[DONE]" ]]; then
echo -n $(echo "$json" | jq -r '.choices[0].delta.content // ""')
fi
fi
done
```
## Advanced Features
### Function Calling with Streaming
```python theme={null}
stream = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
stream=True
)
function_call = {"name": "", "arguments": ""}
for chunk in stream:
delta = chunk.choices[0].delta
if delta.tool_calls:
tool_call = delta.tool_calls[0]
if tool_call.function.name:
function_call["name"] = tool_call.function.name
if tool_call.function.arguments:
function_call["arguments"] += tool_call.function.arguments
elif delta.content:
print(delta.content, end="", flush=True)
# Execute function when complete
if function_call["name"]:
result = execute_function(function_call)
```
### Progress Tracking
```python theme={null}
import time
class StreamProgress:
def __init__(self):
self.tokens = 0
self.chunks = 0
self.start_time = time.time()
def update(self, chunk):
self.chunks += 1
if chunk.choices[0].delta.content:
# Approximate token count
self.tokens += len(chunk.choices[0].delta.content.split())
def get_stats(self):
elapsed = time.time() - self.start_time
return {
"chunks": self.chunks,
"tokens": self.tokens,
"time": elapsed,
"tokens_per_second": self.tokens / elapsed if elapsed > 0 else 0
}
progress = StreamProgress()
for chunk in stream:
progress.update(chunk)
# Process chunk...
print(f"\nStats: {progress.get_stats()}")
```
### Buffering for Performance
```javascript theme={null}
class StreamBuffer {
constructor(onFlush, bufferSize = 10, flushInterval = 100) {
this.buffer = [];
this.onFlush = onFlush;
this.bufferSize = bufferSize;
this.flushInterval = flushInterval;
this.timer = null;
}
add(chunk) {
this.buffer.push(chunk);
if (this.buffer.length >= this.bufferSize) {
this.flush();
} else if (!this.timer) {
this.timer = setTimeout(() => this.flush(), this.flushInterval);
}
}
flush() {
if (this.buffer.length > 0) {
this.onFlush(this.buffer.join(''));
this.buffer = [];
}
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
}
// Usage
const buffer = new StreamBuffer((text) => {
document.getElementById('output').innerHTML += text;
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
buffer.add(content);
}
buffer.flush(); // Final flush
```
## Error Handling
Streaming connections can fail mid-stream. Always implement retry logic.
```python theme={null}
import time
def stream_with_retry(client, messages, max_retries=3):
for attempt in range(max_retries):
try:
stream = client.chat.completions.create(
model="gpt-4",
messages=messages,
stream=True
)
full_response = ""
for chunk in stream:
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response += content
yield content
return # Success
except Exception as e:
if attempt < max_retries - 1:
wait_time = 2 ** attempt # Exponential backoff
print(f"Stream interrupted, retrying in {wait_time}s...")
time.sleep(wait_time)
# Continue from partial response
messages.append({"role": "assistant", "content": full_response})
messages.append({"role": "user", "content": "continue"})
else:
raise e
```
## Best Practices
1. **Buffer for UI updates** - Don't update DOM for every chunk (batching improves performance)
2. **Show loading indicators** - Display typing indicators during streaming
3. **Implement timeouts** - Set reasonable timeouts for connections
4. **Handle interruptions** - Use retry logic with exponential backoff
5. **Clean up resources** - Always close streams properly
6. **Test error scenarios** - Ensure your app handles network failures gracefully
## Performance Tips
Buffer small chunks together before updating the UI to avoid excessive DOM updates.
* Use `flush=True` in Python's print for immediate output
* Implement debouncing for frequent UI updates
* Consider virtualization for long responses
* Use Web Workers for parsing in browsers
* Monitor memory usage for long streams
## Related
* [Chat Completions API](/api-reference/post-chat-completions) - OpenAI-compatible streaming API
* [Messages API](/api-reference/post-v1-messages) - Anthropic-compatible streaming API
* [Function Calling](/api-reference/endpoint/function-calling) - Using functions with streaming
* [Authentication](/dev-docs/getting-started/authentication) - API authentication methods
# API Reference
Source: https://docs.megallm.io/en/api-reference/introduction
Welcome to the MegaLLM API reference documentation. MegaLLM provides unified access to 70+ AI models through OpenAI-compatible and Anthropic-compatible REST APIs.
## Base URLs
MegaLLM supports two API formats:
```
https://ai.megallm.io/v1
```
Compatible with OpenAI SDKs and tools
```
https://ai.megallm.io
```
Compatible with Anthropic Claude SDKs
## Authentication
All API endpoints require authentication using one of these methods:
### Bearer Token (Recommended)
```http theme={null}
Authorization: Bearer YOUR_API_KEY
```
### API Key Header (Anthropic Format)
```http theme={null}
x-api-key: YOUR_API_KEY
anthropic-version: 2023-06-01
```
Get your API key from the [MegaLLM Dashboard](https://megallm.io/dashboard/overview).
## Core Endpoints
OpenAI-compatible chat API with streaming, function calling, and vision support
Anthropic-compatible API with extended thinking, tools, and prompt caching
List all 70+ AI models with capabilities, pricing, and context windows
## Advanced Features
Real-time streaming responses with Server-Sent Events
Enable AI to interact with external tools and APIs
Comprehensive authentication methods and security best practices
## Quick Start
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
```
```javascript theme={null}
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY,
});
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
});
```
```bash theme={null}
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello!"}]
}'
```
## Rate Limits
Rate limits vary by plan tier:
| Tier | Requests/min | Tokens/min | Concurrent |
| ---------- | ------------ | ---------- | ---------- |
| Basic | 60 | 90K | 10 |
| Pro | 300 | 450K | 40 |
| Enterprise | Custom | Custom | Custom |
## SDK Support
MegaLLM is compatible with popular AI SDKs:
* **Python**: `openai`, `anthropic`, `langchain`
* **JavaScript/TypeScript**: `openai`, `@anthropic-ai/sdk`
* **Go**: `go-openai`
* **Ruby**: `anthropic-rb`
* **Rust**: `async-openai`
* **Java**: `openai-java`
* **C#**: `OpenAI-DotNet`
## Need Help?
Comprehensive guides and tutorials
Browse all 70+ available models
# Claude Code Configuration
Source: https://docs.megallm.io/en/cli/claude-config
Configure Claude Code to use MegaLLM
Claude Code uses JSON configuration files for settings and API key approvals. Configuration can be at the system level (global) or project level (local).
## Configuration Files
### System-Level Configuration
**Settings File**: `~/.claude/settings.json`
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
}
}
```
**API Key Approvals**: `~/.claude.json`
```json theme={null}
{
"customApiKeyResponses": {
"approved": ["last-20-chars-of-key"],
"rejected": []
}
}
```
The API key approval file stores the last 20 characters of your API key to remember your approval decision when Claude Code prompts you.
### Project-Level Configuration
**Settings File**: `./.claude/settings.json`
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
}
}
```
**Local Settings (Gitignored)**: `./.claude/settings.local.json`
```json theme={null}
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-your-personal-key"
}
}
```
Use `settings.local.json` to keep your personal API key out of version control while sharing base configuration with your team.
## Environment Variables
The CLI sets these environment variables in your shell configuration:
```bash theme={null}
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY="sk-mega-your-api-key-here"
```
These are added to:
* `~/.bashrc` (bash)
* `~/.zshrc` (zsh)
* `~/.config/fish/config.fish` (fish)
* PowerShell profile (Windows)
### Verify Environment Variables
```bash theme={null}
echo $ANTHROPIC_BASE_URL
# Output: https://ai.megallm.io
echo $ANTHROPIC_API_KEY
# Output: sk-mega-your-api-key-here
```
## Configuration Priority
Claude Code loads configuration in this order (highest to lowest priority):
`ANTHROPIC_BASE_URL` and `ANTHROPIC_API_KEY` from your shell
`./.claude/settings.local.json` in current directory
`./.claude/settings.json` in current directory
`~/.claude/settings.json` in home directory
## Statusline Configuration (Optional)
Claude Code supports an enhanced statusline for better terminal UI:
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-your-api-key-here"
},
"statusline": {
"enabled": true,
"components": {
"directory": true,
"gitBranch": true,
"model": true,
"contextUsage": true,
"cost": true,
"sessionTimer": true,
"tokenAnalytics": true
}
}
}
```
The CLI will prompt you to set this up during configuration.
## Manual Setup
If you prefer not to use the CLI:
### System-Level Manual Setup
```bash theme={null}
# 1. Create directory
mkdir -p ~/.claude
# 2. Create settings file
cat > ~/.claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "your-api-key"
}
}
EOF
# 3. Create API key approval file
cat > ~/.claude.json << 'EOF'
{
"customApiKeyResponses": {
"approved": ["last-20-chars-of-your-key"],
"rejected": []
}
}
EOF
# 4. Add environment variables to shell config
echo 'export ANTHROPIC_BASE_URL="https://ai.megallm.io"' >> ~/.bashrc
echo 'export ANTHROPIC_API_KEY="your-api-key"' >> ~/.bashrc
# 5. Reload shell
source ~/.bashrc
```
### Project-Level Manual Setup
```bash theme={null}
# 1. Navigate to your project
cd ~/projects/my-project
# 2. Create directory
mkdir -p .claude
# 3. Create settings file (without API key for version control)
cat > .claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
}
}
EOF
# 4. Create local settings file (with API key, not committed)
cat > .claude/settings.local.json << 'EOF'
{
"env": {
"ANTHROPIC_API_KEY": "your-api-key"
}
}
EOF
# 5. Add to .gitignore
echo ".claude/settings.local.json" >> .gitignore
echo ".claude.json" >> .gitignore
# 6. Commit shared config
git add .claude/settings.json .gitignore
git commit -m "Add MegaLLM configuration for Claude Code"
```
## Team Configuration
For team projects, separate shared config from personal API keys:
**Shared Configuration** (`.claude/settings.json` - committed to git):
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
},
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096
}
```
**Personal Configuration** (`.claude/settings.local.json` - not committed):
```json theme={null}
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-personal-key-here"
}
}
```
**Setup Instructions for Team** (`.claude/README.md`):
````markdown theme={null}
# MegaLLM Claude Code Setup
## Prerequisites
1. Get your MegaLLM API key from https://megallm.io/dashboard
2. Install Claude Code: `npm install -g @anthropic-ai/claude-code`
## Setup
1. Create `.claude/settings.local.json`:
```json
{
"env": {
"ANTHROPIC_API_KEY": "your-key-here"
}
}
````
2. Or set environment variable:
```bash theme={null}
export ANTHROPIC_API_KEY="your-key-here"
```
````
## Configuration Options
### Available Settings
```json
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-...",
"ANTHROPIC_MODEL": "gpt-5"
},
"model": "gpt-5",
"temperature": 0.7,
"maxTokens": 4096,
"streaming": true,
"contextWindow": 8192,
"autoSave": true,
"fileWatcher": true,
"gitIntegration": true
}
````
### Model Selection
Change the default model:
```json theme={null}
{
"env": {
"ANTHROPIC_MODEL": "claude-opus-4-1-20250805"
}
}
```
Or specify in environment variable:
```bash theme={null}
export ANTHROPIC_MODEL="gpt-5"
```
See [Models Catalog](/home/models) for available models.
## Verification
### Check Configuration Files
```bash theme={null}
# View settings
cat ~/.claude/settings.json | jq .
# View API key approval
cat ~/.claude.json | jq .
# Check project config
cat .claude/settings.json | jq .
cat .claude/settings.local.json | jq .
```
### Test API Connection
```bash theme={null}
# Test API with your credentials
curl -H "Authorization: Bearer $ANTHROPIC_API_KEY" \
-H "Content-Type: application/json" \
$ANTHROPIC_BASE_URL/v1/models
# Should return list of available models
```
### Test Claude Code
```bash theme={null}
# Run Claude Code
claude-code
# Or test with a simple prompt
echo "What is 2+2?" | claude-code
```
## Troubleshooting
**Check file locations:**
```bash theme={null}
ls -la ~/.claude/
ls -la .claude/
```
**Verify JSON syntax:**
```bash theme={null}
jq . ~/.claude/settings.json
# Should show formatted JSON, or error if invalid
```
**Check permissions:**
```bash theme={null}
ls -la ~/.claude/settings.json
# Should be readable: -rw-r--r--
```
**Verify environment variable:**
```bash theme={null}
echo $ANTHROPIC_API_KEY
```
If empty, reload shell:
```bash theme={null}
source ~/.bashrc # or ~/.zshrc
```
**Check API key in config:**
```bash theme={null}
jq .env.ANTHROPIC_API_KEY ~/.claude/settings.json
```
**Verify API key format:**
* Must start with `sk-mega-`
* At least 20 characters long
* No extra spaces or quotes
**Check environment variable:**
```bash theme={null}
echo $ANTHROPIC_BASE_URL
# Should be: https://ai.megallm.io
```
**Verify in config:**
```bash theme={null}
jq .env.ANTHROPIC_BASE_URL ~/.claude/settings.json
```
**Common mistake - trailing slash:**
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io" // ✅ Correct
// "ANTHROPIC_BASE_URL": "https://ai.megallm.io/" // ❌ Wrong
}
}
```
**Check you're in the right directory:**
```bash theme={null}
pwd
ls -la .claude/
```
**Verify config priority:**
```bash theme={null}
# Project config should override system
cat .claude/settings.json
cat ~/.claude/settings.json
```
**Check for settings.local.json:**
```bash theme={null}
cat .claude/settings.local.json
# This has highest priority
```
## Advanced Configuration
### Multiple Profiles
Use different configurations for different use cases:
```bash theme={null}
# Development profile
cat > ~/.claude/settings.dev.json << 'EOF'
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-dev-key"
},
"model": "gpt-4o-mini",
"temperature": 0.9
}
EOF
# Production profile
cat > ~/.claude/settings.prod.json << 'EOF'
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-prod-key"
},
"model": "gpt-5",
"temperature": 0.5
}
EOF
# Switch profiles
cp ~/.claude/settings.dev.json ~/.claude/settings.json
```
### Environment-Specific Configuration
```bash theme={null}
# Set different configs based on environment
if [ "$NODE_ENV" = "production" ]; then
export ANTHROPIC_API_KEY="$PROD_API_KEY"
else
export ANTHROPIC_API_KEY="$DEV_API_KEY"
fi
```
## Best Practices
Use `.gitignore` for `settings.local.json` to keep API keys private
Use project-level config for team projects with shared settings
Prefer environment variables in CI/CD environments
Keep Claude Code updated for latest features and fixes
## Next Steps
Configure Codex/Windsurf
Configure OpenCode
See practical examples
Configure all CLI and GUI agents
Common issues and solutions
# Codex/Windsurf Configuration
Source: https://docs.megallm.io/en/cli/codex-config
Configure Codex and Windsurf to use MegaLLM
Codex and Windsurf use TOML configuration format and only support system-level (global) configuration. The CLI automatically detects which variant you have installed.
Windsurf is a variant of Codex with enhanced features. The configuration is identical for both.
## Configuration File
**Location**: `~/.codex/config.toml`
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[tools]
web_search = true
file_browser = true
```
**Codex/Windsurf only supports system-level configuration.** Project-level configuration is not available.
## Environment Variable
The configuration references an environment variable for the API key:
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
This is added to your shell configuration file:
* `~/.bashrc` (bash)
* `~/.zshrc` (zsh)
* `~/.config/fish/config.fish` (fish)
* PowerShell profile (Windows)
### Verify Environment Variable
```bash theme={null}
echo $MEGALLM_API_KEY
# Output: sk-mega-your-api-key-here
```
## Manual Setup
If you prefer not to use the CLI:
```bash theme={null}
# 1. Create directory
mkdir -p ~/.codex
# 2. Create config file
cat > ~/.codex/config.toml << 'EOF'
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[tools]
web_search = true
file_browser = true
EOF
# 3. Add environment variable to shell config
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
# 4. Reload shell
source ~/.bashrc
```
## Configuration Options
### Model Selection
Change the default model in the configuration:
```toml theme={null}
model = "claude-opus-4-1-20250805" # or any supported model
```
**Available models:**
* `gpt-5` - Latest GPT model
* `gpt-4` - GPT-4
* `gpt-4o` - GPT-4 Optimized
* `claude-opus-4-1-20250805` - Claude Opus
* `claude-sonnet-4` - Claude Sonnet
* `gemini-2.5-pro` - Gemini Pro
* See [Models Catalog](/home/models) for full list
### Tool Settings
Enable or disable integrated tools:
```toml theme={null}
[tools]
web_search = true # Enable web search capability
file_browser = true # Enable file browser
terminal = true # Enable terminal access
code_execution = true # Enable code execution
```
### Advanced Configuration
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
temperature = 0.7
max_tokens = 4096
top_p = 0.9
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[tools]
web_search = true
file_browser = true
terminal = true
[ui]
theme = "dark"
font_size = 14
show_line_numbers = true
```
## Multiple API Keys
If you need different API keys for different purposes:
### Using Environment Variables
```bash theme={null}
# Development key
export MEGALLM_API_KEY="sk-mega-dev-key"
# Production key
export MEGALLM_API_KEY_PROD="sk-mega-prod-key"
```
### Switching Configurations
```bash theme={null}
# Create backup of current config
cp ~/.codex/config.toml ~/.codex/config.toml.backup
# Development config
cat > ~/.codex/config.toml.dev << 'EOF'
model_provider = "megallm"
model = "gpt-4o-mini"
[model_providers.megallm]
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY_DEV"
EOF
# Production config
cat > ~/.codex/config.toml.prod << 'EOF'
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY_PROD"
EOF
# Switch to dev
cp ~/.codex/config.toml.dev ~/.codex/config.toml
# Switch to prod
cp ~/.codex/config.toml.prod ~/.codex/config.toml
```
## Windsurf-Specific Features
Windsurf includes additional configuration options:
```toml theme={null}
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
[windsurf]
cascade_mode = true # Enable Cascade AI feature
multi_file_edit = true # Allow editing multiple files
context_awareness = "enhanced" # enhanced, standard, minimal
[tools]
web_search = true
file_browser = true
terminal = true
supercomplete = true # Windsurf autocomplete feature
```
## Verification
### Check Configuration File
```bash theme={null}
# View configuration
cat ~/.codex/config.toml
# Validate TOML syntax (if toml-cli installed)
toml-check ~/.codex/config.toml
# Check file permissions
ls -la ~/.codex/config.toml
```
### Test API Connection
```bash theme={null}
# Test API with your credentials
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
# Should return list of available models
```
### Test Codex/Windsurf
```bash theme={null}
# Run Codex/Windsurf
codex # or 'windsurf'
# Check version
codex --version # or 'windsurf --version'
```
## Troubleshooting
**Check if directory exists:**
```bash theme={null}
ls -la ~/.codex/
```
**Create if missing:**
```bash theme={null}
mkdir -p ~/.codex
# Then create config.toml
```
**Verify file path:**
```bash theme={null}
# Should be exactly:
~/.codex/config.toml
# Not:
~/.config/codex/config.toml # Wrong location
```
**Check environment variable is set:**
```bash theme={null}
echo $MEGALLM_API_KEY
```
If empty:
```bash theme={null}
# Add to shell config
echo 'export MEGALLM_API_KEY="your-key"' >> ~/.bashrc
source ~/.bashrc
```
**Verify key format:**
* Must start with `sk-mega-`
* At least 20 characters
* No extra spaces or quotes
**Test the key:**
```bash theme={null}
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
https://ai.megallm.io/v1/models
```
**Check config file:**
```bash theme={null}
cat ~/.codex/config.toml | grep model_provider
# Should show: model_provider = "megallm"
```
**Verify base\_url:**
```bash theme={null}
cat ~/.codex/config.toml | grep base_url
# Should show: base_url = "https://ai.megallm.io/v1"
```
**Ensure no typos:**
```toml theme={null}
model_provider = "megallm" # Correct
# model_provider = "megalm" # Wrong (typo)
# model_provider = "openai" # Wrong (different provider)
```
**Validate syntax:**
```bash theme={null}
# If you have toml-cli installed
toml-check ~/.codex/config.toml
# Or use Python
python3 -c "import tomli; tomli.load(open('~/.codex/config.toml', 'rb'))"
```
**Common TOML mistakes:**
```toml theme={null}
# Wrong - missing quotes
model_provider = megallm
# Correct
model_provider = "megallm"
# Wrong - incorrect section syntax
model_providers.megallm
base_url = "..."
# Correct
[model_providers.megallm]
base_url = "..."
```
**Restart Codex/Windsurf:**
```bash theme={null}
# Close all instances
pkill codex # or 'pkill windsurf'
# Start fresh
codex # or 'windsurf'
```
**Check for multiple config files:**
```bash theme={null}
find ~ -name "config.toml" -path "*/.codex/*"
# Should only show one file
```
**Verify permissions:**
```bash theme={null}
chmod 644 ~/.codex/config.toml
```
## Why System-Level Only?
Codex and Windsurf don't support project-level configuration because:
1. **Single Instance** - Codex/Windsurf runs as a single instance across all projects
2. **Global Settings** - Tool preferences apply system-wide
3. **Simplified Management** - One configuration to manage
**Workaround for Project-Specific Keys:**
Use environment variables in your project:
```bash theme={null}
# In project directory
cat > .env << 'EOF'
MEGALLM_API_KEY=project-specific-key
EOF
# Load before running Codex
source .env && codex
```
Or create shell aliases:
```bash theme={null}
# In ~/.bashrc or ~/.zshrc
alias codex-project-a='MEGALLM_API_KEY="key-for-project-a" codex'
alias codex-project-b='MEGALLM_API_KEY="key-for-project-b" codex'
```
## Best Practices
Keep backup of `config.toml` before making changes
Store API keys in environment variables, not in config file
You can commit `config.toml` if env\_key is used (no hardcoded keys)
Keep Codex/Windsurf updated for latest features
## Comparison: Codex vs Windsurf
| Feature | Codex | Windsurf |
| ------------------ | --------------------- | --------------------- |
| Base Configuration | | |
| MegaLLM Support | | |
| Config Location | `~/.codex/` | `~/.codex/` |
| Cascade AI | | |
| Supercomplete | | |
| Multi-file Edit | Basic | Enhanced |
Both Codex and Windsurf use the same configuration file location and format.
## Next Steps
Configure Claude Code
Configure OpenCode
See practical examples
Configure all CLI and GUI agents
Browse available models
# Configuration Overview
Source: https://docs.megallm.io/en/cli/configuration
The MegaLLM CLI configures AI coding assistants by creating configuration files and setting environment variables. Each tool has its own configuration format and storage location.
## Configuration Locations
JSON files in `~/.claude/` or `./.claude/`
TOML file in `~/.codex/`
JSON file in `~/.config/opencode/` or `./`
## Configuration Levels
### System-Level (Global)
Applies to all projects on your machine.
| Tool | Location |
| -------------- | ----------------------------------------------- |
| Claude Code | `~/.claude/settings.json` `~/.claude.json` |
| Codex/Windsurf | `~/.codex/config.toml` |
| OpenCode | `~/.config/opencode/opencode.json` |
### Project-Level (Local)
Applies only to the current project directory.
| Tool | Location |
| -------------- | -------------------------------------------------------------- |
| Claude Code | `./.claude/settings.json` `./.claude/settings.local.json` |
| Codex/Windsurf | Not supported |
| OpenCode | `./opencode.json` |
**Codex/Windsurf only supports system-level configuration**
## Environment Variables
The CLI automatically sets these environment variables in your shell configuration file.
### Claude Code
```bash theme={null}
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY="sk-mega-your-api-key-here"
```
### Codex/Windsurf
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
### OpenCode
```bash theme={null}
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
**Note**: Codex/Windsurf and OpenCode share the same `MEGALLM_API_KEY` environment variable.
### Verify Environment Variables
```bash theme={null}
# Claude Code
echo $ANTHROPIC_BASE_URL
# Output: https://ai.megallm.io
echo $ANTHROPIC_API_KEY
# Output: sk-mega-your-api-key-here
# Codex/Windsurf & OpenCode
echo $MEGALLM_API_KEY
# Output: sk-mega-your-api-key-here
```
## Configuration Priority
When multiple configurations exist, they're applied in this order (highest to lowest):
Highest priority - overrides all file-based configurations
Second priority - applies only to current project
Default - applies globally across all projects
## Backup Files
The CLI automatically creates backup files before modifying configurations:
```
~/.claude/settings.json.backup
~/.codex/config.toml.backup
~/.config/opencode/opencode.json.backup
```
To restore from backup:
```bash theme={null}
mv ~/.claude/settings.json.backup ~/.claude/settings.json
```
## Tool-Specific Configuration
Select your AI tool for detailed configuration information:
JSON configuration, environment variables, and statusline setup
TOML configuration and model provider setup
JSON configuration and API settings
## Quick Configuration Checks
### Verify All Configurations
```bash theme={null}
# Check Claude Code
ls -la ~/.claude/
cat ~/.claude/settings.json
# Check Codex
ls -la ~/.codex/
cat ~/.codex/config.toml
# Check OpenCode
ls -la ~/.config/opencode/
cat ~/.config/opencode/opencode.json
# Check environment variables
env | grep -E "ANTHROPIC|MEGALLM"
```
### Test API Connection
```bash theme={null}
# Test with Claude Code credentials
curl -H "Authorization: Bearer $ANTHROPIC_API_KEY" \
-H "Content-Type: application/json" \
$ANTHROPIC_BASE_URL/v1/models
# Test with Codex/OpenCode credentials
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
```
## Manual Configuration
If you prefer not to use the CLI, you can configure manually:
```bash theme={null}
# Create directory
mkdir -p ~/.claude
# Create settings file
cat > ~/.claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "your-api-key"
}
}
EOF
# Add to shell config
echo 'export ANTHROPIC_BASE_URL="https://ai.megallm.io"' >> ~/.bashrc
echo 'export ANTHROPIC_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
```
```bash theme={null}
# Create directory
mkdir -p ~/.codex
# Create config file
cat > ~/.codex/config.toml << 'EOF'
model_provider = "megallm"
model = "gpt-5"
[model_providers.megallm]
name = "OpenAI using Chat Completions"
base_url = "https://ai.megallm.io/v1"
env_key = "MEGALLM_API_KEY"
query_params = {}
EOF
# Add to shell config
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
```
```bash theme={null}
# Create directory
mkdir -p ~/.config/opencode
# Create config file
cat > ~/.config/opencode/opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
# Add to shell config
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
```
## Configuration Best Practices
Keep project-specific configurations in version control (without API keys)
Never commit API keys. Use `.gitignore` and environment variables
CLI creates automatic backups, but keep your own copies of important configs
Always verify configuration works after manual modifications
## Next Steps
Detailed Claude Code configuration
Detailed Codex/Windsurf configuration
Detailed OpenCode configuration
Practical configuration examples
# Usage Examples
Source: https://docs.megallm.io/en/cli/examples
Practical examples showing how to use the MegaLLM CLI in different scenarios.
## Quick Start Example
The simplest way to get started:
```bash theme={null}
# Run the interactive setup
npx megallm@latest
# Output:
# __ __ _ _ __ __
# | \/ | ___ __ _ __ _| | | | | \/ |
# | |\/| |/ _ \/ _` |/ _` | | | | | |\/| |
# | | | | __/ (_| | (_| | |___| |___| | | |
# |_| |_|\___|\__, |\__,_|_____|_____|_| |_|
# |___/
#
# 🚀 MegaLLM CLI Setup Tool
# Supported: Claude Code, Codex/Windsurf, OpenCode
#
# ✓ System detected: Linux (bash)
# ✓ Tools detected: Claude Code ✓
#
# ? Which tool would you like to configure? Claude Code
# ? Setup level? System-level (global)
# ? Do you have a MegaLLM API key? Yes
# ? Enter your MegaLLM API key: sk-mega-***
#
# Configuration Summary:
# - Tool: Claude Code
# - Level: System-level
# - API Key: sk-mega-***86b9 (last 4 chars)
#
# ? Apply this configuration? Yes
#
# ✓ Configuration applied successfully!
# ✓ Shell reloaded
#
# 🎉 Setup complete! You can now use Claude Code with MegaLLM.
```
## Scenario-Based Examples
### Example 1: First-Time Setup for Claude Code
Setting up Claude Code for the first time on a new machine:
```bash theme={null}
# Step 1: Run the CLI
npx megallm@latest
# Step 2: Follow the prompts
# System detected ✓
# Claude Code detected ✓
#
# ? Which tool would you like to configure?
# › Claude Code
# Codex/Windsurf
# OpenCode
# Configure All
# Step 3: Choose setup level
# ? Setup level?
# › System-level (global) - Applies to all projects
# Project-level (local) - Current directory only
# Step 4: API Key
# ? Do you have a MegaLLM API key?
# › Yes
# No - Show me how to get one
# ? Enter your MegaLLM API key:
# sk-mega-****************************************************************
# Step 5: Confirm and apply
# Configuration Summary:
# Tool: Claude Code
# Level: System-level
# Files: ~/.claude/settings.json, ~/.claude.json
# Env vars: ANTHROPIC_BASE_URL, ANTHROPIC_API_KEY
#
# ? Apply this configuration? Yes
#
# ✓ Created ~/.claude/settings.json
# ✓ Created ~/.claude.json
# ✓ Added environment variables to ~/.bashrc
# ✓ Configuration applied successfully!
```
### Example 2: Project-Specific Setup
Setting up MegaLLM for a specific project:
```bash theme={null}
# Navigate to your project
cd ~/projects/my-app
# Run the CLI
npx megallm@latest
# Choose project-level setup
# ? Setup level?
# System-level (global)
# › Project-level (local) - Current directory only
# After configuration:
ls -la .claude/
# .claude/
# settings.json
# settings.local.json
# Add to .gitignore
echo ".claude/settings.local.json" >> .gitignore
# Commit project settings (without API key)
cat .claude/settings.json
# {
# "env": {
# "ANTHROPIC_BASE_URL": "https://ai.megallm.io"
# }
# }
git add .claude/settings.json .gitignore
git commit -m "Add MegaLLM configuration"
```
### Example 3: Configuring Multiple Tools
Set up both Claude Code and Codex:
```bash theme={null}
# Run the CLI
npx megallm@latest
# Select multiple tools
# ? Which tool would you like to configure?
# Claude Code
# Codex/Windsurf
# OpenCode
# › Configure All
# CLI will configure each tool in sequence
# 1. Configuring Claude Code...
# ✓ Claude Code configured
#
# 2. Configuring Codex...
# ✓ Codex configured
#
# 3. Configuring OpenCode...
# ✓ OpenCode configured
#
# ✓ All tools configured successfully!
```
### Example 4: Updating Existing Configuration
Update your configuration with a new API key:
```bash theme={null}
# Run the CLI again
npx megallm@latest
# Existing configuration detected
# Found MegaLLM configuration:
# - ~/.claude/settings.json
# - ~/.codex/config.toml
#
# ? What would you like to do?
# › Override (remove old, apply new)
# Skip (keep existing)
# Cancel
# Choose Override
# ? Which tool would you like to reconfigure?
# › Claude Code
# Codex/Windsurf
# Both
# Enter new API key
# ? Enter your new MegaLLM API key: sk-mega-new-key-here
# ✓ Backed up old configuration
# ✓ Applied new configuration
```
### Example 5: Team Setup with Shared Config
Set up MegaLLM for a team project with version-controlled configuration:
```bash theme={null}
# Project maintainer: Create base configuration
cd ~/projects/team-project
npx megallm@latest
# Choose project-level
# Setup level: Project-level
# Create .claude/settings.json (without API key)
cat > .claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
}
}
EOF
# Document API key requirement
cat > .claude/README.md << 'EOF'
# MegaLLM Setup
To use this project with MegaLLM:
1. Get your API key from https://megallm.io/dashboard
2. Create `.claude/settings.local.json`:
{
"env": {
"ANTHROPIC_API_KEY": "your-api-key-here"
}
}
3. Or set environment variable:
export ANTHROPIC_API_KEY="your-api-key-here"
EOF
# Commit shared configuration
git add .claude/settings.json .claude/README.md
git commit -m "Add MegaLLM team configuration"
# Team members: Clone and add their own API key
git clone repo
cd repo
echo '{ "env": { "ANTHROPIC_API_KEY": "my-key" } }' > .claude/settings.local.json
```
### Example 6: Debug Mode for Troubleshooting
Use debug mode to diagnose issues:
```bash theme={null}
# Run with debug output
DEBUG=* npx megallm@latest
# Detailed output shows:
# DEBUG: Detecting OS...
# DEBUG: OS: Linux
# DEBUG: Shell: bash
# DEBUG: Shell config: /home/user/.bashrc
# DEBUG: Checking for Claude Code...
# DEBUG: Claude Code found: /usr/local/bin/claude
# DEBUG: Checking for existing config...
# DEBUG: Found: /home/user/.claude/settings.json
# DEBUG: Parsing config file...
# DEBUG: Config valid: true
# ... more detailed logs ...
```
### Example 7: Automated CI/CD Setup
Use the CLI in CI/CD pipelines:
```yaml GitHub Actions theme={null}
name: Setup MegaLLM
on: [push]
jobs:
setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Configure MegaLLM
env:
ANTHROPIC_API_KEY: ${{ secrets.MEGALLM_API_KEY }}
ANTHROPIC_BASE_URL: https://ai.megallm.io
run: |
mkdir -p .claude
cat > .claude/settings.json << EOF
{
"env": {
"ANTHROPIC_BASE_URL": "$ANTHROPIC_BASE_URL",
"ANTHROPIC_API_KEY": "$ANTHROPIC_API_KEY"
}
}
EOF
- name: Run tests with Claude Code
run: npm test
```
```yaml GitLab CI theme={null}
setup_megallm:
stage: setup
image: node:18
script:
- mkdir -p .claude
- |
cat > .claude/settings.json << EOF
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "${MEGALLM_API_KEY}"
}
}
EOF
variables:
ANTHROPIC_BASE_URL: "https://ai.megallm.io"
artifacts:
paths:
- .claude/
```
### Example 8: Windsurf-Specific Setup
Configure Windsurf (Codex variant):
```bash theme={null}
# Run the CLI
npx megallm@latest
# Select Codex
# ? Which tool would you like to configure?
# Claude Code
# › Codex/Windsurf
# OpenCode
# Note: Windsurf is automatically detected
# ✓ Windsurf variant detected
# Configuration proceeds normally
# Creates ~/.codex/config.toml
# Sets MEGALLM_API_KEY environment variable
# Verify configuration
cat ~/.codex/config.toml
# model_provider = "megallm"
# model = "gpt-5"
#
# [model_providers.megallm]
# name = "OpenAI using Chat Completions"
# base_url = "https://ai.megallm.io/v1"
# env_key = "MEGALLM_API_KEY"
```
## Advanced Scenarios
### Switching Between Different APIs
Use different configurations for different projects:
```bash theme={null}
# Project A: Use MegaLLM
cd ~/projects/project-a
npx megallm@latest
# Configure with MegaLLM API key
# Project B: Use different API
cd ~/projects/project-b
# Create custom configuration
cat > .claude/settings.json << 'EOF'
{
"env": {
"ANTHROPIC_BASE_URL": "https://other-api.com",
"ANTHROPIC_API_KEY": "other-key"
}
}
EOF
```
### Environment-Specific Configurations
Different configurations for development, staging, production:
```bash theme={null}
# Development
export ANTHROPIC_API_KEY="sk-mega-dev-key"
# Staging
export ANTHROPIC_API_KEY="sk-mega-staging-key"
# Production
export ANTHROPIC_API_KEY="sk-mega-prod-key"
# Or use different config files
cp .claude/settings.dev.json .claude/settings.json # for dev
cp .claude/settings.prod.json .claude/settings.json # for prod
```
### Docker Container Setup
Use MegaLLM in Docker containers:
```dockerfile theme={null}
FROM node:18
# Install MegaLLM CLI globally
RUN npm install -g megallm
# Set environment variables
ENV ANTHROPIC_BASE_URL=https://ai.megallm.io
ENV ANTHROPIC_API_KEY=your-key-here
# Create configuration directory
RUN mkdir -p /root/.claude
# Copy configuration file
COPY .claude/settings.json /root/.claude/settings.json
# Your app setup
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
```
## Common Workflows
### Workflow 1: New Developer Onboarding
```bash theme={null}
# 1. New developer clones the repository
git clone https://github.com/company/project.git
cd project
# 2. Sees MegaLLM setup instructions in README
cat README.md
# "Get your API key from https://megallm.io/dashboard"
# 3. Gets API key from dashboard
# Opens https://megallm.io/dashboard in browser
# 4. Runs setup (with NO_BANNER for cleaner output)
NO_BANNER=1 npx megallm@latest
# 5. Enters API key when prompted
# Configuration automatically merges with project settings
# 6. Starts developing
npm run dev
```
### Workflow 2: Migration from OpenAI
```bash theme={null}
# 1. Currently using OpenAI directly
# .env file has: MEGALLM_API_KEY=sk-mega-...
# 2. Switch to MegaLLM
npx megallm@latest
# 3. CLI detects Claude Code/Codex and configures
# Sets ANTHROPIC_BASE_URL=https://ai.megallm.io
# 4. Update application code (if needed)
# Change: base_url from api.openai.com to ai.megallm.io
# Or: Let environment variables handle it automatically
# 5. Test the migration
npm test
# 6. Deploy with new configuration
```
### Workflow 3: Multi-Tool Development
```bash theme={null}
# Developer using both Claude Code and Codex
npx megallm@latest
# Configure all tools at once
# ? Which tool? Configure All
# Result:
# - Claude Code uses ANTHROPIC_* env vars
# - Codex uses MEGALLM_API_KEY env var
# - Both point to ai.megallm.io
# Can switch between tools seamlessly
claude-code # Uses ANTHROPIC_API_KEY
codex # Uses MEGALLM_API_KEY
```
## Troubleshooting Examples
### Example: Permission Denied
```bash theme={null}
# Error during setup
npx megallm@latest
# Error: EACCES: permission denied, mkdir '/home/user/.claude'
# Fix permissions
sudo chown -R $USER ~/.claude ~/.codex
chmod -R 755 ~/.claude ~/.codex
# Retry
npx megallm@latest
```
### Example: Configuration Not Loading
```bash theme={null}
# Check if environment variables are set
env | grep -E "ANTHROPIC|MEGALLM"
# (no output = not set)
# Reload shell configuration
source ~/.bashrc # or ~/.zshrc
# Or restart terminal
exit
# Open new terminal and check again
env | grep -E "ANTHROPIC|MEGALLM"
# ANTHROPIC_BASE_URL=https://ai.megallm.io
# ANTHROPIC_API_KEY=sk-mega-...
```
### Example: API Key Validation Failed
```bash theme={null}
npx megallm@latest
# ? Enter your MegaLLM API key: sk-mega-abc
# ✗ API key must be at least 20 characters long
# Fix: Enter complete API key
# ? Enter your MegaLLM API key: sk-mega-****************************************************************
# ✓ API key validated
```
## Next Steps
* Review [Configuration Details](/cli/configuration)
* Read the [FAQ](/cli/faq)
* Check [Troubleshooting Guide](/cli/troubleshooting) for common issues
# FAQ
Source: https://docs.megallm.io/en/cli/faq
Common questions and answers about the MegaLLM NPM package and CLI tool.
## General Questions
The MegaLLM CLI is an interactive setup tool that configures AI coding assistants (Claude Code, Codex/Windsurf, and OpenCode) to use the MegaLLM AI service. It automates the configuration process, manages API keys securely, and provides a seamless setup experience across different platforms.
The CLI currently supports:
* **Claude Code** - System-level and project-level configuration
* **Codex/Windsurf** - System-level configuration only
* **OpenCode** - System-level and project-level configuration
More tools may be added in future releases.
No! You can run it directly using npx:
```bash theme={null}
npx megallm@latest
```
However, you can also install it globally if you prefer:
```bash theme={null}
npm install -g megallm
```
* **Node.js**: Version 18.0.0 or higher
* **Operating System**: macOS, Linux, or Windows
* **Shell**: bash, zsh, fish, or PowerShell
Check your Node.js version with: `node --version`
## Installation & Setup
1. Visit [megallm.io/dashboard](https://megallm.io/dashboard)
2. Sign up or log in to your account
3. Navigate to the API Keys section
4. Click "Create New API Key"
5. Copy your API key (starts with `sk-mega-`)
6. Save it securely - you won't be able to see it again
The CLI can open this page for you automatically during setup.
**System-Level (Global)**:
* Applies to all projects on your machine
* Stored in your home directory (`~/.claude/`, `~/.codex/`, etc.)
* Best for personal development environments
* Easier to manage for single developers
**Project-Level (Local)**:
* Applies only to the current project directory
* Stored in the project folder (`./.claude/`, `./opencode.json`, etc.)
* Best for team projects with shared configurations
* Allows different API keys per project
* Can be version-controlled (without exposing API keys)
**Important**: Codex/Windsurf only supports system-level configuration.
When running the CLI, you'll be asked which tool to configure:
```
? Which tool would you like to configure?
Claude Code
Codex/Windsurf
OpenCode
Configure All ← Select this option
```
Selecting "Configure All" will set up all detected tools in sequence.
Alternatively, run the CLI multiple times and select one tool each time.
Yes! If the CLI detects that a tool is not installed, it will offer to install it:
```
? Claude Code is not installed. Would you like to install it? (Y/n)
```
The CLI installs tools via NPM as global packages.
## Configuration
**Claude Code**:
* System: `~/.claude/settings.json`, `~/.claude.json`
* Project: `./.claude/settings.json` or `./.claude/settings.local.json`
**Codex/Windsurf**:
* System: `~/.codex/config.toml`
**OpenCode**:
* System: `~/.config/opencode/opencode.json`
* Project: `./opencode.json`
**Environment Variables**:
* bash: `~/.bashrc`
* zsh: `~/.zshrc`
* fish: `~/.config/fish/config.fish`
* PowerShell: PowerShell profile
The CLI will detect existing configurations and ask what you'd like to do:
```
Found existing MegaLLM configuration:
- ~/.claude/settings.json
- ~/.codex/config.toml
? What would you like to do?
Override (remove old, apply new)
Skip (keep existing)
Cancel
```
Selecting "Override" will:
1. Create backup files (`.backup` suffix)
2. Remove old configuration
3. Apply new configuration
Selecting "Skip" will keep your existing setup and exit.
Simply run the CLI again and choose "Override" when prompted about existing configuration:
```bash theme={null}
npx megallm@latest
# Choose: Override (remove old, apply new)
# Enter your new API key
```
The CLI will backup your old configuration before applying the new one.
Yes! Use project-level configuration:
```bash theme={null}
# Project A
cd ~/projects/project-a
npx megallm@latest
# Choose: Project-level
# Enter API key for Project A
# Project B
cd ~/projects/project-b
npx megallm@latest
# Choose: Project-level
# Enter API key for Project B
```
Each project will have its own `.claude/settings.json` with its own API key.
## Troubleshooting
The CLI checks for tools using these methods:
1. NPM global package list
2. Command availability in PATH
3. Known installation directories
If detection fails:
1. Ensure the tool is installed globally: `npm list -g --depth=0`
2. Check if the command is available: `which claude-code` or `which codex`
3. Restart your terminal
4. Try installing manually: `npm install -g @anthropic-ai/claude-code`
This usually means you don't have write access to the configuration directories.
**macOS/Linux Fix**:
```bash theme={null}
# Fix ownership of config directories
sudo chown -R $USER ~/.claude ~/.codex ~/.config/opencode
# Fix permissions
chmod -R 755 ~/.claude ~/.codex ~/.config/opencode
```
**Windows Fix**:
* Run your terminal as Administrator
* Or: Fix permissions in File Explorer → Properties → Security
**Check 1: Environment Variables**
```bash theme={null}
# Claude Code
echo $ANTHROPIC_BASE_URL
echo $ANTHROPIC_API_KEY
# Codex
echo $MEGALLM_API_KEY
```
If empty, reload your shell:
```bash theme={null}
source ~/.bashrc # or ~/.zshrc
# Or restart your terminal
```
**Check 2: Configuration Files**
```bash theme={null}
# Claude Code
cat ~/.claude/settings.json
# Codex
cat ~/.codex/config.toml
```
Verify the files exist and contain your API key.
**Check 3: File Permissions**
```bash theme={null}
ls -la ~/.claude/settings.json
ls -la ~/.codex/config.toml
```
Files should be readable by your user.
1. **Verify key format**: Should start with `sk-mega-`
2. **Check for typos**: Copy-paste the key directly from the dashboard
3. **Trim whitespace**: Remove any spaces before/after the key
4. **Key length**: Must be at least 20 characters
5. **Regenerate**: Create a new API key at [megallm.io/dashboard](https://megallm.io/dashboard)
Test your API key:
```bash theme={null}
curl -H "Authorization: Bearer YOUR_API_KEY" \
https://ai.megallm.io/v1/models
```
**Remove Configuration Files**:
```bash theme={null}
# Claude Code
rm -rf ~/.claude/settings.json ~/.claude.json
# Codex
rm -rf ~/.codex/config.toml
# OpenCode
rm -rf ~/.config/opencode/opencode.json
# Project-level
rm -rf ./.claude ./opencode.json
```
**Remove Environment Variables**:
Edit your shell config file (`~/.bashrc`, `~/.zshrc`, etc.) and remove these lines:
```bash theme={null}
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY="sk-mega-..."
export MEGALLM_API_KEY="sk-mega-..."
```
Then reload: `source ~/.bashrc` or restart terminal.
Yes! Run in debug mode:
```bash theme={null}
DEBUG=* npx megallm@latest
```
This will show detailed logs of:
* System detection
* Tool detection
* File operations
* Configuration changes
* Error stack traces
## Advanced Usage
Yes, but it's better to configure manually in CI/CD environments:
```yaml theme={null}
# GitHub Actions example
- name: Configure MegaLLM
env:
ANTHROPIC_API_KEY: ${{ secrets.MEGALLM_API_KEY }}
run: |
mkdir -p .claude
echo '{"env":{"ANTHROPIC_BASE_URL":"https://ai.megallm.io","ANTHROPIC_API_KEY":"'$ANTHROPIC_API_KEY'"}}' > .claude/settings.json
```
This avoids interactive prompts and is more reliable in automated environments.
Add configuration during Docker build:
```dockerfile theme={null}
FROM node:18
# Set environment variables
ENV ANTHROPIC_BASE_URL=https://ai.megallm.io
ENV ANTHROPIC_API_KEY=your-key-here
# Or copy configuration files
COPY .claude/settings.json /root/.claude/settings.json
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
```
Or pass API key at runtime:
```bash theme={null}
docker run -e ANTHROPIC_API_KEY=sk-mega-... myimage
```
**Yes, but carefully**:
**DO commit**:
* Project-level configuration WITHOUT API keys
* `.claude/settings.json` with only `ANTHROPIC_BASE_URL`
* Documentation for team members
**DON'T commit**:
* API keys
* `.claude/settings.local.json`
* Personal environment variables
**Example `.claude/settings.json` for version control**:
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io"
}
}
```
**Add to `.gitignore`**:
```
.claude/settings.local.json
.claude.json
```
**Team members add their own API key**:
```json theme={null}
# .claude/settings.local.json (not committed)
{
"env": {
"ANTHROPIC_API_KEY": "sk-mega-personal-key"
}
}
```
Yes! Each tool's configuration file allows you to specify the model.
**Claude Code** (`~/.claude/settings.json`):
```json theme={null}
{
"env": {
"ANTHROPIC_BASE_URL": "https://ai.megallm.io",
"ANTHROPIC_API_KEY": "sk-mega-...",
"ANTHROPIC_MODEL": "claude-opus-4-1-20250805"
}
}
```
**Codex** (`~/.codex/config.toml`):
```toml theme={null}
model = "gpt-5" # Change to any supported model
```
**OpenCode** (`~/.config/opencode/opencode.json`):
```json theme={null}
{
"model": "gemini-2.5-pro"
}
```
See [Models Catalog](/home/models) for available models.
The CLI creates backup files before modifying configurations:
```
~/.claude/settings.json.backup
~/.codex/config.toml.backup
~/.config/opencode/opencode.json.backup
```
Backups are created with the `.backup` suffix and contain your previous configuration.
To restore from backup:
```bash theme={null}
mv ~/.claude/settings.json.backup ~/.claude/settings.json
```
You can delete backups manually if you don't need them:
```bash theme={null}
rm ~/.claude/*.backup ~/.codex/*.backup
```
## Getting Help
**Documentation**:
* Main Docs: [docs.megallm.io](https://docs.megallm.io)
* API Reference: [docs.megallm.io/api](https://docs.megallm.io/api)
**Support Channels**:
* Email: [support@megallm.io](mailto:support@megallm.io)
* GitHub Issues: [github.com/Megallm/megallm-npm/issues](https://github.com/Megallm/megallm-npm/issues)
* Discord: [discord.gg/Megallm](https://discord.gg/Megallm)
**Community**:
* Twitter/X: [@megallmio](https://x.com/megallmio)
* YouTube: [youtube.com/@Megallmio](https://youtube.com/@Megallmio)
1. Check existing issues: [github.com/Megallm/megallm-npm/issues](https://github.com/Megallm/megallm-npm/issues)
2. If not found, create a new issue with:
* CLI version: `npx megallm@latest --version`
* Node.js version: `node --version`
* Operating system
* Shell type
* Error message/logs (run with `DEBUG=*`)
* Steps to reproduce
Open a feature request on GitHub:
[github.com/Megallm/megallm-npm/issues/new](https://github.com/Megallm/megallm-npm/issues/new)
Include:
* Description of the feature
* Use case / why it's needed
* Any relevant examples or mockups
## Still Have Questions?
Can't find your answer? We're here to help!
Get help from our support team
Report bugs or request features
Chat with the community
Browse complete documentation
# Installation
Source: https://docs.megallm.io/en/cli/installation
Learn how to install and run the MegaLLM CLI on your system.
## Quick Install
The fastest way to get started - no installation required:
```bash theme={null}
npx megallm@latest
```
This runs the latest version directly without installing anything permanently.
## Installation Methods
### Method 1: NPX (Recommended)
Run directly without installation:
```bash theme={null}
npx megallm@latest
```
**Advantages:**
* No installation required
* Always uses the latest version
* No disk space used
* Perfect for one-time or occasional use
**Usage:**
```bash theme={null}
# Run latest version
npx megallm@latest
# Run specific version
npx megallm@2.5.9
# With environment variables
NO_BANNER=1 npx megallm@latest
```
### Method 2: Global Installation
Install once, use anytime:
```bash theme={null}
npm install -g megallm
```
Then run with:
```bash theme={null}
megallm
```
**Advantages:**
* Faster startup (already installed)
* Works offline (after initial install)
* Shorter command
* Good for frequent use
**Update:**
```bash theme={null}
npm update -g megallm
```
**Uninstall:**
```bash theme={null}
npm uninstall -g megallm
```
### Method 3: Local Project Installation
Install as a project dependency:
```bash theme={null}
npm install --save-dev megallm
```
Add to `package.json` scripts:
```json theme={null}
{
"scripts": {
"setup-megallm": "megallm"
}
}
```
Run with:
```bash theme={null}
npm run setup-megallm
```
**Best for:**
* Team projects with standardized setup
* Version-locked installations
* CI/CD pipelines
## System Requirements
### Required
**Node.js 18.0.0 or higher** is required to run the MegaLLM CLI
Check your Node.js version:
```bash theme={null}
node --version
```
If you need to install or update Node.js:
**Using Homebrew:**
```bash theme={null}
brew install node
```
**Using nvm (recommended):**
```bash theme={null}
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install 18
nvm use 18
```
**Official Installer:**
Download from [nodejs.org](https://nodejs.org/)
**Ubuntu/Debian:**
```bash theme={null}
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
```
**Fedora:**
```bash theme={null}
sudo dnf install nodejs
```
**Using nvm (recommended):**
```bash theme={null}
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install 18
nvm use 18
```
**Official Installer:**
Download from [nodejs.org](https://nodejs.org/)
**Using Chocolatey:**
```powershell theme={null}
choco install nodejs
```
**Using nvm-windows:**
```powershell theme={null}
nvm install 18
nvm use 18
```
### Supported Platforms
| Platform | Status | Notes |
| ----------- | ------------------------------------- | ----------------------- |
| **macOS** | Fully Supported | Intel & Apple Silicon |
| **Linux** | Fully Supported | All major distributions |
| **Windows** | Fully Supported | Native & WSL |
### Supported Shells
| Shell | Status | Platform |
| -------------- | ------------------------------------- | ------------- |
| **bash** | Fully Supported | All platforms |
| **zsh** | Fully Supported | macOS, Linux |
| **fish** | Fully Supported | macOS, Linux |
| **PowerShell** | Fully Supported | Windows |
## Verification
After installation, verify everything works:
```bash theme={null}
# Check Node.js version
node --version
# Should show: v18.0.0 or higher
# Check npm version
npm --version
# Should show: 9.0.0 or higher
# Run the CLI
npx megallm@latest --version
# or if installed globally:
megallm --version
```
## Permissions
### macOS/Linux
The CLI needs write permissions to:
* `~/.claude/` - Claude Code configuration
* `~/.codex/` - Codex configuration
* `~/.config/opencode/` - OpenCode configuration
* `~/.bashrc`, `~/.zshrc`, etc. - Shell configuration files
If you encounter permission errors:
```bash theme={null}
# Fix ownership
sudo chown -R $USER ~/.claude ~/.codex ~/.config/opencode
# Fix permissions
chmod -R 755 ~/.claude ~/.codex ~/.config/opencode
```
### Windows
Run your terminal as Administrator if you encounter permission issues.
## AI Tool Installation
The CLI can automatically install AI coding tools if they're missing. Required permissions:
```bash theme={null}
# NPM global install permission
npm install -g @anthropic-ai/claude-code
npm install -g @codeium/windsurf
npm install -g opencode
```
If you don't have permission for global installs:
**Option 1: Use npx**
```bash theme={null}
# Instead of installing globally, use npx
npx @anthropic-ai/claude-code
```
**Option 2: Fix npm permissions**
```bash theme={null}
# macOS/Linux
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
```
## Troubleshooting Installation
Node.js isn't installed or not in your PATH.
**Fix:**
1. Install Node.js from [nodejs.org](https://nodejs.org/)
2. Restart your terminal
3. Verify: `node --version`
npm isn't installed or is an old version.
**Fix:**
```bash theme={null}
# Update npm
npm install -g npm@latest
# Or reinstall Node.js
```
You don't have permission to install global packages.
**Fix:**
```bash theme={null}
# macOS/Linux - Configure npm to use a different directory
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile
```
Or use `sudo` (not recommended):
```bash theme={null}
sudo npm install -g megallm
```
You're running an old version.
**Fix:**
```bash theme={null}
# Clear npm cache
npm cache clean --force
# Update to latest
npm update -g megallm
# Or run latest with npx
npx megallm@latest
```
npm can't connect to the registry.
**Fix:**
```bash theme={null}
# Check npm registry
npm config get registry
# Set registry
npm config set registry https://registry.npmjs.org/
# If behind a proxy
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
```
## Advanced Installation
### Offline Installation
For environments without internet access:
1. **Download on internet-connected machine:**
```bash theme={null}
npm pack megallm
# Creates: megallm-2.5.9.tgz
```
2. **Transfer file to offline machine**
3. **Install from tarball:**
```bash theme={null}
npm install -g ./megallm-2.5.9.tgz
```
### CI/CD Installation
For automated environments:
```yaml GitHub Actions theme={null}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Run MegaLLM CLI
run: npx megallm@latest
env:
NO_BANNER: '1'
```
```yaml GitLab CI theme={null}
setup_cli:
image: node:18
script:
- npm install -g megallm@latest
- megallm --version
```
```groovy Jenkins theme={null}
pipeline {
agent {
docker {
image 'node:18'
}
}
stages {
stage('Setup') {
steps {
sh 'npx megallm@latest'
}
}
}
}
```
### Docker Installation
Include in your Dockerfile:
```dockerfile theme={null}
FROM node:18
# Install megallm globally
RUN npm install -g megallm
# Or use npx
RUN npx megallm@latest --version
# Configure at runtime
CMD ["megallm"]
```
## Version Management
### Check Current Version
```bash theme={null}
# If installed globally
megallm --version
# Using npx
npx megallm@latest --version
```
### Install Specific Version
```bash theme={null}
# Install specific version globally
npm install -g megallm@2.5.9
# Run specific version with npx
npx megallm@2.5.9
```
### Version History
View all available versions:
```bash theme={null}
npm view megallm versions
```
View latest version:
```bash theme={null}
npm view megallm version
```
## Uninstallation
### Remove Global Installation
```bash theme={null}
npm uninstall -g megallm
```
### Remove Configuration Files
The CLI creates configuration files that persist after uninstallation:
```bash theme={null}
# Remove all MegaLLM configurations
rm -rf ~/.claude/settings.json ~/.claude.json
rm -rf ~/.codex/config.toml
rm -rf ~/.config/opencode/opencode.json
# Remove environment variables
# Edit your shell config (~/.bashrc, ~/.zshrc, etc.)
# and remove lines containing:
# ANTHROPIC_BASE_URL
# ANTHROPIC_API_KEY
# MEGALLM_API_KEY
```
### Complete Cleanup
To completely remove all traces:
```bash theme={null}
# Uninstall CLI
npm uninstall -g megallm
# Remove config files
rm -rf ~/.claude ~/.codex ~/.config/opencode
# Remove project-level configs
rm -rf ./.claude ./opencode.json
# Clear npm cache
npm cache clean --force
```
## Next Steps
Complete setup workflow
Configuration details
Usage examples
Common issues
# OpenCode Configuration
Source: https://docs.megallm.io/en/cli/opencode-config
Configure OpenCode to use MegaLLM
OpenCode uses JSON configuration format and supports both system-level (global) and project-level (local) configuration.
## Configuration Files
### System-Level Configuration
**Location**: `~/.config/opencode/opencode.json`
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
The CLI fetches available models from MegaLLM and adds them to the `provider.anthropic.models` section automatically.
### Project-Level Configuration
**Location**: `./opencode.json` (in project root)
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
Use project-level configuration to customize settings per project while keeping a global default. The `{env:MEGALLM_API_KEY}` syntax references the environment variable.
## Manual Setup
### System-Level Manual Setup
```bash theme={null}
# 1. Create directory
mkdir -p ~/.config/opencode
# 2. Create config file
cat > ~/.config/opencode/opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
EOF
# 3. Set environment variable
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
# 4. Verify
cat ~/.config/opencode/opencode.json | jq .
```
### Project-Level Manual Setup
```bash theme={null}
# 1. Navigate to project
cd ~/projects/my-project
# 2. Create config file
cat > opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
EOF
# 3. Set environment variable (if not already set)
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
# 4. Add to .gitignore
echo "opencode.json" >> .gitignore
```
## Configuration Priority
OpenCode loads configuration in this order (highest to lowest priority):
`MEGALLM_API_KEY` environment variable (referenced via `{env:MEGALLM_API_KEY}` in config)
`./opencode.json` in current directory
`~/.config/opencode/opencode.json` in home directory
## Configuration Options
### Provider Settings
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"models": {
"gpt-5": {
"id": "gpt-5",
"name": "GPT-5 (Via MegaLLM)"
},
"claude-sonnet-4": {
"id": "claude-sonnet-4",
"name": "Claude Sonnet 4 (Via MegaLLM)"
}
},
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
```
The CLI automatically fetches and populates the `models` object from the MegaLLM API. You can also manually add or override specific models.
### Tool Settings
```json theme={null}
{
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
},
"autoupdate": true
}
```
**Available models:**
* `gpt-5` - Latest GPT model
* `gpt-4` - GPT-4
* `claude-opus-4-1-20250805` - Claude Opus
* `claude-sonnet-4` - Claude Sonnet
* `gemini-2.5-pro` - Gemini Pro
* See [Models Catalog](/home/models) for full list
### Complete Example
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"models": {
"gpt-5": {
"id": "gpt-5",
"name": "GPT-5 (Via MegaLLM)"
},
"gpt-4": {
"id": "gpt-4",
"name": "GPT-4 (Via MegaLLM)"
},
"claude-sonnet-4": {
"id": "claude-sonnet-4",
"name": "Claude Sonnet 4 (Via MegaLLM)"
}
},
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
## Team Configuration
For team projects, use environment variables to keep API keys out of version control:
**Shared Configuration** (`opencode.json` - committed to git):
```json theme={null}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
},
"autoupdate": true,
"tools": {
"bash": true,
"edit": true,
"write": true,
"read": true
}
}
```
**Add to `.gitignore`**:
```
opencode.json
```
**Setup Instructions for Team** (`README.md`):
````markdown theme={null}
# OpenCode Setup
## Prerequisites
1. Get MegaLLM API key from https://megallm.io/dashboard
2. Install OpenCode: `npm install -g opencode-ai`
## Setup
Set the MEGALLM_API_KEY environment variable:
```bash
export MEGALLM_API_KEY="your-api-key-here"
````
Add to your shell config (\~/.bashrc or \~/.zshrc) to make it permanent.
````
## Environment Variables
The MegaLLM CLI sets the `MEGALLM_API_KEY` environment variable for OpenCode:
```bash
# Set by the CLI automatically
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
````
This environment variable is referenced in the OpenCode configuration using `{env:MEGALLM_API_KEY}`.
Add to your shell configuration:
```bash theme={null}
# ~/.bashrc or ~/.zshrc
export MEGALLM_API_KEY="sk-mega-your-api-key-here"
```
### Verify Environment Variables
```bash theme={null}
echo $MEGALLM_API_KEY
# Output: sk-mega-your-api-key-here
```
## Verification
### Check Configuration Files
```bash theme={null}
# System config
cat ~/.config/opencode/opencode.json | jq .
# Project config
cat opencode.json | jq .
cat opencode.local.json | jq .
# Check file permissions
ls -la ~/.config/opencode/opencode.json
ls -la opencode.json
```
### Validate JSON Syntax
```bash theme={null}
# Validate JSON
jq . ~/.config/opencode/opencode.json
# Should show formatted JSON or error if invalid
```
### Test API Connection
```bash theme={null}
# Test with MEGALLM_API_KEY
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
# Should return list of available models
```
### Test OpenCode
```bash theme={null}
# Run OpenCode
opencode
# Check version
opencode --version
# Test with a file
echo "console.log('test')" > test.js
opencode test.js
```
## Troubleshooting
**Check file locations:**
```bash theme={null}
# System config
ls -la ~/.config/opencode/opencode.json
# Project config
ls -la opencode.json
```
**Validate JSON syntax:**
```bash theme={null}
jq . ~/.config/opencode/opencode.json
# Should show formatted JSON
```
**Check you're in the right directory:**
```bash theme={null}
pwd
# Should be your project directory for project-level config
```
**Check environment variable:**
```bash theme={null}
echo $MEGALLM_API_KEY
```
If empty, set it:
```bash theme={null}
export MEGALLM_API_KEY="your-api-key"
echo 'export MEGALLM_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc
```
**Verify key format:**
* Must start with `sk-mega-`
* At least 20 characters
* No extra spaces or quotes
**Test the key:**
```bash theme={null}
curl -H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
https://ai.megallm.io/v1/models
```
**Check config:**
```bash theme={null}
jq '.provider.anthropic.options.baseURL' ~/.config/opencode/opencode.json
# Should show: "https://ai.megallm.io/v1"
```
**Common mistakes:**
```json theme={null}
{
"provider": {
"anthropic": {
"options": {
"baseURL": "https://ai.megallm.io/v1" // Correct
// "baseURL": "https://ai.megallm.io/v1/" // Wrong (trailing slash)
// "baseURL": "https://ai.megallm.io" // Wrong (missing /v1)
}
}
}
}
```
**Verify you have project config:**
```bash theme={null}
ls -la opencode.json
```
**Ensure proper JSON structure:**
Project config should use the same structure as system config with `provider.anthropic.options`.
**Common JSON mistakes:**
```json theme={null}
// Wrong - trailing comma
{
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
}
}
}
}
// Correct
{
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}"
}
}
}
}
// Wrong - single quotes
{
'provider': {
'anthropic': {
'options': {
'apiKey': '{env:MEGALLM_API_KEY}'
}
}
}
}
// Correct - double quotes
{
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}"
}
}
}
}
```
**Validate:**
```bash theme={null}
jq . ~/.config/opencode/opencode.json
# Will show error if invalid
```
## Best Practices
Keep API keys in `opencode.local.json` and add to `.gitignore`
Commit `opencode.json` without API keys for team consistency
Use project-level config for project-specific context and settings
Always validate JSON syntax after manual edits
## Advanced Usage
### Multiple Profiles
```bash theme={null}
# Create different profiles
cat > ~/.config/opencode/opencode.dev.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY_DEV}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
cat > ~/.config/opencode/opencode.prod.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY_PROD}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
# Set environment variables
export MEGALLM_API_KEY_DEV="sk-mega-dev-key"
export MEGALLM_API_KEY_PROD="sk-mega-prod-key"
# Switch profiles
alias opencode-dev='cp ~/.config/opencode/opencode.dev.json ~/.config/opencode/opencode.json && opencode'
alias opencode-prod='cp ~/.config/opencode/opencode.prod.json ~/.config/opencode/opencode.json && opencode'
```
### CI/CD Configuration
```yaml theme={null}
# GitHub Actions example
- name: Configure OpenCode
env:
MEGALLM_API_KEY: ${{ secrets.MEGALLM_API_KEY }}
run: |
mkdir -p ~/.config/opencode
cat > ~/.config/opencode/opencode.json << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"apiKey": "{env:MEGALLM_API_KEY}",
"baseURL": "https://ai.megallm.io/v1"
}
}
}
}
EOF
```
## Next Steps
Configure Claude Code
Configure Codex/Windsurf
See practical examples
Configure all CLI and GUI agents
Browse available models
# CLI Overview
Source: https://docs.megallm.io/en/cli/overview
The MegaLLM CLI is an interactive setup tool that configures AI coding assistants to use the MegaLLM AI service. It automates configuration, manages API keys securely, and provides seamless setup across multiple platforms.
## Quick Start
Get started in seconds:
```bash theme={null}
npx megallm@latest
```
That's it! The interactive wizard guides you through the entire setup process.
No installation required - just run `npx megallm@latest` and follow the prompts
## What It Does
The CLI automatically:
* **Detects** installed AI tools and your system configuration
* **Installs** missing tools if needed (with your permission)
* **Configures** AI assistants with your MegaLLM API key
* **Backs up** existing configurations before making changes
* **Validates** settings to ensure everything works correctly
## Supported Tools
System & project-level configuration
System-level configuration
System & project-level configuration
## Key Features
Auto-detects OS, shell, installed tools, and existing configurations
Interactive wizard with step-by-step guidance
Automatic backups and secure API key management
Works on macOS, Linux, Windows with all major shells
## How It Works
Execute `npx megallm@latest` in your terminal
CLI detects your OS, shell, and installed AI tools
Select which tool(s) to configure and at what level (system/project)
Provide your MegaLLM API key (or get guided to create one)
Review configuration summary and confirm
CLI configures files, sets environment variables, and reloads shell
## Setup Levels
Choose between two configuration levels:
### System-Level (Global)
Applies to **all projects** on your machine.
**Best for:**
* Personal development environments
* Single developer setups
* Quick testing and prototyping
**Storage:**
* `~/.claude/` - Claude Code
* `~/.codex/` - Codex/Windsurf
* `~/.config/opencode/` - OpenCode
### Project-Level (Local)
Applies **only to the current project** directory.
**Best for:**
* Team projects with shared configurations
* Different API keys per project
* Version-controlled settings
**Storage:**
* `./.claude/` - Claude Code
* `./opencode.json` - OpenCode
**Codex/Windsurf only supports system-level configuration**
## Requirements
**Node.js 18.0.0+** is required. Check your version: `node --version`
**Supported Platforms:**
* macOS (Intel & Apple Silicon)
* Linux (all major distributions)
* Windows (10/11 with WSL or native)
**Supported Shells:**
* bash
* zsh
* fish
* PowerShell
## Installation Options
### NPX (Recommended)
No installation needed:
```bash theme={null}
npx megallm@latest
```
### Global Installation
Install once, use anytime:
```bash theme={null}
npm install -g megallm
megallm
```
### Specific Version
Run a specific version:
```bash theme={null}
npx megallm@2.5.9
```
## First-Time Setup Example
```bash theme={null}
# Run the CLI
npx megallm@latest
# Interactive prompts:
# ✓ System detected: Linux (bash)
# ✓ Tools detected: Claude Code ✓, Codex ✗
#
# ? Which tool? › Claude Code
# ? Setup level? › System-level (global)
# ? Enter API key: sk-mega-***
#
# ✓ Configuration applied successfully!
# 🎉 Ready to use Claude Code with MegaLLM
```
## Getting Your API Key
Go to [megallm.io/dashboard](https://megallm.io/dashboard)
Create an account or log in to existing one
Navigate to API Keys section and click "Create New API Key"
Copy your key (starts with `sk-mega-`) - you won't see it again
Paste the key when prompted during setup
The CLI can automatically open the dashboard for you during setup
## What's Next?
Detailed installation and requirements
Complete setup workflow and configuration
Configuration details for each tool
Practical usage examples
Common issues and solutions
Frequently asked questions
## Quick Links
* [Claude Code Configuration](/cli/claude-config)
* [Codex/Windsurf Configuration](/cli/codex-config)
* [OpenCode Configuration](/cli/opencode-config)
* [GitHub Repository](https://github.com/Megallm/megallm-npm)
* [NPM Package](https://www.npmjs.com/package/megallm)
## Support
Need help? We're here for you:
* **Email**: [support@megallm.io](mailto:support@megallm.io)
* **Discord**: [Join our community](https://discord.gg/Megallm)
* **GitHub Issues**: [Report bugs or request features](https://github.com/Megallm/megallm-npm/issues)
# Troubleshooting
Source: https://docs.megallm.io/en/cli/troubleshooting
Solutions to common issues when using the MegaLLM CLI.
## Installation Issues
Node.js and npm are not installed.
**Solution:**
```bash theme={null}
# Using Homebrew
brew install node
# Or download from nodejs.org
```
```bash theme={null}
# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Fedora
sudo dnf install nodejs
```
Download and install from [nodejs.org](https://nodejs.org/)
Verify installation:
```bash theme={null}
node --version
npm --version
```
You don't have permission to install global packages.
**Solution 1: Configure npm to use a different directory (Recommended)**
```bash theme={null}
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
# Add to ~/.bashrc or ~/.zshrc
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
```
**Solution 2: Fix npm permissions**
```bash theme={null}
sudo chown -R $USER /usr/local/lib/node_modules
sudo chown -R $USER /usr/local/bin
```
**Solution 3: Use npx (no installation needed)**
```bash theme={null}
npx megallm@latest
```
npm cache has an outdated version.
**Solution:**
```bash theme={null}
# Clear npm cache
npm cache clean --force
# Run latest version
npx megallm@latest
# Or update global installation
npm update -g megallm
# Verify version
npx megallm@latest --version
```
## Configuration Issues
Configuration files aren't being read.
**Diagnosis:**
```bash theme={null}
# Check if files exist
ls -la ~/.claude/settings.json
ls -la ~/.codex/config.toml
ls -la ~/.config/opencode/opencode.json
# Check project-level configs
ls -la .claude/settings.json
ls -la opencode.json
```
**Solutions:**
**1. Verify file locations:**
```bash theme={null}
# Claude Code
~/.claude/settings.json # System
./.claude/settings.json # Project
# Codex
~/.codex/config.toml # System only
# OpenCode
~/.config/opencode/opencode.json # System
./opencode.json # Project
```
**2. Check file permissions:**
```bash theme={null}
chmod 644 ~/.claude/settings.json
chmod 644 ~/.codex/config.toml
chmod 644 ~/.config/opencode/opencode.json
```
**3. Validate syntax:**
```bash theme={null}
# JSON files
jq . ~/.claude/settings.json
# TOML files
cat ~/.codex/config.toml
# (install toml-cli for validation)
```
**4. Check working directory:**
```bash theme={null}
pwd
# For project-level configs, you must be in the project directory
```
API key isn't being read from config or environment.
**Diagnosis:**
```bash theme={null}
# Check environment variables
echo $ANTHROPIC_API_KEY
echo $MEGALLM_API_KEY
# Check config files
jq '.env.ANTHROPIC_API_KEY' ~/.claude/settings.json
cat ~/.codex/config.toml | grep MEGALLM_API_KEY
jq '.provider.anthropic.options.apiKey' ~/.config/opencode/opencode.json
```
**Solutions:**
**1. Reload shell configuration:**
```bash theme={null}
source ~/.bashrc # or ~/.zshrc
# Or restart your terminal
```
**2. Verify API key format:**
* Must start with `sk-mega-`
* At least 20 characters long
* No spaces or quotes around the key
**3. Set environment variable manually:**
```bash theme={null}
# Claude Code
export ANTHROPIC_API_KEY="sk-mega-your-key"
# Codex/Windsurf and OpenCode
export MEGALLM_API_KEY="sk-mega-your-key"
```
**4. Test API key directly:**
```bash theme={null}
curl -H "Authorization: Bearer sk-mega-your-key" \
https://ai.megallm.io/v1/models
```
Tool is connecting to the wrong API endpoint.
**Diagnosis:**
```bash theme={null}
# Check environment variables
echo $ANTHROPIC_BASE_URL
# Check config files
jq '.env.ANTHROPIC_BASE_URL' ~/.claude/settings.json
cat ~/.codex/config.toml | grep base_url
jq '.provider.anthropic.options.baseURL' ~/.config/opencode/opencode.json
```
**Solution:**
Ensure base URL is exactly:
```
https://ai.megallm.io
```
**Common mistakes:**
```bash theme={null}
# Wrong
https://ai.megallm.io/ # Trailing slash
https://ai.megallm.io/v1/ # Extra /v1/ (Codex/OpenCode only)
http://ai.megallm.io # HTTP instead of HTTPS
# Correct for Claude Code
https://ai.megallm.io
# Correct for Codex/OpenCode
https://ai.megallm.io/v1
```
**Fix:**
```bash theme={null}
# Claude Code
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
# Update config files with correct URL
```
Wrong configuration is being used.
**Understanding Priority:**
Variables set in shell always take precedence
Config in current directory (`.claude/`, `opencode.json`)
Global config in home directory (`~/.claude/`, `~/.codex/`, `~/.config/opencode/`)
**Diagnosis:**
```bash theme={null}
# Check what's set where
echo "ENV VAR: $ANTHROPIC_API_KEY"
echo "PROJECT: $(jq -r '.env.ANTHROPIC_API_KEY' .claude/settings.json 2>/dev/null)"
echo "SYSTEM: $(jq -r '.env.ANTHROPIC_API_KEY' ~/.claude/settings.json 2>/dev/null)"
```
**Solution:**
Remove conflicting configurations or use the right priority level:
```bash theme={null}
# To force project-level, unset environment variable
unset ANTHROPIC_API_KEY
# To force environment variable, set it
export ANTHROPIC_API_KEY="sk-mega-your-key"
```
## CLI Execution Issues
CLI is stuck during execution.
**Solutions:**
**1. Cancel and retry:**
```bash theme={null}
# Press Ctrl+C to cancel
# Then run again
npx megallm@latest
```
**2. Check for prompts:**
The CLI may be waiting for input. Look for questions like:
* "Enter your API key:"
* "Continue? (y/n)"
**3. Run with debug mode:**
```bash theme={null}
DEBUG=* npx megallm@latest
# Shows detailed logs of what's happening
```
**4. Check for background processes:**
```bash theme={null}
# Check if another instance is running
ps aux | grep megallm
```
CLI says a tool isn't installed but it is.
**Diagnosis:**
```bash theme={null}
# Check if tool is globally installed
npm list -g --depth=0 | grep claude
npm list -g --depth=0 | grep codex
npm list -g --depth=0 | grep opencode
# Check if command is available
which claude-code
which codex
which windsurf
which opencode
```
**Solutions:**
**1. Ensure global installation:**
```bash theme={null}
npm install -g @anthropic-ai/claude-code
npm install -g @codeium/windsurf
npm install -g opencode
```
**2. Restart terminal:**
```bash theme={null}
# Close and reopen terminal
# Or reload shell config
source ~/.bashrc
```
**3. Check PATH:**
```bash theme={null}
echo $PATH
# Should include npm global bin directory
```
**4. Manual configuration:**
If detection fails, configure manually without the CLI.
CLI can't write to configuration directories.
**Solution:**
**macOS/Linux:**
```bash theme={null}
# Fix ownership
sudo chown -R $USER ~/.claude ~/.codex ~/.config/opencode
# Fix permissions
chmod -R 755 ~/.claude ~/.codex ~/.config/opencode
# For project configs
chmod -R 755 .claude
```
**Windows:**
Run terminal as Administrator or fix folder permissions in File Explorer.
## API Connection Issues
Can't connect to MegaLLM API.
**Diagnosis:**
```bash theme={null}
# Test API connection
curl -v https://ai.megallm.io/v1/models \
-H "Authorization: Bearer sk-mega-your-key"
```
**Solutions:**
**1. Check internet connection:**
```bash theme={null}
ping -c 3 ai.megallm.io
```
**2. Check firewall/proxy:**
```bash theme={null}
# If behind a proxy, set npm proxy
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
```
**3. Verify API key:**
* Go to [megallm.io/dashboard](https://megallm.io/dashboard)
* Generate a new API key
* Update your configuration
**4. Check API status:**
Visit [megallm.io/status](https://megallm.io/status) for service status
API key is invalid or expired.
**Solution:**
**1. Generate new API key:**
* Go to [megallm.io/dashboard](https://megallm.io/dashboard)
* Create new API key
* Update configuration
**2. Update config:**
```bash theme={null}
# Run CLI again to reconfigure
npx megallm@latest
# Or update manually
jq '.env.ANTHROPIC_API_KEY = "new-key"' ~/.claude/settings.json > tmp.json && mv tmp.json ~/.claude/settings.json
```
**3. Clear and reset:**
```bash theme={null}
# Remove old config
rm ~/.claude/settings.json ~/.claude.json
rm ~/.codex/config.toml
rm ~/.config/opencode/opencode.json
# Run CLI fresh
npx megallm@latest
```
Too many API requests in a short time.
**Solution:**
**1. Wait and retry:**
Rate limits reset after a period (usually 1 minute).
**2. Check your usage:**
* Go to [megallm.io/dashboard](https://megallm.io/dashboard)
* View API usage statistics
**3. Upgrade plan:**
If you consistently hit rate limits, consider upgrading your plan.
**4. Implement retry logic:**
```python theme={null}
# In your application code
import time
import openai
for i in range(3):
try:
response = client.chat.completions.create(...)
break
except openai.RateLimitError:
if i < 2:
time.sleep(2 ** i) # Exponential backoff
else:
raise
```
## Shell and Environment Issues
Variables are lost after closing terminal.
**Solution:**
Environment variables must be added to shell config files:
```bash theme={null}
# Determine your shell
echo $SHELL
# bash: ~/.bashrc or ~/.bash_profile
echo 'export ANTHROPIC_API_KEY="sk-mega-..."' >> ~/.bashrc
source ~/.bashrc
# zsh: ~/.zshrc
echo 'export ANTHROPIC_API_KEY="sk-mega-..."' >> ~/.zshrc
source ~/.zshrc
# fish: ~/.config/fish/config.fish
echo 'set -x ANTHROPIC_API_KEY "sk-mega-..."' >> ~/.config/fish/config.fish
source ~/.config/fish/config.fish
```
**Verify:**
```bash theme={null}
# Close terminal
# Open new terminal
echo $ANTHROPIC_API_KEY
# Should show your key
```
Changes to shell config aren't taking effect.
**Solutions:**
**1. Reload shell config:**
```bash theme={null}
# bash
source ~/.bashrc
# zsh
source ~/.zshrc
# fish
source ~/.config/fish/config.fish
```
**2. Restart terminal:**
Close and reopen your terminal application.
**3. Check for syntax errors:**
```bash theme={null}
# bash/zsh
bash -n ~/.bashrc # Check syntax
bash -n ~/.zshrc
# fish
fish -n ~/.config/fish/config.fish
```
**4. Check file was actually modified:**
```bash theme={null}
tail -20 ~/.bashrc
# Should show your recent additions
```
## Platform-Specific Issues
PATH isn't updated for npm global packages.
**Solution:**
```bash theme={null}
# Add npm global bin to PATH
echo 'export PATH="$PATH:$(npm config get prefix)/bin"' >> ~/.zshrc
source ~/.zshrc
# Or for bash
echo 'export PATH="$PATH:$(npm config get prefix)/bin"' >> ~/.bashrc
source ~/.bashrc
```
Scripts are blocked by PowerShell execution policy.
**Solution:**
```powershell theme={null}
# Run PowerShell as Administrator
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Then run CLI
npx megallm@latest
```
Can't install global packages due to permissions.
**Solution:**
```bash theme={null}
# Option 1: Use nvm (recommended)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install 18
nvm use 18
# Option 2: Change npm prefix
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
```
## Advanced Troubleshooting
### Enable Debug Mode
Get detailed logs to diagnose issues:
```bash theme={null}
# Run with debug output
DEBUG=* npx megallm@latest
# Or set environment variable
export DEBUG=*
npx megallm@latest
```
### Complete Reset
If all else fails, completely reset configuration:
```bash theme={null}
# 1. Backup existing configs
mkdir ~/megallm-backup
cp -r ~/.claude ~/megallm-backup/
cp -r ~/.codex ~/megallm-backup/
cp -r ~/.config/opencode ~/megallm-backup/
# 2. Remove all configs
rm -rf ~/.claude
rm -rf ~/.codex
rm -rf ~/.config/opencode
rm -rf .claude
rm -f opencode.json
# 3. Remove environment variables
# Edit ~/.bashrc or ~/.zshrc and remove lines with:
# - ANTHROPIC_BASE_URL
# - ANTHROPIC_API_KEY
# - MEGALLM_API_KEY
# 4. Reload shell
source ~/.bashrc
# 5. Run CLI fresh
npx megallm@latest
```
### Collect Diagnostic Information
For support requests, collect this information:
```bash theme={null}
# System info
uname -a
node --version
npm --version
# Check configs
ls -la ~/.claude/ ~/.codex/ ~/.config/opencode/
cat ~/.claude/settings.json
cat ~/.codex/config.toml
cat ~/.config/opencode/opencode.json
# Check environment
env | grep -E "ANTHROPIC|MEGALLM"
# Run with debug
DEBUG=* npx megallm@latest 2>&1 | tee megallm-debug.log
```
## Still Need Help?
If you're still experiencing issues:
Common questions and answers
Get help from our team
Report bugs or search existing issues
Ask the community
## Prevention Tips
Regularly update Node.js, npm, and AI tools
Keep backups of working configurations
Verify configuration after manual edits
Commit working configs (without API keys)
# Messages
Source: https://docs.megallm.io/en/dev-docs/anthropic/messages
The Messages API is the core interface for interacting with Claude models. Send messages and receive AI-generated responses with support for conversations, tool use, and vision.
## Basic Usage
```python Python theme={null}
from anthropic import Anthropic
client = Anthropic(
base_url="https://ai.megallm.io",
api_key="your-api-key"
)
message = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
messages=[
{
"role": "user",
"content": "What are the primary colors?"
}
]
)
print(message.content[0].text)
```
```javascript JavaScript theme={null}
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
baseURL: 'https://ai.megallm.io',
apiKey: process.env.MEGALLM_API_KEY,
});
const message = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 100,
messages: [
{
role: 'user',
content: 'What are the primary colors?'
}
]
});
console.log(message.content[0].text);
```
```bash cURL theme={null}
curl https://ai.megallm.io/v1/messages \
-H "x-api-key: $MEGALLM_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-3-sonnet-20240229",
"max_tokens": 100,
"messages": [
{
"role": "user",
"content": "What are the primary colors?"
}
]
}'
```
```ruby Ruby theme={null}
require 'anthropic'
client = Anthropic::Client.new(
base_url: 'https://ai.megallm.io',
api_key: ENV['MEGALLM_API_KEY']
)
message = client.messages.create(
model: 'claude-3-sonnet-20240229',
max_tokens: 100,
messages: [
{
role: 'user',
content: 'What are the primary colors?'
}
]
)
puts message.content.first.text
```
## Message Structure
### Content Types
Messages can contain different types of content:
```python theme={null}
# Simple text message
message = {
"role": "user",
"content": "Hello, Claude!"
}
# Multi-part message with text and image
message = {
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "base64_encoded_image_data"
}
}
]
}
```
### System Messages
System messages set the assistant's behavior:
```python theme={null}
message = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=200,
system="""You are a helpful coding assistant.
Always provide code examples when explaining concepts.
Use Python unless specified otherwise.""",
messages=[
{
"role": "user",
"content": "Explain list comprehensions"
}
]
)
```
## Advanced Features
### Multi-turn Conversations
Build context-aware conversations:
```python theme={null}
class Conversation:
def __init__(self, client, model="claude-3-sonnet-20240229"):
self.client = client
self.model = model
self.messages = []
self.system = "You are a helpful assistant."
def add_user_message(self, content):
self.messages.append({"role": "user", "content": content})
def get_response(self, max_tokens=200):
response = self.client.messages.create(
model=self.model,
max_tokens=max_tokens,
system=self.system,
messages=self.messages
)
assistant_content = response.content[0].text
self.messages.append({"role": "assistant", "content": assistant_content})
return assistant_content
# Usage
conv = Conversation(client)
conv.system = "You are a math tutor."
print(conv.get_response("What is calculus?"))
print(conv.get_response("Can you give me an example?"))
print(conv.get_response("How is it different from algebra?"))
```
### Prefilled Responses
Guide the assistant's response format:
```python theme={null}
messages = [
{"role": "user", "content": "Write a haiku about programming"},
{"role": "assistant", "content": "Here's a haiku about programming:\n\n"} # Prefill
]
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=50,
messages=messages
)
# The response will continue from the prefilled content
```
### Token Counting
Estimate token usage before sending:
```python theme={null}
def estimate_tokens(messages):
"""Rough estimation of token count"""
total = 0
for message in messages:
if isinstance(message["content"], str):
# Rough estimate: 1 token ≈ 4 characters
total += len(message["content"]) // 4
elif isinstance(message["content"], list):
for part in message["content"]:
if part["type"] == "text":
total += len(part["text"]) // 4
return total
# Check before sending
token_estimate = estimate_tokens(messages)
if token_estimate > 180000: # Leave room for response
print("Warning: Approaching context limit")
```
## Response Handling
### Parse Response Content
```python theme={null}
response = client.messages.create(...)
# Handle different content types
for content_block in response.content:
if content_block.type == "text":
print(f"Text: {content_block.text}")
elif content_block.type == "tool_use":
print(f"Tool call: {content_block.name}")
print(f"Arguments: {content_block.input}")
```
### Error Handling
```python theme={null}
from anthropic import APIError, RateLimitError, APIConnectionError
def safe_message_create(client, **kwargs):
max_retries = 3
retry_delay = 1
for attempt in range(max_retries):
try:
return client.messages.create(**kwargs)
except RateLimitError as e:
if attempt < max_retries - 1:
time.sleep(retry_delay * (2 ** attempt))
continue
raise e
except APIConnectionError as e:
print(f"Connection error: {e}")
if attempt < max_retries - 1:
time.sleep(retry_delay)
continue
raise e
except APIError as e:
print(f"API error: {e}")
raise e
```
## Common Patterns
### Structured Data Extraction
```python theme={null}
def extract_information(text):
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=300,
system="Extract information and return as JSON.",
messages=[
{
"role": "user",
"content": f"""Extract the following from this text:
- Names (list)
- Dates (list)
- Locations (list)
- Key facts (list)
Return as JSON.
Text: {text}"""
}
],
temperature=0 # Consistent formatting
)
import json
return json.loads(response.content[0].text)
```
### Translation
```python theme={null}
def translate(text, target_language="Spanish"):
response = client.messages.create(
model="claude-3.5-sonnet", # Fast model for translation
max_tokens=500,
messages=[
{
"role": "user",
"content": f"Translate to {target_language}:\n\n{text}"
}
],
temperature=0.3
)
return response.content[0].text
```
### Summarization
```python theme={null}
def summarize(text, style="bullet_points"):
styles = {
"bullet_points": "Create a bullet-point summary",
"paragraph": "Write a concise paragraph summary",
"executive": "Write an executive summary",
"eli5": "Explain like I'm 5"
}
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=300,
system="You are a professional summarizer.",
messages=[
{
"role": "user",
"content": f"{styles.get(style, styles['bullet_points'])}:\n\n{text}"
}
],
temperature=0.3
)
return response.content[0].text
```
## Best Practices
**Token Efficiency**: Claude models have large context windows but being concise in prompts saves tokens and improves response time.
### 1. Clear Instructions
````python theme={null}
# Good - Clear and specific
messages = [{
"role": "user",
"content": """Analyze this Python code:
1. Identify any bugs
2. Suggest performance improvements
3. Rate code quality (1-10)
Code:
```python
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
```"""
}]
# Less effective - Vague
messages = [{
"role": "user",
"content": "Look at this fibonacci function and tell me about it"
}]
````
### 2. Temperature Settings
```python theme={null}
# Factual/analytical tasks
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=200,
temperature=0, # Deterministic
messages=messages
)
# Creative tasks
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=200,
temperature=0.8, # More creative
messages=messages
)
```
### 3. Model Selection
| Use Case | Recommended Model | Why |
| ---------------- | ----------------- | -------------------- |
| Complex analysis | claude-3-opus | Best reasoning |
| General tasks | claude-3-sonnet | Balanced performance |
| High volume | claude-3-haiku | Fast and efficient |
| Code generation | claude-3-sonnet | Good balance |
## Rate Limiting
Implement proper rate limiting:
```python theme={null}
from threading import Lock
from collections import deque
import time
class RateLimiter:
def __init__(self, max_requests=50, window=60):
self.max_requests = max_requests
self.window = window
self.requests = deque()
self.lock = Lock()
def wait_if_needed(self):
with self.lock:
now = time.time()
# Remove old requests
while self.requests and self.requests[0] < now - self.window:
self.requests.popleft()
if len(self.requests) >= self.max_requests:
sleep_time = self.window - (now - self.requests[0])
if sleep_time > 0:
time.sleep(sleep_time)
return self.wait_if_needed()
self.requests.append(now)
# Usage
rate_limiter = RateLimiter(max_requests=50, window=60)
def create_message(**kwargs):
rate_limiter.wait_if_needed()
return client.messages.create(**kwargs)
```
## Next Steps
* Learn about [Tool Use](/anthropic/tool-use) for function calling
* Explore [Vision](/anthropic/vision) capabilities
* Implement [Streaming](/anthropic/streaming) for real-time responses
# Anthropic API
Source: https://docs.megallm.io/en/dev-docs/anthropic/overview
MegaLLM provides full compatibility with Anthropic's Claude API format, enabling you to use Claude models through our infrastructure.
**Base URL**: `https://ai.megallm.io` for all Anthropic-compatible endpoints
## Available Endpoints
Create conversational messages with Claude models
Enable Claude to interact with external tools and functions
## Quick Example
```python theme={null}
from anthropic import Anthropic
# Initialize client
client = Anthropic(
base_url="https://ai.megallm.io",
api_key="your-api-key"
)
# Create a message
message = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Explain the theory of relativity in simple terms"
}
]
)
print(message.content[0].text)
```
## Supported Models
| Model ID | Context Window | Use Case |
| -------------------------- | -------------- | -------------------------- |
| `claude-opus-4-1-20250805` | 200K tokens | Complex analysis, research |
| `claude-3.5-sonnet` | 200K tokens | Balanced performance |
| `claude-3.7-sonnet` | 200K tokens | Fast, efficient responses |
| `claude-sonnet-4` | 200K tokens | Advanced generation |
## Features
### Advanced Reasoning
Claude's sophisticated reasoning capabilities for complex tasks.
### Large Context Window
Process up to 200K tokens for extensive document analysis.
### Tool Use
Native support for function calling and tool integration.
### Vision Capabilities
Analyze images and visual content alongside text.
## SDK Support
MegaLLM works with Anthropic-compatible SDKs:
* **Python**: `anthropic` official SDK
* **TypeScript/JavaScript**: `@anthropic-ai/sdk`
* **Go**: Community SDKs
* **Ruby**: `anthropic-rb`
## Key Differences from OpenAI
### Message Format
Anthropic uses a slightly different message format:
```python theme={null}
# Anthropic format
messages = [
{
"role": "user",
"content": "Hello, Claude!"
}
]
# System messages are separate
system = "You are a helpful assistant"
message = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
system=system, # System prompt is separate
messages=messages
)
```
### Response Format
```python theme={null}
# Anthropic response structure
response = {
"id": "msg_123",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! How can I help you today?"
}
],
"model": "claude-3.5-sonnet",
"usage": {
"input_tokens": 10,
"output_tokens": 25
}
}
```
### Tool Use Format
```python theme={null}
tools = [
{
"name": "get_weather",
"description": "Get weather for a location",
"input_schema": { # Note: input_schema, not parameters
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
},
"required": ["location"]
}
}
]
```
## Migration Guide
Migrating from Anthropic to MegaLLM:
```python theme={null}
# Before (Anthropic Cloud)
client = Anthropic(api_key="sk-ant-...")
# After (MegaLLM)
client = Anthropic(
base_url="https://ai.megallm.io",
api_key="your-api-key"
)
```
All your existing Anthropic code continues to work!
## Authentication
Use the `x-api-key` header for Anthropic format:
```bash theme={null}
curl https://ai.megallm.io/v1/messages \
-H "x-api-key: $MEGALLM_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-3.5-sonnet",
"max_tokens": 100,
"messages": [
{"role": "user", "content": "Hello!"}
]
}'
```
## Rate Limits
| Tier | Requests/min | Tokens/min | Concurrent |
| ---------- | ------------ | ---------- | ---------- |
| Basic | 50 | 100,000 | 10 |
| Pro | 200 | 400,000 | 40 |
| Enterprise | Custom | Custom | Custom |
## Error Handling
MegaLLM returns Anthropic-compatible error responses:
```json theme={null}
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "max_tokens is required"
}
}
```
## Advanced Features
### Conversation History
Maintain context across multiple turns:
```python theme={null}
conversation = []
def chat(user_input):
conversation.append({"role": "user", "content": user_input})
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=150,
messages=conversation
)
assistant_message = response.content[0].text
conversation.append({"role": "assistant", "content": assistant_message})
return assistant_message
# Usage
print(chat("What's the capital of France?"))
print(chat("What's its population?")) # Knows "its" refers to Paris
```
### Temperature and Sampling
Control response creativity:
```python theme={null}
# More deterministic
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
temperature=0.0, # Very consistent
messages=messages
)
# More creative
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
temperature=1.0, # More varied
top_p=0.95, # Nucleus sampling
messages=messages
)
```
## Use Cases
### Document Analysis
```python theme={null}
def analyze_document(document_text):
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=500,
system="You are a document analysis expert.",
messages=[
{
"role": "user",
"content": f"""Analyze this document and provide:
1. Main topics
2. Key insights
3. Summary
Document: {document_text}"""
}
]
)
return response.content[0].text
```
### Code Review
````python theme={null}
def review_code(code):
response = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=800,
system="You are an expert code reviewer.",
messages=[
{
"role": "user",
"content": f"""Review this code for:
- Bugs
- Performance issues
- Best practices
- Security concerns
Code:
```python
{code}
```"""
}
]
)
return response.content[0].text
````
**Pro Tip**: Claude excels at tasks requiring careful reasoning, long context understanding, and nuanced responses.
## Best Practices
1. **Use System Prompts**: Claude responds well to clear system instructions
2. **Leverage Context Window**: Take advantage of the 200K token context for large documents
3. **Structured Prompts**: Use clear formatting and numbered lists for complex requests
4. **Temperature Settings**: Use lower temperatures (0-0.3) for factual tasks
5. **Model Selection**: Choose Opus for complex reasoning, Sonnet for balance, Haiku for speed
## Next Steps
* Explore [Messages API](/dev-docs/anthropic/messages) for conversational AI
* Learn about [Function Calling](/api-reference/endpoint/function-calling) for tool use
* Implement [Streaming](/api-reference/endpoint/streaming) for real-time responses
# Authentication
Source: https://docs.megallm.io/en/dev-docs/getting-started/authentication
MegaLLM uses API keys for authentication. This guide covers all authentication methods and best practices.
## Authentication Methods
### Bearer Token
The most common authentication method is using a Bearer token in the Authorization header:
```http theme={null}
Authorization: Bearer YOUR_API_KEY
```
### API Key Header (Anthropic Format)
For Anthropic-compatible endpoints, you can also use the `x-api-key` header:
```http theme={null}
x-api-key: YOUR_API_KEY
```
## Getting Your API Key
The recommended method for obtaining an API key is through the MegaLLM dashboard:
1. Visit [megallm.io/dashboard](https://megallm.io/dashboard/overview)
2. Navigate to API Keys section
3. Click "Create New API Key"
4. Copy the key (starts with `sk-mega-`) and store it securely
If you have the MegaLLM CLI installed:
```bash theme={null}
npx megallm@latest
```
Follow the interactive prompts to set up your API key.
## Security Best Practices
**Never expose your tokens**: Always store tokens in environment variables or secure vaults, never in code.
### Environment Variables
```bash theme={null}
# Add to ~/.bashrc or ~/.zshrc
export MEGALLM_API_KEY="your_api_key_here"
# Or use a .env file
echo "MEGALLM_API_KEY=your_api_key_here" >> .env
```
```powershell theme={null}
# Set environment variable
[System.Environment]::SetEnvironmentVariable("MEGALLM_API_KEY", "your_api_key_here", "User")
# Or use command prompt
setx MEGALLM_API_KEY "your_api_key_here"
```
```dockerfile theme={null}
# In Dockerfile
ENV MEGALLM_API_KEY=${MEGALLM_API_KEY}
# Or in docker-compose.yml
environment:
- MEGALLM_API_KEY=${MEGALLM_API_KEY}
```
## Using SDKs
### OpenAI SDK
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key=os.getenv("MEGALLM_API_KEY")
)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
```
### Anthropic SDK
```python theme={null}
from anthropic import Anthropic
client = Anthropic(
base_url="https://ai.megallm.io",
api_key=os.getenv("MEGALLM_API_KEY")
)
message = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
messages=[{"role": "user", "content": "Hello!"}]
)
```
### LangChain Integration
```python theme={null}
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://ai.megallm.io/v1",
api_key=os.getenv("MEGALLM_API_KEY"),
model="gpt-4"
)
response = llm.invoke("Hello!")
```
## Troubleshooting
### Common Authentication Errors
| Error Code | Message | Solution |
| ---------- | ------------ | ---------------------------------------------- |
| 401 | Unauthorized | Check if your API key is valid and not expired |
| 403 | Forbidden | Verify API key has required access |
| 429 | Rate Limited | Wait and retry or contact support |
### Debugging Authentication
Enable debug mode to see detailed authentication information:
```bash theme={null}
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "X-Debug-Auth: true" \
-d '{"model": "gpt-4", "messages": [...]}'
```
**Need Help?** Check our [FAQ](/faq#authentication) or contact support if you're experiencing authentication issues.
# Quick Start
Source: https://docs.megallm.io/en/dev-docs/getting-started/quick-start
Get up and running with MegaLLM API in just a few minutes. This guide will walk you through the initial setup and your first API call.
**Prerequisites**: You'll need a MegaLLM API key to use our services.
## Installation
First, you'll need to obtain your MegaLLM API key. This key will be used to authenticate all your API requests.
```bash theme={null}
export MEGALLM_API_KEY="your-api-key"
```
See our [Authentication Guide](/dev-docs/getting-started/authentication) for detailed instructions on obtaining your API key.
MegaLLM supports both OpenAI and Anthropic API formats. Choose the one that best fits your needs:
Set your base URL to use OpenAI-compatible endpoints:
```bash theme={null}
export MEGALLM_BASE_URL="https://ai.megallm.io/v1"
# Use your MegaLLM API key
export MEGALLM_API_KEY="your-api-key"
```
Set your base URL to use Anthropic-compatible endpoints:
```bash theme={null}
export ANTHROPIC_BASE_URL="https://ai.megallm.io"
export ANTHROPIC_API_KEY=$MEGALLM_API_KEY
```
Now you're ready to make your first API call!
```bash cURL theme={null}
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Hello! Can you introduce yourself?"
}
],
"max_tokens": 100
}'
```
```python Python theme={null}
import requests
import os
response = requests.post(
"https://ai.megallm.io/v1/chat/completions",
headers={
"Authorization": f"Bearer {os.getenv('MEGALLM_API_KEY')}",
"Content-Type": "application/json"
},
json={
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Hello! Can you introduce yourself?"
}
],
"max_tokens": 100
}
)
print(response.json())
```
```javascript JavaScript theme={null}
const response = await fetch("https://ai.megallm.io/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.MEGALLM_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-4",
messages: [
{
role: "user",
content: "Hello! Can you introduce yourself?"
}
],
max_tokens: 100
})
});
const data = await response.json();
console.log(data);
```
```go Go theme={null}
package main
import (
"bytes"
"encoding/json"
"net/http"
"os"
)
func main() {
payload := map[string]interface{}{
"model": "gpt-4",
"messages": []map[string]string{
{
"role": "user",
"content": "Hello! Can you introduce yourself?",
},
},
"max_tokens": 100,
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST",
"https://ai.megallm.io/v1/chat/completions",
bytes.NewBuffer(body))
req.Header.Set("Authorization", "Bearer " + os.Getenv("MEGALLM_API_KEY"))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
}
```
If everything is set up correctly, you should receive a response like this:
```json theme={null}
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm an AI assistant powered by MegaLLM..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 25,
"total_tokens": 35
}
}
```
## Next Steps
Learn about authentication methods and API key management
Explore the OpenAI-compatible endpoints
Discover Anthropic Claude API features
Common questions and best practices
## Common Issues
**Rate Limiting**: If you encounter rate limit errors, check our [FAQ](/home/faq) for guidance.
**Authentication Failed**: Make sure your API key is valid and has the necessary permissions. Check our [Authentication Guide](/dev-docs/getting-started/authentication) for solutions.
# Chat Completions
Source: https://docs.megallm.io/en/dev-docs/openai/chat-completions
The Chat Completions API enables you to build conversational experiences using GPT models. Send a list of messages and receive an AI-generated response.
## Basic Usage
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the weather like?"}
],
temperature=0.7,
max_tokens=150
)
print(response.choices[0].message.content)
```
```javascript theme={null}
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY,
});
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: "What's the weather like?" }
],
temperature: 0.7,
max_tokens: 150
});
console.log(response.choices[0].message.content);
```
```bash theme={null}
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What'\''s the weather like?"}
],
"temperature": 0.7,
"max_tokens": 150
}'
```
```go theme={null}
package main
import (
"context"
openai "github.com/sashabaranov/go-openai"
)
func main() {
config := openai.DefaultConfig("your-api-key")
config.BaseURL = "https://ai.megallm.io/v1"
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "gpt-4",
Messages: []openai.ChatCompletionMessage{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "What's the weather like?",
},
},
Temperature: 0.7,
MaxTokens: 150,
},
)
if err != nil {
panic(err)
}
println(resp.Choices[0].Message.Content)
}
```
## Advanced Features
### Message Roles
The API supports different message roles for conversation context:
| Role | Description | Example |
| ----------- | ------------------------- | -------------------------------- |
| `system` | Sets behavior and context | "You are a helpful assistant" |
| `user` | User input/questions | "What's the capital of France?" |
| `assistant` | AI responses | "The capital of France is Paris" |
| `tool` | Tool/function results | Function execution results |
### Temperature Control
Adjust response creativity with the temperature parameter:
```python theme={null}
# More deterministic (0.0 - 0.3)
response = client.chat.completions.create(
model="gpt-4",
messages=[...],
temperature=0.2 # More focused, consistent responses
)
# Balanced (0.4 - 0.7)
response = client.chat.completions.create(
model="gpt-4",
messages=[...],
temperature=0.5 # Balanced creativity and coherence
)
# More creative (0.8 - 1.0)
response = client.chat.completions.create(
model="gpt-4",
messages=[...],
temperature=0.9 # More varied, creative responses
)
```
### Multi-turn Conversations
Maintain context across multiple exchanges:
```python theme={null}
messages = [
{"role": "system", "content": "You are a math tutor."},
{"role": "user", "content": "What is 15 * 12?"},
{"role": "assistant", "content": "15 * 12 = 180"},
{"role": "user", "content": "Now divide that by 6"}
]
response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
# Response understands "that" refers to 180
```
### Vision Support
Process images alongside text (requires vision-capable models):
```python theme={null}
response = client.chat.completions.create(
model="gpt-4-vision",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg",
"detail": "high" # "low", "high", or "auto"
}
}
]
}
],
max_tokens=300
)
```
## Response Format
### Standard Response
```json theme={null}
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4",
"usage": {
"prompt_tokens": 13,
"completion_tokens": 17,
"total_tokens": 30
},
"choices": [
{
"message": {
"role": "assistant",
"content": "The capital of France is Paris."
},
"finish_reason": "stop",
"index": 0
}
]
}
```
### Finish Reasons
| Reason | Description |
| ---------------- | ------------------------------ |
| `stop` | Natural completion |
| `length` | Hit max\_tokens limit |
| `tool_calls` | Model wants to call a function |
| `content_filter` | Content was filtered |
## Best Practices
**System Messages**: Always include a clear system message to set the AI's behavior and context.
### Token Optimization
```python theme={null}
# Count tokens before sending
import tiktoken
encoding = tiktoken.encoding_for_model("gpt-4")
tokens = encoding.encode("Your message here")
print(f"Token count: {len(tokens)}")
# Truncate if needed
if len(tokens) > 1000:
truncated = encoding.decode(tokens[:1000])
```
### Error Handling
```python theme={null}
try:
response = client.chat.completions.create(...)
except openai.APIError as e:
print(f"API error: {e}")
except openai.RateLimitError as e:
print(f"Rate limit hit: {e}")
# Implement exponential backoff
except openai.APIConnectionError as e:
print(f"Connection error: {e}")
# Retry with backoff
```
## Common Patterns
### Summarization
```python theme={null}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a summarization expert."},
{"role": "user", "content": f"Summarize this text in 3 bullet points: {long_text}"}
],
temperature=0.3,
max_tokens=150
)
```
### Classification
```python theme={null}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Classify the sentiment as positive, negative, or neutral."},
{"role": "user", "content": "I love this product! It works great."}
],
temperature=0.0,
max_tokens=10
)
```
### Code Generation
```python theme={null}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a Python expert."},
{"role": "user", "content": "Write a function to calculate fibonacci numbers"}
],
temperature=0.2,
max_tokens=500
)
```
## Rate Limiting
Implement proper rate limiting to avoid errors:
```python theme={null}
import time
from typing import Optional
class RateLimiter:
def __init__(self, requests_per_minute: int = 60):
self.requests_per_minute = requests_per_minute
self.request_times = []
def wait_if_needed(self):
now = time.time()
# Remove requests older than 1 minute
self.request_times = [t for t in self.request_times if now - t < 60]
if len(self.request_times) >= self.requests_per_minute:
sleep_time = 60 - (now - self.request_times[0])
if sleep_time > 0:
time.sleep(sleep_time)
self.request_times.append(now)
# Usage
limiter = RateLimiter(60)
for prompt in prompts:
limiter.wait_if_needed()
response = client.chat.completions.create(...)
```
## Next Steps
* Learn about [Streaming](/openai/streaming) for real-time responses
* Explore [Function Calling](/openai/function-calling) for tool integration
* Implement [Structured Output](/openai/structured-output) for validated JSON responses
# Function Calling
Source: https://docs.megallm.io/en/dev-docs/openai/function-calling
Enable your AI assistant to interact with external tools, APIs, and functions. Perfect for building agents that can perform actions, retrieve real-time data, and integrate with your existing systems.
**Parallel Function Calling**: MegaLLM supports parallel function execution for improved performance.
## How It Works
Specify functions the AI can use with JSON Schema descriptions.
The model determines when and how to use tools based on context.
Your application executes the function and returns results to the AI.
The AI incorporates function results into its final response.
## Basic Example
```python theme={null}
from openai import OpenAI
import json
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Define your functions
def get_weather(location: str, unit: str = "celsius"):
"""Get the current weather for a location"""
# Simulate API call
return {
"location": location,
"temperature": 22,
"unit": unit,
"condition": "sunny"
}
# Define tools for the AI
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}
}
]
# Send message with tools
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "What's the weather in London?"}
],
tools=tools,
tool_choice="auto" # Let the model decide
)
# Check if the model wants to call a function
message = response.choices[0].message
if message.tool_calls:
# Execute the function
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
if function_name == "get_weather":
result = get_weather(**function_args)
# Send the result back to the model
follow_up = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "What's the weather in London?"},
message, # Include the assistant's tool call
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
}
]
)
print(follow_up.choices[0].message.content)
```
```javascript theme={null}
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.GITHUB_TOKEN,
});
// Define your function
function getWeather(location, unit = 'celsius') {
// Simulate API call
return {
location,
temperature: 22,
unit,
condition: 'sunny'
};
}
// Define tools
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather in a given location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g. San Francisco, CA'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: 'Temperature unit'
}
},
required: ['location']
}
}
}
];
// Send message with tools
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: "What's the weather in London?" }
],
tools,
tool_choice: 'auto'
});
const message = response.choices[0].message;
if (message.tool_calls) {
const messages = [
{ role: 'user', content: "What's the weather in London?" },
message
];
for (const toolCall of message.tool_calls) {
const functionName = toolCall.function.name;
const functionArgs = JSON.parse(toolCall.function.arguments);
if (functionName === 'get_weather') {
const result = getWeather(functionArgs.location, functionArgs.unit);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result)
});
}
}
const followUp = await openai.chat.completions.create({
model: 'gpt-4',
messages
});
console.log(followUp.choices[0].message.content);
}
```
```typescript theme={null}
import OpenAI from 'openai';
interface WeatherParams {
location: string;
unit?: 'celsius' | 'fahrenheit';
}
interface WeatherResult {
location: string;
temperature: number;
unit: string;
condition: string;
}
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.GITHUB_TOKEN!,
});
// Type-safe function implementation
async function getWeather({ location, unit = 'celsius' }: WeatherParams): Promise {
// Real implementation would call an API
return {
location,
temperature: 22,
unit,
condition: 'sunny'
};
}
// Function registry
const functionRegistry = {
get_weather: getWeather
} as const;
// Tool definitions with proper typing
const tools: OpenAI.Chat.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather in a given location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g. San Francisco, CA'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: 'Temperature unit'
}
},
required: ['location']
}
}
}
];
async function handleToolCalls(message: OpenAI.Chat.ChatCompletionMessage) {
if (!message.tool_calls) return null;
const toolResults = await Promise.all(
message.tool_calls.map(async (toolCall) => {
const fn = functionRegistry[toolCall.function.name as keyof typeof functionRegistry];
if (!fn) throw new Error(`Unknown function: ${toolCall.function.name}`);
const args = JSON.parse(toolCall.function.arguments);
const result = await fn(args);
return {
role: 'tool' as const,
tool_call_id: toolCall.id,
content: JSON.stringify(result)
};
})
);
return toolResults;
}
// Usage
async function askAboutWeather() {
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: "What's the weather in London and Paris?" }],
tools,
tool_choice: 'auto'
});
const toolResults = await handleToolCalls(response.choices[0].message);
if (toolResults) {
const followUp = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: "What's the weather in London and Paris?" },
response.choices[0].message,
...toolResults
]
});
return followUp.choices[0].message.content;
}
return response.choices[0].message.content;
}
```
## Parallel Function Calling
MegaLLM supports calling multiple functions in parallel for better performance:
```python theme={null}
# The AI can call multiple functions at once
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "Compare the weather in London, Paris, and Tokyo"}
],
tools=tools
)
# Process multiple tool calls in parallel
if response.choices[0].message.tool_calls:
import asyncio
async def execute_tool(tool_call):
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
# Execute function asynchronously
result = await async_function_registry[function_name](**function_args)
return {
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
}
# Execute all functions in parallel
tool_results = await asyncio.gather(
*[execute_tool(tc) for tc in response.choices[0].message.tool_calls]
)
```
## Advanced Patterns
### Function Chaining
Build complex workflows by chaining function calls:
```python theme={null}
tools = [
{
"type": "function",
"function": {
"name": "search_products",
"description": "Search for products",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"category": {"type": "string"}
},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
"name": "check_inventory",
"description": "Check product inventory",
"parameters": {
"type": "object",
"properties": {
"product_id": {"type": "string"}
},
"required": ["product_id"]
}
}
},
{
"type": "function",
"function": {
"name": "place_order",
"description": "Place an order for a product",
"parameters": {
"type": "object",
"properties": {
"product_id": {"type": "string"},
"quantity": {"type": "integer"}
},
"required": ["product_id", "quantity"]
}
}
}
]
# The AI will chain these functions to complete complex tasks
messages = [
{"role": "user", "content": "Find and order 2 blue t-shirts if they're in stock"}
]
# The AI might:
# 1. Call search_products(query="blue t-shirt")
# 2. Call check_inventory(product_id="...")
# 3. Call place_order(product_id="...", quantity=2)
```
### Tool Choice Control
Control when and how the AI uses tools:
```python theme={null}
# Force a specific function
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
tool_choice={
"type": "function",
"function": {"name": "get_weather"}
}
)
# Let the AI decide (default)
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
tool_choice="auto"
)
# Prevent function calling
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
tool_choice="none"
)
# Force some function call (any)
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
tool_choice="required"
)
```
### Error Handling
Properly handle function execution errors:
```python theme={null}
def safe_function_call(function_name, arguments):
try:
result = function_registry[function_name](**arguments)
return {"success": True, "result": result}
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__
}
# In your tool response
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
result = safe_function_call(function_name, function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
# The AI will handle the error gracefully
```
## Real-World Examples
### Database Query Assistant
```python theme={null}
tools = [
{
"type": "function",
"function": {
"name": "execute_sql",
"description": "Execute a SQL query on the database",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "SQL query to execute"
},
"database": {
"type": "string",
"enum": ["users", "products", "orders"],
"description": "Target database"
}
},
"required": ["query", "database"]
}
}
}
]
# User: "How many orders were placed last month?"
# AI generates: execute_sql(
# query="SELECT COUNT(*) FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)",
# database="orders"
# )
```
### API Integration Agent
```python theme={null}
tools = [
{
"type": "function",
"function": {
"name": "call_api",
"description": "Make an HTTP API call",
"parameters": {
"type": "object",
"properties": {
"method": {
"type": "string",
"enum": ["GET", "POST", "PUT", "DELETE"]
},
"url": {"type": "string"},
"headers": {"type": "object"},
"body": {"type": "object"}
},
"required": ["method", "url"]
}
}
}
]
# The AI can now interact with any API
```
### File System Operations
```python theme={null}
tools = [
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read contents of a file",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
}
}
},
{
"type": "function",
"function": {
"name": "write_file",
"description": "Write content to a file",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"content": {"type": "string"},
"append": {"type": "boolean", "default": false}
},
"required": ["path", "content"]
}
}
}
]
```
## Best Practices
**Validation**: Always validate function arguments before execution to prevent errors.
### 1. Clear Function Descriptions
```python theme={null}
# Good
"description": "Get current weather data including temperature, humidity, and conditions for a specific location"
# Bad
"description": "Weather function"
```
### 2. Type Safety
```typescript theme={null}
// Define types for all function parameters
interface FunctionParams {
[key: string]: unknown;
}
// Validate before execution
function validateParams(
params: unknown,
schema: JSONSchema
): params is T {
// Implement JSON Schema validation
return ajv.validate(schema, params);
}
```
### 3. Rate Limiting
```python theme={null}
from functools import wraps
import time
def rate_limit(calls_per_second=1):
min_interval = 1.0 / calls_per_second
last_called = [0.0]
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
elapsed = time.time() - last_called[0]
left_to_wait = min_interval - elapsed
if left_to_wait > 0:
time.sleep(left_to_wait)
ret = func(*args, **kwargs)
last_called[0] = time.time()
return ret
return wrapper
return decorator
@rate_limit(calls_per_second=10)
def call_external_api():
# Your API call
pass
```
## Streaming with Functions
Combine streaming with function calling:
```python theme={null}
stream = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
stream=True
)
function_call = None
for chunk in stream:
delta = chunk.choices[0].delta
# Check for tool calls in stream
if delta.tool_calls:
if function_call is None:
function_call = {
"id": "",
"name": "",
"arguments": ""
}
tool_call = delta.tool_calls[0]
if tool_call.id:
function_call["id"] = tool_call.id
if tool_call.function.name:
function_call["name"] = tool_call.function.name
if tool_call.function.arguments:
function_call["arguments"] += tool_call.function.arguments
# When function call is complete
if function_call and chunk.choices[0].finish_reason == "tool_calls":
# Execute function
result = execute_function(function_call)
# Continue conversation with result
```
## Next Steps
* Implement [Structured Output](/openai/structured-output) for validated responses
* Explore [Embeddings](/openai/embeddings) for semantic search
* Learn about [Models](/openai/models) available for different use cases
# OpenAI API
Source: https://docs.megallm.io/en/dev-docs/openai/overview
MegaLLM provides full compatibility with OpenAI's API format, allowing you to use existing OpenAI SDKs and tools seamlessly.
**Base URL**: `https://ai.megallm.io/v1` for all OpenAI-compatible endpoints
## Available Endpoints
Generate conversational responses with GPT models
Real-time streaming responses with Server-Sent Events
Execute functions and tools with parallel support
Browse available models and capabilities
## Quick Example
```python theme={null}
from openai import OpenAI
# Initialize client
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Simple chat completion
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
temperature=0.7,
max_tokens=150
)
print(response.choices[0].message.content)
```
## Supported Models
| Model | Context Window | Use Case |
| --------------- | -------------- | --------------------------------- |
| `gpt-4` | 8,192 tokens | Complex reasoning, analysis |
| `gpt-4-32k` | 32,768 tokens | Long documents, extensive context |
| `gpt-4-turbo` | 128,000 tokens | Large-scale processing |
| `gpt-3.5-turbo` | 16,385 tokens | Fast, cost-effective responses |
## Features
### Full Compatibility
Drop-in replacement for OpenAI API - use your existing code without changes.
### High Performance
Fast response times with optimized infrastructure.
### Usage Tracking
Monitor your API usage and costs.
## SDK Support
MegaLLM works with all OpenAI-compatible SDKs:
* **Python**: `openai` official SDK
* **Node.js**: `openai` official SDK
* **Go**: `go-openai`
* **Rust**: `async-openai`
* **Java**: `openai-java`
* **C#**: `OpenAI-DotNet`
## Rate Limits
| Tier | Requests/min | Tokens/min |
| ---------- | ------------ | ---------- |
| Basic | 60 | 90,000 |
| Pro | 300 | 450,000 |
| Enterprise | Custom | Custom |
## Migration Guide
Migrating from OpenAI to MegaLLM is simple:
```python theme={null}
# Before (OpenAI)
client = OpenAI(api_key="sk-...")
# After (MegaLLM)
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
```
That's it! All your existing code continues to work.
## Error Handling
MegaLLM returns OpenAI-compatible error responses:
```json theme={null}
{
"error": {
"message": "Invalid request parameter",
"type": "invalid_request_error",
"param": "temperature",
"code": null
}
}
```
**Pro Tip**: Enable debug mode with `X-Debug: true` header to get detailed error information during development.
## Next Steps
* Explore [Chat Completions](/dev-docs/openai/chat-completions) for conversational AI
* Learn about [Function Calling](/dev-docs/openai/function-calling) for tool integration
* Implement [Streaming](/dev-docs/openai/streaming) for real-time responses
# Streaming
Source: https://docs.megallm.io/en/dev-docs/openai/streaming
Stream chat completions in real-time for a more interactive user experience. Perfect for chatbots, live assistants, and responsive applications.
**Server-Sent Events (SSE)**: Streaming uses SSE format with `text/event-stream` content type.
## How Streaming Works
Set `stream: true` in your request to receive incremental responses.
Get response tokens as they're generated, not waiting for completion.
Process `data:` events containing JSON chunks until `[DONE]` signal.
## Implementation Examples
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Create a streaming completion
stream = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "Write a haiku about programming"}
],
stream=True
)
# Process the stream
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
```
### With Async Support
```python theme={null}
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
async def stream_chat():
stream = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
async for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
asyncio.run(stream_chat())
```
```javascript theme={null}
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.GITHUB_TOKEN,
});
async function streamChat() {
const stream = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Write a haiku about programming' }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
}
streamChat();
```
### Browser Implementation
```javascript theme={null}
async function streamChatInBrowser() {
const response = await fetch('https://ai.megallm.io/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
try {
const json = JSON.parse(data);
const content = json.choices[0]?.delta?.content || '';
document.getElementById('output').innerHTML += content;
} catch (e) {
console.error('Error parsing JSON:', e);
}
}
}
}
}
```
```jsx theme={null}
import { useState, useCallback } from 'react';
function StreamingChat() {
const [messages, setMessages] = useState([]);
const [streaming, setStreaming] = useState(false);
const [currentResponse, setCurrentResponse] = useState('');
const sendMessage = useCallback(async (content) => {
setStreaming(true);
setCurrentResponse('');
const response = await fetch('https://ai.megallm.io/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.REACT_APP_GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4',
messages: [...messages, { role: 'user', content }],
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulated = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
setMessages(prev => [...prev,
{ role: 'user', content },
{ role: 'assistant', content: accumulated }
]);
setStreaming(false);
return;
}
try {
const json = JSON.parse(data);
const content = json.choices[0]?.delta?.content || '';
accumulated += content;
setCurrentResponse(accumulated);
} catch (e) {
// Handle parsing errors
}
}
}
}
}, [messages]);
return (
{messages.map((msg, i) => (
{msg.content}
))}
{streaming && (
{currentResponse}
▊
)}
);
}
```
```bash theme={null}
# Stream with curl and process line by line
curl -N https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Tell me a joke"}],
"stream": true
}' | while read -r line; do
if [[ $line == data:* ]]; then
# Extract JSON from the data line
json="${line:6}"
if [[ $json != "[DONE]" ]]; then
# Parse and display content (requires jq)
echo -n $(echo "$json" | jq -r '.choices[0].delta.content // ""')
fi
fi
done
```
## Stream Event Format
### Delta Events
Each streaming chunk follows this format:
```json theme={null}
data: {
"id": "chatcmpl-abc123",
"object": "chat.completion.chunk",
"created": 1677858242,
"model": "gpt-4",
"choices": [
{
"index": 0,
"delta": {
"content": "Hello"
},
"finish_reason": null
}
]
}
```
### Stream Lifecycle
1. **Initial chunk** - Contains role but no content:
```json theme={null}
data: {"choices": [{"delta": {"role": "assistant"}}]}
```
2. **Content chunks** - Incremental text:
```json theme={null}
data: {"choices": [{"delta": {"content": "Hello, "}}]}
data: {"choices": [{"delta": {"content": "how "}}]}
data: {"choices": [{"delta": {"content": "are "}}]}
data: {"choices": [{"delta": {"content": "you?"}}]}
```
3. **Final chunk** - Includes finish\_reason:
```json theme={null}
data: {"choices": [{"delta": {}, "finish_reason": "stop"}]}
```
4. **Stream end signal**:
```
data: [DONE]
```
## Advanced Streaming Features
### Function Calling in Streams
Stream function calls as they're generated:
```python theme={null}
stream = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
stream=True
)
function_call = {"name": "", "arguments": ""}
for chunk in stream:
delta = chunk.choices[0].delta
if delta.tool_calls:
tool_call = delta.tool_calls[0]
if tool_call.function.name:
function_call["name"] = tool_call.function.name
if tool_call.function.arguments:
function_call["arguments"] += tool_call.function.arguments
elif delta.content:
print(delta.content, end="")
```
### Progress Tracking
```python theme={null}
class StreamProgress:
def __init__(self):
self.tokens = 0
self.chunks = 0
self.start_time = time.time()
def update(self, chunk):
self.chunks += 1
if chunk.choices[0].delta.content:
# Approximate token count
self.tokens += len(chunk.choices[0].delta.content.split())
def get_stats(self):
elapsed = time.time() - self.start_time
return {
"chunks": self.chunks,
"tokens": self.tokens,
"time": elapsed,
"tokens_per_second": self.tokens / elapsed if elapsed > 0 else 0
}
# Usage
progress = StreamProgress()
for chunk in stream:
progress.update(chunk)
# Process chunk...
print(progress.get_stats())
```
## Error Handling in Streams
Streaming connections can fail mid-stream. Always implement proper error handling.
```python theme={null}
import time
def stream_with_retry(client, messages, max_retries=3):
for attempt in range(max_retries):
try:
stream = client.chat.completions.create(
model="gpt-4",
messages=messages,
stream=True
)
full_response = ""
for chunk in stream:
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response += content
yield content
return # Success
except Exception as e:
if attempt < max_retries - 1:
wait_time = 2 ** attempt # Exponential backoff
print(f"Stream interrupted, retrying in {wait_time}s...")
time.sleep(wait_time)
# Append partial response to continue
messages.append({"role": "assistant", "content": full_response})
messages.append({"role": "user", "content": "continue"})
else:
raise e
```
## Performance Optimization
### Buffering Strategy
```javascript theme={null}
class StreamBuffer {
constructor(onFlush, bufferSize = 10, flushInterval = 100) {
this.buffer = [];
this.onFlush = onFlush;
this.bufferSize = bufferSize;
this.flushInterval = flushInterval;
this.timer = null;
}
add(chunk) {
this.buffer.push(chunk);
if (this.buffer.length >= this.bufferSize) {
this.flush();
} else if (!this.timer) {
this.timer = setTimeout(() => this.flush(), this.flushInterval);
}
}
flush() {
if (this.buffer.length > 0) {
this.onFlush(this.buffer.join(''));
this.buffer = [];
}
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
}
// Usage
const buffer = new StreamBuffer((text) => {
document.getElementById('output').innerHTML += text;
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
buffer.add(content);
}
buffer.flush(); // Final flush
```
## Use Cases
### Live Chat Interface
```python theme={null}
def chat_interface():
print("Chat started. Type 'exit' to quit.")
while True:
user_input = input("\nYou: ")
if user_input.lower() == 'exit':
break
print("Assistant: ", end="")
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": user_input}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print() # New line after response
```
### Real-time Translation
```python theme={null}
def streaming_translator(text, target_language="Spanish"):
stream = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"Translate to {target_language}. Output only the translation."},
{"role": "user", "content": text}
],
stream=True,
temperature=0.3
)
translation = ""
for chunk in stream:
if chunk.choices[0].delta.content:
translation += chunk.choices[0].delta.content
yield chunk.choices[0].delta.content
return translation
```
## Best Practices
1. **Handle connection interruptions** - Implement retry logic with exponential backoff
2. **Buffer for UI updates** - Don't update DOM for every chunk to avoid performance issues
3. **Show loading indicators** - Display typing indicators or progress bars
4. **Implement timeouts** - Set reasonable timeouts for streaming connections
5. **Clean up resources** - Always close streams properly to avoid memory leaks
## Next Steps
* Implement [Function Calling](/openai/function-calling) with streaming
* Learn about [Structured Output](/openai/structured-output) for validated responses
* Explore [Embeddings](/openai/embeddings) for semantic search
# FAQ
Source: https://docs.megallm.io/en/home/faq
Find answers to common questions about MegaLLM - the universal AI platform connecting 70+ models.
## General
MegaLLM is a universal AI platform that connects 70+ large language models from leading providers like OpenAI, Anthropic, and Google through a single API. Think of it as your "super-API" for AI - instead of integrating with multiple providers separately, you get access to all models through one unified interface.
We currently support 70+ models including:
* **OpenAI**: gpt-5, gpt-4.1, gpt-4o, gpt-3.5-turbo
* **Anthropic**: claude-opus-4-1-20250805, claude-sonnet-4, claude-3.5-sonnet, claude-3.7-sonnet
* **Google**: gemini-2.5-pro, gemini-2.0-flash-001
* **Embedding Models**: Various text embedding options
New models are added regularly as they become available.
Yes! Switching models is as simple as changing one parameter in your API call. You can also set up automatic fallbacks between models.
```python theme={null}
# Switch models instantly
response = client.chat.completions.create(
model="gpt-5", # Change this to any supported model ID
messages=[{"role": "user", "content": "Hello!"}]
)
```
No! That's the beauty of MegaLLM. You only need one MegaLLM account to access all 70+ models. We handle the complexity of managing multiple provider relationships, so you don't have to.
## Platform Features
MegaLLM offers several unique advantages:
1. **One API for All**: Access 70+ models through a single, consistent interface
2. **Automatic Fallbacks**: If one model fails, automatically switch to another
3. **Unified Billing**: One invoice for all your AI usage
4. **Performance Optimization**: Intelligent routing and load balancing
5. **Cost Management**: Optimize spending across different models
When you configure fallback models, MegaLLM automatically routes your request to backup models if the primary model encounters issues like:
* Rate limits
* Temporary outages
* Timeout errors
* Capacity constraints
This ensures your application never goes down due to a single model failure.
```python theme={null}
response = client.chat.completions.create(
model="gpt-5",
messages=messages,
fallback_models=["claude-opus-4-1-20250805", "gemini-2.5-pro"]
)
```
## Pricing & Billing
You pay based on actual token usage, just like with individual providers. However, MegaLLM offers several advantages:
* **Unified Billing**: One invoice for all models
* **Volume Discounts**: Better rates for high usage
* **Cost Optimization**: Tools to minimize spending
* **Transparent Pricing**: Clear cost breakdown by model
See our [Models page](/home/models) for detailed pricing information.
For most users, MegaLLM offers better value because:
1. **Volume Pricing**: We pass on volume discounts to customers
2. **Reduced Development Costs**: No need to integrate with multiple APIs
3. **Operational Savings**: Less monitoring, fewer rate limit issues
4. **Fallback Benefits**: Higher uptime means less lost revenue
Plus, you save significant engineering time by not having to manage multiple provider integrations.
Yes! MegaLLM provides comprehensive cost controls:
* Daily/monthly spending limits
* Per-model budget allocation
* Usage alerts and notifications
* Cost optimization recommendations
* Automatic fallback to cheaper models when limits are reached
## Technical Integration
Yes! MegaLLM is fully compatible with OpenAI's API format. Migration is typically just changing the base URL:
```python theme={null}
# Before (OpenAI)
client = OpenAI(api_key="sk-...")
# After (MegaLLM)
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-megallm-key"
)
```
All your existing code continues to work unchanged.
We also support Anthropic's API format perfectly:
```python theme={null}
# Anthropic format works too
client = Anthropic(
base_url="https://ai.megallm.io",
api_key="your-megallm-key"
)
```
You can even mix and match - use OpenAI format to access Claude models, or vice versa.
Different models have different strengths. MegaLLM makes it easy to route requests to the best model for each task:
```python theme={null}
# Code generation
code_response = client.chat.completions.create(
model="gpt-5", # Great for code
messages=[{"role": "user", "content": "Write a Python function..."}]
)
# Creative writing
creative_response = client.chat.completions.create(
model="claude-opus-4-1-20250805", # Great for creative tasks
messages=[{"role": "user", "content": "Write a story..."}]
)
# Fast responses
quick_response = client.chat.completions.create(
model="gpt-4o-mini", # Fast and efficient
messages=[{"role": "user", "content": "Quick question..."}]
)
```
MegaLLM significantly reduces rate limit issues:
1. **Distributed Load**: Requests are distributed across multiple providers
2. **Automatic Fallbacks**: Switch to available models when limits hit
3. **Intelligent Routing**: Route requests to models with available capacity
4. **Rate Limit Prediction**: Avoid hitting limits with predictive routing
You'll experience much higher effective rate limits than any single provider.
## Use Cases
MegaLLM is perfect for:
**Developers**:
* Experiment with different models without rewriting code
* Reduce integration complexity
* Faster time to market
**Businesses**:
* Ensure high availability with fallbacks
* Optimize costs across providers
* Future-proof AI investments
**Researchers**:
* Access cutting-edge models as they're released
* Run comprehensive evaluations
* Test model performance across different tasks
Absolutely! MegaLLM is designed for production use with:
* 99.9% uptime SLA
* Enterprise security and compliance
* 24/7 monitoring and support
* Automatic scaling and load balancing
* Comprehensive logging and analytics
Many companies use MegaLLM to power their production AI features.
Check out our [Models page](/home/models) for detailed guidance. Generally:
* **Fast responses**: gpt-4o-mini, gemini-2.0-flash-001
* **Complex reasoning**: gpt-5, claude-opus-4-1-20250805
* **Code generation**: gpt-5, claude-3.7-sonnet
* **Creative writing**: claude-opus-4-1-20250805, gpt-5
* **Cost-effective**: gpt-4o-mini, gemini-2.0-flash-001
You can also test different models with your specific prompts to find the best fit.
## Support & Getting Started
Getting started is simple:
1. **Sign up** for a MegaLLM account
2. **Get your API key** from the dashboard
3. **Choose your integration method** (OpenAI or Anthropic format)
4. **Make your first API call**
Check our [Quick Start guide](/dev-docs/getting-started/quick-start) for detailed instructions.
Yes! We provide:
* **Documentation**: Comprehensive guides and tutorials
* **Community Support**: Discord community and forums
* **Email Support**: Technical assistance for all users
* **Enterprise Support**: Dedicated support for enterprise customers
* **Professional Services**: Custom integration assistance
Contact us at [support@megallm.io](mailto:support@megallm.io) for any questions.
Migration is typically very straightforward since we maintain API compatibility. We also offer:
* **Migration guides** for popular providers
* **Free migration assistance** for enterprise customers
* **Gradual migration tools** to test before fully switching
* **Cost comparison tools** to optimize your setup
Most customers can migrate in under an hour.
**Still have questions?** Check our [documentation](/home/introduction) or reach out to our team at [support@megallm.io](mailto:support@megallm.io)
# First Request
Source: https://docs.megallm.io/en/home/getting-started/first-request
Let's build a simple AI application step-by-step. You'll learn how to make requests, handle responses, and work with different models.
## Prerequisites
* MegaLLM API key ([Get one here](https://megallm.io/dashboard/overview))
* Python 3.7+ or Node.js 14+ installed
* Basic programming knowledge
## Step 1: Create Project
```bash theme={null}
# Create directory
mkdir my-first-ai-app
cd my-first-ai-app
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install openai python-dotenv
```
```bash theme={null}
# Create directory
mkdir my-first-ai-app
cd my-first-ai-app
# Initialize project
npm init -y
# Install dependencies
npm install openai dotenv
```
## Step 2: Store API Key
Create a `.env` file:
```bash theme={null}
MEGALLM_API_KEY=your-api-key-here
```
Add `.env` to `.gitignore` to avoid committing your API key!
## Step 3: Basic Request
Create `app.py`:
```python theme={null}
import os
from dotenv import load_dotenv
from openai import OpenAI
# Load environment variables
load_dotenv()
# Initialize client
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key=os.getenv("MEGALLM_API_KEY")
)
# Make a request
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "What is MegaLLM?"}
]
)
# Print response
print(response.choices[0].message.content)
```
Run it:
```bash theme={null}
python app.py
```
Create `app.js`:
```javascript theme={null}
import OpenAI from 'openai';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
// Initialize client
const client = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY
});
// Make a request
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: 'What is MegaLLM?' }
]
});
// Print response
console.log(response.choices[0].message.content);
```
Update `package.json`:
```json theme={null}
{
"type": "module"
}
```
Run it:
```bash theme={null}
node app.js
```
## Step 4: Add Conversation Context
Let's make it conversational:
```python theme={null}
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key=os.getenv("MEGALLM_API_KEY")
)
# Conversation history
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is Python?"}
]
# First response
response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
# Add to history
assistant_message = response.choices[0].message.content
messages.append({"role": "assistant", "content": assistant_message})
print(f"Assistant: {assistant_message}\n")
# Follow-up question
messages.append({"role": "user", "content": "What are its key features?"})
response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
print(f"Assistant: {response.choices[0].message.content}")
```
```javascript theme={null}
import OpenAI from 'openai';
import dotenv from 'dotenv';
dotenv.config();
const client = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY
});
// Conversation history
const messages = [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is JavaScript?' }
];
// First response
let response = await client.chat.completions.create({
model: 'gpt-4',
messages: messages
});
// Add to history
const assistantMessage = response.choices[0].message.content;
messages.push({ role: 'assistant', content: assistantMessage });
console.log(`Assistant: ${assistantMessage}\n`);
// Follow-up question
messages.push({ role: 'user', content: 'What are its key features?' });
response = await client.chat.completions.create({
model: 'gpt-4',
messages: messages
});
console.log(`Assistant: ${response.choices[0].message.content}`);
```
## Step 5: Try Different Models
Switch models by changing the `model` parameter:
```python theme={null}
models = ["gpt-4", "claude-3.5-sonnet", "gemini-2.5-pro"]
for model in models:
print(f"\n--- Using {model} ---")
response = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": "Explain quantum computing in one sentence."}
]
)
print(response.choices[0].message.content)
```
```javascript theme={null}
const models = ['gpt-4', 'claude-3.5-sonnet', 'gemini-2.5-pro'];
for (const model of models) {
console.log(`\n--- Using ${model} ---`);
const response = await client.chat.completions.create({
model: model,
messages: [
{ role: 'user', content: 'Explain quantum computing in one sentence.' }
]
});
console.log(response.choices[0].message.content);
}
```
## Step 6: Add Parameters
Customize the response with parameters:
```python theme={null}
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "Write a short poem about AI"}
],
temperature=0.9, # Higher = more creative
max_tokens=100, # Limit response length
top_p=0.95, # Nucleus sampling
frequency_penalty=0.5 # Reduce repetition
)
```
## Step 7: Error Handling
Add proper error handling:
```python theme={null}
from openai import OpenAI, AuthenticationError, RateLimitError
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
except AuthenticationError:
print(" Invalid API key")
except RateLimitError:
print(" Rate limit exceeded")
except Exception as e:
print(f" Error: {e}")
```
```javascript theme={null}
try {
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(response.choices[0].message.content);
} catch (error) {
if (error.status === 401) {
console.log(' Invalid API key');
} else if (error.status === 429) {
console.log(' Rate limit exceeded');
} else {
console.log(` Error: ${error.message}`);
}
}
```
## Step 8: Interactive Chat
Build a simple chatbot:
```python theme={null}
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key=os.getenv("MEGALLM_API_KEY")
)
messages = [
{"role": "system", "content": "You are a helpful assistant."}
]
print("Chat with AI (type 'quit' to exit)\n")
while True:
user_input = input("You: ")
if user_input.lower() == 'quit':
break
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
assistant_message = response.choices[0].message.content
messages.append({"role": "assistant", "content": assistant_message})
print(f"AI: {assistant_message}\n")
```
```javascript theme={null}
import OpenAI from 'openai';
import dotenv from 'dotenv';
import readline from 'readline';
dotenv.config();
const client = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY
});
const messages = [
{ role: 'system', content: 'You are a helpful assistant.' }
];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Chat with AI (type 'quit' to exit)\n");
function chat() {
rl.question('You: ', async (userInput) => {
if (userInput.toLowerCase() === 'quit') {
rl.close();
return;
}
messages.push({ role: 'user', content: userInput });
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: messages
});
const assistantMessage = response.choices[0].message.content;
messages.push({ role: 'assistant', content: assistantMessage });
console.log(`AI: ${assistantMessage}\n`);
chat();
});
}
chat();
```
## Understanding the Response
The API returns a rich response object:
```json theme={null}
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
```
## Next Steps
Real-time streaming responses
Let AI use external tools
Explore all available models
Tips for production apps
## Troubleshooting
Make sure you installed the SDK:
```bash theme={null}
pip install openai # Python
npm install openai # JavaScript
```
* Check your API key is correct
* Verify `.env` file is in the same directory
* Make sure you called `load_dotenv()` (Python) or `dotenv.config()` (JS)
* You're making too many requests
* Add delays between requests
* Consider upgrading your plan
* Try a faster model like `gpt-3.5-turbo`
* Reduce `max_tokens`
* Use streaming for better UX
## Need Help?
* **FAQ**: [Common Questions](/home/faq)
* **API Reference**: [Complete Documentation](/api-reference/introduction)
* **Support**: [support@megallm.io](mailto:support@megallm.io)
* **Discord**: [Join Community](https://discord.gg/Megallm)
# Next Steps
Source: https://docs.megallm.io/en/home/getting-started/next-steps
You've made your first AI request! Here's what to explore next.
## Learn Advanced Features
Get real-time responses as they're generated
Let AI interact with external tools and APIs
Process images with multimodal models
Complete API reference and guides
## Explore Documentation
Complete API documentation
OpenAI-compatible endpoints
Anthropic Claude-compatible endpoints
Browse all 70+ available models
## Build Real Applications
### 1. Chatbot
Build an intelligent chatbot:
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-key"
)
def chatbot(user_message, history=[]):
history.append({"role": "user", "content": user_message})
response = client.chat.completions.create(
model="gpt-4",
messages=history
)
assistant_message = response.choices[0].message.content
history.append({"role": "assistant", "content": assistant_message})
return assistant_message, history
```
### 2. Content Generator
Generate blog posts, emails, or social media content:
```python theme={null}
def generate_content(topic, content_type="blog"):
prompts = {
"blog": f"Write a comprehensive blog post about {topic}",
"email": f"Write a professional email about {topic}",
"tweet": f"Write an engaging tweet about {topic}"
}
response = client.chat.completions.create(
model="claude-3.5-sonnet",
messages=[{"role": "user", "content": prompts[content_type]}],
temperature=0.7
)
return response.choices[0].message.content
```
### 3. Code Assistant
Build a coding helper:
```python theme={null}
def code_assistant(task, language="python"):
prompt = f"Write {language} code to {task}. Include comments and error handling."
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0.2 # Lower temperature for more deterministic code
)
return response.choices[0].message.content
```
### 4. Data Analyzer
Analyze data and generate insights:
```python theme={null}
def analyze_data(data_description):
prompt = f"""
Analyze this data and provide insights:
{data_description}
Provide:
1. Key findings
2. Trends
3. Recommendations
"""
response = client.chat.completions.create(
model="claude-opus-4-1-20250805", # Best for analysis
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
```
## Best Practices
* **GPT-4**: Best for complex reasoning
* **GPT-3.5 Turbo**: Fast and cost-effective
* **Claude Opus**: Excellent for analysis and long context
* **Claude Sonnet**: Balanced performance
* **Gemini Pro**: Strong multimodal capabilities
See [Models Catalog](/home/models) for detailed comparisons.
* Start with cheaper models for testing
* Use `max_tokens` to limit response length
* Cache responses when possible
* Use streaming to improve perceived performance
* Monitor usage in your dashboard
```python theme={null}
from openai import OpenAI, AuthenticationError, RateLimitError
import time
def make_request_with_retry(messages, max_retries=3):
for attempt in range(max_retries):
try:
return client.chat.completions.create(
model="gpt-4",
messages=messages
)
except RateLimitError:
if attempt < max_retries - 1:
time.sleep(2 ** attempt) # Exponential backoff
else:
raise
except AuthenticationError:
print("Invalid API key")
raise
```
* Be specific and clear
* Provide examples when needed
* Use system messages to set context
* Break complex tasks into steps
* Test different temperature settings
* Keep track of conversation history
* Limit history to avoid token limits
* Summarize old messages if needed
* Use prompt caching for repeated content
## Production Considerations
### Security
* Store API keys in environment variables
* Never commit keys to version control
* Use different keys for dev/staging/production
* Rotate keys regularly
* Monitor usage for anomalies
### Performance
* Use streaming for better UX
* Implement caching where appropriate
* Add retry logic with exponential backoff
* Monitor response times
* Consider using webhooks for async operations
### Monitoring
* Track token usage
* Monitor error rates
* Log API requests (without sensitive data)
* Set up alerts for quota limits
* Review costs regularly
### Scaling
* Implement rate limiting
* Use queues for high-volume requests
* Cache common responses
* Consider batching requests
* Plan for failover strategies
## Join the Community
Chat with other developers
View examples and contribute
Follow for updates
Watch tutorials
## Get Help
Most common questions are answered in our [FAQ](/home/faq).
Comprehensive guides available in [Developer Docs](/dev-docs/overview).
Email us at [support@megallm.io](mailto:support@megallm.io) for technical assistance.
Found a bug? Report it on [GitHub](https://github.com/megallm).
## Useful Resources
* **[API Reference](/api-reference/introduction)** - Complete API documentation
* **[Models Catalog](/home/models)** - All 70+ models with pricing
* **[CLI Tool](/cli/overview)** - Set up AI coding assistants
* **[FAQ](/home/faq)** - Common questions and answers
* **[Changelog](/releases/overview)** - Latest updates
## Ready to Build?
Start building your AI application today. If you need help, we're here for you!
Complete documentation
Get help from our team
# Getting Started
Source: https://docs.megallm.io/en/home/getting-started/overview
Get up and running with MegaLLM in just a few minutes. Access 70+ AI models through one unified API.
## Quick Navigation
Make your first API call in 2 minutes
Complete setup and configuration
Your first AI request step-by-step
What to do after getting started
## What is MegaLLM?
MegaLLM is a universal AI platform that provides access to 70+ large language models through a single API. Instead of managing multiple API keys and integrations, you get:
* **One API** for all models
* **One bill** for all usage
* **One integration** to maintain
### Supported Models
* **OpenAI**: GPT-4, GPT-5, GPT-3.5 Turbo
* **Anthropic**: Claude Opus 4, Claude Sonnet, Claude Haiku
* **Google**: Gemini 2.5 Pro, Gemini Flash
* **Meta**: Llama 3 70B, Llama 3 8B
* **And 60+ more models!**
## Why Choose MegaLLM?
Access all major AI models through one API. No need to integrate with multiple providers separately.
Drop-in replacement for OpenAI and Anthropic SDKs. Just change the base URL and you're ready to go.
Built-in failover ensures your application keeps running even when a model is down.
Easily switch between models to optimize for cost, speed, or quality without code changes.
Unified billing across all providers. Track usage and costs in one dashboard.
## 3-Step Setup
Sign up at [megallm.io](https://megallm.io) and generate your API key
Use the OpenAI or Anthropic SDK you already know
```bash theme={null}
pip install openai
# or
pip install anthropic
```
Point to MegaLLM and start using any model
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-megallm-api-key"
)
```
## Choose Your Path
Perfect! Start here:
1. [Quick Start Guide](/home/getting-started/quick-start) - Get your API key and make your first request
2. [First Request Tutorial](/home/getting-started/first-request) - Step-by-step walkthrough
3. [Browse Models](/home/models) - Explore available models
4. [FAQ](/home/faq) - Common questions
Great! Switching is easy:
1. Get your MegaLLM API key
2. Change your base URL to `https://ai.megallm.io/v1`
3. That's it! All your code works the same
See: [OpenAI Migration Guide](/dev-docs/openai/overview)
Awesome! Migration is simple:
1. Get your MegaLLM API key
2. Change your base URL to `https://ai.megallm.io`
3. Done! Use Claude and 70+ other models
See: [Anthropic Migration Guide](/dev-docs/anthropic/overview)
Let's dive in:
1. [API Reference](/api-reference/introduction) - Complete API docs
2. [OpenAI API](/dev-docs/openai/overview) - OpenAI-compatible endpoints
3. [Anthropic API](/dev-docs/anthropic/overview) - Anthropic-compatible endpoints
4. [Streaming](/api-reference/endpoint/streaming) - Real-time responses
5. [Function Calling](/api-reference/endpoint/function-calling) - Tool use
## Next Steps
Make your first request in 2 minutes
Browse 70+ available AI models
Comprehensive API documentation
Set up AI coding assistants
## Need Help?
* **Documentation**: Complete guides and tutorials
* **FAQ**: [Common questions](/home/faq)
* **Support**: [support@megallm.io](mailto:support@megallm.io)
* **Discord**: [Join our community](https://discord.gg/Megallm)
# Quick Start
Source: https://docs.megallm.io/en/home/getting-started/quick-start
Get started with MegaLLM in just 2 minutes. This guide will help you make your first API call.
## 1. Get Your API Key
Visit [megallm.io/auth/signup](https://megallm.io/auth/signup) and create an account
Go to [megallm.io/dashboard](https://megallm.io/dashboard/overview)
Click "Create New API Key" in the API Keys section
Copy your key (starts with `sk-mega-`) and save it securely
Keep your API key secret! Never commit it to version control or share it publicly.
## 2. Make Your First Request
Choose your preferred method:
```bash theme={null}
# Install OpenAI SDK
pip install openai
```
```python theme={null}
from openai import OpenAI
# Initialize client
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-megallm-api-key" # Replace with your key
)
# Make a request
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "user", "content": "Say hello!"}
]
)
print(response.choices[0].message.content)
```
```bash theme={null}
# Install OpenAI SDK
npm install openai
```
```javascript theme={null}
import OpenAI from 'openai';
// Initialize client
const client = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: 'your-megallm-api-key' // Replace with your key
});
// Make a request
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: 'Say hello!' }
]
});
console.log(response.choices[0].message.content);
```
```bash theme={null}
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer YOUR_MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Say hello!"}
]
}'
```
```bash theme={null}
# Install MegaLLM CLI
npx megallm@latest
# Follow the interactive setup
```
See [CLI Documentation](/cli/overview) for details.
## 3. Try Different Models
One of MegaLLM's superpowers is instant model switching. Just change the `model` parameter:
```python theme={null}
# Try GPT-4
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Explain quantum computing"}]
)
# Switch to Claude
response = client.chat.completions.create(
model="claude-opus-4-1-20250805",
messages=[{"role": "user", "content": "Explain quantum computing"}]
)
# Try Gemini
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[{"role": "user", "content": "Explain quantum computing"}]
)
```
Browse all available models at [Models Catalog](/home/models)
## What's Next?
Environment variables and configuration
Detailed walkthrough with examples
Explore all 70+ available models
Complete API documentation
## Common Questions
Start with `gpt-4` for general use, `claude-3.5-sonnet` for long context, or `gpt-3.5-turbo` for speed and cost efficiency.
See [Models Catalog](/home/models) for detailed comparisons.
You pay only for what you use. Different models have different pricing. Most testing can be done for under \$1.
Check current pricing in your [Dashboard](https://megallm.io/dashboard/overview).
Yes! Just change the base URL. All your existing code works without modification.
MegaLLM has automatic failover. If a model is unavailable, you can quickly switch to an alternative.
## Need Help?
* **Full Tutorial**: [First Request Guide](/home/getting-started/first-request)
* **Documentation**: [Developer Docs](/dev-docs/overview)
* **FAQ**: [Common Questions](/home/faq)
* **Support**: [support@megallm.io](mailto:support@megallm.io)
* **Discord**: [Join Community](https://discord.gg/Megallm)
# Setup Guide
Source: https://docs.megallm.io/en/home/getting-started/setup
Complete guide to setting up MegaLLM for your development environment.
## Environment Setup
### 1. Store Your API Key Securely
Add to your shell configuration file:
```bash theme={null}
# ~/.bashrc or ~/.zshrc
export MEGALLM_API_KEY="your-api-key-here"
```
Then reload:
```bash theme={null}
source ~/.bashrc
# or
source ~/.zshrc
```
Or use a `.env` file:
```bash theme={null}
echo "MEGALLM_API_KEY=your-api-key-here" >> .env
```
**PowerShell:**
```powershell theme={null}
[System.Environment]::SetEnvironmentVariable("MEGALLM_API_KEY", "your-api-key-here", "User")
```
**Command Prompt:**
```cmd theme={null}
setx MEGALLM_API_KEY "your-api-key-here"
```
Create a `.env` file in your project root:
```bash theme={null}
MEGALLM_API_KEY=your-api-key-here
```
**Python:**
```python theme={null}
from dotenv import load_dotenv
load_dotenv()
```
**JavaScript:**
```javascript theme={null}
require('dotenv').config();
```
Add `.env` to your `.gitignore` to avoid committing secrets!
### 2. Install SDK
```bash theme={null}
# OpenAI SDK (recommended)
pip install openai
# Or Anthropic SDK
pip install anthropic
# For environment variables
pip install python-dotenv
```
```bash theme={null}
# OpenAI SDK (recommended)
npm install openai
# Or Anthropic SDK
npm install @anthropic-ai/sdk
# For environment variables
npm install dotenv
```
```bash theme={null}
go get github.com/sashabaranov/go-openai
```
MegaLLM works with any HTTP client. See [API Reference](/api-reference/introduction) for details.
### 3. Configure Your Client
```python theme={null}
import os
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key=os.getenv("MEGALLM_API_KEY")
)
# Test the connection
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
```
```python theme={null}
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://ai.megallm.io",
api_key=os.getenv("MEGALLM_API_KEY")
)
# Test the connection
message = client.messages.create(
model="claude-3.5-sonnet",
max_tokens=100,
messages=[{"role": "user", "content": "Hello!"}]
)
print(message.content[0].text)
```
```javascript theme={null}
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: process.env.MEGALLM_API_KEY
});
// Test the connection
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(response.choices[0].message.content);
```
```javascript theme={null}
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
baseURL: 'https://ai.megallm.io',
apiKey: process.env.MEGALLM_API_KEY
});
// Test the connection
const message = await client.messages.create({
model: 'claude-3.5-sonnet',
max_tokens: 100,
messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(message.content[0].text);
```
## Project Setup
### For New Projects
1. **Create project directory:**
```bash theme={null}
mkdir my-ai-project
cd my-ai-project
```
2. **Initialize project:**
```bash theme={null}
# Python
python -m venv venv
source venv/bin/activate
pip install openai python-dotenv
# JavaScript
npm init -y
npm install openai dotenv
```
3. **Create .env file:**
```bash theme={null}
echo "MEGALLM_API_KEY=your-key-here" > .env
echo ".env" >> .gitignore
```
4. **Create first script:**
See [First Request Guide](/home/getting-started/first-request)
### For Existing Projects
If you're already using OpenAI or Anthropic:
1. **Update base URL:**
```python theme={null}
# Before
client = OpenAI(api_key="sk-...")
# After
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-megallm-key"
)
```
2. **That's it!** All your existing code works.
## IDE Setup
### VS Code
Install recommended extensions:
* Python extension (for Python)
* ESLint (for JavaScript)
* REST Client (for testing APIs)
### PyCharm / IntelliJ
Configure environment variables in Run Configurations.
### AI Coding Assistants
MegaLLM provides a CLI to configure AI coding tools:
```bash theme={null}
npx megallm@latest
```
This sets up:
* Claude Code
* Codex/Windsurf
* OpenCode
See [CLI Documentation](/cli/overview) for details.
## Verify Setup
Test your configuration:
```python theme={null}
import os
from openai import OpenAI
# Check API key
api_key = os.getenv("MEGALLM_API_KEY")
if not api_key:
print("❌ API key not set!")
exit(1)
print("✅ API key found")
# Test connection
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key=api_key
)
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say 'Setup successful!'"}],
max_tokens=10
)
print("✅ Connection successful!")
print(f"Response: {response.choices[0].message.content}")
except Exception as e:
print(f"❌ Error: {e}")
```
```javascript theme={null}
import OpenAI from 'openai';
// Check API key
const apiKey = process.env.MEGALLM_API_KEY;
if (!apiKey) {
console.log('❌ API key not set!');
process.exit(1);
}
console.log('✅ API key found');
// Test connection
const client = new OpenAI({
baseURL: 'https://ai.megallm.io/v1',
apiKey: apiKey
});
try {
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: "Say 'Setup successful!'" }],
max_tokens: 10
});
console.log('✅ Connection successful!');
console.log(`Response: ${response.choices[0].message.content}`);
} catch (error) {
console.log(`❌ Error: ${error.message}`);
}
```
```bash theme={null}
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $MEGALLM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Say setup successful!"}],
"max_tokens": 10
}'
```
## Troubleshooting
**Problem:** Environment variable not set
**Solution:**
* Check variable name: `MEGALLM_API_KEY`
* Reload shell: `source ~/.bashrc`
* Verify: `echo $MEGALLM_API_KEY`
**Problem:** Invalid API key
**Solution:**
* Verify key starts with `sk-mega-`
* Check for extra spaces
* Generate new key at [dashboard](https://megallm.io/dashboard/overview)
**Problem:** Network or firewall issues
**Solution:**
* Check internet connection
* Verify firewall settings
* Try without VPN
**Problem:** SDK not installed
**Solution:**
```bash theme={null}
pip install openai # Python
npm install openai # JavaScript
```
## Next Steps
Build your first AI application
Explore 70+ available models
Complete API documentation
Tips and common questions
# MegaLLM Documentation
Source: https://docs.megallm.io/en/home/introduction
Welcome to MegaLLM - the universal AI platform that connects 70+ large language models through a single, powerful API.
**One API, Unlimited Possibilities**: Access GPT-5, Claude Opus 4.1, Gemini 2.5 Pro, and more models without juggling multiple providers.
## What is MegaLLM?
MegaLLM is your **"super-API"** for AI. Instead of integrating with OpenAI, Anthropic, Google, and other providers separately, you get access to all their models through one unified interface.
### Why MegaLLM?
* **Instant Model Switching**: Change models with one parameter
* **Automatic Fallbacks**: Never go down when one model fails
* **Unified Billing**: One invoice for all your AI usage
* **Zero Integration Overhead**: Drop-in replacement for existing code
## Quick Start
Browse 70+ AI models with pricing and capabilities
Get your API key and make your first request
Use OpenAI-compatible endpoints with any model
Access Claude models with Anthropic format
## Core Features
Ensure high availability with intelligent model switching
Simple API key management and security
Frequently asked questions and troubleshooting
## Who Uses MegaLLM?
### Developers
* Experiment with different models without rewriting code
* Reduce integration complexity from weeks to minutes
* Build more robust applications with automatic fallbacks
### Businesses
* Ensure high availability for customer-facing AI features
* Optimize costs across multiple model providers
* Future-proof AI investments with provider flexibility
### Researchers
* Access cutting-edge models as they're released
* Run comprehensive evaluations and benchmarks
* Test model performance across different tasks
## Example: Switching Models
```python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Try GPT-5 for complex reasoning
response = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": "Analyze this data..."}]
)
# Switch to Claude for analysis
response = client.chat.completions.create(
model="claude-3.7-sonnet",
messages=[{"role": "user", "content": "Analyze this data..."}]
)
# Use Claude for creative writing
response = client.chat.completions.create(
model="claude-opus-4-1-20250805",
messages=[{"role": "user", "content": "Write a story about..."}]
)
```
## Popular Model Combinations
| Use Case | Primary Model | Fallback Models | Why |
| -------------------- | ------------------------ | -------------------------------- | -------------------- |
| **Chatbots** | gpt-4o-mini | gpt-3.5-turbo, claude-3.5-sonnet | Fast, cost-effective |
| **Code Generation** | gpt-5 | claude-3.7-sonnet, gpt-4o | Specialized for code |
| **Analysis** | claude-opus-4-1-20250805 | gpt-5, gemini-2.5-pro | Best reasoning |
| **Creative Writing** | claude-opus-4-1-20250805 | gpt-5, claude-sonnet-4 | Creative excellence |
## Getting Started
**Ready to get started?** Head to our [Quick Start guide](/dev-docs/getting-started/quick-start) to make your first API call in minutes.
### 3-Step Setup
1. **Get API Key**: Sign up and get your MegaLLM API key
2. **Choose Format**: Use OpenAI or Anthropic API format
3. **Start Building**: Make your first request to any of 70+ models
## Need Help?
* **Browse our guides**: Comprehensive documentation for every feature
* **Check the FAQ**: Common questions and solutions
* **Contact support**: [support@megallm.io](mailto:support@megallm.io) for technical assistance
* **Use search**: Press Cmd+K to search all documentation
# Models Catalog
Source: https://docs.megallm.io/en/home/models
Access cutting-edge AI models from leading providers through a single, unified API. All models are accessible using their model ID in your API calls.
## Live Models Data
The models catalog is dynamically updated. Visit our [dashboard](https://megallm.io/dashboard/models) for real-time pricing and availability.
## Model Selection Guide
### By Use Case
gpt-5-mini, gpt-4o-mini, gemini-2.0-flash-001, gpt-3.5-turbo
gpt-5, claude-opus-4-1-20250805, gemini-2.5-pro
gpt-4o-mini, gemini-2.0-flash-001
gpt-4.1 (1M+), gemini-2.5-pro (1M+)
gpt-5, gpt-4o, claude-sonnet-4, gemini models
gpt-5, claude-3.7-sonnet, gpt-4o
### By Budget
**Models**: `gpt-4o-mini`, `gemini-2.0-flash-001`
**Use Cases**: Prototyping, simple tasks, testing
**Cost**: Most affordable option for high-volume usage
**Models**: `gpt-5-mini`, `claude-3.5-sonnet`
**Use Cases**: Production apps, chatbots, customer service
**Cost**: Balanced performance and pricing
**Models**: `gpt-5`, `claude-sonnet-4`
**Use Cases**: Advanced reasoning, complex analysis, research
**Cost**: Higher tier for demanding applications
**Models**: `claude-opus-4-1-20250805`, `gpt-4.1`
**Use Cases**: Critical applications, advanced research, maximum capability
**Cost**: Premium pricing for best-in-class performance
## Using Models in Code
Always use the model ID when making API calls:
```python Python theme={null}
from openai import OpenAI
client = OpenAI(
base_url="https://ai.megallm.io/v1",
api_key="your-api-key"
)
# Use model ID, not display name
response = client.chat.completions.create(
model="gpt-5", # Model ID
messages=[{"role": "user", "content": "Hello!"}]
)
# Switch to Claude using model ID
response = client.chat.completions.create(
model="claude-opus-4-1-20250805", # Model ID
messages=[{"role": "user", "content": "Hello!"}]
)
# Try Gemini using model ID
response = client.chat.completions.create(
model="gemini-2.5-pro", # Model ID
messages=[{"role": "user", "content": "Hello!"}]
)
```
```javascript JavaScript theme={null}
// Always use model IDs
const models = ['gpt-5', 'claude-opus-4-1-20250805', 'gemini-2.5-pro'];
for (const modelId of models) {
const response = await fetch("https://ai.megallm.io/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: modelId, // Using model ID
messages: [{ role: "user", content: "Hello!" }]
})
});
console.log(`${modelId} response:`, await response.json());
}
```
```bash cURL theme={null}
# Test multiple models using their IDs
for model in "gpt-5" "claude-opus-4-1-20250805" "gemini-2.5-pro"; do
echo "Testing $model..."
curl https://ai.megallm.io/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"$model\",
\"messages\": [{\"role\": \"user\", \"content\": \"Hello!\"}]
}"
done
```
## Automatic Fallback
Configure automatic fallback using model IDs:
```python theme={null}
response = client.chat.completions.create(
model="gpt-5",
messages=messages,
fallback_models=["claude-opus-4-1-20250805", "gemini-2.5-pro"],
fallback_on_rate_limit=True,
fallback_on_error=True
)
```
## Pricing Calculator
Estimate your costs across different models:
| Usage Level | Tokens/Month | gpt-5-mini | claude-3.5-sonnet | gemini-2.0-flash-001 |
| -------------- | ------------ | ---------- | ----------------- | -------------------- |
| **Hobby** | 1M | \$2.25 | \$18 | \$0.75 |
| **Startup** | 10M | \$22.50 | \$180 | \$7.50 |
| **Business** | 100M | \$225 | \$1,800 | \$75 |
| **Enterprise** | 1B+ | Custom | Custom | Custom |
**Important**: Model IDs are case-sensitive. Always use the exact model ID as shown in the tables above.
## Next Steps
* Read the [FAQ](/home/faq) for common questions about model selection
* Check out the [API Reference](/api-reference/introduction) for detailed endpoint documentation
* View [Developer Docs](/dev-docs/overview) for integration guides
# Release Notes
Source: https://docs.megallm.io/en/releases/overview
Stay up to date with the latest features, improvements, and changes to the MegaLLM platform.
## Latest Updates
**Coming Soon**: Detailed release notes and changelog will be available here.
## Stay Informed
* Follow us on [Twitter/X](https://x.com/megallmio) for announcements
* Join our [Discord](https://discord.gg/Megallm) for early access to new features
* Check the [Dashboard](https://megallm.io/dashboard/overview) for service status
## Subscribe to Updates
Sign up for email notifications about new releases and important updates at [megallm.io](https://megallm.io).
# Resources
Source: https://docs.megallm.io/en/resources/overview
Find additional resources, guides, tutorials, and community content for MegaLLM.
## Community
Join our Discord community for support and discussions
Explore our open-source projects and examples
## External Resources
* [Dashboard](https://megallm.io/dashboard/overview)
* [YouTube Channel](https://youtube.com/@Megallmio)
* [Twitter/X](https://x.com/megallmio)
Have questions? Visit our [FAQ](/home/faq) or reach out to [support@megallm.io](mailto:support@megallm.io)
# Create chat completion
Source: https://docs.megallm.io/api-reference/chat/create-chat-completion
/cn/api-reference/openapi.json post /chat/completions
Generate conversational AI responses using the OpenAI-compatible chat completions API
# Create message
Source: https://docs.megallm.io/api-reference/messages/create-message
/cn/api-reference/openapi.json post /messages
Create conversational messages using the Anthropic-compatible Messages API
# List models
Source: https://docs.megallm.io/api-reference/models/list-models
/cn/api-reference/openapi.json get /models
List all available AI models with their capabilities and pricing