New Tool Radar

Local AI

How to Call the LM Studio Local Server from a Python Script with the SDK

Call a local model from your own Python script with the LM Studio SDK, using lms.llm() and model.respond() instead of typing in the chat window.

Schematic diagram with three boxes: a Python script ask-lighthouse.py, the LM Studio SDK calls lms.llm and model.respond, and a one-sentence answer about a lighthouse keeper.

LM Studio’s chat window is fine for trying a model, but the moment you want your own program to ask the model something — a script that drafts a reply, checks a file, or answers the same question every morning — you need code, not clicks. The LM Studio Python SDK is the official way to do that: a small library that talks to LM Studio running on your own computer, with no cloud account and no API key for local use.

Install the SDK and ask one question

Install the SDK from PyPI:

pip install lmstudio

Then, with LM Studio running and a model loaded, the docs’ own quick example asks one question in three lines:

import lmstudio as lms
model = lms.llm()
print(model.respond("What is the meaning of life?"))

lms.llm() with no argument returns a handle to the model that is already loaded in LM Studio. To name a model explicitly, pass its key, as in the docs’ example lms.llm("qwen2.5-7b-instruct"). For a multi-turn conversation, build a chat object with a system prompt first:

import lmstudio as lms
model = lms.llm()
chat = lms.Chat("You are a resident AI philosopher.")
chat.add_user_message("What is the meaning of life?")
result = model.respond(chat)
print(result)

You can also cap the answer’s length when you call respond(), with the docs’ own example values: model.respond(chat, config={"temperature": 0.6, "maxTokens": 50}). And you can print prediction metadata — the model used, the number of predicted tokens, the time to first token, and the stop reason — from result.stats, for example result.stats.predicted_tokens_count.

Try it: a script that asks a question and prints the answer

Save this as ask-lighthouse.py and run it with python ask-lighthouse.py, with LM Studio running and any small model loaded:

import lmstudio as lms

model = lms.llm()
chat = lms.Chat("You answer in one sentence.")
chat.add_user_message("Explain what a lighthouse keeper does.")
result = model.respond(chat, config={"maxTokens": 50})
print(result)
print("Predicted tokens:", result.stats.predicted_tokens_count)
Check What you should see If you do not see it
Reply printed One sentence about a lighthouse keeper LM Studio is not running, or no model is loaded — load one first
Length cap The answer stops within 50 tokens The config argument was left out of the respond() call
Token count A number after “Predicted tokens:” You printed result instead of result.stats.predicted_tokens_count

If the script prints one sentence and a token count, your code is talking to your local model. Expected result. We checked the LM Studio Python SDK docs. We did not run the LM Studio SDK or a script against it.

Limits

The SDK talks to LM Studio on the same computer, so LM Studio must be running and a model must be loaded before the script runs — lms.llm() with no argument has nothing to return otherwise. Streaming is available with model.respond_stream(), which prints fragments as they arrive instead of waiting for the whole answer, but it adds complexity a first script does not need. The SDK also offers tool use and agentic calls, which are separate topics; this guide covers only a plain chat reply. We did not run the SDK, so no reply here is a measured result.

Sources

See all