Repository navigation
Description
Context & Rationale
Ollama slot leases (src/devops_cli/ai/client/network.py:169) and pydantic-ai limiters (src/devops_cli/ai/concurrency.py:31) count per process; GitHub pacing is already cross-process (src/devops_cli/github/rate_limiter.py:243). devops ai prewarm sends keep_alive: 1h (src/devops_cli/ai/client/ollama.py:60). Chat and stream bodies omit it (ollama.py:287, :463), devops ai chat then calls through pydantic-ai (src/devops_cli/commands/ai.py:1046), and no manifest sets OLLAMA_KEEP_ALIVE. A request without it resets expiry to the server default, so the lease is probably cut short; unchecked on a live server. Each of the 2 Ollama pods is routed 3 named models against OLLAMA_MAX_LOADED_MODELS 2 (k8s/llm/ollama.yaml:66-67); the direct embedding task adds another (#481). No ms/token metric exists, only a tokens/s span attribute on direct non-streaming Ollama calls (src/devops_cli/ai/client/unified.py:69), and nothing reads /api/ps. vibes observations/systems/06 reports 45 to over 2,500 ms/token under eviction cycles, with no method; its claim that prewarm pins VRAM is contradicted above.
Scope & Deliverables
- Context & Rationale*: Ollama slot leases (
src/devops_cli/ai/client/network.py:169) and pydantic-ai limiters (src/devops_cli/ai/concurrency.py:31) count per process; GitHub pacing is already cross-process (src/devops_cli/github/rate_limiter.py:243).devops ai prewarmsendskeep_alive: 1h(src/devops_cli/ai/client/ollama.py:60). Chat and stream bodies omit it (ollama.py:287,:463),devops ai chatthen calls through pydantic-ai (src/devops_cli/commands/ai.py:1046), and no manifest setsOLLAMA_KEEP_ALIVE. A request without it resets expiry to the server default, so the lease is probably cut short; unchecked on a live server. Each of the 2 Ollama pods is routed 3 named models againstOLLAMA_MAX_LOADED_MODELS2 (k8s/llm/ollama.yaml:66-67); the direct embedding task adds another (feat(ai): embeddings through the llm gateway, with embedding model evaluation #481). No ms/token metric exists, only a tokens/s span attribute on direct non-streaming Ollama calls (src/devops_cli/ai/client/unified.py:69), and nothing reads/api/ps. vibesobservations/systems/06reports 45 to over 2,500 ms/token under eviction cycles, with no method; its claim that prewarm pins VRAM is contradicted above. - Deliverable*: Measure first. Add a time-per-output-token histogram to
src/devops_cli/telemetry/instruments.py, emitted beside the AI token counters (src/devops_cli/ai/spend/ledger.py:485) with the same backend labels, and unit and meaning taken from semconvgen_ai.server.time_per_output_token; add p50/p95 to feat(ai): llm gateway calls record their serving backend in the spend ledger #474'sdevops ai cost report --by backend. Wire or delete the unusedrecord_llm_metrics(src/devops_cli/telemetry/tracer.py:436). Report/api/psper pod (loaded models,expires_at, VRAM) fromdevops ai gateway status. Then check withexpires_atwhether a chat shortens a prewarm lease; if it does, send the configured keep-alive on direct Ollama bodies and the pydantic-ai path, and setOLLAMA_KEEP_ALIVEon the StatefulSet. Last, learn why the loaded-model limit is 2, then fit models per pod to it, with feat(ai): embeddings through the llm gateway, with embedding model evaluation #481 choosing the embedding model. Document thatacquire_ollama_slotis per-process by design. - Constraint*:
keep_aliveis an idle timer; it does not stop eviction once the loaded-model limit is reached, so it cannot fix eviction cycles alone. A client-side cross-process semaphore adds a hidden queue ahead of Ollama's own (OLLAMA_NUM_PARALLEL1), repeating fix(k8s): devops-review routing throttles the largest server to the smallest server's request limit #467, and host processes reach the pods directly (hostPort,k8s/llm/ollama.yaml:95) besides the gateway.ValkeyTokenBucketRateLimiterlimits rate, not concurrency, and fails open (src/devops_cli/valkey/rate_limiter.py:106); wiring it in would report arbitration that is not happening. - Measured*:
.venv/bin/python probe/xproc_slot.py process(scratch probe, not in repo): 3 processes leasing one URL atmax_parallel=1held it 3 at once;threadmode, 1..venv/bin/python probe/chat_payload.py(scratch): chat body has nokeep_alive.awk '/model: ollama/{m=$2; getline; print $2, m}' k8s/llm/gateway/configmap.yaml | sort -u: each of 2 Ollama backends routed 3 named models. - Source*: vibes
patterns/multi-agent-codebase-concurrency.md; vibesobservations/systems/06-multi-agent-concurrency-shared-workspace-hazards-and-swarm-coordination.md
Update 2026-10-08: refinement
- Done:
record_llm_metricswas deleted in v0.2.25. - Stale: the pods have no
hostPortsince v0.2.23; the semaphore stays rejected for the hidden-queue reason. - Still true: no time-per-output-token metric (
ai/client/unified.py:97), nothing reads/api/ps, and no request setskeep_alive(ai/client/ollama.py:283-318). - Each tier holds one model, yet the gateway sends 16gib-fast 2, 48gib-fast 3 and 64gib-standard 4. The keep-alive step follows refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001's Ollama decision.
Proposed design
Problem statement
The Ollama model residency and performance in the devops-cli system is suboptimal due to incorrect keep-alive handling, lack of time-per-token metrics, and inefficient model loading strategies that lead to eviction cycles and inconsistent performance.
Acceptance criteria
- Criterion 1: A time-per-output-token histogram is added to telemetry instruments and emitted alongside AI token counters with the same backend labels
Verification: Verify thatdevops_cli/telemetry/instruments.pycontains a new histogram instrument for time-per-output-token and that it's emitted insrc/devops_cli/ai/spend/ledger.pywith backend labels - Criterion 2: The ms/token metric is properly measured and reported in cost reports
Verification: Rundevops ai cost report --by backendand confirm p50/p95 values are present for time-per-output-token - Criterion 3: Ollama requests sent through the direct client path include the configured keep-alive value
Verification: Check that calls toollama.py(lines 287, 463) now includekeep_aliveparameter from configuration - Criterion 4: The pydantic-ai path also sends the configured keep-alive value for Ollama requests
Verification: Confirm thatsrc/devops_cli/commands/ai.py:1046correctly passes keep-alive through to Ollama clients - Criterion 5: The StatefulSet is updated with OLLAMA_KEEP_ALIVE configuration
Verification: Validate that Kubernetes manifest ink8s/llm/ollama.yamlincludesOLLAMA_KEEP_ALIVEenvironment variable set appropriately
Key questions
- What is the current behavior of Ollama slot leases and pydantic-ai limiters?: Ollama slot leases (
src/devops_cli/ai/client/network.py:169) and pydantic-ai limiters (src/devops_cli/ai/concurrency.py:31) count per process; GitHub pacing is already cross-process (src/devops_cli/github/rate_limiter.py:243). [sources: src/devops_cli/ai/client/network.py, src/devops_cli/ai/concurrency.py, src/devops_cli/github/rate_limiter.py] - How is keep-alive currently handled in Ollama requests?:
devops ai prewarmsendskeep_alive: 1h(src/devops_cli/ai/client/ollama.py:60). Chat and stream bodies omit it (ollama.py:287,:463),devops ai chatthen calls through pydantic-ai (src/devops_cli/commands/ai.py:1046) and no manifest setsOLLAMA_KEEP_ALIVE. [sources: src/devops_cli/ai/client/ollama.py, src/devops_cli/commands/ai.py] - What is the current approach for measuring time-per-output-token?: No ms/token metric exists, only a tokens/s span attribute on direct non-streaming Ollama calls (
src/devops_cli/ai/client/unified.py:69), and nothing reads/api/ps. [sources: src/devops_cli/ai/client/unified.py] - Should the Ollama keep-alive feature be used to address eviction cycles?: Keep-alive is an idle timer that does not stop eviction once the loaded-model limit is reached, so it cannot fix eviction cycles alone. The approach should focus on measuring and optimizing model residency strategies. [sources: docs/agent/tasks/task-598-ollama-model-residency-per-pod-model-budget-keep-alive-ms-to.md]
- Should the system measure time-per-output-token metric?: Yes, a time-per-output-token histogram should be added to
src/devops_cli/telemetry/instruments.py, emitted beside the AI token counters with the same backend labels, and unit and meaning taken from semconvgen_ai.server.time_per_output_token. (proposed) [sources: src/devops_cli/telemetry/instruments.py] - Should the unused
record_llm_metricsbe deleted?: Yes, as mentioned in the update,record_llm_metricswas deleted in v0.2.25. [sources: docs/agent/tasks/task-598-ollama-model-residency-per-pod-model-budget-keep-alive-ms-to.md]
Open questions
- What is the current limit for loaded models per Ollama pod? (missing verified source)
Fit
Needs splitting into 5 items:
- Add time-per-output-token histogram to telemetry instruments
- Implement ms/token metric reporting in cost reports
- Send configured keep-alive on direct Ollama bodies and pydantic-ai path
- Set OLLAMA_KEEP_ALIVE on StatefulSet manifest
- Document that acquire_ollama_slot is per-process by design
Suspected block
None.
Dependencies
None.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
dan-petty commentedon Oct 1, 2026
Moved from the shipped v0.2.23 to the backlog. It is still valid (checked 2026-10-01), and P2 items don't displace P1 work in the planned releases.
dan-petty commentedon Oct 3, 2026
Hog's residency is decided in #1064 (2026-10-03). Source: the slow-tier plan (
.data/slow-tier-plan-20261003.md, section 3) and its verification pass. Ollama logs were read withkubectl logs, read-only.The decision.
OLLAMA_MAX_LOADED_MODELS=2andOLLAMA_KEEP_ALIVE=-1, withNUM_PARALLELstill 1.bge-m3:latest, laterqwen3-embedding:0.6bper feat(ai): embeddings through the llm gateway, with embedding model evaluation #481) andqwen3-coder:30basdevops-background.Why the count cap alone failed.
loaded runners count=1after each load.What Ollama does (v0.34.4).
processPendingcompareslen(s.loaded)withmaxRunners.sched.go).MAX_LOADED=2.KEEP_ALIVEbelow 0 means forever (envconfig).keep_aliveoverrides it, and the gateway path sends none.sha256-1194…) goes before bge-m3 (sha256-daec…). The feat(ai): embeddings through the llm gateway, with embedding model evaluation #481 switch therefore stops a model first.What stays here.
k8s/llm/ollama.yamlwithMAX_LOADED2. There are now nine DaemonSets ink8s/llm/profiles/ollama-profiles.yaml, atMAX_LOADED1 (fix(k8s): every ollama tier keeps one model loaded and serves one request at a time, so its kv cache holds one context #1061)./api/psper pod ingateway status, keep-alive on direct Ollama bodies, and fitting models per pod on the other tiers. Gemini (64gib-standard) serves four generation models, so its residency is still open.dan-petty commentedon Oct 17, 2026
Proposal needs splitting into 5 items at 3dd7fa6 (see proposed design in issue body).
dan-petty commentedon Oct 24, 2026
The item premise is invalid against the current repository state. First, k8s/llm/ollama.yaml does not exist. Ollama runs as nine DaemonSets in k8s/llm/profiles/ollama-profiles.yaml under issue #1061. The ollama-48gib-slow tier already configures OLLAMA_KEEP_ALIVE as -1 under issue #1064. Second, record_llm_metrics was removed in version 0.2.25. Third, the scope contains five distinct changes and requires splitting into five items.