Class: Session

Inherits:
ApplicationRecord show all
Defined in:
app/models/session.rb

Overview

A conversation session — the fundamental unit of agent interaction. Owns an ordered stream of Event records representing everything that happened: user messages, agent responses, tool calls, etc.

Constant Summary collapse

DEFAULT_TOKEN_BUDGET =

Claude Sonnet 4 context window minus system prompt reserve.

190_000
BYTES_PER_TOKEN =

Heuristic: average bytes per token for English prose.

4

Instance Method Summary collapse

Instance Method Details

#messages_for_llm(token_budget: DEFAULT_TOKEN_BUDGET) ⇒ Array<Hash>

Builds the message array expected by the Anthropic Messages API. Includes user/agent messages and tool call/response events in Anthropic’s wire format. Consecutive tool_call events are grouped into a single assistant message; consecutive tool_response events are grouped into a single user message with tool_result blocks.

Walks events newest-first and includes them until the token budget is exhausted. Events are full-size or excluded entirely.

Parameters:

  • token_budget (Integer) (defaults to: DEFAULT_TOKEN_BUDGET)

    maximum tokens to include (positive)

Returns:

  • (Array<Hash>)

    Anthropic Messages API format



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/session.rb', line 26

def messages_for_llm(token_budget: DEFAULT_TOKEN_BUDGET)
  selected = []
  remaining = token_budget

  events.context_events.reorder(id: :desc).each do |event|
    cost = (event.token_count > 0) ? event.token_count : estimate_tokens(event)
    break if cost > remaining && selected.any?

    selected << event
    remaining -= cost
  end

  assemble_messages(selected.reverse)
end