Anthropic 호환(Anthropic compatibility)
Anthropic 호환(Anthropic compatibility)
Ollama는 Anthropic Messages API와 호환되어 기존 애플리케이션을 연결할 수 있고, Claude Code 같은 도구까지도 Ollama를 백엔드로 쓰게 만들 수 있어요.
출처: 공식문서
사용법
환경 변수
Anthropic API를 기대하는 도구(Claude Code 등)와 Ollama를 함께 쓰려면 환경 변수를 설정하세요.
export ANTHROPIC_AUTH_TOKEN=ollama # required but ignored
export ANTHROPIC_BASE_URL=http://localhost:11434
간단한 /v1/messages 예시
Python:
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # required but ignored
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
messages=[
{'role': 'user', 'content': 'Hello, how are you?'}
]
)
print(message.content[0].text)
JavaScript:
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama" // required but ignored
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, how are you?" }],
});
console.log(message.content[0].text);
cURL:
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: ollama" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"messages": [{ "role": "user", "content": "Hello, how are you?" }]
}'
스트리밍 예시
Python:
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
with client.messages.stream(
model='qwen3-coder',
max_tokens=1024,
messages=[{'role': 'user', 'content': 'Count from 1 to 10'}]
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)
JavaScript:
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama"
});
const stream = await anthropic.messages.stream({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Count from 1 to 10" }],
});
for await (const event of stream) {
if (
event.type === "content_block_delta" &&
event.delta.type === "text_delta"
) {
process.stdout.write(event.delta.text);
}
}
cURL:
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"stream": true,
"messages": [{ "role": "user", "content": "Count from 1 to 10" }]
}'
도구 호출 예시
Python:
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
tools=[
{
'name': 'get_weather',
'description': 'Get the current weather in a location',
'input_schema': {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'The city and state, e.g. San Francisco, CA'
}
},
'required': ['location']
}
}
],
messages=[{'role': 'user', 'content': "What's the weather in San Francisco?"}]
)
for block in message.content:
if block.type == 'tool_use':
print(f'Tool: {block.name}')
print(f'Input: {block.input}')
JavaScript:
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama"
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
tools: [
{
name: "get_weather",
description: "Get the current weather in a location",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
},
required: ["location"],
},
},
],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
});
for (const block of message.content) {
if (block.type === "tool_use") {
console.log("Tool:", block.name);
console.log("Input:", block.input);
}
}
cURL:
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state"
}
},
"required": ["location"]
}
}
],
"messages": [{ "role": "user", "content": "What is the weather in San Francisco?" }]
}'
Claude Code와 함께 쓰기
Claude Code를 Ollama를 백엔드로 쓰도록 설정할 수 있어요.
추천 모델
코딩 용도로는 glm-4.7, minimax-m2.1, qwen3-coder 같은 모델이 권장됩니다.
모델을 사용하기 전에 받아두세요:
ollama pull qwen3-coder
참고: Qwen 3 coder는 30B 파라미터 모델로, 원활히 실행하려면 최소 24GB의 VRAM이 필요합니다. 더 긴 컨텍스트 길이에는 더 많은 VRAM이 필요해요.
ollama pull glm-4.7:cloud
빠른 설정
ollama launch claude
이 명령은 모델 선택을 안내하고, Claude Code를 자동으로 구성하고 실행합니다. 실행 없이 구성만 하려면:
ollama launch claude --config
수동 설정
환경 변수를 설정하고 Claude Code를 실행합니다:
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coder
또는 셸 프로파일에 환경 변수를 설정해 두면:
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434
어떤 Ollama 모델이든 Claude Code로 실행할 수 있어요:
claude --model qwen3-coder
엔드포인트
/v1/messages
지원 기능:
- 메시지
- 스트리밍
- 시스템 프롬프트
- 다중 턴 대화
- 비전(이미지)
- 도구(함수 호출)
- 도구 결과
- 씽킹/확장 씽킹
지원 요청 필드:
-
model -
max_tokens -
messages- 텍스트
content - 이미지
content(base64) - 콘텐츠 블록 배열
-
tool_use블록 -
tool_result블록 -
thinking블록
- 텍스트
-
system(string 또는 array) -
stream -
temperature -
top_p -
top_k -
stop_sequences -
tools -
thinking -
tool_choice -
metadata
지원 응답 필드:
-
id -
type -
role -
model -
content(text, tool_use, thinking 블록) -
stop_reason(end_turn, max_tokens, tool_use) -
usage(input_tokens, output_tokens)
스트리밍 이벤트:
-
message_start -
content_block_start -
content_block_delta(text_delta, input_json_delta, thinking_delta) -
content_block_stop -
message_delta -
message_stop -
ping -
error
모델
Ollama는 로컬 모델과 클라우드 모델을 모두 지원합니다.
로컬 모델
사용 전에 로컬 모델을 받아두세요:
ollama pull qwen3-coder
권장 로컬 모델:
qwen3-coder— 코딩 작업에 뛰어나요gpt-oss:20b— 강력한 범용 모델
클라우드 모델
클라우드 모델은 pull 없이 즉시 사용 가능합니다:
glm-4.7:cloud— 고성능 클라우드 모델minimax-m2.1:cloud— 빠른 클라우드 모델
기본 모델 이름
claude-3-5-sonnet 같은 기본 Anthropic 모델 이름에 의존하는 도구가 있다면, ollama cp로 기존 모델 이름을 복사하세요:
ollama cp qwen3-coder claude-3-5-sonnet
이후 model 필드에 이 새 모델 이름을 지정할 수 있습니다:
curl http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'
Anthropic API와의 차이점
동작 차이
- API 키는 받아들이지만 검증하지 않습니다.
anthropic-version헤더는 받아들이지만 사용하지 않습니다.- 토큰 수는 기본 모델의 토크나이저에 기반한 근사치입니다.
미지원
다음 Anthropic API 기능은 현재 지원되지 않습니다:
| 기능 | 설명 |
|---|---|
/v1/messages/count_tokens |
토큰 계산 엔드포인트 |
tool_choice |
특정 도구 사용 강제 또는 도구 비활성화 |
metadata |
요청 메타데이터(user_id) |
| 프롬프트 캐싱 | 프리픽스 캐싱을 위한 cache_control 블록 |
| Batches API | 비동기 배치 처리를 위한 /v1/messages/batches |
| 인용(Citations) | citations 콘텐츠 블록 |
| PDF 지원 | PDF 파일을 담는 document 콘텐츠 블록 |
| 서버 전송 오류 | 스트리밍 중 error 이벤트(오류는 HTTP 상태로 반환됨) |
부분 지원
| 기능 | 상태 |
|---|---|
| 이미지 콘텐츠 | base64 이미지 지원, URL 이미지 미지원 |
| 확장 씽킹 | 기본 지원, budget_tokens은 받아들이지만 강제하지 않음 |
더 알아보기 (Learn more)
- OpenAI compatibility — OpenAI 호환 API
- Web search — MCP를 통한 도구 통합