From b9e0928aefec1998987d7eeef70044e3176a8370 Mon Sep 17 00:00:00 2001 From: Elliot Slusky <44592435+ElliotSlusky@users.noreply.github.com> Date: Wed, 5 Aug 2026 20:51:06 -0700 Subject: [PATCH] fix(telemetry): record cloud inference cost (#704) --- src/openjarvis/telemetry/instrumented_engine.py | 1 + tests/telemetry/test_instrumented_engine.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/openjarvis/telemetry/instrumented_engine.py b/src/openjarvis/telemetry/instrumented_engine.py index 2df14634..e0007768 100644 --- a/src/openjarvis/telemetry/instrumented_engine.py +++ b/src/openjarvis/telemetry/instrumented_engine.py @@ -212,6 +212,7 @@ class InstrumentedEngine(InferenceEngine): completion_tokens=completion_tokens, total_tokens=prompt_tok + completion_tokens, latency_seconds=latency, + cost_usd=result.get("cost_usd", 0.0), ttft=ttft, throughput_tok_per_sec=throughput, energy_per_output_token_joules=energy_per_output_token, diff --git a/tests/telemetry/test_instrumented_engine.py b/tests/telemetry/test_instrumented_engine.py index 50e23965..b21169f8 100644 --- a/tests/telemetry/test_instrumented_engine.py +++ b/tests/telemetry/test_instrumented_engine.py @@ -71,6 +71,17 @@ class TestInstrumentedEngine: assert record.prompt_tokens == 10 assert record.completion_tokens == 5 + def test_generate_records_cost(self, mock_engine, bus): + mock_engine.generate.return_value["cost_usd"] = 0.0015 + ie = InstrumentedEngine(mock_engine, bus) + messages = [Message(role=Role.USER, content="Hi")] + ie.generate(messages, model="test") + + event = next( + e for e in bus.history if e.event_type == EventType.TELEMETRY_RECORD + ) + assert event.data["record"].cost_usd == pytest.approx(0.0015) + def test_list_models_delegates(self, mock_engine, bus): ie = InstrumentedEngine(mock_engine, bus) assert ie.list_models() == ["test-model"]