Ollama는 기존 애플리케이션(Claude Code 같은 도구 포함)을 Ollama에 연결할 수 있도록 Anthropic Messages API와 호환됩니다.
사용법
환경 변수
Anthropic API(Claude Code 등)를 요구하는 도구와 Ollama를 함께 사용하려면 다음 환경 변수를 설정하세요:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # 필수이지만 무시됨
export ANTHROPIC_BASE_URL=http://localhost:11434간단한 /v1/messages 예제
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # 필수이지만 무시됨
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
messages=[
{'role': 'user', 'content': 'Hello, how are you?'}
]
)
print(message.content[0].text)javascript
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);shell
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
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
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);
}
}shell
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
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
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);
}
}shell
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 같은 모델을 권장합니다.
사용 전 모델을 다운로드하세요:
shell
ollama pull qwen3-coder참고: Qwen 3 coder는 30B 파라미터 모델로, 원활하게 실행하려면 최소 24GB의 VRAM이 필요합니다. 더 긴 컨텍스트 길이를 사용하려면 더 많은 VRAM이 필요합니다.
shell
ollama pull glm-4.7:cloud빠른 설정
shell
ollama launch claude이 명령을 실행하면 모델을 선택하고 Claude Code를 자동으로 구성한 후 실행합니다. 실행하지 않고 구성하려면:
shell
ollama launch claude --config수동 설정
환경 변수를 설정하고 Claude Code를 실행하세요:
shell
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coder또는 셸 프로필에 환경 변수를 설정하세요:
shell
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434그 다음 원하는 Ollama 모델을 사용해 Claude Code를 실행하세요:
shell
claude --model qwen3-coder엔드포인트
/v1/messages
지원 기능
- [x] 메시지
- [x] 스트리밍
- [x] 시스템 프롬프트
- [x] 다중 턴 대화
- [x] 비전(이미지)
- [x] 도구(함수 호출)
- [x] 도구 결과
- [x] 사고/확장 사고
지원 요청 필드
- [x]
model - [x]
max_tokens - [x]
messages- [x] 텍스트
content - [x] 이미지
content(base64) - [x] 콘텐츠 블록 배열
- [x]
tool_use블록 - [x]
tool_result블록 - [x]
thinking블록
- [x] 텍스트
- [x]
system(문자열 또는 배열) - [x]
stream - [x]
temperature - [x]
top_p - [x]
top_k - [x]
stop_sequences - [x]
tools - [x]
thinking - [ ]
tool_choice - [ ]
metadata
지원 응답 필드
- [x]
id - [x]
type - [x]
role - [x]
model - [x]
content(텍스트, tool_use, thinking 블록) - [x]
stop_reason(end_turn, max_tokens, tool_use) - [x]
usage(input_tokens, output_tokens)
스트리밍 이벤트
- [x]
message_start - [x]
content_block_start - [x]
content_block_delta(text_delta, input_json_delta, thinking_delta) - [x]
content_block_stop - [x]
message_delta - [x]
message_stop - [x]
ping - [x]
error
모델
Ollama는 로컬 모델과 클라우드 모델을 모두 지원합니다.
로컬 모델
사용 전 로컬 모델을 풀(pull)하세요:
shell
ollama pull qwen3-coder권장 로컬 모델:
qwen3-coder- 코딩 작업에 최적화됨gpt-oss:20b- 강력한 범용 모델
클라우드 모델
클라우드 모델은 풀링 없이 즉시 사용할 수 있습니다:
glm-4.7:cloud- 고성능 클라우드 모델minimax-m2.1:cloud- 빠른 클라우드 모델
기본 모델 이름
claude-3-5-sonnet 같은 기본 Anthropic 모델 이름에 의존하는 도구의 경우, ollama cp를 사용해 기존 모델 이름을 복사하세요:
shell
ollama cp qwen3-coder claude-3-5-sonnet그 후 이 새로운 모델 이름을 model 필드에 지정할 수 있습니다:
shell
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 블록 |
| 배치 API | 비동기 배치 처리를 위한 /v1/messages/batches |
| 인용 | citations 콘텐츠 블록 |
| PDF 지원 | PDF 파일이 포함된 document 콘텐츠 블록 |
| 서버 전송 오류 | 스트리밍 중 error 이벤트(오류는 HTTP 상태 코드로 반환됨) |
부분 지원
| 기능 | 상태 |
|---|---|
| 이미지 콘텐츠 | Base64 이미지 지원됨; URL 이미지는 지원되지 않음 |
| 확장 사고 | 기본 지원됨; budget_tokens는 허용되지만 적용되지 않음 |