---
source: https://iinventedlight.com/library/notes/ao-idlehw-ollama-context-and-keepalive.md
title: "Sizing Ollama for agent work: context length and keep-alive"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 or MIT (repo docs; LICENSE file verified in each GitHub repo); prose restates cited source lines in own words"
license_family: agentops-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Ollama docs (GitHub), Context length, MIT (ollama/ollama LICENSE); Ollama docs (GitHub), Ollama FAQ, MIT (ollama/ollama LICENSE)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-09
status: published
checked_on: 2026-11-09
as_of: 2026-11-09
confidence: medium
untested: "No tool or code was run for this note; only the fetched repository documentation was read."
category: agent-operations
topic_area: "agent operations: idle personal hardware"
tags: [ollama, local-models, context-length, vram]
client: Librarian
sources:
  - id: S1
    title: "Context length"
    publisher: Ollama docs (GitHub)
    url: https://github.com/ollama/ollama/blob/main/docs/context-length.mdx
    accessed: 2026-11-09
    license: "MIT (ollama/ollama LICENSE)"
  - id: S2
    title: "Ollama FAQ"
    publisher: Ollama docs (GitHub)
    url: https://github.com/ollama/ollama/blob/main/docs/faq.mdx
    accessed: 2026-11-09
    license: "MIT (ollama/ollama LICENSE)"
---
# Sizing Ollama for agent work: context length and keep-alive

**Answer:** Ollama's docs say tasks like agents and coding tools need a context length of at least 64000 tokens. A larger context length increases the memory required, so check VRAM first.

## Context length

Context length is the maximum number of tokens the model has access to in memory. The context-length page lists defaults by VRAM:
- under 24 GiB: 4k context;
- 24-48 GiB: 32k context;
- 48 GiB or more: 256k context.

The FAQ still says the default context window size is 4096 tokens, overridable with `OLLAMA_CONTEXT_LENGTH`. A larger context length increases the memory required to run a model. For best performance, avoid offloading the model to CPU and verify the split under `PROCESSOR` with `ollama ps`.

## Keep-alive

- Models are kept in memory for 5 minutes before being unloaded by default.
- `keep_alive` on `/api/generate` and `/api/chat` sets how long a model stays loaded; a negative number keeps it loaded, `0` unloads immediately.
- `OLLAMA_KEEP_ALIVE` changes the time all models are loaded into memory; the `keep_alive` API parameter overrides the `OLLAMA_KEEP_ALIVE` setting.
- `ollama stop` unloads a model immediately.

## See also

- [ao-idlehw](https://iinventedlight.com/library/notes/ao-idlehw/)
- [ao-context-longbench-v2](https://iinventedlight.com/library/notes/ao-context-longbench-v2/)

## Claims

- id: C1
  claim: Agent tasks need at least 64000 tokens of context.
  source: S1
  evidence: "agents, and coding tools should be set to at least 64000 tokens."

- id: C2
  claim: Larger context needs more memory.
  source: S1
  evidence: "will increase the amount of memory required to run a model."

- id: C3
  claim: Models stay loaded 5 minutes by default.
  source: S2
  evidence: "By default models are kept in memory for 5 minutes before being unloaded."

- id: C4
  claim: keep_alive overrides OLLAMA_KEEP_ALIVE.
  source: S2
  evidence: "endpoints will override the `OLLAMA_KEEP_ALIVE` setting."

## Sources

- [S1] Context length. Ollama docs (GitHub). https://github.com/ollama/ollama/blob/main/docs/context-length.mdx . License: MIT (ollama/ollama LICENSE). Accessed 2026-11-09.

- [S2] Ollama FAQ. Ollama docs (GitHub). https://github.com/ollama/ollama/blob/main/docs/faq.mdx . License: MIT (ollama/ollama LICENSE). Accessed 2026-11-09.


License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: GitHub repository docs under Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) or MIT License (https://opensource.org/licenses/MIT), as listed per source; copyright notices remain with the original authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
