agentleFS
lauren-frameworklauren-framework/lauren-ai/skills/callback-tracing-handler/SKILL.md

callback-tracing-handler

Skill0 starsChanged 6 months agoMITView on GitHub

Subscribe to SignalBus lifecycle events to build lightweight tracers that record LLM call spans, tool call spans, and run summaries. Use when you need observability into agent execution without adding a full tracing backend, or when building cost/latency dashboards.

The file

Files
SKILL.md

Use codemap find "SignalBus" to locate the signal bus before reading.

Callback / Tracing Handler

Subscribe to lauren_ai._signals events via SignalBus.on() to build a simple tracer that records spans for every agent run.

Available signals

SignalWhen fired
ModelCallStartedBefore each LLM transport call
ModelCallCompleteAfter each LLM call (with usage + cost)
ToolCallStartedBefore each tool execution
ToolCallCompleteAfter each tool execution (with duration)
AgentRunCompleteWhen the full agent run terminates

Simple tracer

from lauren_ai import SignalBus, ModelCallComplete, AgentRunComplete

class SimpleTracer:
    def __init__(self, signal_bus: SignalBus):
        self._spans: list[dict] = []
        self._bus = signal_bus
        signal_bus.on(ModelCallComplete)(self._on_llm_call)
        signal_bus.on(AgentRunComplete)(self._on_run_complete)

    async def _on_llm_call(self, event: ModelCallComplete) -> None:
        self._spans.append({
            "type": "llm_call",
            "model": event.model,
            "cost_usd": event.cost_usd,
            "input_tokens": event.usage.input_tokens if event.usage else 0,
            "output_tokens": event.usage.output_tokens if event.usage else 0,
        })

    async def _on_run_complete(self, event: AgentRunComplete) -> None:
        self._spans.append({
            "type": "run_complete",
            "turns": event.turns,
            "total_cost": event.total_cost_usd,
        })

    @property
    def spans(self) -> list[dict]:
        return list(self._spans)

Wiring to AgentRunner

Pass the SignalBus instance to AgentRunnerBase:

from lauren_ai._agents._runner import AgentRunnerBase as AgentRunner
from lauren_ai._transport._mock import MockTransport
from lauren_ai._config import LLMConfig

bus = SignalBus()
tracer = SimpleTracer(bus)

mock = MockTransport()
cfg = LLMConfig(provider="anthropic", model="mock-model", api_key="mock")
runner = AgentRunner(transport=mock, signals=bus)

Tool tracing

Include tool spans for a complete picture:

from lauren_ai import ToolCallStarted, ToolCallComplete
import time

class DetailedTracer(SimpleTracer):
    def __init__(self, bus: SignalBus):
        super().__init__(bus)
        self._tool_starts: dict[str, float] = {}
        bus.on(ToolCallStarted)(self._on_tool_start)
        bus.on(ToolCallComplete)(self._on_tool_done)

    async def _on_tool_start(self, event: ToolCallStarted) -> None:
        self._tool_starts[event.tool_use_id] = time.monotonic()

    async def _on_tool_done(self, event: ToolCallComplete) -> None:
        self._spans.append({
            "type": "tool_call",
            "tool_name": event.tool_name,
            "success": event.success,
            "duration_ms": event.duration_ms,
        })

Cleanup

Clear handlers between tests to avoid accumulation:

bus.clear(ModelCallComplete)
bus.clear(AgentRunComplete)
# or clear all:
bus.clear()

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.