Class: CountEventTokensJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- CountEventTokensJob
- 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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/jobs/count_event_tokens_job.rb', line 13 def perform(event_id) event = Event.find(event_id) return if already_counted?(event) provider = Providers::Anthropic.new = [{role: event.api_role, content: event.payload["content"].to_s}] token_count = provider.count_tokens( model: Anima::Settings.model, messages: ) # Guard against parallel jobs: reload and re-check before writing. # Uses update! (not update_all) so {Event::Broadcasting} after_update_commit # broadcasts the updated token count to connected clients. event.reload return if already_counted?(event) event.update!(token_count: token_count) end |