Introduction
In 2026, the AI ecosystem has become even more diverse: from free open-source models to premium solutions from leading vendors. However, many developers and businesses face the problem of being "locked in" to a single AI provider: you pay for a subscription, get limited API access, and cannot quickly switch to a cheaper or higher-quality alternative. The solution is to learn how to connect any AI model via OpenRouter, custom endpoints, and proxies. In this article, I'll show you how to do this in 15 minutes, maintaining flexibility and control over costs.
We'll cover three key approaches: OpenRouter as a universal gateway, direct custom APIs for integration with any provider, and proxy servers to bypass regional restrictions. You'll learn how to set up and test each one, as well as what nuances to consider when choosing a tool. Ready? Let's go!
What is OpenRouter and Why Does Everyone Need It?
OpenRouter is an aggregator service that provides a single API interface for dozens of AI models from different providers. Instead of registering separately with OpenAI, Google, Anthropic, and others, you get one endpoint, one access key, and unified billing. This simplifies connecting an AI model and reduces operational costs.
Advantages of OpenRouter:
- Flexibility: switch between models (GPT-4o, Claude 4, Llama 4, etc.) without changing code.
- Cost savings: you only pay for tokens used, not fixed subscriptions.
- Monitoring: built-in analytics for cost, latency, and errors.
How to connect:
1. Register at openrouter.ai (or alternatives like one-api.com).
2. Get an API key from your dashboard.
3. Configure an HTTP client (e.g., cURL or Python requests):
curl https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Tip: In OpenRouter, you can set a fallback model — if the primary one is unavailable, the request is automatically redirected to a backup. This increases your service's fault tolerance.
Custom APIs: Direct Integration with Any AI Provider
If you need full control over endpoints, or you're working with a private model deployed on your own server, use a custom API. This can be a standard OpenAI-compatible endpoint or a proprietary protocol.
Step 1: Choose a Provider and Get Credentials
| Provider | Access Type | Cost (example) |
|---|---|---|
| OpenAI | API key | $0.01-0.03 per 1K tokens |
| Anthropic | API key | $0.015-0.05 per 1K tokens |
| Hugging Face | Free / Pro | $0-0.02 per 1K tokens |
| Local model (Ollama) | HTTP endpoint | Free |
Step 2: Configure the Request
For OpenAI-compatible APIs (OpenAI, Together AI, DeepSeek), the code is nearly identical:
import requests
url = "https://api.openai.com/v1/chat/completions" # or your custom endpoint
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"model": "gpt-4",
"messages": [{"role": "user", "content": "Tell me about custom APIs"}]
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
Step 3: Error Handling and Limits
Custom APIs often have rate limits and different error formats. I recommend adding retry logic with exponential backoff. For example, on HTTP 429 (Too Many Requests), wait 1-5 seconds and retry.
Important: When working with custom endpoints, always check for streaming support (token streaming). This improves the user experience for chatbots.
Proxies for AI Models: Bypassing Restrictions and Security
Sometimes connecting an AI model is impossible directly due to
Comments