In this categoryLocal AI · 24
- How to Install Ollama on macOSStart
- How to Install Ollama on Windows
- How to Run Llama 3 Locally with Ollama
- How to Pick the Right Local AI Model for Your Hardware
- Best GGUF Models to Run by VRAM Tier (8GB, 12GB, 16GB, 24GB, 48GB)
- Run LLMs in Your Browser With WebGPU: No Install, No Server (WebLLM)
- How to Use Ollama as a Drop-In OpenAI API
- GGUF vs MLX vs NVFP4: Local AI Quantization Formats Explained
How to Use Ollama as a Drop-In OpenAI API
Point any OpenAI-compatible SDK or tool at your local Ollama server by changing one URL, and use OLLAMA_HOST to expose it on your network.
Ollama ships a REST API that mirrors the OpenAI chat completions spec. Any library or tool that accepts a custom base URL, including the official OpenAI Python and Node SDKs, LangChain, LlamaIndex, and Continue, can talk to your local Ollama server with zero code changes beyond the base URL and a dummy API key. This guide covers the raw curl calls, the Python SDK, and the environment variables that control how Ollama exposes itself.
What you need
- Ollama installed and running (ollama serve or the background service)
- At least one model already pulled (ollama pull llama3.3)
- curl for the verification steps
- Python 3.9+ or Node 18+ if you want to follow the SDK examples
Step 1: Understand the endpoint layout
Ollama exposes two API families on port 11434. The native Ollama API under /api is richer but non-standard. The OpenAI-compatible API under /v1 is the one you use for drop-in replacement.
- GET http://localhost:11434 — health check, returns 'Ollama is running'
- GET http://localhost:11434/v1/models — list available models in OpenAI format
- POST http://localhost:11434/v1/chat/completions — chat completions (same body as OpenAI)
- POST http://localhost:11434/v1/completions — legacy text completions
- POST http://localhost:11434/v1/embeddings — embeddings (requires an embedding model like nomic-embed-text)
Step 2: Test with raw curl
Before touching any SDK, confirm the endpoint works with curl. The body is identical to what you would send to api.openai.com, but with a local URL and any non-empty string as the API key.
Step 3: Use the OpenAI Python SDK
Install the official openai package and point it at your local server. Change only base_url and api_key. The rest of your code stays identical to what you wrote for the real OpenAI API.
- from openai import OpenAI
- client = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')
- response = client.chat.completions.create(
- model='llama3.3',
- messages=[{'role': 'user', 'content': 'What is the capital of France?'}]
- )
- print(response.choices[0].message.content)
Step 4: Use the OpenAI Node.js SDK
- import OpenAI from 'openai';
- const client = new OpenAI({ baseURL: 'http://localhost:11434/v1', apiKey: 'ollama' });
- const res = await client.chat.completions.create({
- model: 'llama3.3',
- messages: [{ role: 'user', content: 'What is the speed of light?' }],
- });
- console.log(res.choices[0].message.content);
Step 5: Expose Ollama on your local network
By default Ollama listens only on 127.0.0.1, so only the local machine can reach it. Set the OLLAMA_HOST environment variable to bind to all interfaces and reach it from other devices on the same network, such as a phone or another dev machine.
Streaming responses
Ollama supports streaming the same way OpenAI does. Set stream: true in the request body and iterate over the server-sent events. Both the Python and Node SDKs abstract this into async iterators.
At this point any tool or library built for the OpenAI API works against your local Ollama server. Switch model names to llama3.3, codellama, mistral, or any other pulled model. You can also run multiple models and switch between them per request, which the local server handles by loading and unloading from GPU memory automatically.
Local models run better with more VRAM. CompareRTX GPUs on Amazonbefore you upgrade.(affiliate link. We may earn a commission at no extra cost. Disclosure)
Watch related tutorials
5:30
14:20
10:05
9:42
8:55
10:30Weekly local AI drops
New models, what runs on your hardware, and the guides to set them up. One email a week, unsubscribe any time.