aux_accounting.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Ambient session-accounting context for auxiliary LLM calls.
  2. Aux calls (vision, compression, title generation, web_extract, session_search, ...) go
  3. through ``agent.auxiliary_client`` which has no session handle, so their usage was
  4. historically discarded. The agent loop publishes ``(session_db, session_id)`` here
  5. (mirroring ``agent.portal_tags``) and the aux client records usage at its single
  6. response-validation chokepoint. ContextVar semantics isolate concurrent agents, propagate
  7. to worker threads via ``tools.thread_context`` and to asyncio tasks automatically.
  8. """
  9. from __future__ import annotations
  10. import logging
  11. from contextvars import ContextVar
  12. from typing import Any, Optional
  13. logger = logging.getLogger(__name__)
  14. # (session_db, session_id) for the active agent turn, or None outside one.
  15. _accounting: ContextVar[Optional[tuple]] = ContextVar("aux_accounting_context", default=None)
  16. # MoA advisor/aggregator usage is already folded into conversation_loop's
  17. # update_token_counts delta (tokens AND cost); recording it here would double-count.
  18. _EXCLUDED_TASKS = frozenset({"moa_reference", "moa_aggregator"})
  19. def set_accounting_context(session_db: Any, session_id: Optional[str]):
  20. """Publish the active session's accounting handles; returns the token for ``reset_accounting_context``.
  21. ``None`` handles (no DB / no session id) clear the context.
  22. """
  23. if session_db is None or not session_id:
  24. return _accounting.set(None)
  25. return _accounting.set((session_db, session_id))
  26. def reset_accounting_context(token) -> None:
  27. """Restore the previous accounting context (pair with ``set_...``)."""
  28. try:
  29. _accounting.reset(token)
  30. except Exception:
  31. _accounting.set(None)
  32. def record_aux_usage(
  33. response: Any, task: Optional[str], *, provider: Optional[str] = None,
  34. base_url: Optional[str] = None,
  35. ) -> None:
  36. """Record an auxiliary response's token usage against the ambient session.
  37. Strictly best-effort (accounting must never break an aux call). No-ops outside an
  38. agent turn, for main-loop-accounted tasks (``_EXCLUDED_TASKS``), or without usage.
  39. The model is read from ``response.model`` (accurate after aux provider fallback);
  40. *provider*/*base_url* reflect the originally-resolved route.
  41. """
  42. try:
  43. if not task or task in _EXCLUDED_TASKS:
  44. return
  45. ctx = _accounting.get()
  46. if ctx is None:
  47. return
  48. session_db, session_id = ctx
  49. raw_usage = getattr(response, "usage", None)
  50. if raw_usage is None:
  51. return
  52. from agent.usage_pricing import estimate_usage_cost, normalize_usage, with_served_service_tier
  53. usage = with_served_service_tier(normalize_usage(raw_usage, provider=provider), response)
  54. if not (
  55. usage.input_tokens or usage.output_tokens
  56. or usage.cache_read_tokens or usage.cache_write_tokens
  57. or usage.reasoning_tokens
  58. ):
  59. return
  60. model = str(getattr(response, "model", "") or "") or "unknown"
  61. estimated_cost = None
  62. try:
  63. cost = estimate_usage_cost(model, usage, provider=provider, base_url=base_url)
  64. if cost.amount_usd is not None:
  65. estimated_cost = float(cost.amount_usd)
  66. except Exception:
  67. logger.debug("Aux usage cost estimation failed", exc_info=True)
  68. # Inside a Relay-driven tool callback the thread runs Relay's loop; a worker store refuses
  69. # synchronous RPC there (#139029).
  70. from agent.runtime_session_store import call_off_event_loop
  71. call_off_event_loop(
  72. session_db.record_auxiliary_usage,
  73. session_id, task, model=model, billing_provider=provider, billing_base_url=base_url,
  74. input_tokens=usage.input_tokens, output_tokens=usage.output_tokens,
  75. cache_read_tokens=usage.cache_read_tokens, cache_write_tokens=usage.cache_write_tokens,
  76. reasoning_tokens=usage.reasoning_tokens, estimated_cost_usd=estimated_cost,
  77. )
  78. except Exception:
  79. logger.debug("Aux usage recording failed (non-fatal)", exc_info=True)