A local model can ask your application for data that the prompt doesn't contain. On Windows, the LM Studio Python SDK runs a tool-using agent that calls a Python function and uses its result to answer.
The function runs in your Python process. Its name, docstring, and type hints describe the tool to the model. The SDK passes the model's arguments to the function and sends the return value back for the next prediction round.
Before loading the model, your PowerShell session needs to resolve both the bundled lms CLI and python from Python 3.13. The CLI requires you to open LM Studio at least once. LM Studio needs a compatible GGUF runtime and Qwen2.5 1.5B Instruct FP16 in its model library. On Windows 11 on Arm, use the ARM64 releases of LM Studio and Python and select an ARM64 GGUF runtime.
Steps to run a tool-using agent with the LM Studio Python SDK:
- Load the tool-capable model under the identifier seat-agent.
lms load qwen2.5-1.5b-instruct --identifier seat-agent --context-length 2048 --gpu off
The 2048-token context fits the request, tool description, and follow-up. --gpu off runs inference on the CPU.
Related: How to load a model with lms in LM Studio - Start the LM Studio server on 127.0.0.1:1234.
lms server start --bind 127.0.0.1 --port 1234
The loopback address keeps the server accessible only from this computer.
- Create a virtual environment in your project folder.
python -m venv .venv - Install the LM Studio Python SDK in the virtual environment.
.\.venv\Scripts\python.exe -m pip install lmstudio==1.5.0
Calling the virtual environment's Python directly keeps the package local to the project and works without activating a PowerShell script.
Related: How to install lmstudio-python for LM Studio - Define the seat lookup tool in a new file named agent.py in your project folder.
- agent.py
import lmstudio as lms def seat_count(section: str) -> int: """Look up the available seats in the balcony or orchestra section.""" counts = {"balcony": 137, "orchestra": 246} count = counts[section] print(f"seat_count(section={section!r}) -> {count}") return count
The function accepts a section name and reads the count from a small table. Its log line appears when the SDK actually invokes the function. An exposed function runs with your Python process's permissions.
Related: LM Studio tool definitions - Append the agent call after the seat lookup function in agent.py.
prompt = ( "Use the seat_count tool to look up the balcony section. " "Reply with the returned number of seats." ) predictions = [] with lms.Client("127.0.0.1:1234") as client: model = client.llm.model("seat-agent") model.act( prompt, [seat_count], config={"temperature": 0, "maxTokens": 128}, max_prediction_rounds=3, on_round_start=lambda index: print(f"Round {index + 1}"), on_prediction_completed=predictions.append, ) print(f"Final answer: {predictions[-1].content.strip()}")
The client connects to the local server and selects seat-agent. The list containing seat_count gives .act() one callable. The SDK runs the tool loop for up to three prediction rounds, and the callback provides the text of each completed prediction. The script prints the last prediction as the final answer.
Related: LM Studio .act() documentation - Save agent.py in the project folder.
- Run agent.py with the virtual environment's Python.
.\.venv\Scripts\python.exe .\agent.py
- Confirm that the final answer uses the count returned by seat_count.
Round 1 seat_count(section='balcony') -> 137 Round 2 Final answer: The balcony section has 137 available seats.
The wording of the final answer can vary. The tool log must show the argument 'balcony' and the returned count 137 before the final answer reports that count.