---
title: "Ollama's context length trap"
lesson_id: "01"
---

# Ollama's context length trap

- **Lesson ID:** 01
- **Goal:** Detect when Ollama does not read the whole prompt.
- **Human lesson:** [01-ollama-context-length.html](01-ollama-context-length.html)

## Prerequisites

- Ollama installed and a small model pulled (`ollama pull qwen2.5:0.5b`).
- uv and Python 3.11+.

## Inputs, outputs, and artifacts

- **Inputs:** A long prompt and the model name.
- **Outputs:** PASS, or FAIL with the reason.
- **Artifacts:** `ctx_guard.py` in CI.

## Agent build steps

1. Do the environment setup in the human lesson.
2. Run `ollama ps` and note the CONTEXT column.
3. Save `ctx_guard.py` and run `uv run python ctx_guard.py <model>`.
4. If it fails, go to lesson 02.
5. Add the script to CI with the same messages shape your app uses.

## Constraints

Keep the language simple, friendly, and literal. No quizzes, no em dashes. Brand Edge FDE only. Use small local models. Do not invent flags, endpoints, or issue numbers. 

## Key concepts

- Ollama's default context depends on VRAM. Under 24 GiB it is 4k.
- `/v1/chat/completions` ignores `num_ctx`.
- A too-long prompt is rejected or cut, depending on version and runner.
- `prompt_eval_count` and `usage.prompt_tokens` show what the model read.

## Takeaways

- Check `CONTEXT` in `ollama ps`.
- Compare tokens sent with tokens read in CI.
- Fail the build on an error or a cut prompt.

## Acceptance checks

- The script fails on the default 4k context with a 9k prompt.
- Ollama #5356 and #16814 and the Ollama docs are linked.
- [ ] Proceed to [lesson 02 brief](02-set-context-and-size-memory.llms.md).
