Ollama biedt compatibiliteit met de Anthropic Messages API om bestaande applicaties te koppelen aan Ollama, inclusief tools zoals Claude Code.
Gebruik
Omgevingsvariabelen
Om Ollama te gebruiken met tools die de Anthropic API verwachten (zoals Claude Code), stel je deze omgevingsvariabelen in:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # vereist maar wordt genegeerd
export ANTHROPIC_BASE_URL=http://localhost:11434Eenvoudig /v1/messages voorbeeld
python
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
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?" }]
}'Streamingvoorbeeld
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" }]
}'Tool-aanroepvoorbeeld
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?" }]
}'Gebruik met Claude Code
Claude Code kan worden geconfigureerd om Ollama als backend te gebruiken.
Aanbevolen modellen
Voor gebruiksscenario's rond coderen zijn modellen zoals glm-4.7, minimax-m2.1 en qwen3-coder aanbevolen.
Download een model voordat je het gebruikt:
shell
ollama pull qwen3-coderLet op: Qwen 3 coder is een model met 30B parameters dat minimaal 24GB VRAM nodig heeft om soepel te draaien. Voor langere contextlengtes is meer VRAM nodig.
shell
ollama pull glm-4.7:cloudSnelle installatie
shell
ollama launch claudeDit zal je vragen een model te selecteren, Claude Code automatisch te configureren en te starten. Om te configureren zonder te starten:
shell
ollama launch claude --configHandmatige installatie
Stel de omgevingsvariabelen in en voer Claude Code uit:
shell
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coderOf stel de omgevingsvariabelen in in je shellprofiel:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # vereist maar wordt genegeerd
export ANTHROPIC_BASE_URL=http://localhost:11434Voer daarna Claude Code uit met een willekeurig Ollama-model:
shell
claude --model qwen3-coderEindpunten
/v1/messages
Ondersteunde functies
- [x] Berichten
- [x] Streaming
- [x] Systeemprompts
- [x] Multi-turn conversaties
- [x] Visie (afbeeldingen)
- [x] Tools (functieaanroepen)
- [x] Toolresultaten
- [x] Denken/uitgebreid denken
Ondersteunde aanvraagvelden
- [x]
model - [x]
max_tokens - [x]
messages- [x] Tekst
content - [x] Afbeeldings
content(base64) - [x] Array van contentblokken
- [x]
tool_use-blokken - [x]
tool_result-blokken - [x]
thinking-blokken
- [x] Tekst
- [x]
system(tekenreeks of array) - [x]
stream - [x]
temperature - [x]
top_p - [x]
top_k - [x]
stop_sequences - [x]
tools - [x]
thinking - [ ]
tool_choice - [ ]
metadata
Ondersteunde responsvelden
- [x]
id - [x]
type - [x]
role - [x]
model - [x]
content(tekst, tool_use, thinking-blokken) - [x]
stop_reason(end_turn, max_tokens, tool_use) - [x]
usage(input_tokens, output_tokens)
Streaminggebeurtenissen
- [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
Modellen
Ollama ondersteunt zowel lokale als cloudmodellen.
Lokale modellen
Pull een lokaal model voordat je het gebruikt:
shell
ollama pull qwen3-coderAanbevolen lokale modellen:
qwen3-coder- Uitstekend voor coderingsopgavengpt-oss:20b- Sterk algemeen doelmodel
Cloudmodellen
Cloudmodellen zijn direct beschikbaar zonder te hoeven pullen:
glm-4.7:cloud- High-performance cloudmodelminimax-m2.1:cloud- Snel cloudmodel
Standaard modelnamen
Voor tools die vertrouwen op standaard Anthropic modelnamen zoals claude-3-5-sonnet, gebruik je ollama cp om een bestaande modelnaam te kopiëren:
shell
ollama cp qwen3-coder claude-3-5-sonnetDaarna kan deze nieuwe modelnaam worden opgegeven in het model-veld:
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!"
}
]
}'Verschillen met de Anthropic API
Gedragsverschillen
- API-sleutel wordt geaccepteerd maar niet gevalideerd
- De
anthropic-version-header wordt geaccepteerd maar niet gebruikt - Tokenaantallen zijn benaderingen gebaseerd op de tokenizer van het onderliggende model
Niet ondersteund
De volgende Anthropic API-functies worden momenteel niet ondersteund:
| Functie | Beschrijving |
|---|---|
/v1/messages/count_tokens | Eindpunt voor het tellen van tokens |
tool_choice | Geforceerd gebruik van een specifieke tool of het uitschakelen van tools |
metadata | Aanvraagmetadata (user_id) |
| Promptcaching | cache_control-blokken voor het cachen van prefixen |
| Batches API | /v1/messages/batches voor asynchrone batchverwerking |
| Citaten | citations-contentblokken |
| PDF-ondersteuning | document-contentblokken met PDF-bestanden |
| Server-verzonden fouten | error-gebeurtenissen tijdens streaming (fouten retourneren een HTTP-status) |
Gedeeltelijke ondersteuning
| Functie | Status |
|---|---|
| Afbeeldingscontent | Base64-afbeeldingen worden ondersteund; URL-afbeeldingen niet |
| Uitgebreid denken | Basale ondersteuning; budget_tokens wordt geaccepteerd maar niet afgedwongen |