From 811b2e3244b4e66bcc0f1a59d81c5b34fa33772c Mon Sep 17 00:00:00 2001 From: wishhyt <24300810017@m.fudan.edu.cn> Date: Wed, 18 Mar 2026 10:35:03 +0800 Subject: [PATCH] fix: initialize max_tokens before try block in _calc_claude_tokens If an exception is raised before the `max_tokens` assignment (e.g., anthropic import fails or the model key is missing from MAX_TOKENS), the except block references the unbound `max_tokens` variable, raising NameError and masking the original error. Initialize max_tokens to 0 before the try block as a safe fallback. Made-with: Cursor --- pr_agent/algo/token_handler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pr_agent/algo/token_handler.py b/pr_agent/algo/token_handler.py index cb313f023f..bf7121ec5a 100644 --- a/pr_agent/algo/token_handler.py +++ b/pr_agent/algo/token_handler.py @@ -97,10 +97,11 @@ def _get_system_user_tokens(self, pr, encoder, vars: dict, system, user): return 0 def _calc_claude_tokens(self, patch: str) -> int: + max_tokens = 0 try: import anthropic from pr_agent.algo import MAX_TOKENS - + client = anthropic.Anthropic(api_key=get_settings(use_context=False).get('anthropic.key')) max_tokens = MAX_TOKENS[get_settings().config.model]