Skip to content

feat(config): make per-file token limit configurable - #716

Merged
lizhengfeng101 merged 2 commits into
alibaba:mainfrom
Linxiushen:feat/configurable-file-token-limit
Aug 7, 2026
Merged

feat(config): make per-file token limit configurable#716
lizhengfeng101 merged 2 commits into
alibaba:mainfrom
Linxiushen:feat/configurable-file-token-limit

Conversation

@Linxiushen

Copy link
Copy Markdown
Contributor

Description

Expose the per-file prompt token ceiling as a saved max_tokens setting and a per-run --max-tokens override. The resolved value now follows --max-tokens > saved configuration > embedded task-template default for both ocr review and ocr scan.

This keeps the per-file ceiling independent from --max-tokens-budget, which caps aggregate usage across a run. Invalid persisted values and negative CLI values fail with actionable errors.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passes locally
  • Manual testing (describe below)

Automated verification on Windows with Go 1.25.5:

  • go test -count=1 ./...
  • go vet ./...
  • Focused config, flag-parsing, precedence, and persistence round-trip tests
  • git diff --check

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA

Related Issues

Closes #707

AI Assistance Disclosure

AI assistance was used during implementation. I reviewed the complete diff and verified the tests locally.

@CLAassistant

CLAassistant commented Aug 4, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

OpenCodeReview: Review complete: 0 finding(s) across 5 selected item(s).

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, nice work — tests and docs are solid. Two things I'd like to discuss:

1. MaxTokens shouldn't also control LLM output length

Right now Template.MaxTokens is passed directly into ChatRequest.MaxTokens, which maps to the API's max_completion_tokens. That means when a user sets max_tokens 200000 to accommodate a model with a larger context window, they're also telling the model it can produce up to 200,000 output tokens per response — that's not the intent.

This field currently serves two roles: the compression/pre-flight threshold baseline (context window ceiling) and the max output passed to the API. These should be independent. I'd suggest this PR only let the configurable value affect compression thresholds and the pre-flight gate, and leave the LLM call's max output at its existing default (or split them into two fields).

2. Need ocr config unset max_tokens

Once a user runs ocr config set max_tokens 200000, there's no way to revert to the template default — ocr config set max_tokens 0 is rejected by validation, and ocr config unset only supports provider, custom_providers.<name>, and mcp_servers.<name>.

Suggest extending ocr config unset to handle scalar keys by zeroing the field and saving — omitempty will drop it from the JSON automatically:

case "max_tokens":
    cfg.MaxTokens = 0
    return saveConfig(configPath, cfg)

@Linxiushen

Copy link
Copy Markdown
Contributor Author

Addressed both points in 3a2a0ac.

  1. The configurable max_tokens value now controls only prompt sizing, pre-flight filtering, and compression thresholds. The template's original value is preserved as a runtime-only completion cap, and every review/scan LLM request (including compression and relocation) uses that separate cap. A regression test sets a 200,000-token prompt ceiling and verifies the request still sends the original 58,888 completion limit.

  2. ocr config unset max_tokens now clears the saved override while preserving unrelated configuration; because the field is omitempty, it disappears from the JSON and the embedded template default resumes.

Verification: focused config/flag tests pass; internal/config/template, internal/llmloop, internal/agent, and internal/scan all pass. The full cmd/opencodereview package compiles and reaches only two existing Windows-specific test failures (Unix absolute-path interpretation and 0600 permission semantics).

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 3c60eb6 into alibaba:main Aug 7, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants