Python
Trajectory keeps the partial cost total, marked incomplete
Rohit C Prasad9c05cb388d5e
diff --git a/coworker/headless/trajectory.py b/coworker/headless/trajectory.pyindex 3c1d743f..1759b9d5 100644--- a/coworker/headless/trajectory.py+++ b/coworker/headless/trajectory.py@@ -199,8 +199,11 @@ def build_trajectory("total_steps": len(steps),"extra": {"outcome": outcome},}- if cost_known and not cost_unknown:+ if cost_known:+ # The priced part of the run. When a call could not be priced (unknown model,+ # or no usage block) the total is a lower bound, and `cost_complete` says so.final_metrics["total_cost_usd"] = round(cost_total, 6)+ final_metrics["extra"]["cost_complete"] = not cost_unknownreturn {"schema_version": SCHEMA_VERSION,diff --git a/tests/test_headless_trajectory.py b/tests/test_headless_trajectory.pyindex 73f53f5c..d2b64ef6 100644--- a/tests/test_headless_trajectory.py+++ b/tests/test_headless_trajectory.py@@ -89,6 +89,7 @@ def test_agent_steps_carry_raw_counts_host_and_a_priced_total():# Sonnet 5: 1000*2 + 100*10 + 500*0.2 + 200*2.5 per million.assert step["metrics"]["cost_usd"] == round((2000 + 1000 + 100 + 500) / 1e6, 6)assert traj["final_metrics"]["total_cost_usd"] == step["metrics"]["cost_usd"]+ assert traj["final_metrics"]["extra"]["cost_complete"] is Truedef test_an_unpriced_model_gets_counts_but_no_cost():@@ -103,7 +104,7 @@ def test_an_unpriced_model_gets_counts_but_no_cost():assert "total_cost_usd" not in traj["final_metrics"]-def test_mixed_priced_and_unpriced_calls_omit_total_cost():+def test_mixed_priced_and_unpriced_calls_keep_a_partial_total_marked_incomplete():traj = build_trajectory([{"role": "assistant", "usage": {"input": 100, "model": "priced"}},@@ -119,10 +120,12 @@ def test_mixed_priced_and_unpriced_calls_omit_total_cost():assert traj["steps"][0]["metrics"]["cost_usd"] == 0.0001assert "cost_usd" not in traj["steps"][1]["metrics"]assert traj["final_metrics"]["total_prompt_tokens"] == 300- assert "total_cost_usd" not in traj["final_metrics"]+ # The priced part is reported, and the flag says it is not the whole run.+ assert traj["final_metrics"]["total_cost_usd"] == 0.0001+ assert traj["final_metrics"]["extra"]["cost_complete"] is False-def test_call_without_usage_omits_total_cost():+def test_call_without_usage_keeps_a_partial_total_marked_incomplete():traj = build_trajectory([{"role": "assistant", "usage": {"input": 100, "model": "priced"}},@@ -137,4 +140,5 @@ def test_call_without_usage_omits_total_cost():)assert traj["steps"][0]["metrics"]["cost_usd"] == 0.0001assert "metrics" not in traj["steps"][1]- assert "total_cost_usd" not in traj["final_metrics"]+ assert traj["final_metrics"]["total_cost_usd"] == 0.0001+ assert traj["final_metrics"]["extra"]["cost_complete"] is False