Class: RuboCop::Cop::Prompt::MaxTokens

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/prompt/max_tokens.rb

Overview

Checks that documentation text in prompt-related code doesn’t exceed the maximum token limit.

This cop identifies code in classes, modules, or methods with “prompt” in their names and calculates the token count for any string literals or heredoc content using tiktoken_ruby. By default, it warns when the content exceeds 4000 tokens.

Examples:

# bad (assuming very long content that exceeds token limit)
def generate_prompt
  "    # This is a very long prompt that contains thousands of tokens...\n    # [many lines of text]\n  PROMPT\nend\n\n# good\ndef generate_prompt\n  <<~PROMPT\n    # A concise prompt that stays within token limits\n    You are a helpful assistant.\n  PROMPT\nend\n"

Constant Summary collapse

MSG =
"Documentation text exceeds maximum token limit (%<actual>d > %<max>d tokens)"
DEFAULT_MAX_TOKENS =

Default maximum token count

4000

Instance Method Summary collapse

Instance Method Details

#on_dstr(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/prompt/max_tokens.rb', line 46

def on_dstr(node)
  return unless in_prompt_context?(node)

  # Handle heredoc content
  content = node.children.filter_map do |child|
    child.children[0] if child.type == :str
  end.join

  return if content.strip.empty?

  check_token_count(node, content)
end

#on_str(node) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/prompt/max_tokens.rb', line 37

def on_str(node)
  return unless in_prompt_context?(node)

  content = node.children[0]
  return if content.nil? || content.strip.empty?

  check_token_count(node, content)
end