DEV Community

LLMCoder
LLMCoder

Posted on Edited on Originally published at llmcode.io

Running a local model from LM Studio inside Claude Code: what actually breaks

Claude Code is a terminal agent that speaks the Anthropic Messages API. LM Studio is a desktop app that serves local models over an OpenAI-compatible endpoint. Both look like "an API on localhost", so the obvious move is to point one at the other — and it does not work. Not because of a setting you missed: the two speak different protocols.

Here is what actually breaks, and the two ways to bridge it.

The mismatch, in one paragraph

LM Studio exposes POST /v1/chat/completions with OpenAI's request shape and OpenAI's response shape. Claude Code sends POST /v1/messages with Anthropic's shape and expects Anthropic's event stream back. There is no flag on either side that makes them agree, because the disagreement is in the payload, not in the URL.

The second thing that breaks is the model name. Claude Code resolves the model string through the endpoint, so the value has to be something the endpoint can route. A GGUF file name from your models folder routes nowhere.

What breaks first

If you set the base URL to LM Studio directly:

export ANTHROPIC_BASE_URL="http://localhost:1234/v1"
export ANTHROPIC_AUTH_TOKEN="lm-studio"
claude
Enter fullscreen mode Exit fullscreen mode

the CLI posts a Messages-shaped body to /v1/messages, which the local server does not serve. You get a 404, or — worse, because it looks like progress — a 200 with a body the CLI cannot parse, and the session dies with a parse error instead of a network error.

So the fix is never "one more environment variable". You either translate the protocol or you use an endpoint that already speaks it.

Path A: a translator in front of LM Studio

Put a proxy between the CLI and the local server. LiteLLM is the usual choice: it accepts Anthropic-shaped requests, converts them to Chat Completions, and converts the answer back.

model_list:
  - model_name: local
    litellm_params:
      model: openai/local-model
      api_base: http://localhost:1234/v1
      api_key: lm-studio
Enter fullscreen mode Exit fullscreen mode

Run the proxy on a port of its own, then point the CLI at the proxy rather than at LM Studio:

export ANTHROPIC_BASE_URL="http://127.0.0.1:4001"
export ANTHROPIC_AUTH_TOKEN="sk-litellm"
claude /status
Enter fullscreen mode Exit fullscreen mode

claude /status is the cheapest debugging tool here: it prints the base URL and model names the CLI actually resolved. If it still says api.anthropic.com, your export never reached the process — a very common outcome when the CLI is started from a different shell, an IDE task, or a systemd unit.

What degrades on this path is the part that makes Claude Code useful: streaming, tool calls and token accounting all depend on how faithfully the proxy translates, and on whether the local model can hold an agent loop at all. For drafts, offline experiments and text generation it is fine. For multi-file refactoring it usually is not — the model loses the thread between steps and the agent starts repeating itself.

Path B: an endpoint that already speaks Messages

If the model does not have to live on your disk, skip the translator: use an Anthropic-compatible endpoint and keep the same CLI.

export ANTHROPIC_BASE_URL="https://llmcode.io"
export ANTHROPIC_AUTH_TOKEN="$LLMCODE_API_KEY"
export ANTHROPIC_DEFAULT_SONNET_MODEL="claude-sonnet-5-5"
claude /status
Enter fullscreen mode Exit fullscreen mode

Note the base URL has no /v1 — the CLI appends the path itself, and a trailing /v1 produces a doubled path and a 404. Note also that the token is the gateway key, not an Anthropic key: there is no Anthropic account in this setup.

The trade is honest: the weights are not on your machine, the network is required, and you pay per token. If your constraint is that code and prompts must never leave the laptop, this path is disqualified — use Path A and accept the degradation.

How to decide

  • Offline or private by requirement → LM Studio plus a translator. Budget time for the agent loop to misbehave.
  • Agent loop matters more than locality → Anthropic-compatible endpoint. One base URL, one token, no proxy to babysit.
  • Just testing a local model's quality → use LM Studio's own chat UI. You do not need Claude Code to evaluate a 7B model.

Either way, cap the damage before the first long session. A spend limit and an expiry date on the key turn a runaway refactor into a stopped request instead of a stopped weekend, and the request log tells you what a typical task actually costs — usually the number nobody predicts correctly.


I work on LLMCode, the gateway used in Path B, so treat that section accordingly. The protocol explanation and Path A apply to any Anthropic-compatible gateway, and the LiteLLM config works the same in front of vLLM, llama.cpp or Ollama.

Setup notes I keep coming back to: Claude Code with a custom base URL.


Русская версия с теми же шагами и конфигом: как запустить в Claude Code модель из LM Studio.

Top comments (0)