Graphify-Labs/graphify · error · ValueError

token_budget must be positive, got {token_budget}

Error message

token_budget must be positive, got {token_budget}

What it means

Raised at the top of the chunk-packing helper (`pack_files_by_token_budget`-style function) when `token_budget <= 0`. The greedy packer needs a positive budget to decide when a chunk is full; zero or negative would create an infinite-loop/empty-chunk situation, so it fails fast with the offending value in the message.

Source

Thrown at graphify/llm.py:1920

    return len(_TOKENIZER.encode(content, disallowed_special=())) + (_PER_FILE_OVERHEAD_CHARS // _CHARS_PER_TOKEN)


def _pack_chunks_by_tokens(
    files: "list[Path | FileSlice]",
    token_budget: int,
) -> "list[list[Path | FileSlice]]":
    """Greedily pack files/slices into chunks that fit a token budget.

    Units are first grouped by parent directory so related artifacts share a
    chunk (cross-file edges are more likely to be extracted within a chunk
    than across chunks). Within each directory, units are added one at a
    time; a chunk is closed when adding the next would exceed the budget.
    Oversized splittable documents are pre-split into ``FileSlice`` units by
    ``expand_oversized_files`` before packing (#1369), so the old "one file
    larger than the budget" case no longer silently drops content.
    """
    if token_budget <= 0:
        raise ValueError(f"token_budget must be positive, got {token_budget}")

    by_dir: dict[Path, "list[Path | FileSlice]"] = {}
    for f in files:
        by_dir.setdefault(unit_path(f).parent, []).append(f)

    chunks: "list[list[Path | FileSlice]]" = []
    current: "list[Path | FileSlice]" = []
    current_tokens = 0
    current_images = 0

    for directory in sorted(by_dir):
        for unit in by_dir[directory]:
            cost = _estimate_file_tokens(unit)
            is_image = not isinstance(unit, FileSlice) and _is_vision_image(unit)
            over_budget = current_tokens + cost > token_budget
            over_images = is_image and current_images >= _MAX_IMAGES_PER_CHUNK
            if current and (over_budget or over_images):
                chunks.append(current)

View on GitHub (pinned to 7fe58b0b0f)

Solutions

  1. Pass a positive budget appropriate to the model context, e.g. `token_budget=8192` (well under num_ctx).
  2. If computing the budget, clamp with a sane floor: `max(1024, num_ctx // 3)` as the code's own ollama hint suggests.
  3. Fix the upstream flag/env default that yields 0.

Example fix

# before
budget = int(os.environ.get("GRAPHIFY_TOKEN_BUDGET", 0))

# after
budget = int(os.environ.get("GRAPHIFY_TOKEN_BUDGET", "8192"))
Defensive patterns

Strategy: type-guard

Type guard

def is_valid_token_budget(v: object) -> bool:
    """True for positive integers usable as a chunk token budget."""
    return isinstance(v, int) and not isinstance(v, bool) and v > 0

assert is_valid_token_budget(budget), f"bad token_budget: {budget!r}"

Prevention

When it happens

Trigger: Passing `token_budget=0` (e.g. from an unset CLI flag defaulting to 0), a negative value, or a computed budget that underflowed (e.g. `max(0, ctx - overhead)` where overhead ≥ ctx).

Common situations: CLI wrappers that map `--token-budget` to `int(os.environ.get("TOKEN_BUDGET", 0))`; arithmetic deriving the budget from num_ctx minus a fixed overhead that exceeds small contexts; config files with a missing key defaulting to 0.

Related errors


AI-assisted analysis of Graphify-Labs/graphify@7fe58b0b0f (2026-08-14). Data as JSON: /api/errors/119eb0c4851dbd45. Report an issue: GitHub.