Skip to content
Lovelace

Local OpenAI-Compatible API

Point any OpenAI client at http://127.0.0.1:11435/v1 to run it on the models on this computer

While Lattice runs, it serves an OpenAI-compatible API for the local models on the same computer. There is no API key and no account, and requests never leave the machine: the daemon forwards them only to the local model runtime (LM Studio), never to a cloud provider or to Lattice Cloud.

SettingValue
Base URLhttp://127.0.0.1:11435/v1
API keyNone. Clients that require one can send any value
ModelsThe models the local runtime serves (GET /v1/models)

If you have not set up Lattice yet, run lattice-ctl setup. The API is on by default whenever the daemon runs.

Check that it answers#

bash
curl http://127.0.0.1:11435/v1/models
json
{
  "object": "list",
  "data": [
    {
      "id": "llama-3.2-3b-instruct",
      "object": "model",
      "created": 0,
      "owned_by": "lmstudio"
    }
  ]
}

Use it from code#

Change only the base URL. Pass any string where the SDK requires a key.

ts
ts
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "http://127.0.0.1:11435/v1",
  apiKey: "lattice",
});

const completion = await client.chat.completions.create({
  model: "llama-3.2-3b-instruct",
  messages: [{ role: "user", content: "Write a haiku about local models." }],
});

console.log(completion.choices[0]?.message.content);

Tools and frameworks that accept an OpenAI base URL work the same way, for example OPENAI_BASE_URL=http://127.0.0.1:11435/v1 with OPENAI_API_KEY set to any value.

Streaming#

Set stream: true. The response is text/event-stream: one data: <chat.completion.chunk> event per chunk, ending with data: [DONE].

bash
curl -N http://127.0.0.1:11435/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "llama-3.2-3b-instruct", "stream": true, "messages": [{"role": "user", "content": "Count to five."}]}'

Tools and multi-part content#

The request body reaches the local runtime unchanged except for the model id, so tools, tool_choice, assistant tool_calls, tool result messages, multi-part content (text and image_url parts), response_format, and sampling parameters behave as the local runtime implements them. Tool calls come back in the standard choices[0].message.tool_calls field, or as delta.tool_calls when streaming. Whether a model can call tools or read images depends on the model.

Endpoints#

RouteBehavior
GET /v1/modelsLists the local models as OpenAI model objects
GET /v1/models/{id}One model object. Ids may contain /, such as publisher/model-name
POST /v1/chat/completionsChat completion, streaming or not, from a local model
GET /health{"status": "ok"} while the daemon runs

Choosing a model#

  • model names a model from GET /v1/models. A lovelace: prefix is accepted, so lovelace:llama-3.2-3b-instruct and llama-3.2-3b-instruct are the same model.
  • When model is omitted, Lattice uses its configured local default model, or the first chat model the local runtime serves.

Errors#

Errors use the OpenAI shape, so OpenAI SDKs raise them as API errors:

json
{
  "error": {
    "message": "No local model is available on this computer. Run `lattice-ctl setup` to download one.",
    "type": "invalid_request_error",
    "param": "model",
    "code": "no_local_model"
  }
}
StatuscodeMeaning and fix
400nullThe body is not a JSON object with a non-empty messages array
403forbidden_hostThe request was not addressed to 127.0.0.1 or localhost
403forbidden_originA web page on a non-loopback origin called the API; allow it with allowed_origins
404no_local_modelNo local model is available. Run lattice-ctl setup
404model_not_foundThe requested model is not available locally. The message lists the available models
404unknown_urlThe route is not part of the local API
503local_runtime_unavailableThe local model runtime is not running. Run lattice-ctl setup
varieslocal_runtime_errorThe local runtime rejected the request; its status and message are kept

Configuration#

Environment variables win over ~/.lovelace/lattice/config.toml. Restart the daemon after changing either.

toml
[local_api]
enabled = true
port = 11435
allowed_origins = ["https://app.example.com"]
KeyEnvironment variableDefaultEffect
enabledLATTICE_LOCAL_API_ENABLEDtruefalse turns the API off
portLATTICE_LOCAL_API_PORT11435The loopback port
allowed_originsLATTICE_LOCAL_API_ALLOWED_ORIGINSnoneExtra browser origins, comma-separated in the variable

If another program already uses the port, the daemon logs a warning and keeps running without the API; choose another port with LATTICE_LOCAL_API_PORT.

Security#

  • The API listens on 127.0.0.1 only, so other machines cannot reach it.
  • Requests addressed to any host other than 127.0.0.1, localhost, or [::1] are refused, which stops web pages that rebind a domain name to your computer.
  • Web pages can call the API from loopback origins such as http://localhost:3000. Pages on other origins are refused unless listed in allowed_origins. Requests without an Origin header, such as those from SDKs, servers, and curl, are not affected.