Learn › Local AI: Run Models on Your Own Machine › The Ollama API: Your Local Backend

The Ollama API: Your Local Backend

Beginner 🕐 14 min Lesson 5 of 13
What you'll learn
  • Make API calls to Ollama using curl and the official Python library
  • Use the OpenAI-compatible endpoint to drop Ollama into any tool built for the OpenAI API
  • Send images to a vision model via the Ollama API

The Ollama API Server

Ollama does more than power a terminal chat session. It runs a local REST API server at http://localhost:11434 that lets any application — a Python script, a workflow tool, an IDE plugin — talk to your local model using standard HTTP requests.

On macOS, the API server starts automatically when Ollama launches. On Linux and Windows, start it with ollama serve in a terminal. Once running, the server stays active and handles requests until you stop it.

Making Your First API Call

The core endpoint is /api/chat. Here is a minimal request using curl:

curl http://localhost:11434/api/chat -d '{"model": "llama3.3", "messages": [{"role": "user", "content": "What is quantization in AI models?"}], "stream": false}'

Setting "stream": false returns the complete response as a single JSON object. The default is streaming, which sends tokens as they generate — useful for displaying responses progressively in an application.

The response JSON includes the model’s reply in message.content, plus metadata like token counts and generation time.

Using the Official Python Library

Ollama provides an official Python library that handles the HTTP details for you:

pip install ollama

A basic chat call looks like this:

import ollama response = ollama.chat( model='llama3.3', messages=[{'role': 'user', 'content': 'Explain prompt caching in one paragraph'}] ) print(response['message']['content'])

The library also supports streaming responses, which is more practical for applications that display output as it generates:

for chunk in ollama.chat(model='llama3.3', messages=[...], stream=True): print(chunk['message']['content'], end='', flush=True)

The OpenAI-Compatible Endpoint

Ollama also exposes an OpenAI-compatible endpoint at http://localhost:11434/v1. This means you can use the OpenAI Python SDK or any tool built for the OpenAI API — and simply point it at your local Ollama instance instead of OpenAI’s servers:

from openai import OpenAI client = OpenAI( base_url='http://localhost:11434/v1', api_key='ollama' # required by the SDK but ignored by Ollama ) response = client.chat.completions.create( model='llama3.3', messages=[{'role': 'user', 'content': 'Hello'}] ) print(response.choices[0].message.content)

The api_key value does not matter — Ollama does not authenticate local requests. This pattern makes it trivial to swap between cloud and local models by changing one line.

Vision Models: Sending Images

Ollama rebuilt its multimodal engine in May 2026. Several models now support image input: LLaVA, Llama 3.2 Vision, Qwen2.5-VL, Gemma 3 27B, and Mistral Small 3.1, among others. All vision models require 1–3 GB of additional VRAM above what the base text model needs for the image encoder.

To send an image via the API, base64-encode the image file and include it in the images array on the user message:

import ollama, base64 with open('screenshot.png', 'rb') as f: img_b64 = base64.b64encode(f.read()).decode() response = ollama.chat( model='llama3.2-vision', messages=[{ 'role': 'user', 'content': 'What is shown in this screenshot?', 'images': [img_b64] }] ) print(response['message']['content'])

Pull a vision model first: ollama pull llama3.2-vision. LLaVA is also widely supported and has the broadest compatibility with third-party clients: ollama pull llava.

Key takeaways
  • The Ollama API runs at localhost:11434; the OpenAI-compatible endpoint is at localhost:11434/v1 — change one base_url line to switch any OpenAI app to local
  • Ollama's official Python library (pip install ollama) handles HTTP, streaming, and multimodal requests cleanly
  • Vision models (LLaVA, Llama 3.2 Vision, Qwen2.5-VL) accept base64-encoded images in the messages array and need 1-3 GB extra VRAM for the image encoder