Ollama bietet Kompatibilität mit der Anthropic Messages API, um bestehende Anwendungen – einschließlich Tools wie Claude Code – mit Ollama zu verbinden.
Verwendung
Umgebungsvariablen
Um Ollama mit Tools zu verwenden, die die Anthropic API erwarten (wie Claude Code), setzen Sie diese Umgebungsvariablen:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # erforderlich, aber ignoriert
export ANTHROPIC_BASE_URL=http://localhost:11434Einfaches /v1/messages-Beispiel
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # erforderlich, aber ignoriert
)
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", // erforderlich, aber ignoriert
});
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?" }]
}'Streaming-Beispiel
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" }]
}'Beispiel für Tool-Aufrufe
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # erforderlich, aber ignoriert
)
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?" }]
}'Verwendung mit Claude Code
Claude Code kann so konfiguriert werden, dass er Ollama als Backend verwendet.
Empfohlene Modelle
Für Anwendungsfälle im Bereich der Softwareentwicklung werden Modelle wie glm-4.7, minimax-m2.1 und qwen3-coder empfohlen.
Laden Sie ein Modell vor der Verwendung herunter:
shell
ollama pull qwen3-coderHinweis: Qwen 3 Coder ist ein Modell mit 30 Milliarden Parametern, das mindestens 24 GB VRAM für einen reibungslosen Betrieb benötigt. Für längere Kontextlängen wird mehr VRAM benötigt.
shell
ollama pull glm-4.7:cloudSchnelleinrichtung
shell
ollama launch claudeDadurch werden Sie aufgefordert, ein Modell auszuwählen, Claude Code automatisch zu konfigurieren und zu starten. Um die Konfiguration ohne Start durchzuführen:
shell
ollama launch claude --configManuelle Einrichtung
Setzen Sie die Umgebungsvariablen und führen Sie Claude Code aus:
shell
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coderOder setzen Sie die Umgebungsvariablen in Ihrem Shell-Profil:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # erforderlich, aber ignoriert
export ANTHROPIC_BASE_URL=http://localhost:11434Führen Sie anschließend Claude Code mit einem beliebigen Ollama-Modell aus:
shell
claude --model qwen3-coderEndpunkte
/v1/messages
Unterstützte Funktionen
- [x] Messages
- [x] Streaming
- [x] System-Prompts
- [x] Mehrturnige Konversationen
- [x] Vision (Bilder)
- [x] Tools (Funktionsaufrufe)
- [x] Tool-Ergebnisse
- [x] Thinking/Erweitertes Thinking
Unterstützte Anfragefelder
- [x]
model - [x]
max_tokens - [x]
messages- [x] Text
content - [x] Bild
content(Base64) - [x] Array von Inhaltsblöcken
- [x]
tool_use-Blöcke - [x]
tool_result-Blöcke - [x]
thinking-Blöcke
- [x] Text
- [x]
system(Zeichenkette oder Array) - [x]
stream - [x]
temperature - [x]
top_p - [x]
top_k - [x]
stop_sequences - [x]
tools - [x]
thinking - [ ]
tool_choice - [ ]
metadata
Unterstützte Antwortfelder
- [x]
id - [x]
type - [x]
role - [x]
model - [x]
content(Text, tool_use, thinking-Blöcke) - [x]
stop_reason(end_turn, max_tokens, tool_use) - [x]
usage(input_tokens, output_tokens)
Streaming-Ereignisse
- [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
Modelle
Ollama unterstützt sowohl lokale als auch Cloud-Modelle.
Lokale Modelle
Laden Sie ein lokales Modell vor der Verwendung herunter:
shell
ollama pull qwen3-coderEmpfohlene lokale Modelle:
qwen3-coder– Hervorragend für Aufgaben im Bereich der Softwareentwicklunggpt-oss:20b– Leistungsstarkes General-Purpose-Modell
Cloud-Modelle
Cloud-Modelle sind sofort verfügbar, ohne dass ein Herunterladen erforderlich ist:
glm-4.7:cloud– Leistungsstarkes Cloud-Modellminimax-m2.1:cloud– Schnelles Cloud-Modell
Standardmodellnamen
Für Tools, die auf Standardmodellnamen von Anthropic wie claude-3-5-sonnet zurückgreifen, verwenden Sie ollama cp, um einen vorhandenen Modellnamen zu kopieren:
shell
ollama cp qwen3-coder claude-3-5-sonnetAnschließend kann dieser neue Modellname im Feld model angegeben werden:
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!"
}
]
}'Unterschiede zur Anthropic API
Verhaltensunterschiede
- Der API-Schlüssel wird akzeptiert, aber nicht validiert
- Der Header
anthropic-versionwird akzeptiert, aber nicht verwendet - Die Token-Anzahlen sind Näherungswerte basierend auf dem Tokenizer des zugrundeliegenden Modells
Nicht unterstützt
Die folgenden Funktionen der Anthropic API werden derzeit nicht unterstützt:
| Funktion | Beschreibung |
|---|---|
/v1/messages/count_tokens | Endpunkt zur Token-Zählung |
tool_choice | Erzwingen der Verwendung bestimmter Tools oder Deaktivieren von Tools |
metadata | Anfragemetadaten (user_id) |
| Prompt-Caching | cache_control-Blöcke zum Cachen von Präfixen |
| Batches-API | /v1/messages/batches für asynchrone Stapelverarbeitung |
| Zitate | citations-Inhaltsblöcke |
| PDF-Unterstützung | document-Inhaltsblöcke mit PDF-Dateien |
| Server-seitig gesendete Fehler | error-Ereignisse während des Streamings (Fehler geben HTTP-Status zurück) |
Teilweise unterstützt
| Funktion | Status |
|---|---|
| Bildinhalte | Base64-Bilder werden unterstützt; URL-Bilder werden nicht unterstützt |
| Erweitertes Thinking | Grundlegende Unterstützung; budget_tokens wird akzeptiert, aber nicht erzwungen |