Claude Code + LM Studio in 2026: Three Env Vars to Run Anthropic's Terminal Agent on a Local Model

claude-codelm-studiolocal-llmsetup-guideanthropicprivacygpt-ossqwen

TL;DR: Since LM Studio 0.4.1 (January 30, 2026) shipped an Anthropic-compatible /v1/messages endpoint, Claude Code talks to a local model with three environment variables and zero proxy software. Streaming and tool calling work out of the box, on GGUF and Apple-native MLX models alike. The catch: LM Studio’s conservative default context will strangle the agent until you raise it, and a 20B–30B local model is a capable assistant, not a Fable-class engineer.

What you’ll be able to do after this guide:

  • Point the claude CLI at LM Studio’s server and run chat, file edits, and tool calls against a model on your own GPU or Mac — no Anthropic API key, no per-token bill
  • Pick a model tier that actually survives agentic work, from a 16GB-footprint gpt-oss-20b to Qwen3 Coder 30B on a 24GB card
  • Dodge the two setup killers: the 4,096-token default context and the VS Code extension ignoring your shell’s env vars

Honest take: If you already run LM Studio — for MLX speed on a Mac, the GUI model manager, or LM Studio Bionic — this is now the cleanest way to get Claude Code on local hardware, and it’s what I’d set up on an Apple Silicon machine. If you have no runtime installed yet and just want the fastest path, Ollama’s one-command ollama launch claude still wins on setup friction. Either way, keep a paid Sonnet-class model available for the hardest 20% of tasks.

Why no proxy is needed anymore

Claude Code speaks Anthropic’s Messages API — POST /v1/messages — and for most of 2025 that locked it to Anthropic’s servers unless you ran LiteLLM or a homegrown translation shim in the middle. LM Studio removed that requirement in version 0.4.1, released January 30, 2026: the local server now exposes a native Anthropic-compatible endpoint alongside its OpenAI-compatible one.

The official endpoint docs list what the compatibility layer handles: standard message requests, SSE streaming with the full Anthropic event sequence (message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop), and tool use with JSON input_schema definitions — the docs’ own example even passes tool_choice. That covers what Claude Code needs to operate as an agent rather than a chatbot: streamed responses it can render live, and tool calls it can parse into file edits and shell commands.

The practical difference from the Ollama route we covered in July is where the models come from and how they run. LM Studio manages downloads through a GUI catalog, runs MLX builds natively on Apple Silicon (usually the faster option on a Mac), lets you set per-model load defaults, and can gate the whole server behind API tokens — which matters once you expose it beyond localhost.

The setup, start to finish

Current versions as of this writing: LM Studio 0.4.20 (July 22, 2026) and Claude Code 2.1.235. Anything from LM Studio 0.4.1 up has the endpoint.

1. Load a model and start the server. Download a model in LM Studio’s Discover tab (specific picks below), then start the server from the Developer tab or the CLI:

lms server start --port 1234

2. Point Claude Code at it. These are the exact variables from LM Studio’s official Claude Code integration guide:

export ANTHROPIC_BASE_URL=http://localhost:1234
export ANTHROPIC_AUTH_TOKEN=lmstudio
export CLAUDE_CODE_ATTRIBUTION_HEADER=0

The auth token is a placeholder unless you’ve enabled Require Authentication in LM Studio — in that case create an API token and export that instead. The server accepts it as either x-api-key or a standard Authorization: Bearer header. The attribution variable disables an Anthropic-specific request header the local server has no use for.

3. Launch against your model, using LM Studio’s model identifier:

claude --model openai/gpt-oss-20b

4. Verify the wiring before blaming the agent for anything. A raw request to the endpoint should come back as an Anthropic-shaped response:

curl http://localhost:1234/v1/messages \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-oss-20b",
    "max_tokens": 256,
    "messages": [{"role": "user", "content": "Say hello from LM Studio."}]
  }'

Expected: a JSON body with a content array of text blocks and a usage object, not an HTML error page. If the connection itself refuses, work through the LM Studio connection error checklist — same server, same failure modes.

One caveat that catches VS Code users: the Claude Code extension doesn’t inherit shell exports from a terminal you opened later. Community setup notes recommend putting the variables in .vscode/settings.json (or your shell profile, then restarting VS Code) so the extension actually sees them.

The 4K context trap

This is the step that separates “it replied once” from “it works.” LM Studio loads models with a deliberately conservative default context — 4,096 tokens on typical model loads, chosen for hardware safety rather than for agents. Claude Code is one of the most context-hungry clients you can point at a local server: its system prompt, tool definitions, and file contents stack up before you’ve typed a word. LM Studio’s own integration guide tells you to use “more than ~25k context length”; the community guides that have run this longest are blunter — 32K is the floor, 64K is the sweet spot, and anything below 32K is a chat demo.

