Skip to content

Expose max_tokens (per-file review prompt ceiling) as user-configurable #707

Closed

Description

@theo-chiu

Description

max_tokens (the per-file-review-task prompt-size ceiling) is a hardcoded, model-agnostic constant compiled into the engine, with no CLI flag, config key, environment variable, or llm.extra_body override that reaches it. On the version we run (built 2026-08-03, postdating both #417 and #421) the literal value is:

"MAX_TOKENS": 58888

found twice in the compiled binary's embedded static task-config JSON blocks (the main review task and the fix-prompt-generation task), alongside other clearly-static engine defaults (batch size, dedup thresholds, tool-call limits) — not inside any per-model or per-provider metadata structure. context_window / contextWindow / context_length do not appear anywhere in the binary.

Why this matters

We route real, materially cheaper 1M-context models (e.g. GLM-5.2, 1,048,576-token context window) through this engine specifically to reduce review cost. The 58,888-token ceiling has nothing to do with what the routed model can actually hold — it is a flat cap applied identically regardless of provider/model, so a large real-context model gets throttled down to a fraction of a percent of its own window before it ever sees a file.

Concretely, on our repo, a single per-glob --rule file merging JIRA + Confluence + repository guidance + a knowledge-base tree already reaches ~65,500 tokens on its own, before any diff/file content is added. Every file matched by that rule glob blows the ceiling immediately: we observed a completed_with_errors run where two files hit the 80% warning ("prompt tokens (60361) exceed 80% of max_tokens(58888)", from the shared threshold #421 introduced) and the file actually carrying the review's substantive content never completed ("main_task did not complete before stopping").

We worked around this from our side by relevance-trimming our own rule-file content down to a smaller budget before invoking the engine — but this is a lossy mitigation for something the engine already computes and reports the exact number for in its own warnings; it should not require reverse-engineering the compiled binary to discover the ceiling exists, let alone that it cannot be changed.

What we checked (exhaustively) before concluding this is not user-configurable

  • CLI flags (ocr review --help): the only token-related flag is --max-tokens-budget — a different, unrelated total input+output budget for the whole review run (default 0 = unlimited). It does not touch the per-file max_tokens ceiling.
  • Config keys (ocr config set <key> <value> rejects unknown keys against a full enumerated list: provider, model, providers.<name>.<field>, custom_providers.<name>.<field>, mcp_servers.<name>.<field>, llm.url, llm.auth_token, llm.auth_header, llm.model, llm.protocol, llm.use_anthropic, llm.extra_body, llm.extra_headers, language, telemetry.*): max_tokens and review.max_tokens are both rejected as unknown.
  • Environment variables (complete OCR_* list extracted from the binary): OCR_LLM_TIMEOUT, OCR_LLM_MODEL, OCR_LLM_URL, OCR_LLM_PROTOCOL, OCR_ENABLE_TELEMETRY, OCR_USE_ANTHROPIC, OCR_CONTENT_LOGGING, OCR_LLM_EXTRA_HEADERS, OCR_VIEWER_ALLOWED_HOSTS, OCR_LLM_AUTH_HEADER, OCR_CONFIG_PATH, OCR_LLM_TOKEN — none relate to a token/context ceiling.
  • llm.extra_body (used elsewhere for provider-specific request tweaks): this is serialized into the outbound LLM API request body — it cannot reach an engine-side check that happens before the request is even built, comparing the assembled prompt against the compiled-in constant.

Suggested fix

Expose max_tokens (and/or the 80% warning threshold it already computes against, per #417/#421) as user-configurable — a --max-tokens-per-file CLI flag, a max_tokens/review.max_tokens config key, or an OCR_MAX_TOKENS env var would all resolve this cleanly, ideally with a documented interaction with --max-tokens-budget. Even better: derive a sane default from the configured model's own advertised context window when one is available, rather than one flat constant for every provider/model.

Environment

Activity

  1. added
    enhancement
    good first issue
    help wanted
    on Aug 4, 2026
  2. lizhengfeng101 commented on Aug 4, 2026

    @lizhengfeng101
    Contributor

    Thanks for the thorough investigation and detailed write-up, @theo-chiu! The analysis is spot on — MAX_TOKENS is indeed a hardcoded constant today with no user-facing override.

    I think a good approach here would be:

    1. ocr config set max_tokens <value> — allow users to persistently override the per-file prompt ceiling via the config system.
    2. Provider GUI interaction (optional) — when configuring a provider interactively, we could offer an option to set the max tokens ceiling, ideally with a sensible default derived from the model's known context window.

    This would give power users like yourself a straightforward way to unlock the full context window of large-context models, while keeping the current default safe for everyone else.

    We'd love to see community contributions on this! If anyone is interested in picking this up, the main touch points are in cmd/opencodereview/shared_flags.go, internal/config/, and internal/config/template/template.go. Happy to provide guidance on the implementation path.

  3. deleted a comment from on Aug 5, 2026
  4. deleted a comment from on Aug 5, 2026
  5. deleted a comment from on Aug 5, 2026
  6. deleted a comment from on Aug 5, 2026
  7. deleted a comment from on Aug 5, 2026
  8. deleted a comment from on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    configenhancementgood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions