# Data flow — ollama/ollama

> Canonical page: https://codevetter.com/unpack/ollama/data-flow

# Data flow — ollama/ollama

Scanned commit 6383a0fa9cbf97494b847226e189f6e36b401a08 (2026-09-18). Generated by codevetter codevetter 1.12.0 on 2026-09-22.

Requests enter via Gin handlers -> model resolution (manifest -> Model struct) -> option merge (defaults < generation defaults < model < request) -> Scheduler -> runner subprocess over loopback HTTP -> streamed tokens parsed back into API responses. Model artifacts flow registry -> blob downloads -> manifest write; create flows Modelfile/safetensors dir -> layer blobs -> manifest. Chat text flows messages -> Go renderer or llama-server chat template -> prompt -> token stream -> Parser.Add splits content/thinking/tool calls -> NDJSON stream.
- Option precedence is layered in modelOptions: api.DefaultOptions -> model.GenerationDefaults (parsed from GGUF metadata) -> model.Options (Modelfile params) -> request options, with embedding models getting a NumBatch=2048 default capped by context — [server/routes.go#L125-L172](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L125-L172) [server/images.go#L110-L135](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/images.go#L110-L135) [llm/llama_server.go#L90-L104](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L90-L104)
- Chat path in ChatHandler: ShouldBindJSON -> parseAndValidateModelRef -> getExistingName -> GetModel -> (unload shortcut for keep_alive=0) -> scheduleRunner -> renderer/parser selection -> llm runner Chat/Completion -> NDJSON stream to client — [server/routes.go#L2465-L2540](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L2465-L2540) [server/routes.go#L3016](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L3016)
- GetModel resolves a name through manifest.ParseNamedManifest -> config blob decode (ConfigV2) -> layer iteration, grouping split GGUF shards via groupManifestSplitGGUFLayers; parseFromModel auto-pulls when the manifest is missing — [server/model.go#L27-L97](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/model.go#L27-L97) [server/images.go#L656](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/images.go#L656)
- Prompt rendering branches: models with a Go renderer name go through renderers.RenderWithRenderer; others use template.Template (embedded .gotmpl set indexed by index.json) or llama-server's native chat_template via /v1/chat/completions — [model/renderers/renderer.go#L40-L60](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/model/renderers/renderer.go#L40-L60) [template/template.go#L1-L40](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/template/template.go#L1-L40) [llm/llama_server.go#L3-L13](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L3-L13) [server/images.go#L362-L432](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/images.go#L362-L432)
- Output parsing: streamed runner tokens are fed to Parser.Add which returns (content, thinking, toolCalls); PreservedTokens tells llama-server which grammar tokens must survive detokenization — [model/parsers/parsers.go#L12-L24](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/model/parsers/parsers.go#L12-L24) [llm/server.go#L197-L208](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/server.go#L197-L208)
- Pull flow: PullHandler -> PullModel -> registry layer resolution -> blobDownload.Run with chunked resumable parts -> sha256 verification -> manifest write; progress is fanned out as api.ProgressResponse over a channel to streamResponse — [server/routes.go#L1135-L1193](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L1135-L1193) [server/images.go#L1002](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/images.go#L1002) [server/download.go#L187-L476](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/download.go#L187-L476)
- Blob writes are content-addressed: CreateBlobHandler accepts POST /api/blobs/:digest and HeadBlobHandler probes existence; digest validity enforced by manifest.ValidateDigest regex sha256[:-][64 hex] — [server/routes.go#L1715-L1852](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L1715-L1852) [manifest/paths.go#L15-L27](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/manifest/paths.go#L15-L27)
- Runner placement: scheduler loads GGUF metadata (llm.LoadModel), predicts VRAM via llm.PredictServerVRAM, selects GPUs via selectLlamaServerPlacement honoring OLLAMA_SCHED_SPREAD and OLLAMA_GPU_OVERHEAD, then evicts loaded runners if predicted footprint exceeds 80% of free memory — [server/sched.go#L528-L575](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L528-L575) [llm/llama_server.go#L2698](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L2698)
- llama-server subprocesses receive model path, ports, -ngl/-t only when explicitly set (otherwise auto-detect), media marker rewriting ([img-N]) for vision, and report memory back via parsed log lines into runner's vramByDevice/systemFreeAtLoad — [llm/llama_server.go#L400-L440](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L400-L440) [llm/llama_server.go#L120-L160](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L120-L160)
- Desktop app persists chats/settings in sqlite (mattn/go-sqlite3) via app/store with a frozen v2 schema (schema.sql) plus migrations; the React UI talks to the same Ollama HTTP API — [app/store/schema.sql](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/app/store/schema.sql) [app/store/database.go](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/app/store/database.go) [app/ui/app/package.json](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/app/ui/app/package.json) [go.mod#L12](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/go.mod#L12)
- Inference request logging is optional and bounded: withInferenceRequestLogging wraps /api/generate, /api/chat, and the /v1 inference routes, gated by OLLAMA_DEBUG_LOG_REQUESTS — [server/routes.go#L1927-L1946](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L1927-L1946) [server/inference_request_log.go](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/inference_request_log.go) [envconfig/config.go#L220](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/envconfig/config.go#L220)
- Context management: num_ctx defaults come from OLLAMA_CONTEXT_LENGTH or a VRAM-tier auto default (numCtxAuto), clamped to model train context; `shift` enables sliding-window context shift except for deepseek2 family; `truncate` drops history instead of erroring — [server/sched.go#L134-L172](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L134-L172) [server/sched.go#L174-L200](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L174-L200) [envconfig/config.go#L230](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/envconfig/config.go#L230) [envconfig/config.go#L338](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/envconfig/config.go#L338)

Every claim links to its cited files at the pinned commit. Citations identify source evidence; they do not establish runtime correctness. Independent CodeVetter scan — not reviewed by upstream maintainers.

## Public product links

- [CodeVetter](https://codevetter.com/)
- [Download](https://codevetter.com/download)
- [Documentation](https://codevetter.com/docs/)
- [Source](https://github.com/Codevetter/codevetter)
