extract_edges: a repetition loop hits the token cap and is retried with the identical request (raising max_tokens doesn't help)
Summary
On some episodes, extract_edges output does not end because the answer is large. It ends because the model falls into a repetition loop: it emits the same fact over and over until it hits the 16,384-token cap (finish_reason: "length"). The truncated JSON then fails json.loads, and the tenacity retry in LLMClient sends the identical request again, 4 attempts in total. At the same temperature the loop usually happens again, so the episode fails after about 65k wasted output tokens.
Raising the cap does not help this case. #1869 and #723 are about the cap being too low for large legitimate answers, which is a different failure. We raised the cap to 32,768, and the loop simply ran longer until it hit the new cap.
Version: graphiti-core 0.30.2, OpenAIGenericClient (json_schema structured output), extraction model Gemma 27B MoE (OpenAI-compatible endpoint behind LiteLLM), Neo4j 5.26. Input: multilingual (non-English) group-chat transcripts.
What we observed
How often: across our builds of about 1,000 episodes, roughly 200 of about 14,000 LLM calls ended exactly at the cap. Retries recovered almost all of them, because the model is not fully deterministic even at temperature 0. One episode failed on every attempt.
The failing call: edge extraction with a prompt of about 10k tokens (previous-episode context plus the current episode, 32 extracted entities). The output reaches 16,384 tokens and consists of one fact repeated.
Replaying that exact call at different edge-extraction temperatures:
temperature attempts that looped a successful attempt 0 4 / 4 — 0.3 3 / 4 43 facts, 28 distinct 1 0 / 1 38 facts, 24 distinct, one fact repeated 8 times So the loop is a sampling problem at low temperature, not an input that is too large.
Same corpus, other memory systems: no loops
We ran the same corpus, with the same model and temperature 0, through two other memory systems. Neither ever came near its cap:
| input per extraction call | output tokens, p99 | output tokens, max | hit the cap | |
|---|---|---|---|---|
| graphiti-core (our episodes: up to 10 consecutive messages) | ~10k-token prompt | 16,384 (the cap) | cap | ~200 calls |
| Hindsight | 3,000-character chunks | ~2,000 | ~7,000 (cap 64,000) | 0 |
| a third system | ~300-token chunks | ~1,400 | ~4,900 | 0 |
Two points stand out.
First, the loop seems to need a long, list-like answer at low temperature. Hindsight also ran at temperature 0 and never looped. Its answers stay short because its input chunks are small.
Second, Hindsight's handling of truncation is a useful reference. Its OpenAI-compatible provider raises a dedicated OutputTooLongError on finish_reason == "length", and fact extraction then splits the chunk in half and extracts from both halves recursively. The retry changes the input instead of repeating the request. For graphiti the equivalent would be retrying extract_edges with the entity list or the episode split.
In fairness, some of this is our own setup. Our episodes are windows of up to 10 messages, which is larger than the one-message episodes graphiti is usually fed, and that makes edge lists longer. Still, a single long episode can produce this failure, and nothing in the retry path can recover from it.
Suggestions (any one would help)
- Do not retry a truncated answer with an identical request.
finish_reason == "length"is known before parsing. Treat it as its own error rather than as a transientJSONDecodeError, and either fail fast with a clear message naming the prompt, or retry with something changed: split the input as described above, or use a higher temperature or afrequency_penaltyfor that attempt. - Check
finish_reasoninOpenAIGenericClient._generate_response. Today a truncated body surfaces as aJSONDecodeError. That makes it look like a malformed-response problem and sends it through the generic retry path. - Clarify the temperature default.
llm_client/config.pysetsDEFAULT_TEMPERATURE = 1, which is whatLLMConfiguses.llm_client/client.pystill definesDEFAULT_TEMPERATURE = 0, which appears unused. Users who settemperature=0for reproducibility, as we did, make this loop more likely. A note in the docs on the recommended temperature for extraction, or removing the stale constant, would avoid that trap.
We have worked around it locally by excluding the one episode. We're happy to test a patch.
Source: getzep/graphiti