Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active August 13, 2026 06:26
Show Gist options
  • Select an option

  • Save brennanMKE/5d409806e124d2fab4ad37c45feb4bb0 to your computer and use it in GitHub Desktop.

Select an option

Save brennanMKE/5d409806e124d2fab4ad37c45feb4bb0 to your computer and use it in GitHub Desktop.
Claude using LM Studio for Local AI

Claude Code + LM Studio Launcher

A small Zsh script for running Claude Code with a local model hosted by LM Studio.

The script automatically:

  1. Finds the model currently loaded in LM Studio.
  2. Configures the environment variables needed for Claude Code to use LM Studio's Anthropic-compatible API.
  3. Launches Claude Code using the loaded model.
  4. Passes any additional command-line arguments through to Claude Code.

This makes switching between local models easy: load the model you want in LM Studio, then run the script.

Requirements

You'll need:

  • macOS
  • Zsh
  • LM Studio with the lms CLI installed
  • Claude Code
  • Python 3
  • A model loaded in LM Studio that works well with tool/function calling

Verify the commands are available:

lms --version
claude --version
python3 --version

LM Studio Setup

Start the LM Studio API server:

lms server start --port 1234

Alternatively, start the server from the Developer section of LM Studio.

Load the model you want Claude Code to use.

You can see currently loaded models with:

lms ps

or inspect the JSON representation with:

lms ps --json

Installation

Save the script as:

~/bin/claude-local

Make it executable:

chmod +x ~/bin/claude-local

Make sure ~/bin is in your PATH. For example, add this to ~/.zshrc:

export PATH="$HOME/bin:$PATH"

Then reload your shell:

source ~/.zshrc

Usage

Load a model in LM Studio and run:

claude-local

The script determines the loaded model and launches Claude Code with it.

For example:

$ claude-local

LM Studio model: qwen/qwen3-coder-30b
Launching Claude Code...

Passing Claude Code Arguments

Arguments are passed directly to claude.

For example:

claude-local --dangerously-skip-permissions

or:

claude-local -p "Explain this project"

Conceptually, the script runs:

Claude Code
    │
    │ Anthropic Messages API
    ▼
localhost:1234
    │
    ▼
LM Studio
    │
    ▼
Loaded local model

Claude Code continues to provide the agent environment and tools, while inference is performed by the model running locally in LM Studio.

Environment Variables

The launcher configures:

ANTHROPIC_BASE_URL=http://localhost:1234
ANTHROPIC_AUTH_TOKEN=lmstudio
CLAUDE_CODE_ATTRIBUTION_HEADER=0
ANTHROPIC_MODEL=<loaded LM Studio model>

These variables exist only for the launcher process and the Claude Code process it starts. They do not need to be added permanently to your shell configuration.

Authentication

The default configuration assumes LM Studio's Require Authentication option is disabled.

If you've enabled authentication in LM Studio, replace:

export ANTHROPIC_AUTH_TOKEN="lmstudio"

with your LM Studio API token.

Switching Models

There's no need to modify the script when switching models.

Simply load another model in LM Studio:

LM Studio → Load Model

and run:

claude-local

again.

The launcher discovers the currently loaded model each time it starts.

Why Use This?

Claude Code is useful as an agentic coding environment independently of the Claude models themselves. It provides capabilities such as:

  • Project exploration
  • File reading and editing
  • Shell commands
  • Search and grep
  • Agentic coding workflows
  • CLAUDE.md
  • Skills
  • MCP servers
  • Subagents
  • Permission management

LM Studio can expose local models through an Anthropic-compatible /v1/messages API, allowing Claude Code to use a locally hosted model instead of sending inference requests to Anthropic.

This is particularly useful for experimenting with coding models such as Qwen, Nemotron, and other models available through LM Studio.

Model Considerations

Not every local model will work equally well with Claude Code.

Claude Code is an agentic workload. A model needs to do more than generate good source code. In particular, good results depend heavily on:

  • Tool/function calling
  • Instruction following
  • Structured output
  • Long-context performance
  • Reasoning across multiple tool calls
  • Understanding command output
  • Recovering from failed commands
  • Iteratively modifying a codebase

A model that performs well in an ordinary LM Studio chat session may therefore perform poorly when used through Claude Code.

A reasonably large context window is also recommended. Agent sessions can consume context quickly, so 32K or greater is a useful starting point, with 64K+ preferable for larger coding sessions.

Troubleshooting

lms command not found

Install or enable the LM Studio CLI and verify:

lms --version

claude command not found

Install Claude Code and verify:

claude --version

No loaded model

Check:

lms ps

If nothing is listed, load a model in LM Studio before running claude-local.

Cannot connect to LM Studio

Check the server:

lms server status

Start it if necessary:

lms server start --port 1234

You can also test the OpenAI-compatible models endpoint directly:

curl http://localhost:1234/v1/models

Claude Code starts but behaves strangely

The connection may be working correctly while the model itself struggles with Claude Code's tool-calling protocol.

Try a model specifically designed or tuned for coding and agentic tool use.

Local Doesn't Necessarily Mean Offline

The model inference performed through this configuration is sent to your local LM Studio server rather than Anthropic's model API.

However, Claude Code itself may have features that communicate with external services. Don't assume that using a local inference endpoint makes every aspect of Claude Code completely offline.

References

License

This script is small enough to use however you find useful. If you're publishing it as a Gist, consider adding an MIT license if you want to explicitly permit reuse.

#!/bin/zsh
set -e
# Make sure LM Studio CLI is available.
if ! command -v lms >/dev/null 2>&1; then
echo "Error: 'lms' command not found."
exit 1
fi
# Make sure Claude Code is available.
if ! command -v claude >/dev/null 2>&1; then
echo "Error: 'claude' command not found."
exit 1
fi
# Get the currently loaded model.
MODEL=$(lms ps --json | python3 -c '
import json
import sys
data = json.load(sys.stdin)
# lms ps --json returns the loaded model(s).
models = data if isinstance(data, list) else data.get("models", [])
if not models:
sys.exit(1)
model = models[0]
# Prefer the identifier used by the API.
print(
model.get("identifier")
or model.get("modelKey")
or model.get("path")
or ""
)
')
if [[ -z "$MODEL" ]]; then
echo "Error: No model is currently loaded in LM Studio."
echo
echo "Load a model first, then try again."
exit 1
fi
echo "LM Studio model: $MODEL"
# Point Claude Code at LM Studio's Anthropic-compatible API.
export ANTHROPIC_BASE_URL="http://localhost:1234"
export ANTHROPIC_AUTH_TOKEN="lmstudio"
export CLAUDE_CODE_ATTRIBUTION_HEADER="0"
export ANTHROPIC_MODEL="$MODEL"
echo "Launching Claude Code..."
echo
exec claude --model "$MODEL" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment