Datasets:
Download src/tracing.py from tesraghavan/agent-trace: direct link, hf CLI and curl.
- Browser
- Download file 7.01 kB
-
https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/src/tracing.py
- Command line
-
hf download hf://datasets/tesraghavan/agent-trace/src/tracing.py
-
curl -L -o tracing.py https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/src/tracing.py
7.01 kB
| """Trace collection and span data structures.""" | |
| import json | |
| import platform | |
| import sys | |
| import uuid | |
| from dataclasses import dataclass, field, asdict | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from typing import Any | |
| from .run_metadata import COLLECTOR_VERSION, SCHEMA_VERSION | |
| class Telemetry: | |
| """Resource usage telemetry for a tool execution.""" | |
| user_time_s: float = 0.0 | |
| system_time_s: float = 0.0 | |
| max_rss_bytes: int = 0 | |
| read_bytes: int = 0 | |
| write_bytes: int = 0 | |
| class Span: | |
| """A single tool execution span.""" | |
| span_id: str | |
| type: str # "TOOL" for now | |
| tool_name: str | |
| tool_input: str | |
| tool_output: str | |
| start_ns: int | |
| end_ns: int | |
| duration_ms: float | |
| telemetry: Telemetry | |
| exit_code: int = 0 | |
| parent_span_id: str | None = None | |
| def to_dict(self) -> dict[str, Any]: | |
| d = asdict(self) | |
| return d | |
| class LLMStep: | |
| """A single LLM generation step.""" | |
| step_id: str | |
| step_number: int | |
| model_output: str | None # Raw model output (content field) | |
| reasoning_content: str | None # Reasoning/thinking (if available) | |
| tool_calls: list[dict[str, Any]] | None # Parsed tool calls | |
| duration_ms: float | None | |
| input_tokens: int | None | |
| output_tokens: int | None | |
| def to_dict(self) -> dict[str, Any]: | |
| return asdict(self) | |
| class Trace: | |
| """A complete conversation trace with tool spans and LLM steps.""" | |
| trace_id: str | |
| timestamp_utc: str | |
| prompt: str | |
| model: str | |
| spans: list[Span] = field(default_factory=list) | |
| llm_steps: list[LLMStep] = field(default_factory=list) | |
| total_duration_ms: float = 0.0 | |
| metadata: dict[str, Any] = field(default_factory=dict) | |
| def to_dict(self) -> dict[str, Any]: | |
| return { | |
| "trace_id": self.trace_id, | |
| "timestamp_utc": self.timestamp_utc, | |
| "prompt": self.prompt, | |
| "model": self.model, | |
| "spans": [s.to_dict() for s in self.spans], | |
| "llm_steps": [s.to_dict() for s in self.llm_steps], | |
| "total_duration_ms": self.total_duration_ms, | |
| "metadata": self.metadata, | |
| } | |
| class TraceCollector: | |
| """Collects spans and builds a trace.""" | |
| def __init__(self, prompt: str, model: str): | |
| self.trace_id = str(uuid.uuid4()) | |
| self.prompt = prompt | |
| self.model = model | |
| self.spans: list[Span] = [] | |
| self.llm_steps: list[LLMStep] = [] | |
| self.start_time_ns: int | None = None | |
| self.end_time_ns: int | None = None | |
| self.extra_metadata: dict[str, Any] = {} | |
| self._reasoning_buffer: list[str] = [] # Captured from live model responses | |
| def record_span(self, span: Span) -> None: | |
| """Record a tool execution span.""" | |
| self.spans.append(span) | |
| def record_reasoning(self, reasoning: str) -> None: | |
| """Buffer reasoning_content captured from the live model response.""" | |
| self._reasoning_buffer.append(reasoning) | |
| def record_llm_steps_from_result(self, result: Any) -> None: | |
| """Extract and record LLM steps from smolagents RunResult. | |
| Uses reasoning_content from the live-capture buffer (populated by | |
| instrument_model) since smolagents' serialization drops it. | |
| """ | |
| if not hasattr(result, 'steps') or not result.steps: | |
| return | |
| reasoning_idx = 0 | |
| for step in result.steps: | |
| if not isinstance(step, dict): | |
| continue | |
| # Skip task steps (step 0) | |
| step_number = step.get('step_number') | |
| if step_number is None: | |
| continue | |
| # Extract timing | |
| timing = step.get('timing', {}) | |
| duration_ms = None | |
| if isinstance(timing, dict): | |
| duration_ms = timing.get('duration_ms') or timing.get('total_ms') | |
| # Extract token usage | |
| token_usage = step.get('token_usage', {}) | |
| input_tokens = token_usage.get('input_tokens') if token_usage else None | |
| output_tokens = token_usage.get('output_tokens') if token_usage else None | |
| # Extract model output | |
| model_output = None | |
| tool_calls_data = None | |
| msg = step.get('model_output_message') | |
| if msg and isinstance(msg, dict): | |
| model_output = msg.get('content') | |
| # Extract tool calls | |
| tc = msg.get('tool_calls') | |
| if tc: | |
| tool_calls_data = [ | |
| {"name": t.get('function', {}).get('name'), | |
| "arguments": t.get('function', {}).get('arguments')} | |
| for t in tc if isinstance(t, dict) | |
| ] | |
| # Use buffered reasoning (captured from live Pydantic objects) | |
| reasoning_content = None | |
| if reasoning_idx < len(self._reasoning_buffer): | |
| reasoning_content = self._reasoning_buffer[reasoning_idx] | |
| reasoning_idx += 1 | |
| llm_step = LLMStep( | |
| step_id=str(uuid.uuid4()), | |
| step_number=step_number, | |
| model_output=model_output, | |
| reasoning_content=reasoning_content, | |
| tool_calls=tool_calls_data, | |
| duration_ms=duration_ms, | |
| input_tokens=input_tokens, | |
| output_tokens=output_tokens, | |
| ) | |
| self.llm_steps.append(llm_step) | |
| def start(self) -> None: | |
| """Mark the start of trace collection.""" | |
| import time | |
| self.start_time_ns = time.perf_counter_ns() | |
| def stop(self) -> None: | |
| """Mark the end of trace collection.""" | |
| import time | |
| self.end_time_ns = time.perf_counter_ns() | |
| def build_trace(self) -> Trace: | |
| """Build the final trace object.""" | |
| total_duration_ms = 0.0 | |
| if self.start_time_ns and self.end_time_ns: | |
| total_duration_ms = (self.end_time_ns - self.start_time_ns) / 1_000_000 | |
| metadata = { | |
| "schema_version": SCHEMA_VERSION, | |
| "collector_version": COLLECTOR_VERSION, | |
| "python_version": platform.python_version(), | |
| "platform": f"{sys.platform}-{platform.machine()}", | |
| **self.extra_metadata, | |
| } | |
| return Trace( | |
| trace_id=self.trace_id, | |
| timestamp_utc=datetime.now(timezone.utc).isoformat(), | |
| prompt=self.prompt, | |
| model=self.model, | |
| spans=self.spans, | |
| llm_steps=self.llm_steps, | |
| total_duration_ms=total_duration_ms, | |
| metadata=metadata, | |
| ) | |
| def save(self, path: str | Path) -> None: | |
| """Save the trace to a JSONL file (append mode).""" | |
| trace = self.build_trace() | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with open(path, "a") as f: | |
| f.write(json.dumps(trace.to_dict()) + "\n") | |