Class: CountEventTokensJob

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#perform(event_id) ⇒ Object

Parameters:

  • event_id (Integer)

    the Event record to count tokens for



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/jobs/count_event_tokens_job.rb', line 13

def perform(event_id)
  event = Event.find(event_id)
  return if event.token_count > 0

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

  token_count = provider.count_tokens(
    model: LLM::Client::DEFAULT_MODEL,
    messages: messages
  )

  # Atomic update: only write if still uncounted (avoids race with parallel jobs).
  Event.where(id: event.id, token_count: 0).update_all(token_count: token_count)
end