Skip to content
This repository was archived by the owner on May 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions agent/cost_ladder_wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def record_inference_from_response(
provider: Optional[str] = None,
base_url: Optional[str] = None,
api_mode: Optional[str] = None,
route: str = "unknown",
escalated_to_opus: bool = False,
) -> None:
"""Feed the cost-ladder estimator from an inference response.

Expand Down Expand Up @@ -129,6 +131,8 @@ def record_inference_from_response(
model_name=resolved_model,
provider=provider,
base_url=base_url,
route=route,
escalated_to_opus=escalated_to_opus,
)
except Exception as exc:
# Fail-soft per the contract — estimator failures must not
Expand Down
47 changes: 47 additions & 0 deletions agent/cost_state_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ def record_inference(
model_name: str,
provider: Optional[str] = None,
base_url: Optional[str] = None,
route: str = "unknown",
escalated_to_opus: bool = False,
) -> None:
"""Per-call estimator update.

Expand Down Expand Up @@ -305,13 +307,58 @@ def record_inference(
base_url: Optional base URL override; used when the SDK
is configured against a custom endpoint
(provider-fronted Anthropic, etc.).
route: KR-CHEAP-COST-TELEMETRY route label per
``kora_cli.telemetry.cost_telemetry`` taxonomy
(``slack_dm``, ``email_inbound``, ``mcp_tool``,
``alert_investigation``, ``probe_investigation``,
``tool_loop_iteration``, ``scheduled_task``,
``email_outbound_compose``, or ``unknown``).
Defaults to ``"unknown"`` so existing callers keep
working unchanged; they bucket into the unknown
route until tagged explicitly. Telemetry is a
READ-side observer of pricing; it does NOT affect
whether/how much is billed.
escalated_to_opus: Per-call escalation signal — Lock R3-3
tunable. When True, the telemetry counters increment
``escalation_count`` for this route in addition to
the normal call+token counters.
"""
cost_result = estimate_usage_cost(
model_name,
canonical_usage,
provider=provider,
base_url=base_url,
)

# KR-CHEAP-COST-TELEMETRY: tag the per-route counters. This
# is read-only telemetry — does NOT affect billing accumulation
# below. Fail-soft import + record so an unwired test path or
# an import-cycle scenario can't crash the inference handler.
cost_estimate_for_telemetry: Optional[float]
if cost_result.amount_usd is None:
cost_estimate_for_telemetry = None
else:
try:
cost_estimate_for_telemetry = float(cost_result.amount_usd)
except (TypeError, ValueError):
cost_estimate_for_telemetry = None
try:
from kora_cli.telemetry import get_telemetry

get_telemetry().record_call(
route=route,
model=model_name,
canonical_usage=canonical_usage,
cost_estimate_usd=cost_estimate_for_telemetry,
escalated_to_opus=escalated_to_opus,
)
except Exception as exc:
logger.debug(
"[kora.cost_ladder] telemetry record_call failed: %r — "
"billing accumulation continues",
exc,
)

if cost_result.amount_usd is None:
logger.debug(
"[kora.cost_ladder] no pricing for model=%s provider=%s "
Expand Down
7 changes: 7 additions & 0 deletions kora_cli/handlers/slack_dm_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ def _record_inference_to_cost_ladder(
),
model_name=str(model_name),
provider="anthropic",
# KR-CHEAP-COST-TELEMETRY — tag this Kora reply-bill
# under the slack_dm route. First iteration of a
# tool-use loop is attributed to the originating
# route (here slack_dm); iteration 2+ would attribute
# to ``tool_loop_iteration`` once the reasoning
# engine surfaces that signal (deferred follow-on).
route="slack_dm",
)
except Exception as exc:
logger.warning(
Expand Down
6 changes: 6 additions & 0 deletions kora_cli/listeners/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@
# upstream holders + the periodic-task scheduler are guaranteed
# registered before the snapshot task gets enqueued.
from kora_cli.listeners import snapshot_listener # noqa: F401
# KR-CHEAP-COST-TELEMETRY — per-route cost-counter persistence +
# window-reset tasks. Imported AFTER snapshot_listener so the
# heartbeat scheduler picks up the snapshot task first (snapshot
# reads telemetry; persist task writes telemetry — order doesn't
# matter for correctness, only for boot-log readability).
from kora_cli.listeners import cost_telemetry_listener # noqa: F401
Loading