In lmstudio-python, why can lms.llm("your-model", config={"contextLength": 16384}) return a model running at a smaller context, and how do you guarantee yours?
answer
- get, or load if missing
- already loaded wins
- config and ttl silently ignored
- a fresh instance on purpose
- read the load config back
basics
~20 slms.llm(key) gets an already-loaded instance of that model if one exists and only loads when none does, so its config and ttl are ignored for an existing instance. Use load_new_instance() with the config, or unload first, then check get_load_config().
solid answer
~40 s`lms.llm(key)` is the convenience form of `client.llm.model(key)`, a **get-or-load** call: if an instance of that model is already in memory, loaded from the GUI, by `lms load` or by another script, it returns that instance and the `config` you passed (context length, GPU ratio) is **ignored**; a `ttl` passed this way also applies only when the call itself loads. To get exactly your settings, call `client.llm.load_new_instance(key, config={...})`, which always creates a new instance (lmstudio-js calls it `client.llm.load()`), or unload the existing instance first. Then verify rather than trust: `model.get_load_config()` and `model.get_context_length()` report what the instance is really running with. Call `model.unload()` when your service is done with it.
go deeper
Remember that lms.llm(key) returns an already-loaded instance when there is one, and that load_new_instance() is the call that always loads with your settings.
Explain get-or-load versus load-new, why config and ttl are ignored for an existing instance, and how get_load_config() lets you check what you actually got.
Show how a service on a shared machine guarantees its context length: explicit loading, a start-up check that fails loudly, and unloading or a TTL so restarts do not stack instances.
Decide who owns model instances on a shared box, a provisioning step or each service, and how that choice affects memory, start-up latency and settings conflicts between teams.
## Two ways to obtain a model handle The LM Studio SDKs talk to LM Studio's own API, not to its OpenAI-compatible routes, and they give you a **model handle**: an object you call `.respond()`, `.complete()` or `.act()` on. In lmstudio-python there are two ways to get one, and they behave differently on purpose. | Call | When an instance already exists | When none exists | |---|---|---| | `lms.llm(key)` / `client.llm.model(key)` | returns it; `config` and `ttl` are ignored | loads one with your `config` and `ttl` | | `client.llm.load_new_instance(key, config=...)` | loads **another** instance anyway | loads one with your `config` | | `lms.llm()` with no key | returns any currently loaded model | only useful once something is loaded | `lms.llm` is documented as equivalent to `client.llm.model` on the default client. The TypeScript SDK has the same pair under different names: `client.llm.model(key)` for get-or-load and `client.llm.load(key, { config, identifier, ttl })` for a guaranteed new instance. ## Why the config "did not take" The documentation states it plainly: if the model is already loaded, the configuration given to `.model()` is ignored, and a TTL given to `.model()` only applies when that call loads a new instance; it does not retroactively change an existing one. Common ways an instance is already there: - someone loaded it in the desktop app with the model's saved defaults; - a provisioning script ran `lms load` with a different `--context-length`; - the server loaded it on demand for another client; - an earlier run of your own service loaded it and never unloaded. In every case your call succeeds, returns a working handle, and runs with someone else's settings. The symptom is indirect: prompts that fit on your laptop fail or get cut on the shared box. ## Guaranteeing your settings 1. **Load explicitly.** `client.llm.load_new_instance("your-model", config={"contextLength": 16384, "gpu": {"ratio": 0.5}})` creates an instance with exactly that configuration. You can pass a second argument as the instance identifier; an identifier that already exists makes the server raise an error, so omit it unless you need a stable name. 2. **Or clear the field first.** Unload the existing instance (`model.unload()` on its handle, or `client.llm.unload("model-key")`) and then load. 3. **Verify.** `model.get_load_config()` returns the load-time configuration of the instance you hold, and `model.get_context_length()` its context length. A service that depends on a long context should check at start-up and fail loudly. 4. **Clean up.** A service that loads its own instance should `unload()` it on shutdown, or load it with a TTL, so it does not leave memory behind. ## Load config versus prediction config The `config` on `lms.llm()` and `load_new_instance()` is **load-time**: context length, GPU offload ratio and similar settings fixed for the life of the instance. Sampling settings such as `temperature` and `maxTokens` go in the `config` of `.respond()` or `.complete()` and apply per request. Mixing them up produces a second silent failure: a `temperature` passed at load time does nothing for your predictions. ## A start-up check in practice A service that needs a 16k context on a machine other people also use can make its assumption explicit: - load its own instance with `load_new_instance()` and the load config it needs, under a generated identifier; - read `get_context_length()` immediately and refuse to start if it is smaller than required; - log the instance identifier so an operator can match it to a row in `lms ps`; - unload the instance on shutdown, or load it with a `ttl` so a crash does not leave it behind. The cost is a load at every start-up and one more instance in memory; the benefit is that nobody else's settings can change what your service gets. ## Connecting to the right instance With no host given, lmstudio-python scans a fixed set of local ports for LM Studio's API and, since SDK 1.5.0, no longer needs the optional HTTP server to be enabled. To reach a specific instance, pass `"host:port"` to `lms.Client(...)` or call `lms.configure_default_client(...)` before any other convenience call. On a machine with authentication enabled (LM Studio 0.4.0 or newer), the SDK reads the token from `LM_API_TOKEN` or the `api_token` argument.
- When is the get-or-load behaviour of lms.llm(key) exactly what you want?In a script or notebook that just needs some instance of the model and does not care who loaded it: it reuses a warm instance instead of loading a second copy and doubling memory. It is the wrong choice only when your code depends on specific load settings.
- Your service calls load_new_instance() at start-up and crashes on shutdown without unloading. What is left behind?The instance stays in memory: like an `lms load` instance, an explicitly loaded instance without a `ttl` has no idle timer. Load it with a `ttl`, unload it in shutdown handling, or let a deploy step clear stale instances, otherwise each crash-restart adds a copy.
- lms.llm(key, config=...) reloads the model with the new config
- A ttl passed to lms.llm() resets the TTL of the loaded instance
- temperature belongs in the load config
- load_new_instance() returns the existing instance if there is one
- The Python SDK talks to the OpenAI-compatible /v1 routes