---
title: "LM Studio — Anthropic Compatibility Endpoints"
aliases: [lmstudio-anthropic, lm-studio-local-api, anthropic-compat-lmstudio]
tags: [claude-code, lm-studio, local-llm, anthropic-sdk, api]
sources: [raw/Anthropic Compatibility Endpoints.md]
created: 2026-04-30
updated: 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

```bash
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

```bash
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

```python
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`

## Related

- [[wiki/claude-code/headless-cli|Headless CLI]] — running Claude Code non-interactively with `-p`
- [[wiki/claude-code/overview|Claude Code Overview]] — full product capabilities
- [[wiki/llm-models/claude-model-catalog|Claude Model Catalog]] — official Anthropic model IDs for comparison
- [[wiki/concepts/local-llm-serving|Local LLM Serving]] — broader context on self-hosted model inference

## Sources

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