Spring AI with Ollama (Local LLMs)
Run local LLMs in Spring Boot with Spring AI and Ollama: setup, model selection, offline development, cost and privacy trade-offs, and when a local model is the right call.
On this page
Local models are not just a privacy feature — they change your development loop. No API key, no per-call cost, no network dependency in tests. This tutorial covers running Ollama with Spring AI, and is honest about the quality trade-off.
Key Takeaways
- Local models give you free, offline, private inference — at a quality cost.
- Switching Spring AI from OpenAI to Ollama is a dependency and config change; your code is unchanged.
- Best used for development, privacy-sensitive workloads, and cost optimisation of high-volume simple tasks.
- Local embeddings are an excellent default for development, removing cost and latency from ingestion.
Setting up Ollama
Install Ollama, then pull a model:
# Install from ollama.com, then:
ollama pull llama3.2 # a small, fast general model
ollama pull nomic-embed-text # a local embedding model
ollama serve # serves on http://localhost:11434Wiring it into Spring AI
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>spring:
ai:
ollama:
base-url: http://localhost:11434
chat:
options:
model: llama3.2
temperature: 0.2
embedding:
options:
model: nomic-embed-textYour service code is identical to the OpenAI version — that is the whole point of the ChatModel
abstraction:
@Service
public class LocalChatService {
private final ChatClient chatClient;
public LocalChatService(ChatClient.Builder builder) {
this.chatClient = builder.build(); // no idea it is talking to Ollama
}
public String ask(String question) {
return chatClient.prompt().user(question).call().content();
}
}Profiles for local vs hosted
spring:
ai:
ollama:
chat:
options:
model: llama3.2spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o-miniRun locally with --spring.profiles.active=local; deploy with prod. One codebase, two providers.
Choosing a local model
| Size | Runs on | Good for | Not good for |
|---|---|---|---|
| 1-3B | Laptop CPU | Classification, routing, simple extraction | Complex reasoning |
| 7-8B | 16GB RAM, better with GPU | General chat, RAG answering | Hard multi-step tasks |
| 13B+ | Dedicated GPU | Higher-quality reasoning | Anything without a GPU budget |
Start small. A 3B model handles a surprising amount of routine work — intent classification, simple extraction, first-line RAG answering — at zero marginal cost.
Local embeddings: the easy win
Even if you use a hosted chat model, local embeddings are worth it for development. Spring AI supports in-process ONNX models that need no server:
spring:
ai:
embedding:
transformer:
onnx:
model-uri: <all-MiniLM-L6-v2 onnx model>This removes cost and network latency from your ingestion pipeline entirely, which makes iterating on chunking and retrieval far faster.
When a local model is the right call
- Privacy and compliance — data that cannot legally or contractually leave your infrastructure.
- High-volume simple tasks — classifying millions of items where hosted per-call cost adds up and a small model is good enough.
- Development and testing — free, offline, fast.
- Air-gapped environments — where there is no external network at all.
When it is not
- Frontier reasoning — the hardest tasks still need the best hosted models.
- Tiny teams without ops capacity — running and scaling model inference is real operational work; a hosted API removes it.
- Bursty low-volume traffic — you pay for idle hardware; a hosted API bills per use.
The pragmatic pattern many teams land on: local models for the easy, high-volume, or sensitive 80%, and a hosted model for the hard 20%. Routing between them is covered in small language models.
Next
- Spring AI observability & monitoring
- GenAI on AWS, Azure & GCP — the hosted side
Frequently Asked Questions
Why run a local LLM instead of a hosted API?
How do I switch Spring AI from OpenAI to Ollama?
What hardware do I need to run Ollama?
Can I use local embeddings too?
Related tutorials
- Multimodal AI with Spring BootSend images and audio to vision models from Spring Boot with Spring AI: the Media API, image analysis, document extraction from scans, and handling multimodal input safely.
- Spring AI Observability & MonitoringInstrument Spring AI with Micrometer and OpenTelemetry: token and cost metrics per feature, latency tracking, tracing model calls, and dashboards that catch a cost problem before the invoice does.
- Structured Output with Spring AITurn LLM responses into typed Java objects with Spring AI: BeanOutputConverter, .entity(), generic lists, enums and validation — the reliable alternative to parsing text by hand.
- Security in AI-Powered Spring ApplicationsSecure a Spring Boot AI application against the OWASP LLM Top 10: prompt injection defenses, output validation, rate limiting, PII handling and safe tool authorization — with code.