active_harness 0.2.12 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/active_harness/agent/cost.rb +28 -0
- data/lib/active_harness/agent.rb +4 -1
- data/lib/active_harness/costs.rb +14 -3
- data/lib/active_harness/result.rb +2 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0c5339e68dc9f7de87fc67a4e986cd1858e86f52e95ee277ee0f12563eedf428
|
|
4
|
+
data.tar.gz: d563d7088e6b29c2119160a9eb0e0a39eb550d12e1bf828937a7d20fddcf7ad4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9d710fedd64ae1bd1ee40616aacb884e698491ec223895651c1ad28a307121575f5078df54074e3218ade227cf376f20a881af15ecef640e63f74887cd5a9115
|
|
7
|
+
data.tar.gz: '075778aea6ae0b53624b18fd64835cdaaec6e0381ff7616faab86c2b9813d5ae80830bdcf59ce71b255a13930ad6d35f99b1953bde11e58c7a8bda03c36732df'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module ActiveHarness
|
|
2
|
+
class Agent
|
|
3
|
+
private
|
|
4
|
+
|
|
5
|
+
# Calculates the monetary cost of a single request based on token usage
|
|
6
|
+
# and pricing data from ActiveHarness::Costs.
|
|
7
|
+
#
|
|
8
|
+
# Returns a hash { input_cost:, output_cost:, total_cost: } in USD,
|
|
9
|
+
# or nil if usage is absent or the model is not found in the pricing registry.
|
|
10
|
+
def calculate_cost(model_id, usage)
|
|
11
|
+
return nil if model_id.nil? || usage.nil?
|
|
12
|
+
|
|
13
|
+
pricing = ActiveHarness::Costs.find(model_id.to_s)
|
|
14
|
+
return nil unless pricing&.input_per_million && pricing&.output_per_million
|
|
15
|
+
|
|
16
|
+
input_cost = (usage[:input_tokens].to_f / 1_000_000) * pricing.input_per_million
|
|
17
|
+
output_cost = (usage[:output_tokens].to_f / 1_000_000) * pricing.output_per_million
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
input_cost: input_cost.round(8),
|
|
21
|
+
output_cost: output_cost.round(8),
|
|
22
|
+
total_cost: (input_cost + output_cost).round(8)
|
|
23
|
+
}
|
|
24
|
+
rescue StandardError
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/active_harness/agent.rb
CHANGED
|
@@ -113,6 +113,7 @@ module ActiveHarness
|
|
|
113
113
|
def build_result(response, entry, attempts, elapsed)
|
|
114
114
|
raw = response[:content]
|
|
115
115
|
parsed = parse_output(raw)
|
|
116
|
+
usage = response[:usage]
|
|
116
117
|
|
|
117
118
|
Result.new(
|
|
118
119
|
input: @input,
|
|
@@ -125,7 +126,8 @@ module ActiveHarness
|
|
|
125
126
|
model_list: model_list,
|
|
126
127
|
attempts: attempts,
|
|
127
128
|
execution_time: elapsed,
|
|
128
|
-
usage:
|
|
129
|
+
usage: usage,
|
|
130
|
+
cost: calculate_cost(entry[:model], usage)
|
|
129
131
|
)
|
|
130
132
|
end
|
|
131
133
|
|
|
@@ -152,4 +154,5 @@ require_relative "agent/models"
|
|
|
152
154
|
require_relative "agent/providers"
|
|
153
155
|
require_relative "agent/output_parser"
|
|
154
156
|
require_relative "agent/ruby_llm_backend"
|
|
157
|
+
require_relative "agent/cost"
|
|
155
158
|
|
data/lib/active_harness/costs.rb
CHANGED
|
@@ -183,10 +183,21 @@ module ActiveHarness
|
|
|
183
183
|
end
|
|
184
184
|
|
|
185
185
|
def registry
|
|
186
|
-
@registry ||=
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
@registry ||= load_registry
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def load_registry
|
|
190
|
+
if File.exist?(cache_file)
|
|
191
|
+
begin
|
|
192
|
+
data = JSON.parse(File.read(cache_file), symbolize_names: true)
|
|
193
|
+
return data if data.is_a?(Array)
|
|
194
|
+
rescue JSON::ParserError
|
|
195
|
+
# Cache file corrupted — fall through to bundled data
|
|
196
|
+
end
|
|
189
197
|
end
|
|
198
|
+
JSON.parse(File.read(BUNDLED_DATA_FILE), symbolize_names: true)
|
|
199
|
+
rescue JSON::ParserError, Errno::ENOENT
|
|
200
|
+
[]
|
|
190
201
|
end
|
|
191
202
|
|
|
192
203
|
def fetch_models_dev
|
|
@@ -6,5 +6,6 @@ module ActiveHarness
|
|
|
6
6
|
# output — raw string from the provider
|
|
7
7
|
# parsed — for format :json: a Ruby Hash/Array; for format :text: same as output
|
|
8
8
|
# usage — token counts: { input_tokens:, output_tokens:, total_tokens: } or nil for streaming
|
|
9
|
-
|
|
9
|
+
# cost — { input_cost:, output_cost:, total_cost: } in USD, or nil if pricing unavailable
|
|
10
|
+
Result = Struct.new(:input, :output, :parsed, :system_prompt, :provider, :model, :temperature, :model_list, :attempts, :execution_time, :usage, :cost, keyword_init: true)
|
|
10
11
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_harness
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.13
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- the-teacher
|
|
@@ -33,6 +33,7 @@ extra_rdoc_files: []
|
|
|
33
33
|
files:
|
|
34
34
|
- lib/active_harness.rb
|
|
35
35
|
- lib/active_harness/agent.rb
|
|
36
|
+
- lib/active_harness/agent/cost.rb
|
|
36
37
|
- lib/active_harness/agent/hooks.rb
|
|
37
38
|
- lib/active_harness/agent/models.rb
|
|
38
39
|
- lib/active_harness/agent/output_parser.rb
|