obsidian/wiki/claude-code/lmstudio-anthropic-compat.md
2026-04-30 14:27:25 +01:00

3.3 KiB

title aliases tags sources created updated
LM Studio — Anthropic Compatibility Endpoints
lmstudio-anthropic
lm-studio-local-api
anthropic-compat-lmstudio
claude-code
lm-studio
local-llm
anthropic-sdk
api
raw/Anthropic Compatibility Endpoints.md
2026-04-30 2026-04-30

LM Studio — Anthropic Compatibility Endpoints

LM Studio exposes a /v1/messages endpoint that mirrors the Anthropic Messages API. Any code or tool that talks to Anthropic — including Claude Code and the Anthropic Python/JS SDK — can be redirected to a local LM Studio server with two env vars.

Supported Endpoints

Endpoint Method
/v1/messages POST

Quick Start — Claude Code with LM Studio

export ANTHROPIC_BASE_URL=http://localhost:1234
export ANTHROPIC_AUTH_TOKEN=lmstudio
claude --model openai/gpt-oss-20b
  • ANTHROPIC_BASE_URL — points Claude Code at the local server instead of api.anthropic.com
  • ANTHROPIC_AUTH_TOKEN — any non-empty string; LM Studio ignores the value unless auth is enabled
  • --model — pass the LM Studio model ID (e.g. ibm/granite-4-micro, openai/gpt-oss-20b)

Authentication

LM Studio accepts two auth header formats when Require Authentication is enabled:

Header Value
x-api-key $LM_API_TOKEN
Authorization Bearer $LM_API_TOKEN

When auth is disabled, both headers are optional.

cURL Example

curl http://localhost:1234/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: $LM_API_TOKEN" \
  -d '{
    "model": "ibm/granite-4-micro",
    "max_tokens": 256,
    "messages": [
      {"role": "user", "content": "Write a haiku about local LLMs."}
    ]
  }'

Python SDK Example

from anthropic import Anthropic

client = Anthropic(
    base_url="http://localhost:1234",
    api_key="lmstudio",          # any string when auth is disabled
)

message = client.messages.create(
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello from LM Studio"}],
    model="ibm/granite-4-micro",
)
print(message.content)
  • api_key can be omitted entirely when Require Authentication is off
  • Drop-in replacement: only base_url changes vs. the real Anthropic API

Key Takeaways

  • LM Studio's /v1/messages is API-compatible with api.anthropic.com/v1/messages
  • Two env vars (ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN) are all that's needed to redirect Claude Code to a local model
  • Model IDs come from LM Studio, not Anthropic — use whatever is loaded in the LM Studio server
  • Auth is optional by default; enable it in LM Studio settings if the server is exposed beyond localhost
  • Any Anthropic SDK client (Python, JS, cURL) works without code changes beyond base_url

Sources

  • LM Studio Docs: Anthropic Compatibility — raw/Anthropic Compatibility Endpoints.md