Topic 1 of 5 · 4 min read
Calling local models through OpenAI-compatible APIs
Goal: Change an app's API client to call a local model and list the features that behave differently.
Most local runners speak the OpenAI API, so moving an app to a local model is usually a change of base URL, API key and model name, not a rewrite.
Check yourself · 1 of 3
You have an app that uses the OpenAI SDK. You want it to call a model running in Ollama on the same laptop. What is the smallest change?
Why it matters
Ollama, llama-server (from llama.cpp), LM Studio and vLLM all expose /v1/chat/completions and related routes. Code written against the OpenAI SDK, or any library that wraps it, can talk to them. That lets you develop offline, keep private data on your machine and swap models without touching business logic.
How to do it
- Start the runner and note its address. Typical defaults are
http://localhost:11434/v1for Ollama,http://localhost:8080/v1for llama-server,http://localhost:1234/v1for LM Studio andhttp://localhost:8000/v1for vLLM. Check your own setup, since ports are configurable. - Set the client's base URL to that address.
- Pass any non-empty string as the API key. The SDK insists on one; local servers ignore it unless you configured authentication.
- Use the model name the runner knows, such as the tag from
ollama list, not a hosted model name. - Move the URL and model into environment variables so the same code runs against both.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: process.env.LLM_BASE_URL ?? "http://localhost:11434/v1",
apiKey: process.env.LLM_API_KEY ?? "local",
});
const res = await client.chat.completions.create({
model: process.env.LLM_MODEL ?? "qwen3:8b",
messages: [{ role: "user", content: "Reply with the word ready." }],
});
console.log(res.choices[0].message.content);
Where compatibility ends
"Compatible" means the common path works. Check the runner's documentation for each feature you rely on: streaming usage counts, response_format, tool calls, log probabilities, image input, the Responses API and the Batch API are supported unevenly or not at all. Fields the server does not know may be ignored silently rather than rejected, so an app can seem to work while a setting does nothing.
Before and after
Before: The app hardcodes a hosted model name and a hosted key. Pointed at a local server it fails with "model not found".
After: URL, key and model come from configuration, and a startup check calls the runner's model list and fails with a clear message when the configured model is not there.
What goes wrong
- The context window is smaller than the app assumes. Some runners use a small default context unless you raise it, and they may cut the start of a long prompt without an error.
- A feature is ignored silently, for example a
temperatureorseedthe runner does not honour, and results differ from the hosted model for no visible reason. - Prompts tuned for a hosted model behave worse on a small local model. Re-test the prompts; the endpoint being the same does not make the model the same.
Examples
One client, two backends
# local
LLM_BASE_URL=http://localhost:11434/v1 LLM_MODEL=qwen3:8b node app.js
# hosted
LLM_BASE_URL=https://api.openai.com/v1 LLM_API_KEY=sk_live_...redacted LLM_MODEL=<hosted-model> node app.js
The code is identical. Only the three variables change, which makes a local run a cheap way to test the app's plumbing.
A silent context cut
An app sends a 12,000-token contract to a local model and gets a summary of only the last pages. The runner's context setting was lower than the prompt, so the start was dropped with no error. Raising the context and checking the reported prompt token count fixed it.
Key points
- Ollama, llama-server, LM Studio and vLLM expose OpenAI-style routes, so most apps switch with a new base URL, key and model name.
- The API key must be a non-empty string for the SDK, but a local server ignores it unless you set up authentication.
- Put base URL, key and model in configuration so the same code runs against hosted and local models.
- Compatibility covers the common path; check each runner's docs for structured output, tools, images and usage counts.
- Unknown request fields may be ignored without an error, so verify that a setting actually changes behaviour.
Common mistakes
- Leaving a hosted model name in the config and reading "model not found" as a network problem.
- Assuming every request field is honoured because the server returned 200.
- Reusing prompts tuned for a large hosted model without re-testing on the small local one.
- Hardcoding the URL in several files instead of one configuration value.
Check yourself · 2 of 3
After switching to a local server, an app that sets an unusual request field still gets a 200 reply, but the field has no effect. What is the most likely cause?
Check yourself · 3 of 3
A local model summarises only the last pages of a very long document, with no error. Which setting should you check first?
Practice items, not counted towards your mastery.
Start here
- Ollama: OpenAI compatibility (opens in a new tab)DocsFree
Lists the endpoints and request fields Ollama accepts through its OpenAI-style API, and what is not supported.
- llama.cpp server README (opens in a new tab)DocsFree
Flags and endpoints of llama-server, including its OpenAI-compatible routes and JSON schema options.
- vLLM: structured outputs (opens in a new tab)DocsFree
How vLLM constrains output to a JSON schema, regex or grammar.