Connect Claude Code to LM Studio's Anthropic-compatible endpoint and run a local model on Apple Silicon: setup, model choice, context length, and the errors you will actually meet.
Claude Code connects to LM Studio with two environment variables, ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN, because LM Studio has served Anthropic's Messages API on localhost:1234 since version 0.4.1 (January 2026): no proxy, no provider file, no translation layer. This guide covers the setup on a Mac with Apple Silicon (with notes for Windows and Linux), how to choose a model and context length that actually work with an agent, and the failure modes I ran into. It was first written in April 2026 and revised in September and October 2026, and every command below was re-run in October 2026 with LM Studio 0.4.16 and Claude Code 2.1.302.
I use this for the same reason many scientists will: unpublished sequence data and manuscripts should not leave the machine, and a local model costs nothing per token. The trade-off is capability, because a 20–30B local model is a competent assistant for scripts, refactoring and documentation, but not a substitute for the frontier models on large multi-file tasks.
Prerequisites
- A Mac with Apple Silicon and enough unified memory for the model you want (see "Choosing a model" below; 32 GB is comfortable, 16 GB works with small models and a shorter context).
- LM Studio 0.4.1 or newer, opened at least once.
- Claude Code:
curl -fsSL https://claude.ai/install.sh | bashorbrew install --cask claude-code, thenclaude --version.
Step 1: Load a model in LM Studio
LM Studio ships a command-line tool called lms. If your shell does not find it, run the bootstrap step from the LM Studio app (Developer tab) and reopen the terminal.
lms get qwen/qwen3-coder-30b # download from LM Studio's catalog (see note below)
lms ls # models on disk
lms load --estimate-only qwen/qwen3-coder-30b --context-length 32768
lms load qwen/qwen3-coder-30b --context-length 32768 --identifier qwen-local
lms ps # what is loaded, with its context length
Two settings matter more than anything else here:
Context length is the first of them: Claude Code sends a system prompt and tool definitions of roughly 20K tokens before your first word. LM Studio's own guidance is "more than ~25k", while the model's default in LM Studio is often 4K, and with that the session fails on the first request. Use 32K as the floor and 64K if memory allows, because the KV cache grows with it.
Tool calling is the second: the agent edits files and runs commands through tool calls. A model without reliable tool use will chat about your code and never touch it. Coding models with tool support that run well on Apple Silicon:
qwen/qwen3-coder-30b(30B mixture-of-experts, 3B active; about 19 GB at 4-bit) — the best balance I have found for agentic work; wants 32 GB.openai/gpt-oss-20b(about 12 GB loaded with a 32K context; LM Studio's and Ollama's default suggestion)—the choice for 24 GB machines.qwen/qwen3.8-27b(dense 27B, about 18 GB)—the current general model in LM Studio's catalog, strong at tool use; needs 32 GB or more once the context is loaded.google/gemma-4-12b(about 7 GB on disk, 12 GB loaded with 32K)—the practical option for a 16 GB Mac; it completed file-editing tasks reliably in my tests.
Where the model comes from matters, so download through LM Studio's catalog (lms get <model> or the app's Discover tab), which carries chat templates LM Studio has tested. A GGUF taken from an arbitrary Hugging Face repository can have a template that LM Studio's Jinja engine cannot render once an agent sends its tool definitions, and the plain curl test below then works while Claude Code fails with a 500 (see troubleshooting).
Memory on a 16 GB Mac is the limiting factor: the KV cache for a 32K context adds several gigabytes, so a 7 GB model needs about 12 GB loaded, and LM Studio's resource guardrail refuses loads it expects to freeze the machine. Use --context-length 16384 or a smaller model there, and close memory-hungry applications.
Which model for which Mac
The same figures as a table, measured with the models loaded in LM Studio on Apple Silicon (the loaded size includes the KV cache for the given context).
| Unified memory | Model | Context | Loaded size |
|---|---|---|---|
| 16 GB | google/gemma-4-12b | 16K to 32K | about 12 GB at 32K |
| 24 GB | openai/gpt-oss-20b | 32K | about 12 GB |
| 32 GB | qwen/qwen3-coder-30b | 32K to 64K | about 19 GB at 32K |
| 32 GB | qwen/qwen3.8-27b | 32K | about 18 GB plus cache |
| 48 GB and more | any of the above | 64K | headroom for other apps |
Memory is the hard limit, the rest is preference. On a 24 GB machine the 30B coder loads only partly and becomes slow, which is why the 20B model is listed there even though the 30B model is the better coder.
Step 2: Start the server and check it with curl
lms server start --port 1234
lms server status
The Anthropic-compatible endpoint of LM Studio is http://localhost:1234/v1/messages. Check it before involving Claude Code—first the model list, then one message:
curl http://localhost:1234/v1/models # "qwen-local" must be in the list
curl http://localhost:1234/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: lmstudio" \
-d '{"model":"qwen-local","max_tokens":64,
"messages":[{"role":"user","content":"Say hello in one line."}]}'
A JSON reply with "type":"message" means the server side is done. Note that /v1/models lists every downloaded model when LM Studio's just-in-time loading is on (the default), whether it is loaded or not, so a model name in that list does not mean the model is in memory. lms ps shows what is loaded and with which context, and curl http://localhost:1234/api/v0/models reports a state of loaded or not-loaded per model. The custom identifier (qwen-local) exists only while the model is loaded.
Step 3: Point Claude Code at LM Studio
export ANTHROPIC_BASE_URL=http://localhost:1234
export ANTHROPIC_AUTH_TOKEN=lmstudio
export ANTHROPIC_DEFAULT_HAIKU_MODEL=qwen-local
export ANTHROPIC_DEFAULT_SONNET_MODEL=qwen-local
export ANTHROPIC_DEFAULT_OPUS_MODEL=qwen-local
export CLAUDE_CODE_MAX_CONTEXT_TOKENS=32768
claude --model qwen-local
The lines in that snippet do the following:
ANTHROPIC_BASE_URLsends every request to LM Studio.ANTHROPIC_AUTH_TOKENis the credential. LM Studio ignores its value unless Require Authentication is on, in which case use your LM Studio API token. Setting it also tells Claude Code not to use your claude.ai login for the session.- The three
ANTHROPIC_DEFAULT_*_MODELlines map Claude Code's model aliases to the local model. This is the step most guides leave out: Claude Code runs some background work on itshaikualias, and if that alias still resolves to an Anthropic model name, LM Studio answers 404 and the session breaks in odd places. With the mapping in place,/model sonnetand friends all mean "the local model". CLAUDE_CODE_MAX_CONTEXT_TOKENStells Claude Code how large the loaded context is. For a model name it does not recognise it assumes 200K tokens and prints[claude-code:unrecognized_model]at startup; with the real value it compacts the conversation before LM Studio rejects it. Match the--context-lengthyou loaded.--model qwen-localselects the model for this session;ANTHROPIC_MODELdoes the same from the environment.
Older guides mention ANTHROPIC_SMALL_FAST_MODEL for the background model, which is deprecated in favour of ANTHROPIC_DEFAULT_HAIKU_MODEL.
For a fully offline session add DISABLE_TELEMETRY=1, DISABLE_ERROR_REPORTING=1 and CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1. Claude Code reads only whether these are set, so any value works. LM Studio's integration page also suggests the variable CLAUDE_CODE_ATTRIBUTION_HEADER=0.
Inside Claude Code, /status shows which base URL and credential source are in use—the first thing to check when something is off.
Per-project instead of per-shell
If a project should always use the local model, put the variables in .claude/settings.json in that project (or ~/.claude/settings.json for all projects):
{
"env": {
"ANTHROPIC_BASE_URL": "http://localhost:1234",
"ANTHROPIC_AUTH_TOKEN": "lmstudio",
"ANTHROPIC_MODEL": "qwen-local",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "qwen-local",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "qwen-local",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "qwen-local",
"CLAUDE_CODE_MAX_CONTEXT_TOKENS": "32768"
}
}
A settings-file value wins over a shell export of the same variable, which is worth remembering when a session ignores what you just exported.
On Windows and Linux
The server side and the variables are the same, and only the way the variables are set differs. I have run this on macOS only, so the notes below follow LM Studio's and Claude Code's documentation.
LM Studio has Windows and Linux builds with the same
lmscommands and the same port. On Linux the model runs through CUDA or Vulkan, and the memory figures above apply to the graphics card's memory rather than to unified memory.Claude Code installs natively on Windows 10 (1809) or newer from PowerShell with
irm https://claude.ai/install.ps1 | iexorwinget install Anthropic.ClaudeCode. Git for Windows is recommended so that the agent has a Bash tool. In PowerShell the exports below replace the shell exports used above and look like this:$env:ANTHROPIC_BASE_URL = "http://localhost:1234" $env:ANTHROPIC_AUTH_TOKEN = "lmstudio" $env:ANTHROPIC_DEFAULT_HAIKU_MODEL = "qwen-local" $env:ANTHROPIC_DEFAULT_SONNET_MODEL = "qwen-local" $env:ANTHROPIC_DEFAULT_OPUS_MODEL = "qwen-local" $env:CLAUDE_CODE_MAX_CONTEXT_TOKENS = "32768" claude --model qwen-local
They last for that window only, while the
settings.jsonabove is identical on every platform (%USERPROFILE%\.claude\settings.jsonfor all projects) and is the better choice on Windows, because it sidesteps the shell question.Claude Code inside WSL with LM Studio on the Windows side:
localhostin WSL 2 is not the Windows host unless WSL's mirrored networking mode is on (networkingMode=mirroredin.wslconfig). Otherwise enable Serve on Local Network in LM Studio's server settings and put the Windows host's address inANTHROPIC_BASE_URL. Installing Claude Code natively on Windows avoids this.
Step 4: Work
cd /path/to/project
claude --model qwen-local
Ask it to inspect the repository, propose edits, run the tests, and review the diff before you commit. Keep tasks small and concrete, because local models do far better with "add a --dry-run flag to this script" than with "clean up the project".
Troubleshooting
Connection refused- Nothing listens on the port.
lms server status; start the server; confirm the port inANTHROPIC_BASE_URL. - Claude Code shows the login screen although
curlworks - The credential variable is not set in the shell you launched
claudefrom, or a settings file overrides it.echo $ANTHROPIC_AUTH_TOKEN; check/status. - A startup warning about two credential sources ("auth may not work as expected")
- Both
ANTHROPIC_API_KEYandANTHROPIC_AUTH_TOKENare set. Keep one; LM Studio accepts either header. 404or "model not found" in the middle of a session- A background call used the
haikualias. SetANTHROPIC_DEFAULT_HAIKU_MODEL(and the sonnet/opus ones) to your local model as above. 500 Error rendering prompt with jinja template- Followed by "Unknown StringValue filter: safe" or "Cannot apply filter trim to type: Undefined". The model file's chat template does not survive the tool definitions Claude Code sends; the simple
curlworks because it sends none. I hit this with a community GGUF of Qwen3-Coder. Download the model again from LM Studio's catalog (lms get qwen/qwen3-coder-30b), which uses a corrected template, or override the template under My Models → Prompt Template. Nothing on the Claude Code side is wrong. [claude-code:unrecognized_model]at startup, or "assumes a context window of 200k"- Informational: Claude Code does not know the local model's window. Set
CLAUDE_CODE_MAX_CONTEXT_TOKENSto the loaded context length. - "claude.ai connectors are disabled because ANTHROPIC_API_KEY or another auth source is set"
- Expected when
ANTHROPIC_AUTH_TOKENis set: the session uses the local credential and skips the claude.ai features that need your login. Harmless. 400naming a token or context limit- The loaded context is too small. Reload with
--context-length 32768or more;lms psshows the value in force. curl http://localhost:1234/v1/modelslists the model, Claude Code still fails- The model is downloaded but not loaded (
lms pssays so). With just-in-time loading on, the first request loads it with the model's default settings, usually a 4K context, and the session fails with the400above; with just-in-time loading off, the request gets a 404. Load the model yourself withlms loadand--context-lengthbefore starting Claude Code, as in step 1. - The model explains what it would do but never edits a file
- No (working) tool calling. Switch to one of the models listed above.
- The first answer takes a minute, later ones are fast
- Prompt processing of the ~20K-token system prompt. Normal; larger context and a bigger model make it longer.
- Everything is slow
- The model does not fit comfortably. Use a smaller quantisation or model, lower the context length, close memory-hungry apps.
lms log stream --source serverandlms log stream --source model --filter output --statsshow what the server is doing.
Summary
lms load <model> --context-length 32768 --identifier qwen-locallms server start --port 1234export ANTHROPIC_BASE_URL=http://localhost:1234 ANTHROPIC_AUTH_TOKEN=lmstudio, map theANTHROPIC_DEFAULT_*_MODELaliases to the local model and setCLAUDE_CODE_MAX_CONTEXT_TOKENSto the loaded contextclaude --model qwen-local
Related guides: Claude Code with Ollama on macOS and Linux and OpenAI Codex CLI with a local model.
Sources: LM Studio, Claude Code integration · LM Studio, Anthropic compatibility endpoints · Claude Code, connect to an LLM gateway · Claude Code, model configuration.