obsidian/wiki/claude-code/lmstudio-messages-api.md
2026-04-30 14:42:43 +01:00

120 lines
3.6 KiB
Markdown

---
title: "LM Studio — Anthropic Messages API"
aliases: [lmstudio-messages, lm-studio-anthropic-messages]
tags: [lmstudio, anthropic, api, messages, local-llm, streaming, tools]
sources: [raw/Messages.md]
created: 2026-04-30
updated: 2026-04-30
---
# LM Studio — Anthropic Messages API
The `/v1/messages` endpoint in LM Studio mirrors the Anthropic Messages API exactly — same request shape, same response shape. Use it as a local drop-in for any code already calling Anthropic's cloud API.
## Endpoint
```
POST http://localhost:1234/v1/messages
```
Required headers:
- `Content-Type: application/json`
- `x-api-key: $LM_API_TOKEN` — optional if **Require Authentication** is disabled in LM Studio
## Basic Request
```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": "Say hello from LM Studio."}
]
}'
```
## Streaming
Add `"stream": true` to receive Server-Sent Events (SSE):
```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",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 256,
"stream": true
}'
```
SSE event sequence:
1. `message_start`
2. `content_block_start`
3. `content_block_delta` (repeating)
4. `content_block_stop`
5. `message_delta`
6. `message_stop`
## Tool Use
Pass a `tools` array with JSON Schema input definitions and a `tool_choice` policy:
```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": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
"tool_choice": {"type": "any"},
"messages": [
{"role": "user", "content": "What is the weather like in San Francisco?"}
]
}'
```
`tool_choice` options (Anthropic-compat): `"auto"`, `"any"`, `{"type": "tool", "name": "…"}`.
## Authentication
| Scenario | Header needed |
|----------|---------------|
| Auth disabled in LM Studio | No `x-api-key` required |
| Auth enabled | `x-api-key: $LM_API_TOKEN` |
## Key Takeaways
- `POST /v1/messages` on `localhost:1234` is a drop-in for `api.anthropic.com/v1/messages`
- Same request body — swap the base URL and optionally add `x-api-key`
- Streaming uses standard Anthropic SSE event names — existing stream parsers work unchanged
- Tool use with `input_schema` / `tool_choice` is supported
- Auth header is optional when LM Studio's **Require Authentication** is off
- See [[wiki/claude-code/lmstudio-anthropic-compat|lmstudio-anthropic-compat]] for redirecting the full Anthropic SDK via env vars
## Related
- [[wiki/claude-code/lmstudio-anthropic-compat|LM Studio Anthropic Compat Setup]] — redirect Claude Code / SDK to local server
- [[wiki/claude-code/lmstudio-chat-completions|LM Studio Chat Completions]] — OpenAI-compatible `/v1/chat/completions`
- [[wiki/claude-code/lmstudio-rest-api|LM Studio REST API]] — native v1 endpoints and feature comparison table
- [[wiki/claude-code/lmstudio-idle-ttl-auto-evict|Idle TTL & Auto-Evict]] — memory management for loaded models