Add support for Azure-hosted OpenAI-compatible models including OpenAI (GPT-4, GPT-5.2), DeepSeek, and other compatible models.
Implementation:
- Add Azure provider configuration with auto-discovery from environment
- Environment variables: AZURE_ENDPOINT, AZURE_API_KEY, AZURE_DEPLOYMENT, AZURE_API_VERSION
- URL fix middleware to handle Azure's specific URL format
- Tool call ID sanitization for Azure's 40-character limit
- Onboarding wizard support for interactive Azure setup
- Comprehensive tests for configuration and URL handling
- Documentation with setup guide and troubleshooting
Technical details:
- Azure uses different URL structure: {endpoint}/openai/deployments/{deployment}/chat/completions?api-version={version}
- OpenAI SDK constructs URLs incorrectly for Azure, placing query params before path
- URL fix middleware intercepts and corrects malformed URLs transparently
- Supports max_completion_tokens for newer models via compat config
- Tool call IDs automatically truncated to 40 characters for Azure compatibility
Onboard integration:
- Added Azure to auth choice groups (appears after OpenAI)
- Interactive prompts for endpoint, deployment name, API key, and API version
- Auto-discovery from environment variables if already configured
- Supports both manual configuration and environment variable detection
Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
7.1 KiB
Azure Provider
Azure supports deploying OpenAI-compatible models through Azure infrastructure. Moltbot's Azure provider allows you to use various models deployed on Azure, including OpenAI models (GPT-4, GPT-3.5), DeepSeek, and other compatible models.
Prerequisites
Before using Azure with Moltbot, you need:
- An active Azure subscription with Azure AI access
- An Azure resource with model deployments
- Your Azure API key and endpoint
- A deployed model (OpenAI, DeepSeek, or other compatible models)
Configuration
Environment Variables
The easiest way to configure Azure is through environment variables:
export AZURE_ENDPOINT="https://your-resource.cognitiveservices.azure.com"
export AZURE_API_KEY="your-api-key-here"
export AZURE_DEPLOYMENT="your-deployment-name"
export AZURE_API_VERSION="2024-02-01" # Optional, defaults to 2024-08-01-preview
Required Variables
-
AZURE_ENDPOINT: Your Azure resource endpoint URL- Format:
https://{resource-name}.cognitiveservices.azure.comorhttps://{region}.api.cognitive.microsoft.com - Find this in the Azure Portal under your resource's "Keys and Endpoint" section
- Format:
-
AZURE_API_KEY: Your Azure API key- Find this in the Azure Portal under "Keys and Endpoint"
- Either KEY 1 or KEY 2 will work
-
AZURE_DEPLOYMENT: The name of your model deployment- This is the deployment name you configured in Azure AI Studio
- Must match exactly as configured in Azure
- Examples:
gpt-4,gpt-5.2,deepseek-chat
Optional Variables
AZURE_API_VERSION: Azure API version- Default:
2024-08-01-preview - Use a stable API version for production workloads
- Common versions:
2024-02-01,2024-08-01-preview
- Default:
models.json Configuration
Alternatively, you can configure Azure in your models.json file:
{
"providers": {
"azure": {
"baseUrl": "https://your-resource.cognitiveservices.azure.com/openai/deployments/your-deployment/chat/completions?api-version=2024-02-01",
"apiKey": "AZURE_API_KEY",
"api": "openai-completions",
"headers": {
"api-key": "${AZURE_API_KEY}"
},
"models": [
{
"id": "",
"name": "Azure GPT-4",
"reasoning": false,
"input": ["text"],
"cost": {
"input": 10,
"output": 30,
"cacheRead": 2.5,
"cacheWrite": 12.5
},
"contextWindow": 200000,
"maxTokens": 16384,
"compat": {
"maxTokensField": "max_completion_tokens"
}
}
]
}
}
}
Supported Models
Azure supports various OpenAI-compatible models:
OpenAI Models
- GPT-4 Series:
gpt-4,gpt-4-turbo,gpt-4-vision - GPT-5.2: Latest models with extended context
- GPT-3.5:
gpt-35-turbo(note the hyphen instead of dot) - o1/o3 Models: Reasoning models with extended thinking
DeepSeek Models
- DeepSeek-V3: Latest DeepSeek model
- DeepSeek-Chat: General conversation model
Other Compatible Models
Any OpenAI API-compatible model deployed on Azure will work with this provider.
Usage
With Environment Variables
# Configure Azure
export AZURE_ENDPOINT="https://eastus2.api.cognitive.microsoft.com"
export AZURE_API_KEY="your-key-here"
export AZURE_DEPLOYMENT="gpt-5.2"
export AZURE_API_VERSION="2024-02-01"
# Use with Moltbot
moltbot agent --message "Hello" --model azure/gpt-5.2
List Available Models
moltbot models list | grep azure
Expected output:
azure/{deployment-name} Azure {deployment-name} ...
Deployment Name Mapping
Azure uses deployment names instead of model IDs. When you configure AZURE_DEPLOYMENT=gpt-5.2, Moltbot will expose this as:
azure/gpt-5.2
The deployment name becomes the model identifier in Moltbot.
Important Notes
URL Construction
Azure has a specific URL format that differs from standard OpenAI:
Standard OpenAI: https://api.openai.com/v1/chat/completions
Azure: https://{endpoint}/openai/deployments/{deployment}/chat/completions?api-version={version}
Moltbot automatically handles URL construction through an internal URL fix middleware, so you don't need to worry about the format differences.
API Compatibility
Azure's API is compatible with OpenAI's API format but uses:
- Header:
api-keyinstead ofAuthorization: Bearer - Parameter:
max_completion_tokensinstead ofmax_tokens(for newer models) - Query parameter:
api-versionis required
Troubleshooting
HTTP 404 Errors
Problem: Getting 404 errors when making API calls
Solutions:
- Verify your
AZURE_ENDPOINTis correct (should not include/openai/deployments/) - Verify your
AZURE_DEPLOYMENTname matches exactly in Azure Portal - Check that
AZURE_API_VERSIONis supported by your deployment - Ensure your Azure resource has the model deployed
Authentication Errors
Problem: 401 Unauthorized errors
Solutions:
- Verify
AZURE_API_KEYis correct - Check that the API key hasn't been regenerated in Azure Portal
- Ensure the key matches the endpoint (don't mix keys from different resources)
Unsupported Parameter Errors
Problem: Unsupported parameter: 'max_tokens' error
Solution: Newer Azure models require max_completion_tokens instead of max_tokens. Moltbot handles this automatically through the compat.maxTokensField setting.
Model Not Found
Problem: Model doesn't appear in moltbot models list
Solutions:
- Check all required environment variables are set
- Verify
AZURE_DEPLOYMENTis exactly as configured in Azure - Restart Moltbot after changing environment variables
Security Best Practices
- Never commit API keys: Use environment variables or Azure Key Vault
- Rotate keys regularly: Regenerate your API keys periodically
- Use RBAC: Configure role-based access control in Azure
- Monitor usage: Enable Azure Monitor for usage tracking
- Set spending limits: Configure budgets in Azure Cost Management
API Versions
Azure uses API versioning. Common versions:
2024-02-01: Stable production version2024-08-01-preview: Preview with latest features2023-12-01: Older stable version
Check Azure OpenAI API documentation for the latest versions.
Comparison with Standard OpenAI
| Feature | Azure | Standard OpenAI |
|---|---|---|
| Endpoint | Regional (e.g., eastus2) | Global (api.openai.com) |
| Authentication | api-key header | Bearer token |
| Deployment | Named deployments | Model IDs |
| Billing | Azure subscription | OpenAI account |
| Data residency | Regional | Global |
| Enterprise features | Azure integration | OpenAI org settings |