AutoGen Observability & Recovery (Python)
Logging, OpenTelemetry tracing, token accounting, cancellation and state checkpoints for Microsoft AutoGen 0.7 teams.
Microsoft AutoGen (
autogen-agentchat0.7.5, checked 2026-11-19) — not AG2. The project is archived; prefer Microsoft Agent Framework for new deployments.
Logging
Section titled “Logging”AutoGen logs through the standard logging module under well-known logger names:
import logging
from autogen_agentchat import EVENT_LOGGER_NAME as AGENTCHAT_EVENTS, TRACE_LOGGER_NAME as AGENTCHAT_TRACEfrom autogen_core import EVENT_LOGGER_NAME as CORE_EVENTS
logging.basicConfig(level=logging.WARNING)logging.getLogger(AGENTCHAT_TRACE).setLevel(logging.DEBUG) # human-readable debug tracelogging.getLogger(AGENTCHAT_EVENTS).setLevel(logging.INFO) # structured agent/team eventslogging.getLogger(CORE_EVENTS).setLevel(logging.INFO) # LLM calls, tool calls, message deliveryEvent loggers emit structured records (for example LLM call events with prompt and completion token counts), suitable for shipping to a log pipeline.
OpenTelemetry tracing
Section titled “OpenTelemetry tracing”The Core runtime emits OpenTelemetry spans for message delivery, agent handlers and tool execution. Give it a tracer provider and let the team use that runtime:
from opentelemetry import tracefrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from autogen_agentchat.teams import RoundRobinGroupChatfrom autogen_core import SingleThreadedAgentRuntime
provider = TracerProvider()provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter())) # swap for an OTLP exportertrace.set_tracer_provider(provider)
def traced_team(agents, termination) -> tuple[RoundRobinGroupChat, SingleThreadedAgentRuntime]: runtime = SingleThreadedAgentRuntime(tracer_provider=provider) team = RoundRobinGroupChat(agents, termination_condition=termination, runtime=runtime) return team, runtime
# runtime.start(); await team.run(task=...); await runtime.stop()Set AUTOGEN_DISABLE_RUNTIME_TRACING=true to turn runtime spans off.
Token usage
Section titled “Token usage”Every TaskResult.messages entry from a model carries models_usage (prompt_tokens, completion_tokens). Sum them per run, or stop runaway teams with TokenUsageTermination(max_total_token=...).
def total_tokens(result) -> int: return sum( (m.models_usage.prompt_tokens + m.models_usage.completion_tokens) for m in result.messages if getattr(m, "models_usage", None) )Termination and recovery
Section titled “Termination and recovery”- Bound every team: combine a goal condition with a hard cap, e.g.
TextMentionTermination("APPROVE") | MaxMessageTermination(20) | TimeoutTermination(300). - Cancel from outside: pass a
CancellationTokentorun()/run_stream()and calltoken.cancel(); or attachExternalTermination()and call.set()to stop after the current speaker finishes. - Checkpoint:
state = await team.save_state()after each run and persist the JSON;await team.load_state(state)restores a fresh team in a new process. The same works per agent. - Retry transient model errors at the model-client level (the OpenAI clients accept
max_retries=), not by re-running whole teams. - Escalate to a human with
HandoffTermination(target="user")in aSwarm, or aUserProxyAgentparticipant.