@@ -21,6 +21,10 @@ def get_max_input_tokens_for_alias(alias: str, mode: str) -> int:
2121 mode_lower = (mode or "" ).strip ().lower ()
2222 alias_lower = (alias or "" ).strip ().lower ()
2323
24+ # Socratic mode: Always conservative (questions don't need massive context)
25+ if mode_lower == SOCRATIC_MODE :
26+ return 6000 # ~24K chars
27+
2428 # Gemini Pro: 2M token window
2529 if "gemini-pro" in alias_lower or alias_lower == "technical-primary" :
2630 return 18000 # ~72K chars; stay well under 2M
@@ -41,10 +45,6 @@ def get_max_input_tokens_for_alias(alias: str, mode: str) -> int:
4145 if "openrouter" in alias_lower :
4246 return 5000 # ~20K chars minimum safe
4347
44- # Socratic mode: Always conservative (questions don't need massive context)
45- if mode_lower == SOCRATIC_MODE :
46- return 6000 # ~24K chars
47-
4848 # Default fallback
4949 return 8000 # ~32K chars
5050
@@ -76,15 +76,16 @@ async def truncate_input_if_needed(
7676 "truncation_reason" : None ,
7777 }
7878
79- if input_tokens <= max_tokens :
80- return user_input , metadata
81-
82- # Truncate to ~75% of max (buffer for system prompt + output)
83- safe_token_limit = int (max_tokens * 0.75 )
79+ # Truncate to ~80% of max (buffer for system prompt + output)
80+ safe_token_limit = int (max_tokens * 0.8 )
8481
8582 # Estimate: 1 token ≈ 4 chars
8683 safe_char_limit = safe_token_limit * 4
8784
85+ # Allow both token and character thresholds to trigger truncation.
86+ if input_tokens <= max_tokens and len (user_input ) <= safe_char_limit :
87+ return user_input , metadata
88+
8889 # Truncate at sentence boundary (don't split mid-word)
8990 truncated = user_input [:safe_char_limit ].rstrip ()
9091
@@ -102,9 +103,12 @@ async def truncate_input_if_needed(
102103
103104 truncated_tokens = count_prompt_tokens (truncated )
104105 metadata ["truncation_reason" ] = (
105- f"Input ({ input_tokens } tokens) exceeded { mode } mode limit ({ max_tokens } tokens) "
106- f"for model alias '{ alias } '. Truncated to { truncated_tokens } tokens."
106+ f"Input ({ input_tokens } tokens) exceeded safe limits for { mode } mode "
107+ f"(max { max_tokens } tokens) on model alias '{ alias } '. "
108+ f"Truncated from { len (user_input )} to { len (truncated )} characters "
109+ f"(~{ truncated_tokens } tokens)."
107110 )
108111 truncated += f"\n \n _[Input truncated from { len (user_input )} to { len (truncated )} characters]_"
112+ metadata ["truncated_to" ] = len (truncated )
109113
110114 return truncated , metadata
0 commit comments