Fix it at model load time: in My Models, open the model’s load settings (gear icon), raise Context Length to at least 32,768, and save it as that model’s default so the JIT loader respects it. Budget memory for it too — KV cache grows with context, which is why a model whose weights fit comfortably can still spill at 64K. On the Cline + LM Studio setup we saw the identical failure wearing a different error message; it’s the single most common local-agent misconfiguration across every tool we’ve tested this year.

Which models survive agent work

An agent multiplies model weaknesses: one dropped tool-call bracket ends a session, and Claude Code chains dozens of calls per task. These three earn their VRAM in August 2026:

gpt-oss-20bQwen3 Coder 30B A3BQwen3.8-27B
Why this oneLM Studio’s own doc example; OpenAI says it runs within 16GB of memory (MXFP4, ~3.6B active params)The consensus local-agent pick; strong tool-calling reputation, ~18.6GB at Q4_K_MStrongest raw coding scores of the three — our review has it crowding Sonnet 5 on SWE-bench Pro
Hardware floor16GB VRAM or unified memory24GB card, or 32GB+ Mac24GB card; dense 27B, so slower than the MoE peers
Max context131K262,144262,144
LicenseApache 2.0Apache 2.0Apache 2.0

On a 16GB card, take gpt-oss-20b and 32K context and call it done. At 24GB, Qwen3 Coder 30B A3B is the default answer — MoE with ~3.3B active parameters, so it generates fast while leaving headroom for real context. Qwen3.8-27B trades speed for output quality; pick it when you care more about the diff than the tokens-per-second. For the GPU decision itself, a used RTX 3090 at 24GB is still the price-performance move — our sister site’s local-AI GPU buying guide and VRAM-tier model rundown break down the tiers. No GPU at all? RunPod rents you one and the wiring is identical, with the privacy dial at “trusted cloud” instead of “none.”

One refinement worth stealing from the community env-var guide: Claude Code internally routes work to Opus-, Sonnet-, and Haiku-class slots, and you can map each slot to a local model:

export ANTHROPIC_DEFAULT_OPUS_MODEL=qwen/qwen3-coder-30b
export ANTHROPIC_DEFAULT_SONNET_MODEL=qwen/qwen3-coder-30b
export ANTHROPIC_DEFAULT_HAIKU_MODEL=openai/gpt-oss-20b

Pointing the Haiku slot at a smaller model keeps background tasks snappy instead of queueing everything behind your main model.

Where it still won’t feel like Claude

Be honest about what you’re getting. A 20B–30B open model executes single-file edits, writes tests, and handles focused refactors competently. It does not plan like Fable 5 or Sonnet 5 on gnarly multi-file work — expect more wrong turns, more re-prompting, and the occasional malformed tool call on heavily quantized builds. Long sessions also get slower as context fills, since your GPU reprocesses what Anthropic’s infrastructure would optimize away.

None of that is a reason to skip this setup. It costs nothing per token, works on a plane, and keeps proprietary code on your machine — LM Studio’s server binds to localhost until you decide otherwise, and if you do expose it, Require Authentication plus API tokens is the sane default. It’s the same trade we described for the full local-first stack: you’re trading the top of the capability curve for control. If you’re new to LM Studio itself, the FOSS-side LM Studio review at aifoss.dev covers the app beyond this one integration.

Worth knowing before you commit an afternoon: if your rig and your laptop are different machines, LM Studio’s LM Link runs the model on the rig while Claude Code runs on the laptop — the integration guide explicitly supports it, and it beats SSH-tunneling the port by hand.

FAQ

Do I need an Anthropic account or API key? No. ANTHROPIC_BASE_URL plus ANTHROPIC_AUTH_TOKEN replaces the hosted backend entirely. The token value only has to be real if you enabled Require Authentication in LM Studio.

Does tool calling actually work, or does the model just print JSON? The Anthropic-compat layer translates tool definitions and tool-use blocks natively — LM Studio’s docs demonstrate tools with input_schema and tool_choice on the endpoint. Whether calls come out well-formed is a model property: the three models above are solid; sub-12GB quants get flaky.

GGUF or MLX? On Apple Silicon, prefer the MLX build of your model when the catalog offers one — it’s typically faster on the same hardware. On NVIDIA/AMD boxes, GGUF is the path.

Is this cheaper than a Claude subscription? Per token, it’s free. But that’s the wrong comparison — the real question is whether local output quality clears your bar. For grunt work it does; for the hardest agentic tasks, what a real Claude Code session costs is often money well spent.

Claude Code connects but replies get cut off or the agent loops. Why? Context, almost every time. Reload the model at 32K+ (My Models → gear → Context Length) and confirm the loaded value in the Developer tab, not just the saved setting.

Sources

Last verified August 19, 2026. LM Studio and Claude Code both ship updates frequently; re-check the official docs above if a setting has moved.

Was this article helpful?