reflect: Ollama's context-overflow 400 is not recognised as an overflow, and `REFLECT_MAX_CONTEXT_TOKENS` ignores `LLM_OLLAMA_NUM_CTX`

Open
#7,260 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ollama, python
Domain
ai, backend

Research direction

Start by reading _is_context_overflow_error in engine/reflect/agent.py and trace where the reflect context-token budget is configured from HINDSIGHT_API_LLM_OLLAMA_NUM_CTX and HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS. Check the existing overflow and budget tests before changing behavior. Done means Ollama’s reported wording triggers final synthesis, and the default budget respects the configured context size when the override is unset.

Written by the indexing model from the issue text.

Description

core
Bug Description

With the Ollama provider, a mental-model refresh whose reflect loop outgrows the model's context fails outright
instead of falling back to a final synthesis from the evidence already gathered, and the page is then paused. Two
things combine:

  1. _is_context_overflow_error (engine/reflect/agent.py) looks for phrases such as context_length_exceeded,
    maximum context length and prompt is too long. Ollama words the error as "the prompt is longer than the context
    length currently available to the model", which matches none of them, so the branch that forces final synthesis on
    an overflow is skipped and the 400 ends the refresh.
  2. HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS keeps its default of 100,000 even when
    HINDSIGHT_API_LLM_OLLAMA_NUM_CTX tells Hindsight the model has 32,768. The budget is the threshold at which the
    loop forces synthesis, so with these settings the loop keeps adding evidence long after the prompt has outgrown the
    context Hindsight itself asked Ollama for.
Environment
  • hindsight-api 1.3.0 (through hindsight-embed) on macOS; both points are unchanged in v1.3.1 and on main.
  • HINDSIGHT_API_LLM_PROVIDER=ollama, HINDSIGHT_API_LLM_OLLAMA_NUM_CTX=32768,
    HINDSIGHT_API_REFLECT_MAX_COMPLETION_TOKENS=8192; HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS not set.
  • Ollama 0.40.19 with qwen3.8:27b (Q4_K_M); Ollama's server log shows n_ctx = 32768 on every load.
Actual Behavior

In the API logs we keep (2026-11-04 to 2026-12-04), 9 refresh_mental_model tasks failed with this error, one on
2026-11-25, seven on 2026-11-29 and one on 2026-11-30:

Task execution failed: refresh_mental_model, error: UpstreamHTTPError: HTTP 400 from http://127.0.0.1:11434/api/chat: {"error":"the prompt is longer than the context length currently available to the model; shorten the prompt, adjust the context length in settings, or use a model with a longer context length"}

The same 400 was logged twice more by reflect.agent as "closing done call failed, using the standalone prompt";
that fallback re-sends a prompt sized from the same budget. Each failed refresh set last_refresh_failed_at, so those
pages were left paused (they show in paused_mental_models since #6375).

Expected Behavior
  • Ollama's wording counts as a context overflow, so the refresh synthesizes from what it has, as it would on
    OpenAI's context_length_exceeded.
  • With REFLECT_MAX_CONTEXT_TOKENS unset and LLM_OLLAMA_NUM_CTX set, the default follows the context: for example
    num_ctx - reflect_max_completion_tokens, less a margin, since the budget is an o200k_base estimate of the
    messages that leaves out the tool schemas sent with each request (and with no completion cap set, some fixed share
    of num_ctx). Without num_ctx, Ollama 0.40.19 reports a loaded model's context as context_length in
    GET /api/ps; a startup warning when the reflect budget exceeds it would at least make the mismatch visible.
Workaround

Setting HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS=24576 (32,768 - 8,192) has held since 2026-12-04 14:56: to 2026-12-05 08:49, 41 mental-model refreshes started (38 of them on 12-05) and none failed on the context.

Dominant language
Python
Stars
45.2k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vectorize-io/hindsight

All issues in vectorize-io/hindsight

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.