Skip to content

Personal AI, On Personal Devices

Python

fix(telemetry): record cloud inference cost (#704)

Elliot Sluskyb9e0928aefec
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"]