reflect: a single OutputTooLongError re-runs the whole task (local embed+rerank included) up to 3x

Open
#7,259 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
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
ai, backend

Research direction

Start in engine/memory_engine.py at _is_non_retryable_task_error() around line 1814, then trace the reflect and consolidation retry paths cited in the issue. Check how OutputTooLongError and provider finish_reason="length" are represented, and run the relevant retry tests. Done means deterministic output length failures do not repeat the costly task work, with tests covering reflect and consolidation behavior.

Written by the indexing model from the issue text.

Description

core

Summary

On hindsight-api 1.3.1 a reflect task that fails with OutputTooLongError (raised at scope=reflect_tool_call by an OpenAI-compatible provider) is classified as retryable, and the worker retries the whole task — not the failed LLM call. Because the reflect agent drives recall / expand / reflect_tool_call and every one of those runs embeddings + a cross-encoder rerank in-process, each retry re-pays the entire local inference cost. With worker_max_retries=3 and a 60 s backoff, one deterministic provider-side token-cap error becomes ~3× the retrieval work.

Observed

Output generation failed: API error: Output generation exceeded token limits: ... (scope=reflect_tool_call)
Retryable error, scheduling retry in 60s (attempt 1/3)

Consolidation shows the same shape with an unbounded budget:

Consolidation failed with transient error; retrying in Ns (indefinite retry)

Cost when the models run locally: py-spy on the same process attributes ~49% of samples to the cross-encoder (ModernBERT) and ~39% to the bi-encoder (BERT) during these windows. A single failed reflect task is roughly a minute of embed+rerank on 12 cores, and it is paid three times.

Why this looks wrong

  • OutputTooLongError is a function of (prompt, model), exactly like the cases already treated as deterministic — e.g. ProviderContentPolicyError at engine/memory_engine.py:1837, justified with "re-running the task feeds the same chunk to the same model and earns the same refusal". _is_non_retryable_task_error() (engine/memory_engine.py:1814) does not include OutputTooLongError.
  • Retry granularity is the task. The retrieval half of the task cannot produce a different answer on the second attempt, so re-running it is pure CPU (and, with local models, the dominant cost).
  • consolidation's indefinite retry (engine/memory_engine.py:5555-5566) has the same property: same unconsolidated rows, same embedding work, per attempt.

Asks

  1. Classify OutputTooLongError (and provider finish_reason="length" mapped onto it) as non-retryable at the task level for reflect / consolidation, or at minimum fail fast after one attempt instead of three.
  2. On retry, reuse the retrieval result — cache the recall/expand output (or at least the query embeddings and rerank order) keyed by (bank, query, filters) for the retry window, so a retry only re-runs the LLM call.
  3. Consider sending an explicit max_completion_tokens for reflect tool calls. DEFAULT_REFLECT_MAX_COMPLETION_TOKENS is None (config.py:2443), so the ceiling is whatever the provider defaults to, which makes the failure mode provider-specific and hard to bound.

Environment

  • hindsight-api 1.3.1 (PyPI), SQLite store
  • Local embeddings bge-small-en-v1.5, local reranker hs-rerank-1
  • LLM: DeepSeek v4-flash behind an OpenAI-compatible gateway
  • HINDSIGHT_API_RERANKER_MAX_CANDIDATES=80, HINDSIGHT_API_REFLECT_WALL_TIMEOUT=1800
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.