@@ -43,6 +43,9 @@ def calculate_max_content_tokens(self, custom_prompt=None):
4343 Calculate the maximum tokens available for content based on prompt size.
4444 Dynamically adjusts for custom prompts to prevent exceeding context window.
4545 Uses conservative estimates to account for tokenizer inaccuracies.
46+
47+ Raises:
48+ ValueError: If custom prompt is too large to leave any space for content
4649 """
4750 # Calculate custom prompt tokens if provided
4851 prompt_tokens = 0
@@ -62,6 +65,22 @@ def calculate_max_content_tokens(self, custom_prompt=None):
6265 # Count tokens for the prompt parts (excluding content placeholder)
6366 prompt_tokens = self .count_tokens (system_message ) + self .count_tokens (user_template )
6467
68+ # Check if prompt alone exceeds safe limits
69+ # Reserve at least 20k tokens for content + output + safety margin
70+ MIN_CONTENT_SPACE = 20000
71+ max_allowed_prompt = self .model_context_window - self .max_output_tokens - MIN_CONTENT_SPACE - 1000
72+
73+ if prompt_tokens > max_allowed_prompt :
74+ current_app .logger .error (
75+ f"Custom prompt is too large: { prompt_tokens } tokens exceeds maximum of { max_allowed_prompt } . "
76+ f"This rule cannot be processed."
77+ )
78+ raise ValueError (
79+ f"Custom moderation rule is too large ({ prompt_tokens } tokens). "
80+ f"Maximum allowed is { max_allowed_prompt } tokens. "
81+ f"Please reduce the size of your custom rule."
82+ )
83+
6584 # Log large prompts for debugging
6685 if prompt_tokens > 10000 :
6786 current_app .logger .warning (
@@ -415,6 +434,19 @@ def _analyze_with_custom_prompt(self, content, custom_prompt):
415434
416435Does content violate this rule? JSON only:"""
417436
437+ # DEBUG: Log exact token counts before API call
438+ system_tokens = self .count_tokens (system_message )
439+ user_tokens = self .count_tokens (user_message )
440+ total_tokens = system_tokens + user_tokens
441+ current_app .logger .info (
442+ f"[DEBUG] About to call OpenAI API - "
443+ f"System: { system_tokens } tokens, "
444+ f"User: { user_tokens } tokens, "
445+ f"Total: { total_tokens } tokens, "
446+ f"Custom prompt length: { len (custom_prompt )} chars, "
447+ f"Content length: { len (content )} chars"
448+ )
449+
418450 # Wrap API call with retry logic
419451 def make_api_call ():
420452 client = self .client_manager .get_client ()
0 commit comments