File size: 14,528 Bytes
2a2e170 754345f 2a2e170 754345f 2a2e170 7867a7a 2a2e170 7867a7a 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 754345f 2a2e170 ff8c636 754345f ff8c636 754345f ff8c636 754345f ff8c636 754345f ff8c636 2715896 754345f 2715896 754345f 2715896 754345f 2715896 754345f 2715896 2a2e170 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | """All agent observability in one module.
Every telemetry signal the agent emits β LLM-call usage / cost, hf_jobs
lifecycle, sandbox lifecycle, user feedback, mid-turn heartbeat saves β is
defined here so business-logic files stay free of instrumentation noise.
Callsites are one-liners::
await telemetry.record_llm_call(session, model=..., response=r, ...)
await telemetry.record_hf_job_submit(session, job, args, image=..., job_type="Python")
HeartbeatSaver.maybe_fire(session)
All ``record_*`` functions emit a single ``Event`` via ``session.send_event``
and never raise β telemetry is best-effort and must not break the agent.
"""
from __future__ import annotations
import asyncio
import logging
import time
from typing import Any
logger = logging.getLogger(__name__)
# ββ usage extraction ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def extract_usage(response_or_chunk: Any) -> dict:
"""Flat usage dict from a litellm response or final-chunk usage object.
Normalizes across providers: Anthropic exposes cache tokens as
``cache_read_input_tokens`` / ``cache_creation_input_tokens``; OpenAI uses
``prompt_tokens_details.cached_tokens``. Exposed under the stable keys
``cache_read_tokens`` / ``cache_creation_tokens``.
"""
u = getattr(response_or_chunk, "usage", None)
if u is None and isinstance(response_or_chunk, dict):
u = response_or_chunk.get("usage")
if u is None:
return {}
def _g(name, default=0):
if isinstance(u, dict):
return u.get(name, default) or default
return getattr(u, name, default) or default
prompt = _g("prompt_tokens")
completion = _g("completion_tokens")
total = _g("total_tokens") or (prompt + completion)
cache_read = _g("cache_read_input_tokens")
cache_creation = _g("cache_creation_input_tokens")
if not cache_read:
details = _g("prompt_tokens_details", None)
if details is not None:
if isinstance(details, dict):
cache_read = details.get("cached_tokens", 0) or 0
else:
cache_read = getattr(details, "cached_tokens", 0) or 0
return {
"prompt_tokens": int(prompt),
"completion_tokens": int(completion),
"total_tokens": int(total),
"cache_read_tokens": int(cache_read),
"cache_creation_tokens": int(cache_creation),
}
# ββ llm_call ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def record_llm_call(
session: Any,
*,
model: str,
response: Any = None,
latency_ms: int,
finish_reason: str | None,
kind: str = "main",
) -> dict:
"""Emit an ``llm_call`` event and return the extracted usage dict so
callers can stash it on their result object if they want.
``kind`` tags the call site so downstream analytics can break spend
down by category. Values currently emitted by the codebase:
* ``main`` β agent loop turn (user-facing reply or tool follow-up)
* ``research`` β research sub-agent inner loop (3 call sites)
* ``compaction`` β context-window summary on overflow
* ``effort_probe``β effort cascade walk on rejection / model switch
* ``restore`` β session re-seed summary after a Space restart
Pre-2026-04-29 only ``main`` calls were instrumented; observed gap on
Cost Explorer was ~67%, with the other 5 call sites accounting for
the rest. Tagging lets us split the dataset's ``total_cost_usd`` by
category and validate against AWS billing.
The ``/title`` (HF Router, not Bedrock) and ``/health/llm`` (diagnostic
endpoint, no session context) call sites are intentionally not
instrumented β together they're <1% of spend.
"""
usage = extract_usage(response) if response is not None else {}
cost_usd = 0.0
if response is not None:
try:
from litellm import completion_cost
cost_usd = float(completion_cost(completion_response=response) or 0.0)
except Exception:
cost_usd = 0.0
from agent.core.session import Event # local import to avoid cycle
try:
await session.send_event(
Event(
event_type="llm_call",
data={
"model": model,
"latency_ms": latency_ms,
"finish_reason": finish_reason,
"cost_usd": cost_usd,
"kind": kind,
**usage,
},
)
)
except Exception as e:
logger.debug("record_llm_call failed (non-fatal): %s", e)
return usage
# ββ hf_jobs ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _infer_push_to_hub(script_or_cmd: Any) -> bool:
if not isinstance(script_or_cmd, str):
return False
return (
"push_to_hub=True" in script_or_cmd
or "push_to_hub=true" in script_or_cmd
or "hub_model_id" in script_or_cmd
)
async def record_hf_job_submit(
session: Any,
job: Any,
args: dict,
*,
image: str,
job_type: str,
) -> float:
"""Emit ``hf_job_submit``. Returns the monotonic start timestamp so the
caller can pass it back into :func:`record_hf_job_complete`."""
from agent.core.session import Event
t_start = time.monotonic()
try:
script_text = args.get("script") or args.get("command") or ""
await session.send_event(
Event(
event_type="hf_job_submit",
data={
"job_id": getattr(job, "id", None),
"job_url": getattr(job, "url", None),
"flavor": args.get("hardware_flavor", "cpu-basic"),
"timeout": args.get("timeout", "30m"),
"job_type": job_type,
"image": image,
"namespace": args.get("namespace"),
"push_to_hub": _infer_push_to_hub(script_text),
},
)
)
except Exception as e:
logger.debug("record_hf_job_submit failed (non-fatal): %s", e)
return t_start
async def record_hf_job_complete(
session: Any,
job: Any,
*,
flavor: str,
final_status: str,
submit_ts: float,
) -> None:
from agent.core.session import Event
try:
wall_time_s = int(time.monotonic() - submit_ts)
await session.send_event(
Event(
event_type="hf_job_complete",
data={
"job_id": getattr(job, "id", None),
"flavor": flavor,
"final_status": final_status,
"wall_time_s": wall_time_s,
},
)
)
except Exception as e:
logger.debug("record_hf_job_complete failed (non-fatal): %s", e)
# ββ sandbox βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def record_sandbox_create(
session: Any,
sandbox: Any,
*,
hardware: str,
create_latency_s: int,
) -> None:
from agent.core.session import Event
try:
# Pin created-at on the session so record_sandbox_destroy can diff.
session._sandbox_created_at = time.monotonic() - create_latency_s
await session.send_event(
Event(
event_type="sandbox_create",
data={
"sandbox_id": getattr(sandbox, "space_id", None),
"hardware": hardware,
"create_latency_s": int(create_latency_s),
},
)
)
except Exception as e:
logger.debug("record_sandbox_create failed (non-fatal): %s", e)
async def record_sandbox_destroy(session: Any, sandbox: Any) -> None:
from agent.core.session import Event
try:
created = getattr(session, "_sandbox_created_at", None)
lifetime_s = int(time.monotonic() - created) if created else None
await session.send_event(
Event(
event_type="sandbox_destroy",
data={
"sandbox_id": getattr(sandbox, "space_id", None),
"lifetime_s": lifetime_s,
},
)
)
except Exception as e:
logger.debug("record_sandbox_destroy failed (non-fatal): %s", e)
# ββ feedback βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async def record_feedback(
session: Any,
*,
rating: str,
turn_index: int | None = None,
message_id: str | None = None,
comment: str | None = None,
) -> None:
from agent.core.session import Event
try:
await session.send_event(
Event(
event_type="feedback",
data={
"rating": rating,
"turn_index": turn_index,
"message_id": message_id,
"comment": (comment or "")[:500],
},
)
)
except Exception as e:
logger.debug("record_feedback failed (non-fatal): %s", e)
async def record_jobs_access_blocked(
session: Any,
*,
tool_call_ids: list[str],
plan: str,
eligible_namespaces: list[str],
) -> None:
from agent.core.session import Event
try:
await session.send_event(
Event(
event_type="jobs_access_blocked",
data={
"tool_call_ids": tool_call_ids,
"plan": plan,
"eligible_namespaces": eligible_namespaces,
},
)
)
except Exception as e:
logger.debug("record_jobs_access_blocked failed (non-fatal): %s", e)
async def record_pro_cta_click(
session: Any,
*,
source: str,
target: str = "pro_pricing",
) -> None:
from agent.core.session import Event
try:
await session.send_event(
Event(
event_type="pro_cta_click",
data={"source": source, "target": target},
)
)
except Exception as e:
logger.debug("record_pro_cta_click failed (non-fatal): %s", e)
async def record_pro_conversion(
session: Any,
*,
first_seen_at: str | None = None,
) -> None:
"""Emit a ``pro_conversion`` event for a user we've previously observed
as non-Pro and now see as Pro for the first time. Detected upstream in
``MongoSessionStore.mark_pro_seen``; fired into the user's first Pro
session so the rollup picks it up alongside other event-driven KPIs."""
from agent.core.session import Event
try:
await session.send_event(
Event(
event_type="pro_conversion",
data={"first_seen_at": first_seen_at},
)
)
except Exception as e:
logger.debug("record_pro_conversion failed (non-fatal): %s", e)
async def record_credits_topped_up(
session: Any,
*,
namespace: str | None = None,
) -> None:
"""Emit a ``credits_topped_up`` event when an hf_job submits successfully
in a session that previously hit ``jobs_access_blocked`` β i.e. the user
came back from the HF billing top-up flow and unblocked themselves.
Caller is responsible for firing this at most once per session."""
from agent.core.session import Event
try:
await session.send_event(
Event(
event_type="credits_topped_up",
data={"namespace": namespace},
)
)
except Exception as e:
logger.debug("record_credits_topped_up failed (non-fatal): %s", e)
# ββ heartbeat ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Module-level reference set for fire-and-forget heartbeat tasks. asyncio only
# keeps *weak* references to tasks, so the returned Task would otherwise be
# eligible for GC before running β the task gets discarded and the upload
# silently never happens. Hold strong refs until the task completes.
_heartbeat_tasks: set[asyncio.Task] = set()
class HeartbeatSaver:
"""Time-gated mid-turn flush.
Called from ``Session.send_event`` after every event. Fires
``save_and_upload_detached`` in a worker thread at most once per
``heartbeat_interval_s`` (default 60s). Guards against losing trace data
on long-running turns that crash before ``turn_complete``.
"""
@staticmethod
def maybe_fire(session: Any) -> None:
if not getattr(session.config, "save_sessions", False):
return
interval = getattr(session.config, "heartbeat_interval_s", 0) or 0
if interval <= 0:
return
now = time.monotonic()
last = getattr(session, "_last_heartbeat_ts", None)
if last is None:
# Initialise on first event; no save yet.
session._last_heartbeat_ts = now
return
if now - last < interval:
return
session._last_heartbeat_ts = now
repo_id = session.config.session_dataset_repo
try:
task = asyncio.get_running_loop().create_task(
asyncio.to_thread(session.save_and_upload_detached, repo_id)
)
# Hold a strong reference until the task finishes so asyncio can't
# GC it. ``set.discard`` is a no-op on missing keys β safe callback.
_heartbeat_tasks.add(task)
task.add_done_callback(_heartbeat_tasks.discard)
except RuntimeError:
try:
session.save_and_upload_detached(repo_id)
except Exception as e:
logger.debug("Heartbeat save failed (non-fatal): %s", e)
|