Skip to content
Kward Search API index

Class: Kward::Tools::ContextBudgetStats

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/context_budget_stats.rb

Overview

Reports approximate context budget savings for the current process.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(context_budget_meter: nil) ⇒ ContextBudgetStats

Returns a new instance of ContextBudgetStats.



9
10
11
12
13
14
15
16
17
# File 'lib/kward/tools/context_budget_stats.rb', line 9

def initialize(context_budget_meter: nil)
  @context_budget_meter = context_budget_meter
  super(
    "context_budget_stats",
    "Return approximate per-session context budget savings from tool output compaction and deduplication.",
    properties: {},
    required: []
  )
end

Instance Method Details

#call(_args, conversation, cancellation: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kward/tools/context_budget_stats.rb', line 19

def call(_args, conversation, cancellation: nil)
  cancellation&.raise_if_cancelled!
  meter = conversation.respond_to?(:context_budget_meter) ? conversation.context_budget_meter : @context_budget_meter
  return "Error: context budget stats are unavailable" unless meter

  snapshot = meter.snapshot
  lines = [
    "# Context budget stats",
    "- Calls: #{snapshot.calls}",
    "- Original bytes: #{snapshot.original_bytes}",
    "- Returned bytes: #{snapshot.returned_bytes}",
    "- Saved bytes: #{snapshot.saved_bytes}",
    "- Estimated tokens saved: #{estimated_tokens(snapshot.saved_bytes)}"
  ]
  lines.concat(tool_breakdown_lines(snapshot.tool_breakdown))
  lines.join("\n")
end