ollama/ollama

Get up and running with Kimi-K2.5, GLM-5, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

169,478 stars Go 9 components

12 hidden assumptions · 7-stage pipeline · 9 components

Like any codebase, this ml inference makes assumptions it never checks — most are routine. The ones worth your attention are below.

Runs language models locally with REST API and desktop interface

When a user sends a chat message through the React UI, it's posted as JSON to the HTTP server's /api/chat endpoint. The server validates the request, spawns or reuses a model runner process, and streams the model's token-by-token response back through server-sent events. The runner loads the specified model (downloading if needed), tokenizes the input using the model's vocabulary, runs inference through optimized backends like llama.cpp, and streams generated tokens back. The UI receives these streaming events and updates the conversation display in real-time.

Under the hood, the system uses 4 feedback loops, 4 data pools, 4 control points to manage its runtime behavior.

A 9-component ml inference. 1144 files analyzed. Data flows through 7 distinct pipeline stages.

Hidden Assumptions

Most of what this code assumes is routine. These 3 are the ones most likely to cause trouble here. The rest are minor; they're under "Show everything".

Worth your attention first

In development, if port 3001 is occupied by another service or Ollama starts on a different port, all API calls fail silently with CORS/network errors

Worth your attention first

If MLX becomes unavailable during runtime (GPU driver issues, memory exhaustion), the engine crashes with no fallback mechanism

Worth your attention first

If Ollama binary is missing or not executable, subprocess spawning fails with cryptic exec errors instead of clear 'binary not found' messages

Show everything (9 more)
Scale

Metal GPU memory limit defaulted to 32GB is appropriate for all macOS systems - assumes user systems have sufficient GPU memory and doesn't validate against actual hardware

If this fails: On systems with less than 32GB GPU memory, Metal allocation attempts can cause out-of-memory crashes or system instability

x/imagegen/cmd/engine/main.go:wiredLimitGB=32
Contract

User data fetch completes before router initialization and returns valid userData object with expected schema - no error handling if fetchUser rejects or returns malformed data

If this fails: If user authentication fails or returns unexpected data structure, QueryClient gets corrupted state leading to auth-related UI failures throughout the app

app/ui/app/src/main.tsx:fetchUser()
Domain

Settings object has nested 'settings' property with 'LastHomeView' field that equals exactly 'chat' string - assumes specific settings schema without validation

If this fails: If settings structure changes or LastHomeView has different values, routing logic breaks and users get stuck on wrong pages or infinite redirects

app/ui/app/src/routes/index.tsx:settingsData?.settings?.LastHomeView
Environment

LOCALAPPDATA environment variable exists and points to writable directory - constructs critical paths based on this Windows environment variable

If this fails: If LOCALAPPDATA is unset or points to read-only location, app installation, logging, and model storage all fail with permission errors

app/cmd/app/app_windows.go:appPath
Ordering

Root DOM element starts empty on first render - checks innerHTML to prevent double-rendering but assumes empty string means fresh page load

If this fails: If root element has whitespace, comments, or other invisible content, check fails and React root never initializes, leaving blank page

app/ui/app/src/main.tsx:rootElement.innerHTML check
Contract

Flag parsing will only pass valid string values to Set method - flag framework contract assumes no nil or malformed inputs

If this fails: If flag parsing internals pass nil or corrupt data to Set method, append operation could panic or corrupt the slice leading to invalid input image handling

x/imagegen/cmd/engine/main.go:stringSlice.Set()
Temporal

Local Ollama server is always reachable so network failures should be ignored - sets networkMode to 'always' assuming server availability

If this fails: If local server is down, queries and mutations continue attempting requests indefinitely without proper offline handling or user feedback

app/ui/app/src/main.tsx:queryClient networkMode always
Resource

System has writable temp directory with sufficient disk space - creates temp directory without checking available space or permissions

If this fails: On systems with full disk or restrictive temp permissions, extraction silently fails or partially completes leaving corrupted examples

docs/tools/extract-examples/main.go:os.MkdirTemp()
Environment

Windows Startup folder structure follows standard APPDATA/Microsoft/Windows/Start Menu/Programs/Startup pattern - hardcoded path assumes Windows version compatibility

If this fails: On non-standard Windows installations or future Windows versions with different startup folder locations, auto-start registration fails silently

app/cmd/app/app_windows.go:startupShortcut path

Open the standalone hidden-assumptions report for ollama →

How Data Flows Through the System

When a user sends a chat message through the React UI, it's posted as JSON to the HTTP server's /api/chat endpoint. The server validates the request, spawns or reuses a model runner process, and streams the model's token-by-token response back through server-sent events. The runner loads the specified model (downloading if needed), tokenizes the input using the model's vocabulary, runs inference through optimized backends like llama.cpp, and streams generated tokens back. The UI receives these streaming events and updates the conversation display in real-time.

  1. HTTP request parsing — Gin HTTP handler in server/ parses incoming POST request to /api/chat, validates JSON body against ChatRequest schema, extracts model name, messages, and generation options [HTTP JSON body → ChatRequest]
  2. Model resolution and loading — ModelManager checks if requested model exists locally in ~/.ollama/models, downloads from registry if missing using manifest-based layer fetching, validates model files and capabilities [ChatRequest → Model]
  3. Runner process spawning — Server spawns appropriate runner subprocess (LlamaRunner for GGUF models, MLXRunner for macOS), passes model path and RunnerOptions including context length, temperature, and GPU settings [Model → RunnerOptions]
  4. Text tokenization — TokenizerManager loads model-specific tokenizer from GGUF metadata, converts input messages to token IDs using vocabulary lookup, handles special tokens and chat templates [Message → TokenData]
  5. Inference execution — Runner subprocess executes model forward pass using llama.cpp or MLX backend, applies sampling with temperature/top-p, generates tokens one at a time with probability scores [TokenData → Generated tokens]
  6. Response streaming — Server streams each generated token as JSON event through HTTP response, includes token text and metadata, handles connection cleanup and error recovery [Generated tokens → Streaming ChatResponse events]
  7. UI real-time updates — React UI receives server-sent events through fetch streaming, uses Streamdown to render markdown progressively, updates conversation state with React Query mutations [Streaming ChatResponse events → Rendered conversation]

