3.6 KiB
3.6 KiB
| title | aliases | tags | sources | created | updated | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| LM Studio — Anthropic Messages API |
|
|
|
2026-04-30 | 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/jsonx-api-key: $LM_API_TOKEN— optional if Require Authentication is disabled in LM Studio
Basic Request
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):
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:
message_startcontent_block_startcontent_block_delta(repeating)content_block_stopmessage_deltamessage_stop
Tool Use
Pass a tools array with JSON Schema input definitions and a tool_choice policy:
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/messagesonlocalhost:1234is a drop-in forapi.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_choiceis supported - Auth header is optional when LM Studio's Require Authentication is off
- See wiki/claude-code/lmstudio-anthropic-compat for redirecting the full Anthropic SDK via env vars
Related
- wiki/claude-code/lmstudio-anthropic-compat — redirect Claude Code / SDK to local server
- wiki/claude-code/lmstudio-chat-completions — OpenAI-compatible
/v1/chat/completions - wiki/claude-code/lmstudio-rest-api — native v1 endpoints and feature comparison table
- wiki/claude-code/lmstudio-idle-ttl-auto-evict — memory management for loaded models