Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Fix/cost reporting (#363)

Louis Choquel30470e86216f
diff --git a/pipelex/cogt/usage/cost_registry.py b/pipelex/cogt/usage/cost_registry.py
index 87010bfa4..3da2ce91b 100644
--- a/pipelex/cogt/usage/cost_registry.py
+++ b/pipelex/cogt/usage/cost_registry.py
@@ -167,8 +167,15 @@ class CostRegistry(RootModel[CostRegistryRoot]):
if not records:
return
+ # Collect all unique field names across all records
+ all_fieldnames: set[str] = set()
+ for record in records:
+ all_fieldnames.update(record.keys())
+
+ # Sort fieldnames for consistent column order
+ fieldnames = sorted(all_fieldnames)
+
with open(file_path, "w", newline="", encoding="utf-8") as csvfile:
- fieldnames = records[0].keys()
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(records)
diff --git a/pyproject.toml b/pyproject.toml
index a51d34f97..771783435 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -21,7 +21,7 @@ dependencies = [
"aiofiles>=23.2.1",
"backports.strenum>=1.3.0 ; python_version < '3.11'",
"filetype>=1.2.0",
- "httpx>=0.27.0",
+ "httpx>=0.23.0,<1.0.0",
"instructor>=1.8.3,<1.11.0", # 1.11.x causes typing errors with mypy
"jinja2>=3.1.4",
"json2html>=1.3.0",
diff --git a/tests/unit/pipelex/cogt/usage/test_cost_registry.py b/tests/unit/pipelex/cogt/usage/test_cost_registry.py
index 12aa95737..6a2ba78c2 100644
--- a/tests/unit/pipelex/cogt/usage/test_cost_registry.py
+++ b/tests/unit/pipelex/cogt/usage/test_cost_registry.py
@@ -106,6 +106,74 @@ class TestCostRegistry:
# File should not be created for empty records
assert not csv_file.exists()
+ def test_save_to_csv_with_varying_fields(self, tmp_path: Path):
+ """Test that save_to_csv handles records with different fields correctly.
+
+ This test ensures the bug where different records have different fields
+ (e.g., some with output_audio, output_reasoning tokens) is caught.
+ """
+ # Prepare test data with varying fields across records
+ records: list[dict[str, Any]] = [
+ {
+ LLMTokenCostReportField.LLM_NAME: "model-standard",
+ LLMTokenCostReportField.NB_TOKENS_INPUT_CACHED: 100,
+ LLMTokenCostReportField.COST_INPUT_CACHED: 0.10,
+ },
+ {
+ LLMTokenCostReportField.LLM_NAME: "model-with-audio",
+ LLMTokenCostReportField.NB_TOKENS_INPUT_CACHED: 50,
+ LLMTokenCostReportField.COST_INPUT_CACHED: 0.05,
+ "nb_tokens_output_audio": 200, # Extra field not in first record
+ "cost_output_audio": 0.20, # Extra field not in first record
+ },
+ {
+ LLMTokenCostReportField.LLM_NAME: "model-with-reasoning",
+ LLMTokenCostReportField.NB_TOKENS_INPUT_CACHED: 75,
+ LLMTokenCostReportField.COST_INPUT_CACHED: 0.075,
+ "nb_tokens_output_reasoning": 150, # Extra field not in other records
+ "cost_output_reasoning": 0.15, # Extra field not in other records
+ },
+ ]
+
+ # Save to CSV - should handle varying fields without errors
+ csv_file = tmp_path / "varying_fields_report.csv"
+ CostRegistry.save_to_csv(records, str(csv_file))
+
+ # Verify file exists
+ assert csv_file.exists()
+
+ # Read and verify all fields are present in headers
+ with open(csv_file, encoding="utf-8") as file:
+ reader = csv.DictReader(file)
+ rows = list(reader)
+
+ # Should have 3 rows
+ assert len(rows) == 3
+
+ # Verify all fields are in the CSV (even if some rows have empty values)
+ assert LLMTokenCostReportField.LLM_NAME in rows[0]
+ assert LLMTokenCostReportField.NB_TOKENS_INPUT_CACHED in rows[0]
+ assert "nb_tokens_output_audio" in rows[0]
+ assert "nb_tokens_output_reasoning" in rows[0]
+ assert "cost_output_audio" in rows[0]
+ assert "cost_output_reasoning" in rows[0]
+
+ # Verify first row values (should have empty strings for missing fields)
+ assert rows[0][LLMTokenCostReportField.LLM_NAME] == "model-standard"
+ assert rows[0][LLMTokenCostReportField.NB_TOKENS_INPUT_CACHED] == "100"
+ assert rows[0]["nb_tokens_output_audio"] == "" # Missing field = empty string
+ assert rows[0]["nb_tokens_output_reasoning"] == "" # Missing field = empty string
+
+ # Verify second row has its extra field
+ assert rows[1][LLMTokenCostReportField.LLM_NAME] == "model-with-audio"
+ assert rows[1]["nb_tokens_output_audio"] == "200"
+ assert rows[1]["cost_output_audio"] == "0.2"
+
+ # Verify third row has its extra field
+ assert rows[2][LLMTokenCostReportField.LLM_NAME] == "model-with-reasoning"
+ assert rows[2]["nb_tokens_output_reasoning"] == "150"
+ assert rows[2]["cost_output_reasoning"] == "0.15"
+
def test_generate_report_aggregation(self, mocker: MockerFixture, tmp_path: Path):
"""Test groupby logic and cost calculations are correct."""
# Mock console output to avoid printing during tests
diff --git a/uv.lock b/uv.lock
index 1615d2ccb..54efef1c5 100644
--- a/uv.lock
+++ b/uv.lock
@@ -2157,7 +2157,7 @@ requires-dist = [
{ name = "filetype", specifier = ">=1.2.0" },
{ name = "google-auth-oauthlib", marker = "extra == 'google'", specifier = ">=1.2.1" },
{ name = "google-genai", marker = "extra == 'google-genai'" },
- { name = "httpx", specifier = ">=0.27.0" },
+ { name = "httpx", specifier = ">=0.23.0,<1.0.0" },
{ name = "instructor", specifier = ">=1.8.3,<1.11.0" },
{ name = "instructor", extras = ["google-genai"], marker = "extra == 'google-genai'" },
{ name = "jinja2", specifier = ">=3.1.4" },