Data Models

The data structures that flow between stages — the contracts that hold the system together.

ChatRequest api/
struct with model: string, messages: []Message, stream: bool, options: map[string]interface{}, tools: []Tool, images: []string
Created from HTTP JSON body, validated, passed to model runner, generates streaming ChatResponse events
Message app/types/
struct with role: string (user/assistant/system), content: string, images: []string, tool_calls: []ToolCall
Stored in conversation history, loaded for context, converted to model-specific prompt format
Model api/
struct with name: string, size: int64, digest: string, details: ModelDetails, modified_at: time.Time
Retrieved from local storage or downloaded from registry, cached in memory, used to spawn appropriate runner
RunnerOptions runner/
struct with NumCtx: int, Temperature: float32, TopK: int, TopP: float32, NumGPU: int, MainGPU: int
Parsed from request options, merged with model defaults, passed to runner subprocess for inference control
Manifest manifest/
struct with MediaType: string, Size: int64, Digest: string, Config: ConfigBlob, Layers: []Layer
Downloaded from model registry, parsed to determine required files, used to validate complete model installation
TokenData tokenizer/
struct with Token: int, Text: string, Logprob: float32, SpecialToken: bool
Generated from input text using tokenizer, passed to model for embedding lookup, converted back to text for output

System Behavior

How the system operates at runtime — where data accumulates, what loops, what waits, and what controls what.

Data Pools

Model Registry Cache (file-store)
Downloaded GGUF model files, manifests, and layer blobs cached locally to avoid re-downloading
Conversation Database (database)
SQLite database storing chat history, user settings, and application state for persistence across sessions
Runner Process Pool (in-memory)
Active model runner subprocesses kept alive for faster subsequent requests, managed with reference counting
Token Cache (cache)
Cached tokenizer instances and vocabulary mappings to avoid reloading on each request

Feedback Loops

Delays

Control Points

Technology Stack

Go (runtime)
Primary backend language for HTTP server, model management, and system integration
Gin (framework)
HTTP web framework providing routing, middleware, and JSON handling for REST API
React (framework)
Frontend UI framework for chat interface with hooks, components, and state management
llama.cpp (compute)
Optimized C++ inference engine for running quantized language models efficiently
SQLite (database)
Local database for persisting conversations, settings, and application state
TanStack Query (library)
Data fetching and caching library for managing API calls and streaming responses in React
GGUF (serialization)
Binary format for storing quantized model weights and metadata optimized for inference
WebView (framework)
Native container for displaying web UI within desktop application shell

Key Components

Explore the interactive analysis

See the full architecture map, data flow, and code patterns visualization.

Analyze on CodeSea

Related Ml Inference Repositories

Frequently Asked Questions

What is ollama used for?

Runs language models locally with REST API and desktop interface ollama/ollama is a 9-component ml inference written in Go. Data flows through 7 distinct pipeline stages. The codebase contains 1144 files.

How is ollama architected?

ollama is organized into 5 architecture layers: Desktop Application Shell, HTTP Server & API, Model Management, Inference Execution, and 1 more. Data flows through 7 distinct pipeline stages. This layered structure keeps concerns separated and modules independent.

How does data flow through ollama?

Data moves through 7 stages: HTTP request parsing → Model resolution and loading → Runner process spawning → Text tokenization → Inference execution → .... When a user sends a chat message through the React UI, it's posted as JSON to the HTTP server's /api/chat endpoint. The server validates the request, spawns or reuses a model runner process, and streams the model's token-by-token response back through server-sent events. The runner loads the specified model (downloading if needed), tokenizes the input using the model's vocabulary, runs inference through optimized backends like llama.cpp, and streams generated tokens back. The UI receives these streaming events and updates the conversation display in real-time. This pipeline design reflects a complex multi-stage processing system.

What technologies does ollama use?

The core stack includes Go (Primary backend language for HTTP server, model management, and system integration), Gin (HTTP web framework providing routing, middleware, and JSON handling for REST API), React (Frontend UI framework for chat interface with hooks, components, and state management), llama.cpp (Optimized C++ inference engine for running quantized language models efficiently), SQLite (Local database for persisting conversations, settings, and application state), TanStack Query (Data fetching and caching library for managing API calls and streaming responses in React), and 2 more. A focused set of dependencies that keeps the build manageable.

What system dynamics does ollama have?

ollama exhibits 4 data pools (Model Registry Cache, Conversation Database), 4 feedback loops, 4 control points, 4 delays. The feedback loops handle retry and circuit-breaker. These runtime behaviors shape how the system responds to load, failures, and configuration changes.

What design patterns does ollama use?

5 design patterns detected: Process Isolation, Registry Pattern, Streaming Architecture, Plugin Architecture, Progressive Enhancement.

Analyzed on April 20, 2026 by CodeSea. Written by .