# Behavior traces — ollama/ollama

> Canonical page: https://codevetter.com/unpack/ollama/behavior-traces

# Behavior traces — ollama/ollama

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

Three canonical flows: (1) serve startup: main -> cobra serve -> initializeKeypair -> net.Listen -> server.Serve -> blob prune -> GenerateRoutes -> InitScheduler.Run; (2) `ollama run X prompt`: CLI heartbeat/pull -> api.Client.Chat -> ChatHandler -> GetModel -> getRunner -> load -> llama-server subprocess -> streamed parse -> NDJSON; (3) pull: PullHandler -> PullModel -> blob downloads -> manifest write. Safetensors models diverge at sched.load into the mlxrunner subprocess.
- Startup order: main() -> NewCLI() -> serveCmd -> RunServer -> initializeKeypair (ed25519 at ~/.ollama) -> net.Listen(envconfig.Host) -> server.Serve(ln) — [main.go](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/main.go) [cmd/cmd.go#L2101-L2117](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/cmd/cmd.go#L2101-L2117) [cmd/cmd.go#L2119-L2163](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/cmd/cmd.go#L2119-L2163)
- server.Serve sequence: slog logger init -> log cloud-disabled status -> llm.PruneSplitModelDirs on blobs dir -> fixBlobs -> PruneLayers + manifest.PruneDirectory (unless OLLAMA_NOPRUNE) -> GenerateRoutes -> InitScheduler -> sched.Run goroutines -> http.Serve — [server/routes.go#L1969-L2031](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L1969-L2031) [server/sched.go#L91-L106](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L91-L106) [server/sched.go#L219-L220](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L219-L220)
- CLI run flow: RunHandler parses --think/--keepalive/--format flags -> api.ClientFromEnvironment -> showOrPullModel (pulls if absent) -> inferThinkingOption -> isEmbeddingModel branch -> generateChat/generateInteractive -> client.Chat -> POST /api/chat — [cmd/cmd.go#L814-L953](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/cmd/cmd.go#L814-L953) [api/client.go#L74-L79](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/api/client.go#L74-L79)
- Server chat flow: ChatHandler binds JSON -> parseAndValidateModelRef -> cloud refs proxied via proxyCloudJSONRequest -> GetModel -> keep_alive=0 unloads via sched.expireRunner -> remote models proxied to RemoteHost -> otherwise modelOptions + scheduleRunner — [server/routes.go#L2465-L2553](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L2465-L2553) [server/routes.go#L2555-L2621](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L2555-L2621) [server/routes.go#L205-L253](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L205-L253)
- Scheduling flow: getRunner builds LlmRequest -> loaded-map fast path via schedulerModelKey (modelPaths joined with NUL, or digest:/name: fallback) -> else enqueue pendingReqCh (ErrMaxQueue when full) -> scheduler picks GPUs -> load() spawns runner -> useLoadedRunner — [server/sched.go#L108-L132](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L108-L132) [server/sched.go#L174-L216](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L174-L216) [server/sched.go#L520-L632](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L520-L632)
- GGUF runner launch: sched.load -> llm.LoadModel (gguf.ReadModel metadata) -> PredictServerVRAM -> selectLlamaServerPlacement -> newServerFn = llm.NewLlamaServer -> NewLlamaServerRunner -> startLlamaServer -> exec.Command(llama-server, params) -> WaitUntilRunning polls /health — [server/sched.go#L528-L600](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L528-L600) [llm/server.go#L93-L111](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/server.go#L93-L111) [llm/llama_server.go#L348](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L348) [llm/llama_server.go#L424](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L424) [llm/llama_server.go#L851](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/llm/llama_server.go#L851)
- MLX runner launch: sched.load -> m.IsMLX() -> mlxrunner.NewClient(modelName, numCtx) -> exec.Command(self, "runner", --model, --port) -> hidden cobra `runner` cmd -> mlxrunner.Execute -> HTTP server inside subprocess — [server/sched.go#L591-L594](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L591-L594) [mlxrunner/client.go#L349](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/mlxrunner/client.go#L349) [cmd/cmd.go#L2569-L2576](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/cmd/cmd.go#L2569-L2576)
- Pull trace: PullHandler binds api.PullRequest -> parseNormalizePullModelRef (normalizes :cloud) -> PullModel in goroutine -> per-layer blobDownload (chunked parts with backoff) -> manifest write -> progress channel -> streamResponse emits api.ProgressResponse NDJSON — [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-L333](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/download.go#L187-L333)
- Create trace (Modelfile): CreateHandler -> readCreateModelfile -> parser.ParseFile -> reject adapter/typical_p -> modelfile.CreateRequest -> createBlob uploads per file (or sharedBlobStore local shortcut) -> client.Create -> /api/create -> server assembles layers + manifest — [cmd/cmd.go#L220-L241](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/cmd/cmd.go#L220-L241) [cmd/cmd.go#L317-L400](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/cmd/cmd.go#L317-L400) [parser/parser.go#L56-L120](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/parser/parser.go#L56-L120) [server/routes.go#L1917](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L1917)
- Unload/keep-alive: empty chat request with keep_alive=0 triggers sched.expireRunner and returns done_reason=unload without inference; runners expire via expiredCh after sessionDuration (OLLAMA_KEEP_ALIVE default 5m) — [server/routes.go#L2541-L2553](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/routes.go#L2541-L2553) [server/sched.go#L61-L82](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L61-L82) [envconfig/config.go#L126-L144](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/envconfig/config.go#L126-L144)
- OOM resilience: on llm.IsOutOfMemory during Load, scheduler reduces automatically-derived num_ctx/num_batch once (oomRetryAttempted) and retries; evict-all-and-retry is guarded against infinite loops — [server/sched.go#L39-L42](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L39-L42) [server/sched.go#L632-L659](https://github.com/ollama/ollama/blob/6383a0fa9cbf97494b847226e189f6e36b401a08/server/sched.go#L632-L659)

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)
