Class: CountMessageTokensJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/count_message_tokens_job.rb

Overview

Counts tokens in a message’s payload via the Anthropic API and caches the result on the message record. Enqueued automatically after each LLM message is created.

Instance Method Summary collapse

Instance Method Details

#perform(message_id) ⇒ Object

Parameters:

  • message_id (Integer)

    the Message record to count tokens for



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/jobs/count_message_tokens_job.rb', line 13

def perform(message_id)
  message = Message.find(message_id)
  return if already_counted?(message)

  provider = Providers::Anthropic.new
  api_messages = [{role: message.api_role, content: message.payload["content"].to_s}]

  token_count = provider.count_tokens(
    model: Anima::Settings.model,
    messages: api_messages
  )

  # Guard against parallel jobs: reload and re-check before writing.
  # Uses update! (not update_all) so {Message::Broadcasting} after_update_commit
  # broadcasts the updated token count to connected clients.
  message.reload
  return if already_counted?(message)

  message.update!(token_count: token_count)
end