Local OpenAI-Compatible API
Point any OpenAI client at
http://127.0.0.1:11435/v1to 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.
| Setting | Value |
|---|---|
| Base URL | http://127.0.0.1:11435/v1 |
| API key | None. Clients that require one can send any value |
| Models | The 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#
curl http://127.0.0.1:11435/v1/models
{
"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.
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].
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#
| Route | Behavior |
|---|---|
GET /v1/models | Lists 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/completions | Chat completion, streaming or not, from a local model |
GET /health | {"status": "ok"} while the daemon runs |
Choosing a model#
modelnames a model fromGET /v1/models. Alovelace:prefix is accepted, solovelace:llama-3.2-3b-instructandllama-3.2-3b-instructare the same model.- When
modelis 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:
{
"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"
}
}
| Status | code | Meaning and fix |
|---|---|---|
| 400 | null | The body is not a JSON object with a non-empty messages array |
| 403 | forbidden_host | The request was not addressed to 127.0.0.1 or localhost |
| 403 | forbidden_origin | A web page on a non-loopback origin called the API; allow it with allowed_origins |
| 404 | no_local_model | No local model is available. Run lattice-ctl setup |
| 404 | model_not_found | The requested model is not available locally. The message lists the available models |
| 404 | unknown_url | The route is not part of the local API |
| 503 | local_runtime_unavailable | The local model runtime is not running. Run lattice-ctl setup |
| varies | local_runtime_error | The 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.
[local_api]
enabled = true
port = 11435
allowed_origins = ["https://app.example.com"]
| Key | Environment variable | Default | Effect |
|---|---|---|---|
enabled | LATTICE_LOCAL_API_ENABLED | true | false turns the API off |
port | LATTICE_LOCAL_API_PORT | 11435 | The loopback port |
allowed_origins | LATTICE_LOCAL_API_ALLOWED_ORIGINS | none | Extra 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.1only, 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 inallowed_origins. Requests without anOriginheader, such as those from SDKs, servers, andcurl, are not affected.