Skip to content

Commit 848c285

Browse files
committed
feat(models): start local services on demand
1 parent bf17d01 commit 848c285

24 files changed

Lines changed: 1527 additions & 91 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai
66

77
### Changes
88

9+
- Models: add provider-level `localService` startup for on-demand local model servers before OpenAI-compatible requests, including one-shot model probes.
910
- Agents: trim default system prompt guidance and send-only message tool schemas to reduce prompt tokens while preserving GPT-5 personality guidance.
1011
- Context: add `/context map` to send a treemap image of the current session context contributors. (#79867)
1112
- Slack: add `unfurlLinks` and `unfurlMedia` config for bot `chat.postMessage` replies, including per-account overrides, so Slack link and media previews can be suppressed without workspace-wide settings. Fixes #48435. (#80145) Thanks @esegev1 and @HemantSudarshan.

‎docs/docs.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,8 @@
15561556
"gateway/openresponses-http-api",
15571557
"gateway/tools-invoke-http-api",
15581558
"gateway/cli-backends",
1559-
"gateway/local-models"
1559+
"gateway/local-models",
1560+
"gateway/local-model-services"
15601561
]
15611562
},
15621563
{

‎docs/gateway/config-agents.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ Time format in system prompt. Default: `auto` (OS preference).
397397
- `compat.thinkingFormat`: OpenAI-compatible thinking payload style. Use `"qwen"` for Qwen-style top-level `enable_thinking`, or `"qwen-chat-template"` for `chat_template_kwargs.enable_thinking` on Qwen-family backends that support request-level chat-template kwargs, such as vLLM. OpenClaw maps disabled thinking to `false` and enabled thinking to `true`.
398398
- `compat.supportedReasoningEfforts`: per-model OpenAI-compatible reasoning effort list. Include `"xhigh"` for custom endpoints that truly accept it; OpenClaw then exposes `/think xhigh` in command menus, Gateway session rows, session patch validation, agent CLI validation, and `llm-task` validation for that configured provider/model. Use `compat.reasoningEffortMap` when the backend wants a provider-specific value for a canonical level.
399399
- `params.preserveThinking`: Z.AI-only opt-in for preserved thinking. When enabled and thinking is on, OpenClaw sends `thinking.clear_thinking: false` and replays prior `reasoning_content`; see [Z.AI thinking and preserved thinking](/providers/zai#thinking-and-preserved-thinking).
400+
- `localService`: optional provider-level process manager for local/self-hosted model servers. When the selected model belongs to that provider, OpenClaw probes `healthUrl` (or `baseUrl + "/models"`), starts `command` with `args` if the endpoint is down, waits up to `readyTimeoutMs`, then sends the model request. `command` must be an absolute path. `idleStopMs: 0` keeps the process alive until OpenClaw exits; a positive value stops the OpenClaw-spawned process after that many idle milliseconds. See [Local model services](/gateway/local-model-services).
400401
- Runtime policy belongs on providers or models, not on `agents.defaults`. Use `models.providers.<provider>.agentRuntime` for provider-wide rules or `agents.defaults.models["provider/model"].agentRuntime` / `agents.list[].models["provider/model"].agentRuntime` for model-specific rules. OpenAI agent models on the official OpenAI provider select Codex by default.
401402
- Config writers that mutate these fields (for example `/models set`, `/models set-image`, and fallback add/remove commands) save canonical object form and preserve existing fallback lists when possible.
402403
- `maxConcurrent`: max parallel agent runs across sessions (each session still serialized). Default: 4.

‎docs/gateway/configuration-reference.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ The `models` root also owns global model-catalog behavior.
7676

7777
- `models.mode`: provider catalog behavior (`merge` or `replace`).
7878
- `models.providers`: custom provider map keyed by provider id.
79+
- `models.providers.*.localService`: optional on-demand process manager for
80+
local model servers. OpenClaw probes the configured health endpoint, starts
81+
the absolute `command` when needed, waits for readiness, then sends the model
82+
request. See [Local model services](/gateway/local-model-services).
7983
- `models.pricing.enabled`: controls the background pricing bootstrap that
8084
starts after sidecars and channels reach the Gateway ready path. When `false`,
8185
the Gateway skips OpenRouter and LiteLLM pricing-catalog fetches; configured
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
summary: "Start local model servers on demand before OpenClaw model requests"
3+
read_when:
4+
- You want OpenClaw to start a local model server only when its model is selected
5+
- You run ds4, inferrs, vLLM, llama.cpp, MLX, or another OpenAI-compatible local server
6+
- You need to control cold start, readiness, and idle shutdown for local providers
7+
title: "Local model services"
8+
---
9+
10+
`models.providers.<id>.localService` lets OpenClaw start a provider-owned local
11+
model server on demand. It is provider-level config: when the selected model
12+
belongs to that provider, OpenClaw probes the service, starts the process if the
13+
endpoint is down, waits for readiness, then sends the model request.
14+
15+
Use it for local servers that are expensive to keep running all day, or for
16+
manual setups where model selection should be enough to bring the backend up.
17+
18+
## How it works
19+
20+
1. A model request resolves to a configured provider.
21+
2. If that provider has `localService`, OpenClaw probes `healthUrl`.
22+
3. If the probe succeeds, OpenClaw uses the existing server.
23+
4. If the probe fails, OpenClaw starts `command` with `args`.
24+
5. OpenClaw polls readiness until `readyTimeoutMs` expires.
25+
6. The model request is sent through the normal provider transport.
26+
7. If OpenClaw started the process and `idleStopMs` is positive, the process is
27+
stopped after the last in-flight request has been idle for that long.
28+
29+
OpenClaw does not install launchd, systemd, Docker, or a daemon for this. The
30+
server is a child process of the OpenClaw process that first needed it.
31+
32+
## Config shape
33+
34+
```json5
35+
{
36+
models: {
37+
providers: {
38+
local: {
39+
baseUrl: "http://127.0.0.1:8000/v1",
40+
apiKey: "local-model",
41+
api: "openai-completions",
42+
timeoutSeconds: 300,
43+
localService: {
44+
command: "/absolute/path/to/server",
45+
args: ["--host", "127.0.0.1", "--port", "8000"],
46+
cwd: "/absolute/path/to/working-dir",
47+
env: { LOCAL_MODEL_CACHE: "/absolute/path/to/cache" },
48+
healthUrl: "http://127.0.0.1:8000/v1/models",
49+
readyTimeoutMs: 180000,
50+
idleStopMs: 0,
51+
},
52+
models: [
53+
{
54+
id: "my-local-model",
55+
name: "My Local Model",
56+
reasoning: false,
57+
input: ["text"],
58+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
59+
contextWindow: 131072,
60+
maxTokens: 8192,
61+
},
62+
],
63+
},
64+
},
65+
},
66+
}
67+
```
68+
69+
## Fields
70+
71+
- `command`: absolute executable path. Shell lookup is not used.
72+
- `args`: process arguments. No shell expansion, pipes, globbing, or quoting
73+
rules are applied.
74+
- `cwd`: optional working directory for the process.
75+
- `env`: optional environment variables merged over the OpenClaw process
76+
environment.
77+
- `healthUrl`: readiness URL. If omitted, OpenClaw appends `/models` to
78+
`baseUrl`, so `http://127.0.0.1:8000/v1` becomes
79+
`http://127.0.0.1:8000/v1/models`.
80+
- `readyTimeoutMs`: startup readiness deadline. Default: `120000`.
81+
- `idleStopMs`: idle shutdown delay for OpenClaw-started processes. `0` or
82+
omitted keeps the process alive until OpenClaw exits.
83+
84+
## Inferrs example
85+
86+
Inferrs is a custom OpenAI-compatible `/v1` backend, so the same local service
87+
API works with the `inferrs` provider entry.
88+
89+
```json5
90+
{
91+
agents: {
92+
defaults: {
93+
model: { primary: "inferrs/google/gemma-4-E2B-it" },
94+
},
95+
},
96+
models: {
97+
mode: "merge",
98+
providers: {
99+
inferrs: {
100+
baseUrl: "http://127.0.0.1:8080/v1",
101+
apiKey: "inferrs-local",
102+
api: "openai-completions",
103+
timeoutSeconds: 300,
104+
localService: {
105+
command: "/opt/homebrew/bin/inferrs",
106+
args: [
107+
"serve",
108+
"google/gemma-4-E2B-it",
109+
"--host",
110+
"127.0.0.1",
111+
"--port",
112+
"8080",
113+
"--device",
114+
"metal",
115+
],
116+
healthUrl: "http://127.0.0.1:8080/v1/models",
117+
readyTimeoutMs: 180000,
118+
idleStopMs: 0,
119+
},
120+
models: [
121+
{
122+
id: "google/gemma-4-E2B-it",
123+
name: "Gemma 4 E2B (inferrs)",
124+
reasoning: false,
125+
input: ["text"],
126+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
127+
contextWindow: 131072,
128+
maxTokens: 4096,
129+
compat: {
130+
requiresStringContent: true,
131+
},
132+
},
133+
],
134+
},
135+
},
136+
},
137+
}
138+
```
139+
140+
Replace `command` with the result of `which inferrs` on the machine running
141+
OpenClaw.
142+
143+
## ds4 example
144+
145+
```json5
146+
{
147+
models: {
148+
providers: {
149+
ds4: {
150+
baseUrl: "http://127.0.0.1:18000/v1",
151+
apiKey: "ds4-local",
152+
api: "openai-completions",
153+
timeoutSeconds: 300,
154+
localService: {
155+
command: "/Users/you/Projects/oss/ds4/ds4-server",
156+
args: [
157+
"--model",
158+
"/Users/you/Projects/oss/ds4/ds4flash.gguf",
159+
"--host",
160+
"127.0.0.1",
161+
"--port",
162+
"18000",
163+
"--ctx",
164+
"393216",
165+
],
166+
cwd: "/Users/you/Projects/oss/ds4",
167+
healthUrl: "http://127.0.0.1:18000/v1/models",
168+
readyTimeoutMs: 300000,
169+
idleStopMs: 0,
170+
},
171+
models: [],
172+
},
173+
},
174+
},
175+
}
176+
```
177+
178+
## Operational notes
179+
180+
- One OpenClaw process manages the child it started. Another OpenClaw process
181+
that sees the same health URL already live will reuse it without adopting it.
182+
- Startup is serialized per provider command and argument set, so concurrent
183+
requests do not spawn duplicate servers for the same config.
184+
- Active streaming responses hold a lease; idle shutdown waits until response
185+
body handling is complete.
186+
- Use `timeoutSeconds` on slow local providers so cold starts and long generations
187+
do not hit the default model request timeout.
188+
- Use an explicit `healthUrl` if your server exposes readiness somewhere other
189+
than `/v1/models`.
190+
191+
## Related
192+
193+
<CardGroup cols={2}>
194+
<Card title="Local models" href="/gateway/local-models" icon="server">
195+
Local model setup, provider choices, and safety guidance.
196+
</Card>
197+
<Card title="Inferrs" href="/providers/inferrs" icon="cpu">
198+
Run OpenClaw through the inferrs OpenAI-compatible local server.
199+
</Card>
200+
</CardGroup>

‎docs/gateway/local-models.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ title: "Local models"
99

1010
Local models are doable. They also raise the bar on hardware, context size, and prompt-injection defense — small or aggressively quantized cards truncate context and leak safety. This page is the opinionated guide for higher-end local stacks and custom OpenAI-compatible local servers. For lowest-friction onboarding, start with [LM Studio](/providers/lmstudio) or [Ollama](/providers/ollama) and `openclaw onboard`.
1111

12+
For local servers that should start only when a selected model needs them, see
13+
[Local model services](/gateway/local-model-services).
14+
1215
## Hardware floor
1316

1417
Aim high: **≥2 maxed-out Mac Studios or an equivalent GPU rig (~$30k+)** for a comfortable agent loop. A single **24 GB** GPU works only for lighter prompts at higher latency. Always run the **largest / full-size variant you can host**; small or heavily quantized checkpoints raise prompt-injection risk (see [Security](/gateway/security)).

‎docs/providers/inferrs.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,60 @@ This example uses Gemma 4 on a local `inferrs` server.
8686
}
8787
```
8888

89+
## On-demand startup
90+
91+
Inferrs can also be started by OpenClaw only when an `inferrs/...` model is
92+
selected. Add `localService` to the same provider entry:
93+
94+
```json5
95+
{
96+
models: {
97+
providers: {
98+
inferrs: {
99+
baseUrl: "http://127.0.0.1:8080/v1",
100+
apiKey: "inferrs-local",
101+
api: "openai-completions",
102+
timeoutSeconds: 300,
103+
localService: {
104+
command: "/opt/homebrew/bin/inferrs",
105+
args: [
106+
"serve",
107+
"google/gemma-4-E2B-it",
108+
"--host",
109+
"127.0.0.1",
110+
"--port",
111+
"8080",
112+
"--device",
113+
"metal",
114+
],
115+
healthUrl: "http://127.0.0.1:8080/v1/models",
116+
readyTimeoutMs: 180000,
117+
idleStopMs: 0,
118+
},
119+
models: [
120+
{
121+
id: "google/gemma-4-E2B-it",
122+
name: "Gemma 4 E2B (inferrs)",
123+
reasoning: false,
124+
input: ["text"],
125+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
126+
contextWindow: 131072,
127+
maxTokens: 4096,
128+
compat: {
129+
requiresStringContent: true,
130+
},
131+
},
132+
],
133+
},
134+
},
135+
},
136+
}
137+
```
138+
139+
`command` must be absolute. Use `which inferrs` on the Gateway host and put that
140+
path in config. For the full field reference, see
141+
[Local model services](/gateway/local-model-services).
142+
89143
## Advanced configuration
90144

91145
<AccordionGroup>
@@ -206,6 +260,9 @@ For general help, see [Troubleshooting](/help/troubleshooting) and [FAQ](/help/f
206260
<Card title="Local models" href="/gateway/local-models" icon="server">
207261
Running OpenClaw against local model servers.
208262
</Card>
263+
<Card title="Local model services" href="/gateway/local-model-services" icon="play">
264+
Starting local model servers on demand for configured providers.
265+
</Card>
209266
<Card title="Gateway troubleshooting" href="/gateway/troubleshooting#local-openai-compatible-backend-passes-direct-probes-but-agent-runs-fail" icon="wrench">
210267
Debugging local OpenAI-compatible backends that pass probes but fail agent runs.
211268
</Card>

‎src/agents/pi-embedded-runner/model.inline-provider.ts‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ModelDefinitionConfig, ModelProviderConfig } from "../../config/ty
33
import { normalizeGoogleApiBaseUrl } from "../../infra/google-api-base-url.js";
44
import { normalizeOptionalLowercaseString } from "../../shared/string-coerce.js";
55
import { isSecretRefHeaderValueMarker } from "../model-auth-markers.js";
6+
import { attachModelProviderLocalService } from "../provider-local-service.js";
67
import {
78
attachModelProviderRequestTransport,
89
resolveProviderRequestConfig,
@@ -27,6 +28,7 @@ export type InlineProviderConfig = {
2728
authHeader?: boolean;
2829
timeoutSeconds?: ModelProviderConfig["timeoutSeconds"];
2930
request?: ModelProviderConfig["request"];
31+
localService?: ModelProviderConfig["localService"];
3032
};
3133

3234
export function normalizeResolvedTransportApi(
@@ -154,24 +156,27 @@ export function buildInlineProviderModels(
154156
capability: "llm",
155157
transport: "stream",
156158
});
157-
return attachModelProviderRequestTransport(
158-
{
159-
...model,
160-
contextWindow: model.contextWindow ?? entry?.contextWindow,
161-
contextTokens: model.contextTokens ?? entry?.contextTokens,
162-
maxTokens: model.maxTokens ?? entry?.maxTokens,
163-
input: resolveProviderModelInput({
159+
return attachModelProviderLocalService(
160+
attachModelProviderRequestTransport(
161+
{
162+
...model,
163+
contextWindow: model.contextWindow ?? entry?.contextWindow,
164+
contextTokens: model.contextTokens ?? entry?.contextTokens,
165+
maxTokens: model.maxTokens ?? entry?.maxTokens,
166+
input: resolveProviderModelInput({
167+
provider: trimmed,
168+
modelId: model.id,
169+
modelName: model.name,
170+
input: model.input,
171+
}),
164172
provider: trimmed,
165-
modelId: model.id,
166-
modelName: model.name,
167-
input: model.input,
168-
}),
169-
provider: trimmed,
170-
baseUrl: requestConfig.baseUrl ?? transport.baseUrl,
171-
api: requestConfig.api ?? model.api,
172-
headers: requestConfig.headers,
173-
},
174-
providerRequest,
173+
baseUrl: requestConfig.baseUrl ?? transport.baseUrl,
174+
api: requestConfig.api ?? model.api,
175+
headers: requestConfig.headers,
176+
},
177+
providerRequest,
178+
),
179+
entry?.localService,
175180
);
176181
});
177182
});

0 commit comments

Comments
 (0)