Events & Observability

Subscribe to lightweight runtime events for observability, progress reporting, and safety gates.

Overview

AbstractCore emits events for generation, tool execution, retries, sessions, and compaction. Use these hooks to build metrics, dashboards, or guardrails around tool usage without tying yourself to a specific provider.

Keep event handlers fast. If you need heavier work (I/O, uploads, analytics), enqueue to a background worker.

Global event handlers

Register a process-wide handler with on_global():

from abstractcore import create_llm
from abstractcore.events import EventType, on_global

def on_event(event):
    print(event.type.value, event.source, event.data)

on_global(EventType.GENERATION_COMPLETED, on_event)

llm = create_llm("ollama", model="qwen3:4b")
llm.generate("Say 'ok' and nothing else.")

Common event types

  • EventType.GENERATION_STARTED / EventType.GENERATION_COMPLETED
  • EventType.TOOL_STARTED / EventType.TOOL_PROGRESS / EventType.TOOL_COMPLETED
  • EventType.RETRY_ATTEMPTED / EventType.RETRY_EXHAUSTED
  • EventType.VALIDATION_FAILED
  • EventType.COMPACTION_STARTED / EventType.COMPACTION_COMPLETED

For the full list, see API Reference (Event System).

Async handlers

If you need async handlers, use GlobalEventBus.on_async(...) (or per-emitter on_async(...)) and emit events with the async emission methods. Most applications only need the sync hooks above.

Related Documentation

Structured Logging

Configure console/file logging and verbatim capture

Token Management

Budget validation and usage monitoring patterns

Tool Calling

Execution boundaries, approval gates, and tool safety

API Reference

Full API listing (incl. events)