feat(platform): extract OpenRouter cost from baseline copilot path

The baseline copilot path uses the same OpenRouter API but wasn't
extracting the x-total-cost header. Now extracts cost from the
streaming response headers and passes it to persist_and_record_usage,
giving us actual USD cost for both copilot modes.
This commit is contained in:
Zamil Majdy
2026-04-02 18:39:55 +02:00
parent 993c43b623
commit 4bad08394c

View File

@@ -87,6 +87,7 @@ class _BaselineStreamState:
text_started: bool = False
turn_prompt_tokens: int = 0
turn_completion_tokens: int = 0
cost_usd: float | None = None
async def _baseline_llm_caller(
@@ -158,6 +159,16 @@ async def _baseline_llm_caller(
if tc.function and tc.function.arguments:
entry["arguments"] += tc.function.arguments
# Extract OpenRouter cost from response headers
try:
raw_resp = getattr(response, "response", None)
if raw_resp and hasattr(raw_resp, "headers"):
cost_header = raw_resp.headers.get("x-total-cost")
if cost_header:
state.cost_usd = float(cost_header)
except (ValueError, AttributeError):
pass
# Close text block
if state.text_started:
state.pending_events.append(StreamTextEnd(id=state.text_block_id))
@@ -585,6 +596,7 @@ async def stream_chat_completion_baseline(
prompt_tokens=state.turn_prompt_tokens,
completion_tokens=state.turn_completion_tokens,
log_prefix="[Baseline]",
cost_usd=state.cost_usd,
)
# Persist assistant response