fix(backend): use actual provider in persist_and_record_usage cost logging

The provider field was hardcoded to "open_router" for all PlatformCostLog
entries, even when the SDK (Anthropic) path was the caller. Add a provider
parameter that defaults to "open_router" for backward compatibility and
pass "anthropic" from the SDK service layer.
This commit is contained in:
Zamil Majdy
2026-04-03 14:37:47 +02:00
parent 960f893295
commit 71315aa982
2 changed files with 4 additions and 1 deletions

View File

@@ -2362,6 +2362,7 @@ async def stream_chat_completion_sdk(
log_prefix=log_prefix,
cost_usd=turn_cost_usd,
model=config.model,
provider="anthropic",
)
# --- Persist session messages ---

View File

@@ -30,6 +30,7 @@ async def persist_and_record_usage(
log_prefix: str = "",
cost_usd: float | str | None = None,
model: str | None = None,
provider: str = "open_router",
) -> int:
"""Persist token usage to session and record for rate limiting.
@@ -42,6 +43,7 @@ async def persist_and_record_usage(
cache_creation_tokens: Tokens written to prompt cache (Anthropic only).
log_prefix: Prefix for log messages (e.g. "[SDK]", "[Baseline]").
cost_usd: Optional cost for logging (float from SDK, str otherwise).
provider: Cost provider name (e.g. "anthropic", "open_router").
Returns:
The computed total_tokens (prompt + completion; cache excluded).
@@ -126,7 +128,7 @@ async def persist_and_record_usage(
graph_exec_id=session_id,
block_id="copilot",
block_name=f"copilot:{log_prefix.strip(' []')}".rstrip(":"),
provider="open_router",
provider=provider,
credential_id="copilot_system",
cost_microdollars=cost_microdollars,
input_tokens=prompt_tokens,