Skip to main content

OpenAI API on a VDS: Connection and Cost Optimization

AI Agents on VPS · 19.04.2026 · 5 min read
Illustration for “OpenAI API on a VDS: Connection and Cost Optimization”

Why Use the OpenAI API on a VDS?

Even with local models available, the OpenAI API (GPT-4o, GPT-4-turbo) outperforms them in quality for complex tasks. A VDS is ideal for: production applications with an API key, proxy servers for a team, multi-agent systems with different models.

Installing the OpenAI SDK

>pip install openai python-dotenv

Basic Usage

import os
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a hosting assistant."},
        {"role": "user", "content": "What is a VDS?"}
    ]
)
print(response.choices[0].message.content)

Mixed Strategy: OpenAI + Ollama

Use a cheap local model for simple tasks and OpenAI for complex ones:

def get_llm(complexity: str):
    if complexity == "simple":
        from langchain_ollama import OllamaLLM
        return OllamaLLM(model="llama3.2")
    else:
        from langchain_openai import ChatOpenAI
        return ChatOpenAI(model="gpt-4o-mini")

Proxy Server for a Team

LiteLLM lets you create an OpenAI-compatible proxy that routes requests between Ollama and OpenAI:

>pip install litellm
litellm --model ollama/llama3.2 --port 8000
Savings: GPT-4o-mini costs 30 times less than GPT-4o with similar quality for most tasks. Use it by default.

Common errors when working with the API

«Incorrect API key provided»

openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided'}}

The key was copied with a stray space, or it belongs to a different project in the organization. Check that the OPENAI_API_KEY environment variable is actually picked up by the process — a common VDS mistake is a .env file sitting in a different working directory than the one the script runs from.

«You exceeded your current quota»

openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota'}}

A 429 with this particular text does not mean a temporary rate-limit hit — it means the paid balance is exhausted or there is no payment method on file. Do not confuse this with ordinary throttling: the billing message will not go away on its own after a pause.

Timeouts on long responses

openai.APITimeoutError: Request timed out

The client has a finite response timeout by default. For long generations with a large token count, increase the timeout explicitly in the client constructor and consider streaming mode (stream=True) so you receive tokens as they are generated instead of waiting for one complete response.

«The model does not exist or you do not have access to it»

openai.NotFoundError: Error code: 404 - The model 'gpt-4' does not exist or you do not have access to it

Either the model name has a typo, or the account simply does not have access to that specific model or version. Fetch the current list of models your key can access programmatically and check the name against it, rather than against documentation that can lag behind.

Comparing ways to connect to OpenAI

The difference between these options is not in the API call itself, but in who controls spending and how requests are routed.

ApproachCost controlLatencyComplexity
Direct SDK call from the appManualMinimalLow
A LiteLLM proxy routing to OllamaFlexibleSmall proxy overheadMedium
A single gateway for the whole teamCentralizedDepends on the gatewayHigh
Checking available models via the APINot applicableA single requestLow

When an external API is not a good fit

An external API is convenient but not universal — in some scenarios it loses to local inference or is simply unavailable for reasons outside your control.

  • Data cannot leave the company perimeter under confidentiality requirements — the only option then is local models via Ollama or LocalAI
  • Load is predictable and steady — at a large stable request volume, local inference on your own GPU can end up cheaper than paying per token
  • You need guaranteed operation with no internet, or over an unstable connection — an external API is simply unavailable without a network
  • The project budget cannot absorb variable per-token spending — a fixed server rental is easier to forecast than a token bill
  • You need a model version that is gated for your account — access to some models is restricted by the provider's own policies

Verifying the result

Before switching production traffic over, check not just for a successful response but the actual cost of a request.

  1. Send a test request and confirm the response comes back without authentication errors
  2. Check your actual token spend after the test in the OpenAI dashboard
  3. If you use a LiteLLM proxy, confirm both routes — to OpenAI and to Ollama — respond correctly
  4. Measure response latency across different models to pick the right balance of price and speed
  5. Fetch the list of available models programmatically and check it against what your code actually uses

Related: a comparison of the major AI APIs — OpenAI, Claude, Gemini, and Groq, Anthropic's API — Claude API, a local alternative with no per-token cost — Ollama on a VDS.

Was this article helpful?
← Back to Knowledge Base Ask Support