Skip to content

fix: raise max_tokens for AI analysis to avoid thinking-model truncation#241

Merged
AmintaCCCP merged 2 commits into
mainfrom
fix/ai-analysis-max-tokens-budget
Jul 11, 2026
Merged

fix: raise max_tokens for AI analysis to avoid thinking-model truncation#241
AmintaCCCP merged 2 commits into
mainfrom
fix/ai-analysis-max-tokens-budget

Conversation

@AmintaCCCP

@AmintaCCCP AmintaCCCP commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Reasoning models (e.g. agnes-2.0-flash) enable thinking by default and emit reasoning_content. With the previous 1000/1200/800 token caps, the thinking chain consumed the entire budget and hit finish_reason:"length" before the final JSON was written to content, causing repository AI analysis to fail (empty content → "No content received from AI service").
  • Raise the analysis and semantic-reranking token budgets to a shared ANALYSIS_MAX_TOKENS = 4096 so the model can finish thinking and emit the answer.

Why this approach

  • max_tokens is a standard OpenAI-compatible parameter accepted by any endpoint — no risk of 400s from unknown/vendor-specific fields, and no model/provider database to maintain.
  • This is the conservative fix: it does not try to disable thinking (which requires vendor-specific params like chat_template_kwargs/enable_thinking/thinking.type that vary per provider and can error on strict relays). It simply gives the token budget enough room for thinking + final JSON.

Changes

src/services/aiService.ts:

  • Add private static readonly ANALYSIS_MAX_TOKENS = 4096;
  • analyzeRepository: maxTokens 1000 → 4096
  • searchGistsWithReranking: maxTokens 1200 → 4096
  • searchRepositoriesWithSemanticReranking: maxTokens 800 → 4096

Test plan

  • Configure an OpenAI-compatible channel using a thinking model (e.g. agnes-2.0-flash) with reasoning effort "none".
  • Run AI analysis on a repository; confirm finish_reason:"stop", content contains valid JSON, and analysis succeeds.
  • tsc --noEmit and eslint pass (verified locally).

🤖 Generated with OpenCode

Summary by CodeRabbit

  • Improvements
    • Increased and standardized the maximum response size for repository and gist analysis and reranking to allow more complete, higher-quality semantic search results.
    • Reranking now uses a consistent token budget across gist and repository flows, improving result coverage and relevance.

…el truncation

Reasoning models (e.g. agnes-2.0-flash) enable thinking by default and emit
reasoning_content. With the previous 1000/1200/800 token cap the thinking
chain consumed the whole budget and hit finish_reason:"length" before the
final JSON was written to content, causing repository analysis to fail.

Bump the analysis and semantic-reranking token budgets to a shared
ANALYSIS_MAX_TOKENS=4096 so the model can finish thinking and emit the answer.
max_tokens is a standard OpenAI param accepted by any compatible endpoint.
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d054c203-b9a1-4093-8da8-6b726352cdcf

📥 Commits

Reviewing files that changed from the base of the PR and between c26dcd7 and 8006507.

📒 Files selected for processing (1)
  • src/services/aiService.ts

📝 Walkthrough

Walkthrough

AIService now centralizes 4096-token limits for repository analysis and gist or repository semantic reranking requests.

Changes

AI analysis token budgeting

Layer / File(s) Summary
Centralized analysis token budget
src/services/aiService.ts
Adds separate shared 4096-token constants for analysis and reranking, replacing hardcoded token limits in repository analysis and both semantic reranking flows.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: increasing token limits to prevent truncation during AI analysis.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ai-analysis-max-tokens-budget

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request replaces hardcoded token limits across different AI service calls with a single static constant, ANALYSIS_MAX_TOKENS. The reviewer suggests defining separate constants for repository analysis and search reranking (e.g., RERANKING_MAX_TOKENS) to allow independent tuning of token limits, preventing unintended latency and cost increases.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/services/aiService.ts
private config: AIConfig;
private language: string;
private static readonly ANALYSIS_MAX_ATTEMPTS = 3;
private static readonly ANALYSIS_MAX_TOKENS = 4096;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Sharing a single constant ANALYSIS_MAX_TOKENS for both repository analysis and search reranking reduces maintainability and flexibility. Repository analysis (which generates a detailed summary, tags, and platforms) and search reranking (which only outputs a list of IDs) have very different output sizes and latency requirements.

If you want to adjust the token limit for repository analysis in the future (e.g., increasing it to 8192), it will unintentionally increase the limit for search reranking as well, potentially leading to higher latency and costs for search queries.

Consider defining separate constants for these tasks so they can be tuned independently.

Suggested change
private static readonly ANALYSIS_MAX_TOKENS = 4096;
private static readonly ANALYSIS_MAX_TOKENS = 4096;
private static readonly RERANKING_MAX_TOKENS = 4096;

Comment thread src/services/aiService.ts Outdated
user: `Query: ${query}\n\nGists:\n${this.sanitizeForPrompt(gistSummaries)}`,
temperature: 0.1,
maxTokens: 1200,
maxTokens: AIService.ANALYSIS_MAX_TOKENS,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the separate RERANKING_MAX_TOKENS constant here to allow independent tuning of the token limit for Gist search reranking.

Suggested change
maxTokens: AIService.ANALYSIS_MAX_TOKENS,
maxTokens: AIService.RERANKING_MAX_TOKENS,

Comment thread src/services/aiService.ts Outdated
user: `Query: ${query}\n\nRepositories:\n${this.sanitizeForPrompt(repoSummaries)}`,
temperature: 0.1,
maxTokens: 800,
maxTokens: AIService.ANALYSIS_MAX_TOKENS,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the separate RERANKING_MAX_TOKENS constant here to allow independent tuning of the token limit for repository search reranking.

Suggested change
maxTokens: AIService.ANALYSIS_MAX_TOKENS,
maxTokens: AIService.RERANKING_MAX_TOKENS,

Address gemini-code-assist review: repository analysis and search reranking
have different output sizes/latency needs, so decouple their token limits
into ANALYSIS_MAX_TOKENS and RERANKING_MAX_TOKENS for independent tuning.
@AmintaCCCP AmintaCCCP merged commit e853d1f into main Jul 11, 2026
5 checks passed
@AmintaCCCP AmintaCCCP deleted the fix/ai-analysis-max-tokens-budget branch July 11, 2026 14:06
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.

1 participant